2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/inspector/InjectedScriptCanvasModule.h"
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"
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;
48 InjectedScriptCanvasModule::InjectedScriptCanvasModule()
49 : InjectedScriptModule("InjectedScriptCanvasModule")
53 InjectedScriptCanvasModule InjectedScriptCanvasModule::moduleForState(InjectedScriptManager* injectedScriptManager, ScriptState* scriptState)
55 InjectedScriptCanvasModule result;
56 result.ensureInjected(injectedScriptManager, scriptState);
60 String InjectedScriptCanvasModule::source() const
62 const blink::WebData& canvasModuleSourceResource = blink::Platform::current()->loadResource("InjectedScriptCanvasModuleSource.js");
63 return String(canvasModuleSourceResource.data(), canvasModuleSourceResource.size());
66 ScriptValue InjectedScriptCanvasModule::wrapCanvas2DContext(const ScriptValue& context)
68 return callWrapContextFunction("wrapCanvas2DContext", context);
71 ScriptValue InjectedScriptCanvasModule::wrapWebGLContext(const ScriptValue& glContext)
73 return callWrapContextFunction("wrapWebGLContext", glContext);
76 ScriptValue InjectedScriptCanvasModule::callWrapContextFunction(const String& functionName, const ScriptValue& context)
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()) {
89 void InjectedScriptCanvasModule::markFrameEnd()
91 ScriptFunctionCall function(injectedScriptObject(), "markFrameEnd");
92 RefPtr<JSONValue> resultValue;
93 makeCall(function, &resultValue);
97 void InjectedScriptCanvasModule::captureFrame(ErrorString* errorString, TraceLogId* traceLogId)
99 callStartCapturingFunction("captureFrame", errorString, traceLogId);
102 void InjectedScriptCanvasModule::startCapturing(ErrorString* errorString, TraceLogId* traceLogId)
104 callStartCapturingFunction("startCapturing", errorString, traceLogId);
107 void InjectedScriptCanvasModule::callStartCapturingFunction(const String& functionName, ErrorString* errorString, TraceLogId* traceLogId)
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;
116 void InjectedScriptCanvasModule::stopCapturing(ErrorString* errorString, const TraceLogId& traceLogId)
118 callVoidFunctionWithTraceLogIdArgument("stopCapturing", errorString, traceLogId);
121 void InjectedScriptCanvasModule::dropTraceLog(ErrorString* errorString, const TraceLogId& traceLogId)
123 callVoidFunctionWithTraceLogIdArgument("dropTraceLog", errorString, traceLogId);
126 void InjectedScriptCanvasModule::callVoidFunctionWithTraceLogIdArgument(const String& functionName, ErrorString* errorString, const TraceLogId& traceLogId)
128 ScriptFunctionCall function(injectedScriptObject(), functionName);
129 function.appendArgument(traceLogId);
130 bool hadException = false;
131 callFunctionWithEvalEnabled(function, hadException);
132 ASSERT(!hadException);
134 *errorString = "Internal error: " + functionName;
137 void InjectedScriptCanvasModule::traceLog(ErrorString* errorString, const TraceLogId& traceLogId, const int* startOffset, const int* maxLength, RefPtr<TraceLog>* traceLog)
139 ScriptFunctionCall function(injectedScriptObject(), "traceLog");
140 function.appendArgument(traceLogId);
142 function.appendArgument(*startOffset);
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";
152 *traceLog = TraceLog::runtimeCast(resultValue);
155 void InjectedScriptCanvasModule::replayTraceLog(ErrorString* errorString, const TraceLogId& traceLogId, int stepNo, RefPtr<ResourceState>* result, double* replayTime)
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";
167 RefPtr<JSONObject> resultObject = resultValue->asObject();
168 RefPtr<JSONObject> resourceStateObject = resultObject->getObject("resourceState");
169 if (!resourceStateObject) {
170 *errorString = "Internal error: replayTraceLog: no resourceState";
173 *result = ResourceState::runtimeCast(resourceStateObject);
174 if (!resultObject->getNumber("replayTime", replayTime))
175 *errorString = "Internal error: replayTraceLog: no replayTime";
178 void InjectedScriptCanvasModule::resourceState(ErrorString* errorString, const TraceLogId& traceLogId, const ResourceId& resourceId, RefPtr<ResourceState>* result)
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";
190 *result = ResourceState::runtimeCast(resultValue);
193 void InjectedScriptCanvasModule::evaluateTraceLogCallArgument(ErrorString* errorString, const TraceLogId& traceLogId, int callIndex, int argumentIndex, const String& objectGroup, RefPtr<RemoteObject>* result, RefPtr<ResourceState>* resourceState)
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";
207 RefPtr<JSONObject> resultObject = resultValue->asObject();
208 RefPtr<JSONObject> remoteObject = resultObject->getObject("result");
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";