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