Home | History | Annotate | Download | only in inspector
      1 /*
      2  * Copyright (C) 2012 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "core/inspector/InjectedScriptCanvasModule.h"
     33 
     34 #include "bindings/core/v8/ScriptFunctionCall.h"
     35 #include "bindings/core/v8/ScriptValue.h"
     36 #include "public/platform/Platform.h"
     37 #include "public/platform/WebData.h"
     38 
     39 using blink::TypeBuilder::Array;
     40 using blink::TypeBuilder::Canvas::ResourceId;
     41 using blink::TypeBuilder::Canvas::ResourceState;
     42 using blink::TypeBuilder::Canvas::TraceLog;
     43 using blink::TypeBuilder::Canvas::TraceLogId;
     44 using blink::TypeBuilder::Runtime::RemoteObject;
     45 
     46 namespace blink {
     47 
     48 InjectedScriptCanvasModule::InjectedScriptCanvasModule()
     49     : InjectedScriptModule("InjectedScriptCanvasModule")
     50 {
     51 }
     52 
     53 InjectedScriptCanvasModule InjectedScriptCanvasModule::moduleForState(InjectedScriptManager* injectedScriptManager, ScriptState* scriptState)
     54 {
     55     InjectedScriptCanvasModule result;
     56     result.ensureInjected(injectedScriptManager, scriptState);
     57     return result;
     58 }
     59 
     60 String InjectedScriptCanvasModule::source() const
     61 {
     62     const blink::WebData& canvasModuleSourceResource = blink::Platform::current()->loadResource("InjectedScriptCanvasModuleSource.js");
     63     return String(canvasModuleSourceResource.data(), canvasModuleSourceResource.size());
     64 }
     65 
     66 ScriptValue InjectedScriptCanvasModule::wrapCanvas2DContext(const ScriptValue& context)
     67 {
     68     return callWrapContextFunction("wrapCanvas2DContext", context);
     69 }
     70 
     71 ScriptValue InjectedScriptCanvasModule::wrapWebGLContext(const ScriptValue& glContext)
     72 {
     73     return callWrapContextFunction("wrapWebGLContext", glContext);
     74 }
     75 
     76 ScriptValue InjectedScriptCanvasModule::callWrapContextFunction(const String& functionName, const ScriptValue& context)
     77 {
     78     ScriptFunctionCall function(injectedScriptObject(), functionName);
     79     function.appendArgument(context);
     80     bool hadException = false;
     81     ScriptValue resultValue = callFunctionWithEvalEnabled(function, hadException);
     82     if (hadException || resultValue.isEmpty() || !resultValue.isObject()) {
     83         ASSERT_NOT_REACHED();
     84         return ScriptValue();
     85     }
     86     return resultValue;
     87 }
     88 
     89 void InjectedScriptCanvasModule::markFrameEnd()
     90 {
     91     ScriptFunctionCall function(injectedScriptObject(), "markFrameEnd");
     92     RefPtr<JSONValue> resultValue;
     93     makeCall(function, &resultValue);
     94     ASSERT(resultValue);
     95 }
     96 
     97 void InjectedScriptCanvasModule::captureFrame(ErrorString* errorString, TraceLogId* traceLogId)
     98 {
     99     callStartCapturingFunction("captureFrame", errorString, traceLogId);
    100 }
    101 
    102 void InjectedScriptCanvasModule::startCapturing(ErrorString* errorString, TraceLogId* traceLogId)
    103 {
    104     callStartCapturingFunction("startCapturing", errorString, traceLogId);
    105 }
    106 
    107 void InjectedScriptCanvasModule::callStartCapturingFunction(const String& functionName, ErrorString* errorString, TraceLogId* traceLogId)
    108 {
    109     ScriptFunctionCall function(injectedScriptObject(), functionName);
    110     RefPtr<JSONValue> resultValue;
    111     makeCall(function, &resultValue);
    112     if (!resultValue || resultValue->type() != JSONValue::TypeString || !resultValue->asString(traceLogId))
    113         *errorString = "Internal error: " + functionName;
    114 }
    115 
    116 void InjectedScriptCanvasModule::stopCapturing(ErrorString* errorString, const TraceLogId& traceLogId)
    117 {
    118     callVoidFunctionWithTraceLogIdArgument("stopCapturing", errorString, traceLogId);
    119 }
    120 
    121 void InjectedScriptCanvasModule::dropTraceLog(ErrorString* errorString, const TraceLogId& traceLogId)
    122 {
    123     callVoidFunctionWithTraceLogIdArgument("dropTraceLog", errorString, traceLogId);
    124 }
    125 
    126 void InjectedScriptCanvasModule::callVoidFunctionWithTraceLogIdArgument(const String& functionName, ErrorString* errorString, const TraceLogId& traceLogId)
    127 {
    128     ScriptFunctionCall function(injectedScriptObject(), functionName);
    129     function.appendArgument(traceLogId);
    130     bool hadException = false;
    131     callFunctionWithEvalEnabled(function, hadException);
    132     ASSERT(!hadException);
    133     if (hadException)
    134         *errorString = "Internal error: " + functionName;
    135 }
    136 
    137 void InjectedScriptCanvasModule::traceLog(ErrorString* errorString, const TraceLogId& traceLogId, const int* startOffset, const int* maxLength, RefPtr<TraceLog>* traceLog)
    138 {
    139     ScriptFunctionCall function(injectedScriptObject(), "traceLog");
    140     function.appendArgument(traceLogId);
    141     if (startOffset)
    142         function.appendArgument(*startOffset);
    143     if (maxLength)
    144         function.appendArgument(*maxLength);
    145     RefPtr<JSONValue> resultValue;
    146     makeCall(function, &resultValue);
    147     if (!resultValue || resultValue->type() != JSONValue::TypeObject) {
    148         if (!resultValue->asString(errorString))
    149             *errorString = "Internal error: traceLog";
    150         return;
    151     }
    152     *traceLog = TraceLog::runtimeCast(resultValue);
    153 }
    154 
    155 void InjectedScriptCanvasModule::replayTraceLog(ErrorString* errorString, const TraceLogId& traceLogId, int stepNo, RefPtr<ResourceState>* result, double* replayTime)
    156 {
    157     ScriptFunctionCall function(injectedScriptObject(), "replayTraceLog");
    158     function.appendArgument(traceLogId);
    159     function.appendArgument(stepNo);
    160     RefPtr<JSONValue> resultValue;
    161     makeCall(function, &resultValue);
    162     if (!resultValue || resultValue->type() != JSONValue::TypeObject) {
    163         if (!resultValue->asString(errorString))
    164             *errorString = "Internal error: replayTraceLog";
    165         return;
    166     }
    167     RefPtr<JSONObject> resultObject = resultValue->asObject();
    168     RefPtr<JSONObject> resourceStateObject = resultObject->getObject("resourceState");
    169     if (!resourceStateObject) {
    170         *errorString = "Internal error: replayTraceLog: no resourceState";
    171         return;
    172     }
    173     *result = ResourceState::runtimeCast(resourceStateObject);
    174     if (!resultObject->getNumber("replayTime", replayTime))
    175         *errorString = "Internal error: replayTraceLog: no replayTime";
    176 }
    177 
    178 void InjectedScriptCanvasModule::resourceState(ErrorString* errorString, const TraceLogId& traceLogId, const ResourceId& resourceId, RefPtr<ResourceState>* result)
    179 {
    180     ScriptFunctionCall function(injectedScriptObject(), "resourceState");
    181     function.appendArgument(traceLogId);
    182     function.appendArgument(resourceId);
    183     RefPtr<JSONValue> resultValue;
    184     makeCall(function, &resultValue);
    185     if (!resultValue || resultValue->type() != JSONValue::TypeObject) {
    186         if (!resultValue->asString(errorString))
    187             *errorString = "Internal error: resourceState";
    188         return;
    189     }
    190     *result = ResourceState::runtimeCast(resultValue);
    191 }
    192 
    193 void InjectedScriptCanvasModule::evaluateTraceLogCallArgument(ErrorString* errorString, const TraceLogId& traceLogId, int callIndex, int argumentIndex, const String& objectGroup, RefPtr<RemoteObject>* result, RefPtr<ResourceState>* resourceState)
    194 {
    195     ScriptFunctionCall function(injectedScriptObject(), "evaluateTraceLogCallArgument");
    196     function.appendArgument(traceLogId);
    197     function.appendArgument(callIndex);
    198     function.appendArgument(argumentIndex);
    199     function.appendArgument(objectGroup);
    200     RefPtr<JSONValue> resultValue;
    201     makeCall(function, &resultValue);
    202     if (!resultValue || resultValue->type() != JSONValue::TypeObject) {
    203         if (!resultValue->asString(errorString))
    204             *errorString = "Internal error: evaluateTraceLogCallArgument";
    205         return;
    206     }
    207     RefPtr<JSONObject> resultObject = resultValue->asObject();
    208     RefPtr<JSONObject> remoteObject = resultObject->getObject("result");
    209     if (remoteObject)
    210         *result = RemoteObject::runtimeCast(remoteObject);
    211     RefPtr<JSONObject> resourceStateObject = resultObject->getObject("resourceState");
    212     if (resourceStateObject)
    213         *resourceState = ResourceState::runtimeCast(resourceStateObject);
    214     if (!remoteObject && !resourceStateObject)
    215         *errorString = "Internal error: no result and no resource state";
    216 }
    217 
    218 } // namespace blink
    219