author | Eugene Ostroukhov <eugeneo@symbian.org> |
Wed, 28 Jul 2010 09:27:51 -0700 | |
changeset 455 | 5da55957c779 |
parent 355 | 8726e95bcbba |
permissions | -rw-r--r-- |
2 | 1 |
// Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 |
// Use of this source code is governed by a BSD-style license that can be |
|
3 |
// found in the LICENSE file. |
|
4 |
||
5 |
package org.chromium.sdk.internal; |
|
6 |
||
7 |
import java.util.Collection; |
|
8 |
import java.util.Collections; |
|
9 |
import java.util.HashMap; |
|
10 |
import java.util.HashSet; |
|
11 |
import java.util.List; |
|
12 |
import java.util.Map; |
|
13 |
||
14 |
import org.chromium.sdk.Script; |
|
15 |
import org.chromium.sdk.internal.ScriptImpl.Descriptor; |
|
16 |
import org.chromium.sdk.internal.protocol.data.ScriptHandle; |
|
17 |
import org.chromium.sdk.internal.protocol.data.SomeHandle; |
|
18 |
import org.chromium.sdk.internal.tools.v8.V8ProtocolUtil; |
|
19 |
||
20 |
/** |
|
21 |
* Manages scripts known in the corresponding browser tab. |
|
22 |
*/ |
|
23 |
public class ScriptManager { |
|
24 |
||
25 |
public interface Callback { |
|
26 |
/** |
|
27 |
* This method gets invoked for every script in the manager. |
|
28 |
* |
|
29 |
* @param script to process |
|
30 |
* @return whether other scripts should be processed. If false, the #forEach |
|
31 |
* method terminates. |
|
32 |
*/ |
|
33 |
boolean process(Script script); |
|
34 |
} |
|
35 |
||
36 |
/** |
|
37 |
* Maps script id's to scripts. |
|
38 |
*/ |
|
39 |
private final Map<Long, ScriptImpl> idToScript = |
|
40 |
Collections.synchronizedMap(new HashMap<Long, ScriptImpl>()); |
|
41 |
||
42 |
private final V8ContextFilter contextFilter; |
|
355
8726e95bcbba
Initial commit of updated Chrome Java SDK
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
276
diff
changeset
|
43 |
private final DebugSession debugSession; |
2 | 44 |
|
355
8726e95bcbba
Initial commit of updated Chrome Java SDK
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
276
diff
changeset
|
45 |
ScriptManager(V8ContextFilter contextFilter, DebugSession debugSession) { |
2 | 46 |
this.contextFilter = contextFilter; |
355
8726e95bcbba
Initial commit of updated Chrome Java SDK
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
276
diff
changeset
|
47 |
this.debugSession = debugSession; |
2 | 48 |
} |
49 |
||
50 |
/** |
|
51 |
* Adds a script using a "script" V8 response. |
|
52 |
* |
|
53 |
* @param scriptBody to add the script from |
|
54 |
* @param refs that contain the associated script debug context |
|
55 |
* @return the new script, or {@code null} if the response does not contain |
|
56 |
* a valid script JSON |
|
57 |
*/ |
|
58 |
public synchronized Script addScript(ScriptHandle scriptBody, List<SomeHandle> refs) { |
|
59 |
||
60 |
ScriptImpl theScript = findById(V8ProtocolUtil.getScriptIdFromResponse(scriptBody)); |
|
61 |
||
62 |
if (theScript == null) { |
|
63 |
Descriptor desc = Descriptor.forResponse(scriptBody, refs, contextFilter); |
|
64 |
if (desc == null) { |
|
65 |
return null; |
|
66 |
} |
|
355
8726e95bcbba
Initial commit of updated Chrome Java SDK
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
276
diff
changeset
|
67 |
theScript = new ScriptImpl(desc, debugSession); |
2 | 68 |
idToScript.put(desc.id, theScript); |
69 |
} |
|
70 |
if (scriptBody.source() != null) { |
|
71 |
setSourceCode(scriptBody, theScript); |
|
72 |
} |
|
73 |
||
74 |
return theScript; |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* Associates a source received in a "source" V8 response with the given |
|
79 |
* script. |
|
80 |
* |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
81 |
* @param body the JSON response body |
2 | 82 |
* @param script the script to associate the source with |
83 |
*/ |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
84 |
private void setSourceCode(ScriptHandle body, ScriptImpl script) { |
2 | 85 |
String src = body.source(); |
86 |
if (src == null) { |
|
87 |
return; |
|
88 |
} |
|
89 |
if (script != null) { |
|
90 |
script.setSource(src); |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* @param id of the script to find |
|
96 |
* @return the script with {@code id == ref} or {@code null} if none found |
|
97 |
*/ |
|
98 |
public ScriptImpl findById(Long id) { |
|
99 |
return idToScript.get(id); |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* Determines whether all scripts added into this manager have associated |
|
104 |
* sources. |
|
105 |
* |
|
106 |
* @return whether all known scripts have associated sources |
|
107 |
*/ |
|
108 |
public boolean isAllSourcesLoaded() { |
|
109 |
final boolean[] result = new boolean[1]; |
|
110 |
result[0] = true; |
|
111 |
forEach(new Callback() { |
|
112 |
public boolean process(Script script) { |
|
113 |
if (!script.hasSource()) { |
|
114 |
result[0] = false; |
|
115 |
return false; |
|
116 |
} |
|
117 |
return true; |
|
118 |
} |
|
119 |
}); |
|
120 |
return result[0]; |
|
121 |
} |
|
122 |
||
123 |
public Collection<Script> allScripts() { |
|
124 |
final Collection<Script> result = new HashSet<Script>(); |
|
125 |
forEach(new Callback() { |
|
126 |
public boolean process(Script script) { |
|
127 |
result.add(script); |
|
128 |
return true; |
|
129 |
} |
|
130 |
}); |
|
131 |
return result; |
|
132 |
} |
|
133 |
||
134 |
/** |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
135 |
* This method allows running the same code for all scripts in the manager. All modifications |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
136 |
* are blocked for this period of time. |
2 | 137 |
* @param callback to invoke for every script, until |
138 |
* {@link Callback#process(Script)} returns {@code false}. |
|
139 |
*/ |
|
140 |
public synchronized void forEach(Callback callback) { |
|
141 |
for (Script script : idToScript.values()) { |
|
142 |
if (!callback.process(script)) { |
|
143 |
return; |
|
144 |
} |
|
145 |
} |
|
146 |
} |
|
147 |
||
148 |
public void reset() { |
|
149 |
idToScript.clear(); |
|
150 |
} |
|
151 |
||
152 |
public V8ContextFilter getContextFilter() { |
|
153 |
return contextFilter; |
|
154 |
} |
|
155 |
} |