org.chromium.sdk/src/org/chromium/sdk/internal/tools/v8/processor/AfterCompileProcessor.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.tools.v8.processor;
       
     6 
       
     7 import java.util.Collections;
       
     8 import java.util.List;
       
     9 
       
    10 import org.chromium.sdk.Script;
       
    11 import org.chromium.sdk.internal.DebugSession;
       
    12 import org.chromium.sdk.internal.V8ContextFilter;
       
    13 import org.chromium.sdk.internal.protocol.AfterCompileBody;
       
    14 import org.chromium.sdk.internal.protocol.CommandResponse;
       
    15 import org.chromium.sdk.internal.protocol.EventNotification;
       
    16 import org.chromium.sdk.internal.protocol.SuccessCommandResponse;
       
    17 import org.chromium.sdk.internal.protocol.data.ScriptHandle;
       
    18 import org.chromium.sdk.internal.protocolparser.JsonProtocolParseException;
       
    19 import org.chromium.sdk.internal.tools.v8.ChromeDevToolSessionManager;
       
    20 import org.chromium.sdk.internal.tools.v8.V8CommandProcessor;
       
    21 import org.chromium.sdk.internal.tools.v8.V8ProtocolUtil;
       
    22 import org.chromium.sdk.internal.tools.v8.request.DebuggerMessageFactory;
       
    23 
       
    24 /**
       
    25  * Listens for scripts sent in the "afterCompile" events and requests their
       
    26  * sources.
       
    27  */
       
    28 public class AfterCompileProcessor extends V8EventProcessor {
       
    29 
       
    30   public AfterCompileProcessor(DebugSession debugSession) {
       
    31     super(debugSession);
       
    32   }
       
    33 
       
    34   @Override
       
    35   public void messageReceived(EventNotification eventMessage) {
       
    36     final DebugSession debugSession = getDebugSession();
       
    37     ScriptHandle script = getScriptToLoad(eventMessage,
       
    38         debugSession.getScriptManager().getContextFilter());
       
    39     if (script == null) {
       
    40       return;
       
    41     }
       
    42     debugSession.sendMessageAsync(
       
    43         DebuggerMessageFactory.scripts(
       
    44             Collections.singletonList(V8ProtocolUtil.getScriptIdFromResponse(script)), true),
       
    45         true,
       
    46         new V8CommandProcessor.V8HandlerCallback(){
       
    47           public void messageReceived(CommandResponse response) {
       
    48             SuccessCommandResponse successResponse = response.asSuccess();
       
    49             if (successResponse == null) {
       
    50               return;
       
    51             }
       
    52             List<ScriptHandle> body;
       
    53             try {
       
    54               body = successResponse.getBody().asScripts();
       
    55             } catch (JsonProtocolParseException e) {
       
    56               throw new RuntimeException(e);
       
    57             }
       
    58             // body is an array of scripts
       
    59             if (body.size() == 0) {
       
    60               return; // The script did not arrive (bad id?)
       
    61             }
       
    62             Script newScript = debugSession.getScriptManager().addScript(
       
    63                 body.get(0),
       
    64                 successResponse.getRefs());
       
    65             if (newScript != null) {
       
    66               getDebugSession().getSessionManager().getDebugEventListener().scriptLoaded(newScript);
       
    67             }
       
    68           }
       
    69 
       
    70           public void failure(String message) {
       
    71             // The script is now missing.
       
    72           }
       
    73         },
       
    74         null);
       
    75   }
       
    76 
       
    77   private static ScriptHandle getScriptToLoad(EventNotification eventResponse,
       
    78       V8ContextFilter contextFilter) {
       
    79     AfterCompileBody body;
       
    80     try {
       
    81       body = eventResponse.getBody().asAfterCompileBody();
       
    82     } catch (JsonProtocolParseException e) {
       
    83       throw new RuntimeException(e);
       
    84     }
       
    85     ScriptHandle script = body.getScript();
       
    86     if (ChromeDevToolSessionManager.JAVASCRIPT_VOID.equals(script.sourceStart()) ||
       
    87         script.context() == null ||
       
    88         V8ProtocolUtil.getScriptType(script.scriptType()) ==
       
    89             Script.Type.NATIVE) {
       
    90       return null;
       
    91     }
       
    92     return V8ProtocolUtil.validScript(script, eventResponse.getRefs(), contextFilter);
       
    93   }
       
    94 }