OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2007-2011 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 "bindings/core/v8/V8InjectedScriptHost.h" |
| 33 |
| 34 #include "bindings/core/v8/BindingSecurity.h" |
| 35 #include "bindings/core/v8/ExceptionState.h" |
| 36 #include "bindings/core/v8/ScriptDebugServer.h" |
| 37 #include "bindings/core/v8/ScriptValue.h" |
| 38 #include "bindings/core/v8/V8AbstractEventListener.h" |
| 39 #include "bindings/core/v8/V8Binding.h" |
| 40 #include "bindings/core/v8/V8DOMTokenList.h" |
| 41 #include "bindings/core/v8/V8EventTarget.h" |
| 42 #include "bindings/core/v8/V8Node.h" |
| 43 #include "bindings/core/v8/V8NodeList.h" |
| 44 #include "bindings/core/v8/V8ScriptRunner.h" |
| 45 #include "bindings/core/v8/custom/V8Float32ArrayCustom.h" |
| 46 #include "bindings/core/v8/custom/V8Float64ArrayCustom.h" |
| 47 #include "bindings/core/v8/custom/V8Int16ArrayCustom.h" |
| 48 #include "bindings/core/v8/custom/V8Int32ArrayCustom.h" |
| 49 #include "bindings/core/v8/custom/V8Int8ArrayCustom.h" |
| 50 #include "bindings/core/v8/custom/V8Uint16ArrayCustom.h" |
| 51 #include "bindings/core/v8/custom/V8Uint32ArrayCustom.h" |
| 52 #include "bindings/core/v8/custom/V8Uint8ArrayCustom.h" |
| 53 #include "bindings/core/v8/custom/V8Uint8ClampedArrayCustom.h" |
| 54 #include "core/events/EventTarget.h" |
| 55 #include "core/frame/LocalDOMWindow.h" |
| 56 #include "core/inspector/InjectedScript.h" |
| 57 #include "core/inspector/InjectedScriptHost.h" |
| 58 #include "core/inspector/JavaScriptCallFrame.h" |
| 59 #include "platform/JSONValues.h" |
| 60 |
| 61 namespace blink { |
| 62 |
| 63 Node* InjectedScriptHost::scriptValueAsNode(ScriptState* scriptState, ScriptValu
e value) |
| 64 { |
| 65 ScriptState::Scope scope(scriptState); |
| 66 if (!value.isObject() || value.isNull()) |
| 67 return 0; |
| 68 return V8Node::toNative(v8::Handle<v8::Object>::Cast(value.v8Value())); |
| 69 } |
| 70 |
| 71 ScriptValue InjectedScriptHost::nodeAsScriptValue(ScriptState* scriptState, Node
* node) |
| 72 { |
| 73 ScriptState::Scope scope(scriptState); |
| 74 v8::Isolate* isolate = scriptState->isolate(); |
| 75 ExceptionState exceptionState(ExceptionState::ExecutionContext, "nodeAsScrip
tValue", "InjectedScriptHost", scriptState->context()->Global(), isolate); |
| 76 return ScriptValue(scriptState, toV8(node, scriptState->context()->Global(),
isolate)); |
| 77 } |
| 78 |
| 79 void V8InjectedScriptHost::inspectedObjectMethodCustom(const v8::FunctionCallbac
kInfo<v8::Value>& info) |
| 80 { |
| 81 if (info.Length() < 1) |
| 82 return; |
| 83 |
| 84 if (!info[0]->IsInt32()) { |
| 85 V8ThrowException::throwTypeError("argument has to be an integer", info.G
etIsolate()); |
| 86 return; |
| 87 } |
| 88 |
| 89 InjectedScriptHost* host = V8InjectedScriptHost::toNative(info.Holder()); |
| 90 InjectedScriptHost::InspectableObject* object = host->inspectedObject(info[0
]->ToInt32()->Value()); |
| 91 v8SetReturnValue(info, object->get(ScriptState::current(info.GetIsolate())).
v8Value()); |
| 92 } |
| 93 |
| 94 static v8::Handle<v8::String> functionDisplayName(v8::Handle<v8::Function> funct
ion) |
| 95 { |
| 96 v8::Handle<v8::Value> value = function->GetDisplayName(); |
| 97 if (value->IsString() && v8::Handle<v8::String>::Cast(value)->Length()) |
| 98 return v8::Handle<v8::String>::Cast(value); |
| 99 |
| 100 value = function->GetName(); |
| 101 if (value->IsString() && v8::Handle<v8::String>::Cast(value)->Length()) |
| 102 return v8::Handle<v8::String>::Cast(value); |
| 103 |
| 104 value = function->GetInferredName(); |
| 105 if (value->IsString() && v8::Handle<v8::String>::Cast(value)->Length()) |
| 106 return v8::Handle<v8::String>::Cast(value); |
| 107 |
| 108 return v8::Handle<v8::String>(); |
| 109 } |
| 110 |
| 111 void V8InjectedScriptHost::internalConstructorNameMethodCustom(const v8::Functio
nCallbackInfo<v8::Value>& info) |
| 112 { |
| 113 if (info.Length() < 1 || !info[0]->IsObject()) |
| 114 return; |
| 115 |
| 116 v8::Local<v8::Object> object = info[0]->ToObject(); |
| 117 v8::Local<v8::String> result = object->GetConstructorName(); |
| 118 |
| 119 if (!result.IsEmpty() && toCoreStringWithUndefinedOrNullCheck(result) == "Ob
ject") { |
| 120 v8::Local<v8::String> constructorSymbol = v8AtomicString(info.GetIsolate
(), "constructor"); |
| 121 if (object->HasRealNamedProperty(constructorSymbol) && !object->HasRealN
amedCallbackProperty(constructorSymbol)) { |
| 122 v8::TryCatch tryCatch; |
| 123 v8::Local<v8::Value> constructor = object->GetRealNamedProperty(cons
tructorSymbol); |
| 124 if (!constructor.IsEmpty() && constructor->IsFunction()) { |
| 125 v8::Local<v8::String> constructorName = functionDisplayName(v8::
Handle<v8::Function>::Cast(constructor)); |
| 126 if (!constructorName.IsEmpty() && !tryCatch.HasCaught()) |
| 127 result = constructorName; |
| 128 } |
| 129 } |
| 130 } |
| 131 |
| 132 v8SetReturnValue(info, result); |
| 133 } |
| 134 |
| 135 void V8InjectedScriptHost::isHTMLAllCollectionMethodCustom(const v8::FunctionCal
lbackInfo<v8::Value>& info) |
| 136 { |
| 137 // FIXME(sky): remove |
| 138 v8SetReturnValue(info, false); |
| 139 } |
| 140 |
| 141 void V8InjectedScriptHost::subtypeMethodCustom(const v8::FunctionCallbackInfo<v8
::Value>& info) |
| 142 { |
| 143 if (info.Length() < 1) |
| 144 return; |
| 145 v8::Isolate* isolate = info.GetIsolate(); |
| 146 |
| 147 v8::Handle<v8::Value> value = info[0]; |
| 148 if (value->IsArray() || value->IsTypedArray() || value->IsArgumentsObject())
{ |
| 149 v8SetReturnValue(info, v8AtomicString(isolate, "array")); |
| 150 return; |
| 151 } |
| 152 if (value->IsDate()) { |
| 153 v8SetReturnValue(info, v8AtomicString(isolate, "date")); |
| 154 return; |
| 155 } |
| 156 if (value->IsRegExp()) { |
| 157 v8SetReturnValue(info, v8AtomicString(isolate, "regexp")); |
| 158 return; |
| 159 } |
| 160 if (value->IsMap() || value->IsWeakMap()) { |
| 161 v8SetReturnValue(info, v8AtomicString(isolate, "map")); |
| 162 return; |
| 163 } |
| 164 if (value->IsSet() || value->IsWeakSet()) { |
| 165 v8SetReturnValue(info, v8AtomicString(isolate, "set")); |
| 166 return; |
| 167 } |
| 168 if (V8Node::hasInstance(value, isolate)) { |
| 169 v8SetReturnValue(info, v8AtomicString(isolate, "node")); |
| 170 return; |
| 171 } |
| 172 if (V8NodeList::hasInstance(value, isolate) |
| 173 || V8DOMTokenList::hasInstance(value, isolate)) { |
| 174 v8SetReturnValue(info, v8AtomicString(isolate, "array")); |
| 175 return; |
| 176 } |
| 177 } |
| 178 |
| 179 void V8InjectedScriptHost::functionDetailsMethodCustom(const v8::FunctionCallbac
kInfo<v8::Value>& info) |
| 180 { |
| 181 if (info.Length() < 1 || !info[0]->IsFunction()) |
| 182 return; |
| 183 |
| 184 v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(info[0]); |
| 185 int lineNumber = function->GetScriptLineNumber(); |
| 186 int columnNumber = function->GetScriptColumnNumber(); |
| 187 |
| 188 v8::Isolate* isolate = info.GetIsolate(); |
| 189 v8::Local<v8::Object> location = v8::Object::New(isolate); |
| 190 location->Set(v8AtomicString(isolate, "lineNumber"), v8::Integer::New(isolat
e, lineNumber)); |
| 191 location->Set(v8AtomicString(isolate, "columnNumber"), v8::Integer::New(isol
ate, columnNumber)); |
| 192 location->Set(v8AtomicString(isolate, "scriptId"), v8::Integer::New(isolate,
function->ScriptId())->ToString()); |
| 193 |
| 194 v8::Local<v8::Object> result = v8::Object::New(isolate); |
| 195 result->Set(v8AtomicString(isolate, "location"), location); |
| 196 |
| 197 v8::Handle<v8::String> name = functionDisplayName(function); |
| 198 result->Set(v8AtomicString(isolate, "functionName"), name.IsEmpty() ? v8Atom
icString(isolate, "") : name); |
| 199 |
| 200 InjectedScriptHost* host = V8InjectedScriptHost::toNative(info.Holder()); |
| 201 ScriptDebugServer& debugServer = host->scriptDebugServer(); |
| 202 v8::Handle<v8::Value> scopes = debugServer.functionScopes(function); |
| 203 if (!scopes.IsEmpty() && scopes->IsArray()) |
| 204 result->Set(v8AtomicString(isolate, "rawScopes"), scopes); |
| 205 |
| 206 v8SetReturnValue(info, result); |
| 207 } |
| 208 |
| 209 void V8InjectedScriptHost::collectionEntriesMethodCustom(const v8::FunctionCallb
ackInfo<v8::Value>& info) |
| 210 { |
| 211 if (info.Length() < 1 || !info[0]->IsObject()) |
| 212 return; |
| 213 |
| 214 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(info[0]); |
| 215 |
| 216 InjectedScriptHost* host = V8InjectedScriptHost::toNative(info.Holder()); |
| 217 ScriptDebugServer& debugServer = host->scriptDebugServer(); |
| 218 v8SetReturnValue(info, debugServer.collectionEntries(object)); |
| 219 } |
| 220 |
| 221 void V8InjectedScriptHost::getInternalPropertiesMethodCustom(const v8::FunctionC
allbackInfo<v8::Value>& info) |
| 222 { |
| 223 if (info.Length() < 1 || !info[0]->IsObject()) |
| 224 return; |
| 225 |
| 226 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(info[0]); |
| 227 |
| 228 InjectedScriptHost* host = V8InjectedScriptHost::toNative(info.Holder()); |
| 229 ScriptDebugServer& debugServer = host->scriptDebugServer(); |
| 230 v8SetReturnValue(info, debugServer.getInternalProperties(object)); |
| 231 } |
| 232 |
| 233 void V8InjectedScriptHost::getEventListenersMethodCustom(const v8::FunctionCallb
ackInfo<v8::Value>& info) |
| 234 { |
| 235 // FIXME(sky): remove |
| 236 } |
| 237 |
| 238 void V8InjectedScriptHost::inspectMethodCustom(const v8::FunctionCallbackInfo<v8
::Value>& info) |
| 239 { |
| 240 if (info.Length() < 2) |
| 241 return; |
| 242 |
| 243 InjectedScriptHost* host = V8InjectedScriptHost::toNative(info.Holder()); |
| 244 ScriptState* scriptState = ScriptState::current(info.GetIsolate()); |
| 245 ScriptValue object(scriptState, info[0]); |
| 246 ScriptValue hints(scriptState, info[1]); |
| 247 host->inspectImpl(object.toJSONValue(scriptState), hints.toJSONValue(scriptS
tate)); |
| 248 } |
| 249 |
| 250 void V8InjectedScriptHost::evalMethodCustom(const v8::FunctionCallbackInfo<v8::V
alue>& info) |
| 251 { |
| 252 v8::Isolate* isolate = info.GetIsolate(); |
| 253 if (info.Length() < 1) { |
| 254 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(iso
late, "One argument expected."))); |
| 255 return; |
| 256 } |
| 257 |
| 258 v8::Handle<v8::String> expression = info[0]->ToString(); |
| 259 if (expression.IsEmpty()) { |
| 260 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(iso
late, "The argument must be a string."))); |
| 261 return; |
| 262 } |
| 263 |
| 264 ASSERT(isolate->InContext()); |
| 265 v8::TryCatch tryCatch; |
| 266 v8::Handle<v8::Value> result = V8ScriptRunner::compileAndRunInternalScript(e
xpression, info.GetIsolate()); |
| 267 if (tryCatch.HasCaught()) { |
| 268 v8SetReturnValue(info, tryCatch.ReThrow()); |
| 269 return; |
| 270 } |
| 271 v8SetReturnValue(info, result); |
| 272 } |
| 273 |
| 274 void V8InjectedScriptHost::evaluateWithExceptionDetailsMethodCustom(const v8::Fu
nctionCallbackInfo<v8::Value>& info) |
| 275 { |
| 276 v8::Isolate* isolate = info.GetIsolate(); |
| 277 if (info.Length() < 1) { |
| 278 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(iso
late, "One argument expected."))); |
| 279 return; |
| 280 } |
| 281 |
| 282 v8::Handle<v8::String> expression = info[0]->ToString(); |
| 283 if (expression.IsEmpty()) { |
| 284 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(iso
late, "The argument must be a string."))); |
| 285 return; |
| 286 } |
| 287 |
| 288 ASSERT(isolate->InContext()); |
| 289 v8::TryCatch tryCatch; |
| 290 v8::Handle<v8::Value> result = V8ScriptRunner::compileAndRunInternalScript(e
xpression, info.GetIsolate()); |
| 291 |
| 292 v8::Local<v8::Object> wrappedResult = v8::Object::New(isolate); |
| 293 if (tryCatch.HasCaught()) { |
| 294 wrappedResult->Set(v8::String::NewFromUtf8(isolate, "result"), tryCatch.
Exception()); |
| 295 wrappedResult->Set(v8::String::NewFromUtf8(isolate, "exceptionDetails"),
JavaScriptCallFrame::createExceptionDetails(tryCatch.Message(), isolate)); |
| 296 } else { |
| 297 wrappedResult->Set(v8::String::NewFromUtf8(isolate, "result"), result); |
| 298 wrappedResult->Set(v8::String::NewFromUtf8(isolate, "exceptionDetails"),
v8::Undefined(isolate)); |
| 299 } |
| 300 v8SetReturnValue(info, wrappedResult); |
| 301 } |
| 302 |
| 303 void V8InjectedScriptHost::setFunctionVariableValueMethodCustom(const v8::Functi
onCallbackInfo<v8::Value>& info) |
| 304 { |
| 305 v8::Handle<v8::Value> functionValue = info[0]; |
| 306 int scopeIndex = info[1]->Int32Value(); |
| 307 String variableName = toCoreStringWithUndefinedOrNullCheck(info[2]); |
| 308 v8::Handle<v8::Value> newValue = info[3]; |
| 309 |
| 310 InjectedScriptHost* host = V8InjectedScriptHost::toNative(info.Holder()); |
| 311 ScriptDebugServer& debugServer = host->scriptDebugServer(); |
| 312 v8SetReturnValue(info, debugServer.setFunctionVariableValue(functionValue, s
copeIndex, variableName, newValue)); |
| 313 } |
| 314 |
| 315 static bool getFunctionLocation(const v8::FunctionCallbackInfo<v8::Value>& info,
String* scriptId, int* lineNumber, int* columnNumber) |
| 316 { |
| 317 if (info.Length() < 1) |
| 318 return false; |
| 319 v8::Handle<v8::Value> fn = info[0]; |
| 320 if (!fn->IsFunction()) |
| 321 return false; |
| 322 v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(fn); |
| 323 *lineNumber = function->GetScriptLineNumber(); |
| 324 *columnNumber = function->GetScriptColumnNumber(); |
| 325 if (*lineNumber == v8::Function::kLineOffsetNotFound || *columnNumber == v8:
:Function::kLineOffsetNotFound) |
| 326 return false; |
| 327 *scriptId = String::number(function->ScriptId()); |
| 328 return true; |
| 329 } |
| 330 |
| 331 void V8InjectedScriptHost::debugFunctionMethodCustom(const v8::FunctionCallbackI
nfo<v8::Value>& info) |
| 332 { |
| 333 String scriptId; |
| 334 int lineNumber; |
| 335 int columnNumber; |
| 336 if (!getFunctionLocation(info, &scriptId, &lineNumber, &columnNumber)) |
| 337 return; |
| 338 |
| 339 InjectedScriptHost* host = V8InjectedScriptHost::toNative(info.Holder()); |
| 340 host->debugFunction(scriptId, lineNumber, columnNumber); |
| 341 } |
| 342 |
| 343 void V8InjectedScriptHost::undebugFunctionMethodCustom(const v8::FunctionCallbac
kInfo<v8::Value>& info) |
| 344 { |
| 345 String scriptId; |
| 346 int lineNumber; |
| 347 int columnNumber; |
| 348 if (!getFunctionLocation(info, &scriptId, &lineNumber, &columnNumber)) |
| 349 return; |
| 350 |
| 351 InjectedScriptHost* host = V8InjectedScriptHost::toNative(info.Holder()); |
| 352 host->undebugFunction(scriptId, lineNumber, columnNumber); |
| 353 } |
| 354 |
| 355 void V8InjectedScriptHost::monitorFunctionMethodCustom(const v8::FunctionCallbac
kInfo<v8::Value>& info) |
| 356 { |
| 357 String scriptId; |
| 358 int lineNumber; |
| 359 int columnNumber; |
| 360 if (!getFunctionLocation(info, &scriptId, &lineNumber, &columnNumber)) |
| 361 return; |
| 362 |
| 363 v8::Handle<v8::Value> name; |
| 364 if (info.Length() > 0 && info[0]->IsFunction()) { |
| 365 v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(info[
0]); |
| 366 name = function->GetName(); |
| 367 if (!name->IsString() || !v8::Handle<v8::String>::Cast(name)->Length()) |
| 368 name = function->GetInferredName(); |
| 369 } |
| 370 |
| 371 InjectedScriptHost* host = V8InjectedScriptHost::toNative(info.Holder()); |
| 372 host->monitorFunction(scriptId, lineNumber, columnNumber, toCoreStringWithUn
definedOrNullCheck(name)); |
| 373 } |
| 374 |
| 375 void V8InjectedScriptHost::unmonitorFunctionMethodCustom(const v8::FunctionCallb
ackInfo<v8::Value>& info) |
| 376 { |
| 377 String scriptId; |
| 378 int lineNumber; |
| 379 int columnNumber; |
| 380 if (!getFunctionLocation(info, &scriptId, &lineNumber, &columnNumber)) |
| 381 return; |
| 382 |
| 383 InjectedScriptHost* host = V8InjectedScriptHost::toNative(info.Holder()); |
| 384 host->unmonitorFunction(scriptId, lineNumber, columnNumber); |
| 385 } |
| 386 |
| 387 void V8InjectedScriptHost::callFunctionMethodCustom(const v8::FunctionCallbackIn
fo<v8::Value>& info) |
| 388 { |
| 389 if (info.Length() < 2 || info.Length() > 3 || !info[0]->IsFunction()) { |
| 390 ASSERT_NOT_REACHED(); |
| 391 return; |
| 392 } |
| 393 |
| 394 v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(info[0]); |
| 395 v8::Handle<v8::Value> receiver = info[1]; |
| 396 |
| 397 if (info.Length() < 3 || info[2]->IsUndefined()) { |
| 398 v8::Local<v8::Value> result = function->Call(receiver, 0, 0); |
| 399 v8SetReturnValue(info, result); |
| 400 return; |
| 401 } |
| 402 |
| 403 if (!info[2]->IsArray()) { |
| 404 ASSERT_NOT_REACHED(); |
| 405 return; |
| 406 } |
| 407 |
| 408 v8::Handle<v8::Array> arguments = v8::Handle<v8::Array>::Cast(info[2]); |
| 409 size_t argc = arguments->Length(); |
| 410 OwnPtr<v8::Handle<v8::Value>[]> argv = adoptArrayPtr(new v8::Handle<v8::Valu
e>[argc]); |
| 411 for (size_t i = 0; i < argc; ++i) |
| 412 argv[i] = arguments->Get(i); |
| 413 |
| 414 v8::Local<v8::Value> result = function->Call(receiver, argc, argv.get()); |
| 415 v8SetReturnValue(info, result); |
| 416 } |
| 417 |
| 418 void V8InjectedScriptHost::suppressWarningsAndCallFunctionMethodCustom(const v8:
:FunctionCallbackInfo<v8::Value>& info) |
| 419 { |
| 420 InjectedScriptHost* host = V8InjectedScriptHost::toNative(info.Holder()); |
| 421 ScriptDebugServer& debugServer = host->scriptDebugServer(); |
| 422 debugServer.muteWarningsAndDeprecations(); |
| 423 |
| 424 callFunctionMethodCustom(info); |
| 425 |
| 426 debugServer.unmuteWarningsAndDeprecations(); |
| 427 } |
| 428 |
| 429 } // namespace blink |
OLD | NEW |