org.chromium.sdk/src/org/chromium/sdk/internal/ScriptManager.java
changeset 2 e4420d2515f1
child 276 f2f4a1259de8
equal deleted inserted replaced
1:ef76fc2ac88c 2:e4420d2515f1
       
     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;
       
    43 
       
    44   ScriptManager(V8ContextFilter contextFilter) {
       
    45     this.contextFilter = contextFilter;
       
    46   }
       
    47 
       
    48   /**
       
    49    * Adds a script using a "script" V8 response.
       
    50    *
       
    51    * @param scriptBody to add the script from
       
    52    * @param refs that contain the associated script debug context
       
    53    * @return the new script, or {@code null} if the response does not contain
       
    54    *         a valid script JSON
       
    55    */
       
    56   public synchronized Script addScript(ScriptHandle scriptBody, List<SomeHandle> refs) {
       
    57 
       
    58     ScriptImpl theScript = findById(V8ProtocolUtil.getScriptIdFromResponse(scriptBody));
       
    59 
       
    60     if (theScript == null) {
       
    61       Descriptor desc = Descriptor.forResponse(scriptBody, refs, contextFilter);
       
    62       if (desc == null) {
       
    63         return null;
       
    64       }
       
    65       theScript = new ScriptImpl(desc);
       
    66       idToScript.put(desc.id, theScript);
       
    67     }
       
    68     if (scriptBody.source() != null) {
       
    69       setSourceCode(scriptBody, theScript);
       
    70     }
       
    71 
       
    72     return theScript;
       
    73   }
       
    74 
       
    75   /**
       
    76    * Associates a source received in a "source" V8 response with the given
       
    77    * script.
       
    78    *
       
    79    * @param scriptBody the JSON response body
       
    80    * @param script the script to associate the source with
       
    81    */
       
    82   public void setSourceCode(ScriptHandle body, ScriptImpl script) {
       
    83     String src = body.source();
       
    84     if (src == null) {
       
    85       return;
       
    86     }
       
    87     if (script != null) {
       
    88       script.setSource(src);
       
    89     }
       
    90   }
       
    91 
       
    92   /**
       
    93    * @param id of the script to find
       
    94    * @return the script with {@code id == ref} or {@code null} if none found
       
    95    */
       
    96   public ScriptImpl findById(Long id) {
       
    97     return idToScript.get(id);
       
    98   }
       
    99 
       
   100   /**
       
   101    * Determines whether all scripts added into this manager have associated
       
   102    * sources.
       
   103    *
       
   104    * @return whether all known scripts have associated sources
       
   105    */
       
   106   public boolean isAllSourcesLoaded() {
       
   107     final boolean[] result = new boolean[1];
       
   108     result[0] = true;
       
   109     forEach(new Callback() {
       
   110       public boolean process(Script script) {
       
   111         if (!script.hasSource()) {
       
   112           result[0] = false;
       
   113           return false;
       
   114         }
       
   115         return true;
       
   116       }
       
   117     });
       
   118     return result[0];
       
   119   }
       
   120 
       
   121   public Collection<Script> allScripts() {
       
   122     final Collection<Script> result = new HashSet<Script>();
       
   123     forEach(new Callback() {
       
   124       public boolean process(Script script) {
       
   125         result.add(script);
       
   126         return true;
       
   127       }
       
   128     });
       
   129     return result;
       
   130   }
       
   131 
       
   132   /**
       
   133    * This method allows running the same code for all scripts in the manager.
       
   134    *
       
   135    * @param callback to invoke for every script, until
       
   136    *        {@link Callback#process(Script)} returns {@code false}.
       
   137    */
       
   138   public synchronized void forEach(Callback callback) {
       
   139     for (Script script : idToScript.values()) {
       
   140       if (!callback.process(script)) {
       
   141         return;
       
   142       }
       
   143     }
       
   144   }
       
   145 
       
   146   public void reset() {
       
   147     idToScript.clear();
       
   148   }
       
   149 
       
   150   public V8ContextFilter getContextFilter() {
       
   151     return contextFilter;
       
   152   }
       
   153 }