| OLD | NEW |
| (Empty) |
| 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 "sky/engine/config.h" | |
| 32 | |
| 33 | |
| 34 #include "sky/engine/core/inspector/InjectedScriptBase.h" | |
| 35 | |
| 36 #include "sky/engine/bindings/core/v8/ScriptFunctionCall.h" | |
| 37 #include "sky/engine/core/inspector/InspectorTraceEvents.h" | |
| 38 #include "sky/engine/platform/JSONValues.h" | |
| 39 #include "sky/engine/wtf/text/WTFString.h" | |
| 40 | |
| 41 using blink::TypeBuilder::Array; | |
| 42 using blink::TypeBuilder::Runtime::RemoteObject; | |
| 43 | |
| 44 namespace blink { | |
| 45 | |
| 46 static PassRefPtr<TypeBuilder::Debugger::ExceptionDetails> toExceptionDetails(Pa
ssRefPtr<JSONObject> object) | |
| 47 { | |
| 48 String text; | |
| 49 if (!object->getString("text", &text)) | |
| 50 return nullptr; | |
| 51 | |
| 52 RefPtr<TypeBuilder::Debugger::ExceptionDetails> exceptionDetails = TypeBuild
er::Debugger::ExceptionDetails::create().setText(text); | |
| 53 String url; | |
| 54 if (object->getString("url", &url)) | |
| 55 exceptionDetails->setUrl(url); | |
| 56 int line = 0; | |
| 57 if (object->getNumber("line", &line)) | |
| 58 exceptionDetails->setLine(line); | |
| 59 int column = 0; | |
| 60 if (object->getNumber("column", &column)) | |
| 61 exceptionDetails->setColumn(column); | |
| 62 RefPtr<JSONArray> stackTrace = object->getArray("stackTrace"); | |
| 63 if (stackTrace && stackTrace->length() > 0) { | |
| 64 RefPtr<TypeBuilder::Array<TypeBuilder::Console::CallFrame> > frames = Ty
peBuilder::Array<TypeBuilder::Console::CallFrame>::create(); | |
| 65 for (unsigned i = 0; i < stackTrace->length(); ++i) { | |
| 66 RefPtr<JSONObject> stackFrame = stackTrace->get(i)->asObject(); | |
| 67 int lineNumber = 0; | |
| 68 stackFrame->getNumber("lineNumber", &lineNumber); | |
| 69 int column = 0; | |
| 70 stackFrame->getNumber("column", &column); | |
| 71 int scriptId = 0; | |
| 72 stackFrame->getNumber("scriptId", &scriptId); | |
| 73 String sourceURL; | |
| 74 stackFrame->getString("scriptNameOrSourceURL", &sourceURL); | |
| 75 String functionName; | |
| 76 stackFrame->getString("functionName", &functionName); | |
| 77 | |
| 78 RefPtr<TypeBuilder::Console::CallFrame> callFrame = TypeBuilder::Con
sole::CallFrame::create() | |
| 79 .setFunctionName(functionName) | |
| 80 .setScriptId(String::number(scriptId)) | |
| 81 .setUrl(sourceURL) | |
| 82 .setLineNumber(lineNumber) | |
| 83 .setColumnNumber(column); | |
| 84 | |
| 85 frames->addItem(callFrame.release()); | |
| 86 } | |
| 87 exceptionDetails->setStackTrace(frames.release()); | |
| 88 } | |
| 89 return exceptionDetails.release(); | |
| 90 } | |
| 91 | |
| 92 InjectedScriptBase::InjectedScriptBase(const String& name) | |
| 93 : m_name(name) | |
| 94 { | |
| 95 } | |
| 96 | |
| 97 InjectedScriptBase::InjectedScriptBase(const String& name, ScriptValue injectedS
criptObject) | |
| 98 : m_name(name) | |
| 99 , m_injectedScriptObject(injectedScriptObject) | |
| 100 { | |
| 101 } | |
| 102 | |
| 103 void InjectedScriptBase::initialize(ScriptValue injectedScriptObject) | |
| 104 { | |
| 105 m_injectedScriptObject = injectedScriptObject; | |
| 106 } | |
| 107 | |
| 108 const ScriptValue& InjectedScriptBase::injectedScriptObject() const | |
| 109 { | |
| 110 return m_injectedScriptObject; | |
| 111 } | |
| 112 | |
| 113 ScriptValue InjectedScriptBase::callFunctionWithEvalEnabled(ScriptFunctionCall&
function, bool& hadException) const | |
| 114 { | |
| 115 ASSERT(!isEmpty()); | |
| 116 ExecutionContext* executionContext = m_injectedScriptObject.scriptState()->e
xecutionContext(); | |
| 117 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "FunctionCall",
"data", InspectorFunctionCallEvent::data(executionContext, 0, name(), 1)); | |
| 118 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.stack"), "
CallStack", TRACE_EVENT_SCOPE_PROCESS, "stack", InspectorCallStackEvent::current
CallStack()); | |
| 119 | |
| 120 ScriptState* scriptState = m_injectedScriptObject.scriptState(); | |
| 121 bool evalIsDisabled = false; | |
| 122 if (scriptState) { | |
| 123 evalIsDisabled = !scriptState->evalEnabled(); | |
| 124 // Temporarily enable allow evals for inspector. | |
| 125 if (evalIsDisabled) | |
| 126 scriptState->setEvalEnabled(true); | |
| 127 } | |
| 128 | |
| 129 ScriptValue resultValue = function.call(hadException); | |
| 130 | |
| 131 if (evalIsDisabled) | |
| 132 scriptState->setEvalEnabled(false); | |
| 133 | |
| 134 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "Update
Counters", TRACE_EVENT_SCOPE_PROCESS, "data", InspectorUpdateCountersEvent::data
()); | |
| 135 return resultValue; | |
| 136 } | |
| 137 | |
| 138 void InjectedScriptBase::makeCall(ScriptFunctionCall& function, RefPtr<JSONValue
>* result) | |
| 139 { | |
| 140 if (isEmpty()) { | |
| 141 *result = JSONValue::null(); | |
| 142 return; | |
| 143 } | |
| 144 | |
| 145 bool hadException = false; | |
| 146 ScriptValue resultValue = callFunctionWithEvalEnabled(function, hadException
); | |
| 147 | |
| 148 ASSERT(!hadException); | |
| 149 if (!hadException) { | |
| 150 *result = resultValue.toJSONValue(m_injectedScriptObject.scriptState()); | |
| 151 if (!*result) | |
| 152 *result = JSONString::create(String::format("Object has too long ref
erence chain(must not be longer than %d)", JSONValue::maxDepth)); | |
| 153 } else { | |
| 154 *result = JSONString::create("Exception while making a call."); | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 void InjectedScriptBase::makeEvalCall(ErrorString* errorString, ScriptFunctionCa
ll& function, RefPtr<TypeBuilder::Runtime::RemoteObject>* objectResult, TypeBuil
der::OptOutput<bool>* wasThrown, RefPtr<TypeBuilder::Debugger::ExceptionDetails>
* exceptionDetails) | |
| 159 { | |
| 160 RefPtr<JSONValue> result; | |
| 161 makeCall(function, &result); | |
| 162 if (!result) { | |
| 163 *errorString = "Internal error: result value is empty"; | |
| 164 return; | |
| 165 } | |
| 166 if (result->type() == JSONValue::TypeString) { | |
| 167 result->asString(errorString); | |
| 168 ASSERT(errorString->length()); | |
| 169 return; | |
| 170 } | |
| 171 RefPtr<JSONObject> resultPair = result->asObject(); | |
| 172 if (!resultPair) { | |
| 173 *errorString = "Internal error: result is not an Object"; | |
| 174 return; | |
| 175 } | |
| 176 RefPtr<JSONObject> resultObj = resultPair->getObject("result"); | |
| 177 bool wasThrownVal = false; | |
| 178 if (!resultObj || !resultPair->getBoolean("wasThrown", &wasThrownVal)) { | |
| 179 *errorString = "Internal error: result is not a pair of value and wasThr
own flag"; | |
| 180 return; | |
| 181 } | |
| 182 if (wasThrownVal) { | |
| 183 RefPtr<JSONObject> objectExceptionDetails = resultPair->getObject("excep
tionDetails"); | |
| 184 if (objectExceptionDetails) | |
| 185 *exceptionDetails = toExceptionDetails(objectExceptionDetails.releas
e()); | |
| 186 } | |
| 187 *objectResult = TypeBuilder::Runtime::RemoteObject::runtimeCast(resultObj); | |
| 188 *wasThrown = wasThrownVal; | |
| 189 } | |
| 190 | |
| 191 } // namespace blink | |
| 192 | |
| OLD | NEW |