org.chromium.sdk/src/org/chromium/sdk/internal/tools/v8/V8Helper.java
changeset 276 f2f4a1259de8
parent 2 e4420d2515f1
child 355 8726e95bcbba
--- a/org.chromium.sdk/src/org/chromium/sdk/internal/tools/v8/V8Helper.java	Thu Mar 18 11:10:35 2010 -0700
+++ b/org.chromium.sdk/src/org/chromium/sdk/internal/tools/v8/V8Helper.java	Thu Mar 18 11:56:59 2010 -0700
@@ -7,7 +7,6 @@
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
-import java.util.concurrent.Semaphore;
 import java.util.concurrent.TimeUnit;
 
 import org.chromium.sdk.CallbackSemaphore;
@@ -48,44 +47,33 @@
    */
   private final DebugSession debugSession;
 
-  /**
-   * A semaphore that prevents concurrent script reloading (which may effectively
-   * double the efforts.)
-   */
-  private final Semaphore scriptsReloadSemaphore = new Semaphore(1);
-
   public V8Helper(DebugSession debugSession) {
     this.debugSession = debugSession;
   }
 
+  public interface ScriptLoadCallback {
+    void success();
+    void failure(String message);
+  }
+
   /**
-   * Reloads all normal scripts found in the page. First, all scripts without
-   * their sources are retrieved to save bandwidth (script list change during a
-   * page lifetime is a relatively rare event.) If at least one script has been
-   * added, the script cache is dropped and re-populated with new scripts that
-   * are re-requested together with their sources.
-   *
+   * Loads all scripts and stores them in ScriptManager.
    * @param callback to invoke when the script reloading has completed
+   * @param syncCallback to invoke after callback whether it normally returned or threw an exception
    */
-  public void reloadAllScriptsAsync(V8CommandProcessor.V8HandlerCallback callback,
-      SyncCallback syncCallback) {
-    final V8CommandProcessor.V8HandlerCallback finalCallback = callback != null
-        ? callback
-        : V8CommandProcessor.V8HandlerCallback.NULL_CALLBACK;
-    lock();
+  public static void reloadAllScriptsAsync(final DebugSession debugSession,
+      final ScriptLoadCallback callback, SyncCallback syncCallback) {
     debugSession.sendMessageAsync(
         DebuggerMessageFactory.scripts(ScriptsMessage.SCRIPTS_NORMAL, true),
         true,
-        new V8CommandProcessor.V8HandlerCallback() {
+        new V8CommandCallbackBase() {
+          @Override
           public void failure(String message) {
-            unlock();
-            finalCallback.failure(message);
+            callback.failure(message);
           }
 
-          public void messageReceived(CommandResponse response) {
-            SuccessCommandResponse successResponse = response.asSuccess();
-
-            // TODO(peter.rybin): add try/finally for unlock, with some error reporting probably.
+          @Override
+          public void success(SuccessCommandResponse successResponse) {
             List<ScriptHandle> body;
             try {
               body = successResponse.getBody().asScripts();
@@ -98,30 +86,15 @@
               Long id = V8ProtocolUtil.getScriptIdFromResponse(scriptHandle);
               if (scriptManager.findById(id) == null &&
                   !ChromeDevToolSessionManager.JAVASCRIPT_VOID.equals(scriptHandle.source())) {
-                scriptManager.addScript(
-                    scriptHandle,
-                    successResponse.getRefs());
+                scriptManager.addScript(scriptHandle, successResponse.getRefs());
               }
             }
-            unlock();
-            finalCallback.messageReceived(response);
+            callback.success();
           }
         },
         syncCallback);
   }
 
-  protected void lock() {
-    try {
-      scriptsReloadSemaphore.acquire();
-    } catch (InterruptedException e) {
-      // consider it a successful acquisition
-    }
-  }
-
-  protected void unlock() {
-    scriptsReloadSemaphore.release();
-  }
-
   /**
    * Gets all resolved locals for the call frame, caches scripts and objects in
    * the scriptManager and handleManager.
@@ -222,7 +195,7 @@
     String className = valueHandle.className();
     Type type = JsDataTypeUtil.fromJsonTypeAndClassName(valueHandle.type(), className);
     if (type == null) {
-      throw new ValueLoadException("Bad value object");
+      type = Type.TYPE_OBJECT;
     }
     String text = valueHandle.text();
     return createMirrorFromLookup(valueHandle, type).getValueMirror();