| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "bindings/core/v8/PrivateScriptRunner.h" | 5 #include "bindings/core/v8/PrivateScriptRunner.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/DOMWrapperWorld.h" | 7 #include "bindings/core/v8/DOMWrapperWorld.h" |
| 8 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "bindings/core/v8/V8Binding.h" | 9 #include "bindings/core/v8/V8Binding.h" |
| 10 #include "bindings/core/v8/V8PerContextData.h" | 10 #include "bindings/core/v8/V8PerContextData.h" |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 fprintf(stderr, "Private script error: Target source code was not found.
(Class name = %s)\n", className.utf8().data()); | 155 fprintf(stderr, "Private script error: Target source code was not found.
(Class name = %s)\n", className.utf8().data()); |
| 156 RELEASE_ASSERT_NOT_REACHED(); | 156 RELEASE_ASSERT_NOT_REACHED(); |
| 157 } | 157 } |
| 158 String resourceData = loadResourceAsASCIIString(kPrivateScriptSources[index]
.resourceFile); | 158 String resourceData = loadResourceAsASCIIString(kPrivateScriptSources[index]
.resourceFile); |
| 159 return compileAndRunPrivateScript(ScriptState::current(isolate), className,
resourceData.utf8().data(), resourceData.length()); | 159 return compileAndRunPrivateScript(ScriptState::current(isolate), className,
resourceData.utf8().data(), resourceData.length()); |
| 160 } | 160 } |
| 161 | 161 |
| 162 static v8::Local<v8::Object> classObjectOfPrivateScript(ScriptState* scriptState
, String className) | 162 static v8::Local<v8::Object> classObjectOfPrivateScript(ScriptState* scriptState
, String className) |
| 163 { | 163 { |
| 164 ASSERT(scriptState->perContextData()); | 164 ASSERT(scriptState->perContextData()); |
| 165 ASSERT(scriptState->executionContext()); | 165 ASSERT(scriptState->getExecutionContext()); |
| 166 v8::Isolate* isolate = scriptState->isolate(); | 166 v8::Isolate* isolate = scriptState->isolate(); |
| 167 v8::Local<v8::Value> compiledClass = scriptState->perContextData()->compiled
PrivateScript(className); | 167 v8::Local<v8::Value> compiledClass = scriptState->perContextData()->compiled
PrivateScript(className); |
| 168 if (compiledClass.IsEmpty()) { | 168 if (compiledClass.IsEmpty()) { |
| 169 v8::Local<v8::Value> installedClasses = scriptState->perContextData()->c
ompiledPrivateScript("PrivateScriptRunner"); | 169 v8::Local<v8::Value> installedClasses = scriptState->perContextData()->c
ompiledPrivateScript("PrivateScriptRunner"); |
| 170 if (installedClasses.IsEmpty()) { | 170 if (installedClasses.IsEmpty()) { |
| 171 installedClasses = installPrivateScriptRunner(isolate); | 171 installedClasses = installPrivateScriptRunner(isolate); |
| 172 scriptState->perContextData()->setCompiledPrivateScript("PrivateScri
ptRunner", installedClasses); | 172 scriptState->perContextData()->setCompiledPrivateScript("PrivateScri
ptRunner", installedClasses); |
| 173 } | 173 } |
| 174 RELEASE_ASSERT(!installedClasses.IsEmpty()); | 174 RELEASE_ASSERT(!installedClasses.IsEmpty()); |
| 175 RELEASE_ASSERT(installedClasses->IsObject()); | 175 RELEASE_ASSERT(installedClasses->IsObject()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 189 v8::Local<v8::Object> holderObject = v8::Local<v8::Object>::Cast(holder); | 189 v8::Local<v8::Object> holderObject = v8::Local<v8::Object>::Cast(holder); |
| 190 v8::Isolate* isolate = scriptState->isolate(); | 190 v8::Isolate* isolate = scriptState->isolate(); |
| 191 v8::Local<v8::Context> context = scriptState->context(); | 191 v8::Local<v8::Context> context = scriptState->context(); |
| 192 v8::Local<v8::Value> isInitialized = V8HiddenValue::getHiddenValue(scriptSta
te, holderObject, V8HiddenValue::privateScriptObjectIsInitialized(isolate)); | 192 v8::Local<v8::Value> isInitialized = V8HiddenValue::getHiddenValue(scriptSta
te, holderObject, V8HiddenValue::privateScriptObjectIsInitialized(isolate)); |
| 193 if (isInitialized.IsEmpty()) { | 193 if (isInitialized.IsEmpty()) { |
| 194 v8::TryCatch block(isolate); | 194 v8::TryCatch block(isolate); |
| 195 v8::Local<v8::Value> initializeFunction; | 195 v8::Local<v8::Value> initializeFunction; |
| 196 if (classObject->Get(scriptState->context(), v8String(isolate, "initiali
ze")).ToLocal(&initializeFunction) && initializeFunction->IsFunction()) { | 196 if (classObject->Get(scriptState->context(), v8String(isolate, "initiali
ze")).ToLocal(&initializeFunction) && initializeFunction->IsFunction()) { |
| 197 v8::TryCatch block(isolate); | 197 v8::TryCatch block(isolate); |
| 198 v8::Local<v8::Value> result; | 198 v8::Local<v8::Value> result; |
| 199 if (!V8ScriptRunner::callFunction(v8::Local<v8::Function>::Cast(init
ializeFunction), scriptState->executionContext(), holder, 0, 0, isolate).ToLocal
(&result)) { | 199 if (!V8ScriptRunner::callFunction(v8::Local<v8::Function>::Cast(init
ializeFunction), scriptState->getExecutionContext(), holder, 0, 0, isolate).ToLo
cal(&result)) { |
| 200 fprintf(stderr, "Private script error: Object constructor threw
an exception.\n"); | 200 fprintf(stderr, "Private script error: Object constructor threw
an exception.\n"); |
| 201 dumpV8Message(context, block.Message()); | 201 dumpV8Message(context, block.Message()); |
| 202 RELEASE_ASSERT_NOT_REACHED(); | 202 RELEASE_ASSERT_NOT_REACHED(); |
| 203 } | 203 } |
| 204 } | 204 } |
| 205 | 205 |
| 206 // Inject the prototype object of the private script into the prototype
chain of the holder object. | 206 // Inject the prototype object of the private script into the prototype
chain of the holder object. |
| 207 // This is necessary to let the holder object use properties defined on
the prototype object | 207 // This is necessary to let the holder object use properties defined on
the prototype object |
| 208 // of the private script. (e.g., if the prototype object has |foo|, the
holder object should be able | 208 // of the private script. (e.g., if the prototype object has |foo|, the
holder object should be able |
| 209 // to use it with |this.foo|.) | 209 // to use it with |this.foo|.) |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 RELEASE_ASSERT_NOT_REACHED(); | 298 RELEASE_ASSERT_NOT_REACHED(); |
| 299 } | 299 } |
| 300 v8::Local<v8::Value> getter; | 300 v8::Local<v8::Value> getter; |
| 301 if (!v8::Local<v8::Object>::Cast(descriptor)->Get(scriptState->context(), v8
String(isolate, "get")).ToLocal(&getter) || !getter->IsFunction()) { | 301 if (!v8::Local<v8::Object>::Cast(descriptor)->Get(scriptState->context(), v8
String(isolate, "get")).ToLocal(&getter) || !getter->IsFunction()) { |
| 302 fprintf(stderr, "Private script error: Target DOM attribute getter was n
ot found. (Class name = %s, Attribute name = %s)\n", className, attributeName); | 302 fprintf(stderr, "Private script error: Target DOM attribute getter was n
ot found. (Class name = %s, Attribute name = %s)\n", className, attributeName); |
| 303 RELEASE_ASSERT_NOT_REACHED(); | 303 RELEASE_ASSERT_NOT_REACHED(); |
| 304 } | 304 } |
| 305 initializeHolderIfNeeded(scriptState, classObject, holder); | 305 initializeHolderIfNeeded(scriptState, classObject, holder); |
| 306 v8::TryCatch block(isolate); | 306 v8::TryCatch block(isolate); |
| 307 v8::Local<v8::Value> result; | 307 v8::Local<v8::Value> result; |
| 308 if (!V8ScriptRunner::callFunction(v8::Local<v8::Function>::Cast(getter), scr
iptState->executionContext(), holder, 0, 0, isolate).ToLocal(&result)) { | 308 if (!V8ScriptRunner::callFunction(v8::Local<v8::Function>::Cast(getter), scr
iptState->getExecutionContext(), holder, 0, 0, isolate).ToLocal(&result)) { |
| 309 rethrowExceptionInPrivateScript(isolate, block, scriptStateInUserScript,
ExceptionState::GetterContext, attributeName, className); | 309 rethrowExceptionInPrivateScript(isolate, block, scriptStateInUserScript,
ExceptionState::GetterContext, attributeName, className); |
| 310 block.ReThrow(); | 310 block.ReThrow(); |
| 311 return v8::Local<v8::Value>(); | 311 return v8::Local<v8::Value>(); |
| 312 } | 312 } |
| 313 return result; | 313 return result; |
| 314 } | 314 } |
| 315 | 315 |
| 316 bool PrivateScriptRunner::runDOMAttributeSetter(ScriptState* scriptState, Script
State* scriptStateInUserScript, const char* className, const char* attributeName
, v8::Local<v8::Value> holder, v8::Local<v8::Value> v8Value) | 316 bool PrivateScriptRunner::runDOMAttributeSetter(ScriptState* scriptState, Script
State* scriptStateInUserScript, const char* className, const char* attributeName
, v8::Local<v8::Value> holder, v8::Local<v8::Value> v8Value) |
| 317 { | 317 { |
| 318 v8::Isolate* isolate = scriptState->isolate(); | 318 v8::Isolate* isolate = scriptState->isolate(); |
| 319 v8::Local<v8::Object> classObject = classObjectOfPrivateScript(scriptState,
className); | 319 v8::Local<v8::Object> classObject = classObjectOfPrivateScript(scriptState,
className); |
| 320 v8::Local<v8::Value> descriptor; | 320 v8::Local<v8::Value> descriptor; |
| 321 if (!classObject->GetOwnPropertyDescriptor(scriptState->context(), v8String(
isolate, attributeName)).ToLocal(&descriptor) || !descriptor->IsObject()) { | 321 if (!classObject->GetOwnPropertyDescriptor(scriptState->context(), v8String(
isolate, attributeName)).ToLocal(&descriptor) || !descriptor->IsObject()) { |
| 322 fprintf(stderr, "Private script error: Target DOM attribute setter was n
ot found. (Class name = %s, Attribute name = %s)\n", className, attributeName); | 322 fprintf(stderr, "Private script error: Target DOM attribute setter was n
ot found. (Class name = %s, Attribute name = %s)\n", className, attributeName); |
| 323 RELEASE_ASSERT_NOT_REACHED(); | 323 RELEASE_ASSERT_NOT_REACHED(); |
| 324 } | 324 } |
| 325 v8::Local<v8::Value> setter; | 325 v8::Local<v8::Value> setter; |
| 326 if (!v8::Local<v8::Object>::Cast(descriptor)->Get(scriptState->context(), v8
String(isolate, "set")).ToLocal(&setter) || !setter->IsFunction()) { | 326 if (!v8::Local<v8::Object>::Cast(descriptor)->Get(scriptState->context(), v8
String(isolate, "set")).ToLocal(&setter) || !setter->IsFunction()) { |
| 327 fprintf(stderr, "Private script error: Target DOM attribute setter was n
ot found. (Class name = %s, Attribute name = %s)\n", className, attributeName); | 327 fprintf(stderr, "Private script error: Target DOM attribute setter was n
ot found. (Class name = %s, Attribute name = %s)\n", className, attributeName); |
| 328 RELEASE_ASSERT_NOT_REACHED(); | 328 RELEASE_ASSERT_NOT_REACHED(); |
| 329 } | 329 } |
| 330 initializeHolderIfNeeded(scriptState, classObject, holder); | 330 initializeHolderIfNeeded(scriptState, classObject, holder); |
| 331 v8::Local<v8::Value> argv[] = { v8Value }; | 331 v8::Local<v8::Value> argv[] = { v8Value }; |
| 332 v8::TryCatch block(isolate); | 332 v8::TryCatch block(isolate); |
| 333 v8::Local<v8::Value> result; | 333 v8::Local<v8::Value> result; |
| 334 if (!V8ScriptRunner::callFunction(v8::Local<v8::Function>::Cast(setter), scr
iptState->executionContext(), holder, WTF_ARRAY_LENGTH(argv), argv, isolate).ToL
ocal(&result)) { | 334 if (!V8ScriptRunner::callFunction(v8::Local<v8::Function>::Cast(setter), scr
iptState->getExecutionContext(), holder, WTF_ARRAY_LENGTH(argv), argv, isolate).
ToLocal(&result)) { |
| 335 rethrowExceptionInPrivateScript(isolate, block, scriptStateInUserScript,
ExceptionState::SetterContext, attributeName, className); | 335 rethrowExceptionInPrivateScript(isolate, block, scriptStateInUserScript,
ExceptionState::SetterContext, attributeName, className); |
| 336 block.ReThrow(); | 336 block.ReThrow(); |
| 337 return false; | 337 return false; |
| 338 } | 338 } |
| 339 return true; | 339 return true; |
| 340 } | 340 } |
| 341 | 341 |
| 342 v8::Local<v8::Value> PrivateScriptRunner::runDOMMethod(ScriptState* scriptState,
ScriptState* scriptStateInUserScript, const char* className, const char* method
Name, v8::Local<v8::Value> holder, int argc, v8::Local<v8::Value> argv[]) | 342 v8::Local<v8::Value> PrivateScriptRunner::runDOMMethod(ScriptState* scriptState,
ScriptState* scriptStateInUserScript, const char* className, const char* method
Name, v8::Local<v8::Value> holder, int argc, v8::Local<v8::Value> argv[]) |
| 343 { | 343 { |
| 344 v8::Local<v8::Object> classObject = classObjectOfPrivateScript(scriptState,
className); | 344 v8::Local<v8::Object> classObject = classObjectOfPrivateScript(scriptState,
className); |
| 345 v8::Local<v8::Value> method; | 345 v8::Local<v8::Value> method; |
| 346 if (!classObject->Get(scriptState->context(), v8String(scriptState->isolate(
), methodName)).ToLocal(&method) || !method->IsFunction()) { | 346 if (!classObject->Get(scriptState->context(), v8String(scriptState->isolate(
), methodName)).ToLocal(&method) || !method->IsFunction()) { |
| 347 fprintf(stderr, "Private script error: Target DOM method was not found.
(Class name = %s, Method name = %s)\n", className, methodName); | 347 fprintf(stderr, "Private script error: Target DOM method was not found.
(Class name = %s, Method name = %s)\n", className, methodName); |
| 348 RELEASE_ASSERT_NOT_REACHED(); | 348 RELEASE_ASSERT_NOT_REACHED(); |
| 349 } | 349 } |
| 350 initializeHolderIfNeeded(scriptState, classObject, holder); | 350 initializeHolderIfNeeded(scriptState, classObject, holder); |
| 351 v8::TryCatch block(scriptState->isolate()); | 351 v8::TryCatch block(scriptState->isolate()); |
| 352 v8::Local<v8::Value> result; | 352 v8::Local<v8::Value> result; |
| 353 if (!V8ScriptRunner::callFunction(v8::Local<v8::Function>::Cast(method), scr
iptState->executionContext(), holder, argc, argv, scriptState->isolate()).ToLoca
l(&result)) { | 353 if (!V8ScriptRunner::callFunction(v8::Local<v8::Function>::Cast(method), scr
iptState->getExecutionContext(), holder, argc, argv, scriptState->isolate()).ToL
ocal(&result)) { |
| 354 rethrowExceptionInPrivateScript(scriptState->isolate(), block, scriptSta
teInUserScript, ExceptionState::ExecutionContext, methodName, className); | 354 rethrowExceptionInPrivateScript(scriptState->isolate(), block, scriptSta
teInUserScript, ExceptionState::ExecutionContext, methodName, className); |
| 355 block.ReThrow(); | 355 block.ReThrow(); |
| 356 return v8::Local<v8::Value>(); | 356 return v8::Local<v8::Value>(); |
| 357 } | 357 } |
| 358 return result; | 358 return result; |
| 359 } | 359 } |
| 360 | 360 |
| 361 } // namespace blink | 361 } // namespace blink |
| OLD | NEW |