package com.oracle.truffle.tools.chromeinspector;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.oracle.truffle.api.debug.DebugValue;
import com.oracle.truffle.tools.chromeinspector.types.RemoteObject;
public final class RemoteObjectsHandler {
private final Map<String, RemoteObject> remotesByIDs = new HashMap<>(100);
private final Map<DebugValue, RemoteObject> remotesByValue = new HashMap<>(100);
private final Map<String, DebugValue> customPreviewBodies = new HashMap<>();
private final Map<String, DebugValue> customPreviewConfigs = new HashMap<>();
private final Map<String, Set<String>> objectGroups = new HashMap<>();
private final InspectorExecutionContext context;
RemoteObjectsHandler(InspectorExecutionContext context) {
this.context = context;
}
public RemoteObject getRemote(DebugValue value) {
RemoteObject remote;
synchronized (remotesByIDs) {
remote = remotesByValue.get(value);
if (remote == null) {
remote = new RemoteObject(value, false, context);
remotesByValue.put(value, remote);
if (remote.getId() != null) {
remotesByIDs.put(remote.getId(), remote);
}
}
}
return remote;
}
RemoteObject getRemote(String objectId) {
synchronized (remotesByIDs) {
return remotesByIDs.get(objectId);
}
}
void register(RemoteObject remote) {
register(remote, null);
}
void register(RemoteObject remote, String objectGroup) {
if (remote.getId() != null) {
synchronized (remotesByIDs) {
remotesByIDs.put(remote.getId(), remote);
if (objectGroup != null) {
Set<String> group = objectGroups.get(objectGroup);
if (group == null) {
group = new HashSet<>();
objectGroups.put(objectGroup, group);
}
group.add(remote.getId());
}
}
}
}
String getObjectGroupOf(String objectId) {
synchronized (remotesByIDs) {
for (Map.Entry<String, Set<String>> groupEntry : objectGroups.entrySet()) {
if (groupEntry.getValue().contains(objectId)) {
return groupEntry.getKey();
}
}
}
return null;
}
void releaseObject(String objectId) {
synchronized (remotesByIDs) {
remotesByIDs.remove(objectId);
}
}
void releaseObjectGroup(String objectGroup) {
synchronized (remotesByIDs) {
Set<String> group = objectGroups.remove(objectGroup);
if (group != null) {
for (String objectId : group) {
remotesByIDs.remove(objectId);
}
}
}
}
public Set<String> getRegisteredIDs() {
Set<String> ids = new HashSet<>();
synchronized (remotesByIDs) {
ids.addAll(remotesByIDs.keySet());
}
return ids;
}
void reset() {
synchronized (remotesByIDs) {
remotesByValue.clear();
customPreviewBodies.clear();
customPreviewConfigs.clear();
if (objectGroups.isEmpty()) {
remotesByIDs.clear();
} else {
Set<String> grouppedIds;
if (objectGroups.size() == 1) {
grouppedIds = objectGroups.values().iterator().next();
} else {
grouppedIds = new HashSet<>();
for (Set<String> group : objectGroups.values()) {
grouppedIds.addAll(group);
}
}
remotesByIDs.keySet().retainAll(grouppedIds);
}
}
}
public void registerCustomPreviewBody(String id, DebugValue body) {
synchronized (remotesByIDs) {
customPreviewBodies.put(id, body);
}
}
DebugValue getCustomPreviewBody(String id) {
synchronized (remotesByIDs) {
return customPreviewBodies.get(id);
}
}
public void registerCustomPreviewConfig(String objectId, DebugValue config) {
synchronized (remotesByIDs) {
customPreviewConfigs.put(objectId, config);
}
}
DebugValue getCustomPreviewConfig(String id) {
synchronized (remotesByIDs) {
return customPreviewConfigs.get(id);
}
}
}