1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "config.h"
#include "bindings/core/v8/PrivateScriptRunner.h"
#include "bindings/core/v8/DOMWrapperWorld.h"
#include "bindings/core/v8/ExceptionState.h"
#include "bindings/core/v8/V8Binding.h"
#include "bindings/core/v8/V8PerContextData.h"
#include "bindings/core/v8/V8ScriptRunner.h"
#include "core/PrivateScriptSources.h"
#ifndef NDEBUG
#include "core/PrivateScriptSourcesForTesting.h"
#endif
#include "core/dom/Document.h"
#include "core/dom/ExceptionCode.h"
#include "platform/PlatformResourceLoader.h"
namespace blink {
static void dumpV8Message(v8::Handle<v8::Message> message)
{
if (message.IsEmpty())
return;
// FIXME: GetScriptOrigin() and GetLineNumber() return empty handles
// when they are called at the first time if V8 has a pending exception.
// So we need to call twice to get a correct ScriptOrigin and line number.
// This is a bug of V8.
message->GetScriptOrigin();
message->GetLineNumber();
v8::Handle<v8::Value> resourceName = message->GetScriptOrigin().ResourceName();
String fileName = "Unknown JavaScript file";
if (!resourceName.IsEmpty() && resourceName->IsString())
fileName = toCoreString(v8::Handle<v8::String>::Cast(resourceName));
int lineNumber = message->GetLineNumber();
v8::Handle<v8::String> errorMessage = message->Get();
fprintf(stderr, "%s (line %d): %s\n", fileName.utf8().data(), lineNumber, toCoreString(errorMessage).utf8().data());
}
static void importFunction(const v8::FunctionCallbackInfo<v8::Value>& args);
static v8::Handle<v8::Value> compileAndRunPrivateScript(v8::Isolate* isolate, String scriptClassName, const char* source, size_t size)
{
v8::TryCatch block;
String sourceString(source, size);
String fileName = scriptClassName + ".js";
v8::Handle<v8::Object> global = isolate->GetCurrentContext()->Global();
v8::Handle<v8::Value> privateScriptController = global->Get(v8String(isolate, "privateScriptController"));
RELEASE_ASSERT(privateScriptController->IsUndefined() || privateScriptController->IsObject());
if (privateScriptController->IsObject()) {
v8::Handle<v8::Object> privateScriptControllerObject = privateScriptController->ToObject(isolate);
v8::Handle<v8::Value> importFunctionValue = privateScriptControllerObject->Get(v8String(isolate, "import"));
if (importFunctionValue->IsUndefined())
privateScriptControllerObject->Set(v8String(isolate, "import"), v8::FunctionTemplate::New(isolate, importFunction)->GetFunction());
}
v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(v8String(isolate, sourceString), fileName, TextPosition::minimumPosition(), 0, 0, isolate, NotSharableCrossOrigin);
if (block.HasCaught()) {
fprintf(stderr, "Private script error: Compile failed. (Class name = %s)\n", scriptClassName.utf8().data());
dumpV8Message(block.Message());
RELEASE_ASSERT_NOT_REACHED();
}
v8::Handle<v8::Value> result = V8ScriptRunner::runCompiledInternalScript(isolate, script);
if (block.HasCaught()) {
fprintf(stderr, "Private script error: installClass() failed. (Class name = %s)\n", scriptClassName.utf8().data());
dumpV8Message(block.Message());
RELEASE_ASSERT_NOT_REACHED();
}
return result;
}
void importFunction(const v8::FunctionCallbackInfo<v8::Value>& args)
{
v8::Isolate* isolate = args.GetIsolate();
RELEASE_ASSERT(isolate && (args.Length() == 1));
String resourceFileName = toCoreString(args[0]->ToString());
String resourceData = loadResourceAsASCIIString(resourceFileName.utf8().data());
RELEASE_ASSERT(resourceData.length());
if (resourceFileName.endsWith(".js"))
compileAndRunPrivateScript(isolate, resourceFileName.replace(".js", ""), resourceData.utf8().data(), resourceData.length());
else if (resourceFileName.endsWith(".css"))
args.GetReturnValue().Set(v8String(isolate, resourceData));
}
// FIXME: If we have X.js, XPartial-1.js and XPartial-2.js, currently all of the JS files
// are compiled when any of the JS files is requested. Ideally we should avoid compiling
// unrelated JS files. For example, if a method in XPartial-1.js is requested, we just
// need to compile X.js and XPartial-1.js, and don't need to compile XPartial-2.js.
static void installPrivateScript(v8::Isolate* isolate, String className)
{
int compiledScriptCount = 0;
// |kPrivateScriptSourcesForTesting| is defined in V8PrivateScriptSources.h, which is auto-generated
// by make_private_script_source.py.
#ifndef NDEBUG
for (size_t index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSourcesForTesting); index++) {
if (className == kPrivateScriptSourcesForTesting[index].className) {
compileAndRunPrivateScript(isolate, kPrivateScriptSourcesForTesting[index].scriptClassName, kPrivateScriptSourcesForTesting[index].source, kPrivateScriptSourcesForTesting[index].size);
compiledScriptCount++;
}
}
#endif
// |kPrivateScriptSources| is defined in V8PrivateScriptSources.h, which is auto-generated
// by make_private_script_source.py.
for (size_t index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSources); index++) {
if (className == kPrivateScriptSources[index].className) {
String resourceData = loadResourceAsASCIIString(kPrivateScriptSources[index].resourceFile);
compileAndRunPrivateScript(isolate, kPrivateScriptSources[index].scriptClassName, resourceData.utf8().data(), resourceData.length());
compiledScriptCount++;
}
}
if (!compiledScriptCount) {
fprintf(stderr, "Private script error: Target source code was not found. (Class name = %s)\n", className.utf8().data());
RELEASE_ASSERT_NOT_REACHED();
}
}
static v8::Handle<v8::Value> installPrivateScriptRunner(v8::Isolate* isolate)
{
const String className = "PrivateScriptRunner";
size_t index;
// |kPrivateScriptSources| is defined in V8PrivateScriptSources.h, which is auto-generated
// by make_private_script_source.py.
for (index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSources); index++) {
if (className == kPrivateScriptSources[index].className)
break;
}
if (index == WTF_ARRAY_LENGTH(kPrivateScriptSources)) {
fprintf(stderr, "Private script error: Target source code was not found. (Class name = %s)\n", className.utf8().data());
RELEASE_ASSERT_NOT_REACHED();
}
String resourceData = loadResourceAsASCIIString(kPrivateScriptSources[index].resourceFile);
return compileAndRunPrivateScript(isolate, className, resourceData.utf8().data(), resourceData.length());
}
static v8::Handle<v8::Object> classObjectOfPrivateScript(ScriptState* scriptState, String className)
{
ASSERT(scriptState->perContextData());
ASSERT(scriptState->executionContext());
v8::Isolate* isolate = scriptState->isolate();
v8::Handle<v8::Value> compiledClass = scriptState->perContextData()->compiledPrivateScript(className);
if (compiledClass.IsEmpty()) {
v8::Handle<v8::Value> installedClasses = scriptState->perContextData()->compiledPrivateScript("PrivateScriptRunner");
if (installedClasses.IsEmpty()) {
installedClasses = installPrivateScriptRunner(isolate);
scriptState->perContextData()->setCompiledPrivateScript("PrivateScriptRunner", installedClasses);
}
RELEASE_ASSERT(!installedClasses.IsEmpty());
RELEASE_ASSERT(installedClasses->IsObject());
installPrivateScript(isolate, className);
compiledClass = v8::Handle<v8::Object>::Cast(installedClasses)->Get(v8String(isolate, className));
RELEASE_ASSERT(!compiledClass.IsEmpty());
RELEASE_ASSERT(compiledClass->IsObject());
scriptState->perContextData()->setCompiledPrivateScript(className, compiledClass);
}
return v8::Handle<v8::Object>::Cast(compiledClass);
}
static void initializeHolderIfNeeded(ScriptState* scriptState, v8::Handle<v8::Object> classObject, v8::Handle<v8::Value> holder)
{
RELEASE_ASSERT(!holder.IsEmpty());
RELEASE_ASSERT(holder->IsObject());
v8::Handle<v8::Object> holderObject = v8::Handle<v8::Object>::Cast(holder);
v8::Isolate* isolate = scriptState->isolate();
v8::Handle<v8::Value> isInitialized = V8HiddenValue::getHiddenValue(isolate, holderObject, V8HiddenValue::privateScriptObjectIsInitialized(isolate));
if (isInitialized.IsEmpty()) {
v8::TryCatch block;
v8::Handle<v8::Value> initializeFunction = classObject->Get(v8String(isolate, "initialize"));
if (!initializeFunction.IsEmpty() && initializeFunction->IsFunction()) {
v8::TryCatch block;
V8ScriptRunner::callFunction(v8::Handle<v8::Function>::Cast(initializeFunction), scriptState->executionContext(), holder, 0, 0, isolate);
if (block.HasCaught()) {
fprintf(stderr, "Private script error: Object constructor threw an exception.\n");
dumpV8Message(block.Message());
RELEASE_ASSERT_NOT_REACHED();
}
}
// Inject the prototype object of the private script into the prototype chain of the holder object.
// This is necessary to let the holder object use properties defined on the prototype object
// of the private script. (e.g., if the prototype object has |foo|, the holder object should be able
// to use it with |this.foo|.)
if (classObject->GetPrototype() != holderObject->GetPrototype())
classObject->SetPrototype(holderObject->GetPrototype());
holderObject->SetPrototype(classObject);
isInitialized = v8Boolean(true, isolate);
V8HiddenValue::setHiddenValue(isolate, holderObject, V8HiddenValue::privateScriptObjectIsInitialized(isolate), isInitialized);
}
}
v8::Handle<v8::Value> PrivateScriptRunner::installClassIfNeeded(Document* document, String className)
{
v8::HandleScope handleScope(toIsolate(document));
v8::Handle<v8::Context> context = toV8Context(document->contextDocument().get(), DOMWrapperWorld::privateScriptIsolatedWorld());
if (context.IsEmpty())
return v8::Handle<v8::Value>();
ScriptState* scriptState = ScriptState::from(context);
if (!scriptState->executionContext())
return v8::Handle<v8::Value>();
ScriptState::Scope scope(scriptState);
return classObjectOfPrivateScript(scriptState, className);
}
namespace {
void rethrowExceptionInPrivateScript(v8::Isolate* isolate, v8::TryCatch& block, ScriptState* scriptStateInUserScript, ExceptionState::Context errorContext, const char* propertyName, const char* interfaceName)
{
v8::Handle<v8::Value> exception = block.Exception();
RELEASE_ASSERT(!exception.IsEmpty() && exception->IsObject());
v8::Handle<v8::Object> exceptionObject = v8::Handle<v8::Object>::Cast(exception);
v8::Handle<v8::Value> name = exceptionObject->Get(v8String(isolate, "name"));
RELEASE_ASSERT(!name.IsEmpty() && name->IsString());
v8::Handle<v8::Message> tryCatchMessage = block.Message();
v8::Handle<v8::Value> message = exceptionObject->Get(v8String(isolate, "message"));
String messageString;
if (!message.IsEmpty() && message->IsString())
messageString = toCoreString(v8::Handle<v8::String>::Cast(message));
String exceptionName = toCoreString(v8::Handle<v8::String>::Cast(name));
if (exceptionName == "PrivateScriptException") {
v8::Handle<v8::Value> code = exceptionObject->Get(v8String(isolate, "code"));
RELEASE_ASSERT(!code.IsEmpty() && code->IsInt32());
ScriptState::Scope scope(scriptStateInUserScript);
ExceptionState exceptionState(errorContext, propertyName, interfaceName, scriptStateInUserScript->context()->Global(), scriptStateInUserScript->isolate());
exceptionState.throwDOMException(toInt32(code), messageString);
exceptionState.throwIfNeeded();
return;
}
// Standard JS errors thrown by a private script are treated as real errors
// of the private script and crash the renderer, except for a stack overflow
// error. A stack overflow error can happen in a valid private script
// if user's script can create a recursion that involves the private script.
if (exceptionName == "RangeError" && messageString.contains("Maximum call stack size exceeded")) {
ScriptState::Scope scope(scriptStateInUserScript);
ExceptionState exceptionState(errorContext, propertyName, interfaceName, scriptStateInUserScript->context()->Global(), scriptStateInUserScript->isolate());
exceptionState.throwDOMException(V8RangeError, messageString);
exceptionState.throwIfNeeded();
return;
}
fprintf(stderr, "Private script error: %s was thrown.\n", exceptionName.utf8().data());
dumpV8Message(tryCatchMessage);
RELEASE_ASSERT_NOT_REACHED();
}
} // namespace
v8::Handle<v8::Value> PrivateScriptRunner::runDOMAttributeGetter(ScriptState* scriptState, ScriptState* scriptStateInUserScript, const char* className, const char* attributeName, v8::Handle<v8::Value> holder)
{
v8::Handle<v8::Object> classObject = classObjectOfPrivateScript(scriptState, className);
v8::Handle<v8::Value> descriptor = classObject->GetOwnPropertyDescriptor(v8String(scriptState->isolate(), attributeName));
if (descriptor.IsEmpty() || !descriptor->IsObject()) {
fprintf(stderr, "Private script error: Target DOM attribute getter was not found. (Class name = %s, Attribute name = %s)\n", className, attributeName);
RELEASE_ASSERT_NOT_REACHED();
}
v8::Handle<v8::Value> getter = v8::Handle<v8::Object>::Cast(descriptor)->Get(v8String(scriptState->isolate(), "get"));
if (getter.IsEmpty() || !getter->IsFunction()) {
fprintf(stderr, "Private script error: Target DOM attribute getter was not found. (Class name = %s, Attribute name = %s)\n", className, attributeName);
RELEASE_ASSERT_NOT_REACHED();
}
initializeHolderIfNeeded(scriptState, classObject, holder);
v8::TryCatch block;
v8::Handle<v8::Value> result = V8ScriptRunner::callFunction(v8::Handle<v8::Function>::Cast(getter), scriptState->executionContext(), holder, 0, 0, scriptState->isolate());
if (block.HasCaught()) {
rethrowExceptionInPrivateScript(scriptState->isolate(), block, scriptStateInUserScript, ExceptionState::GetterContext, attributeName, className);
block.ReThrow();
return v8::Handle<v8::Value>();
}
return result;
}
bool PrivateScriptRunner::runDOMAttributeSetter(ScriptState* scriptState, ScriptState* scriptStateInUserScript, const char* className, const char* attributeName, v8::Handle<v8::Value> holder, v8::Handle<v8::Value> v8Value)
{
v8::Handle<v8::Object> classObject = classObjectOfPrivateScript(scriptState, className);
v8::Handle<v8::Value> descriptor = classObject->GetOwnPropertyDescriptor(v8String(scriptState->isolate(), attributeName));
if (descriptor.IsEmpty() || !descriptor->IsObject()) {
fprintf(stderr, "Private script error: Target DOM attribute setter was not found. (Class name = %s, Attribute name = %s)\n", className, attributeName);
RELEASE_ASSERT_NOT_REACHED();
}
v8::Handle<v8::Value> setter = v8::Handle<v8::Object>::Cast(descriptor)->Get(v8String(scriptState->isolate(), "set"));
if (setter.IsEmpty() || !setter->IsFunction()) {
fprintf(stderr, "Private script error: Target DOM attribute setter was not found. (Class name = %s, Attribute name = %s)\n", className, attributeName);
RELEASE_ASSERT_NOT_REACHED();
}
initializeHolderIfNeeded(scriptState, classObject, holder);
v8::Handle<v8::Value> argv[] = { v8Value };
v8::TryCatch block;
V8ScriptRunner::callFunction(v8::Handle<v8::Function>::Cast(setter), scriptState->executionContext(), holder, WTF_ARRAY_LENGTH(argv), argv, scriptState->isolate());
if (block.HasCaught()) {
rethrowExceptionInPrivateScript(scriptState->isolate(), block, scriptStateInUserScript, ExceptionState::SetterContext, attributeName, className);
block.ReThrow();
return false;
}
return true;
}
v8::Handle<v8::Value> PrivateScriptRunner::runDOMMethod(ScriptState* scriptState, ScriptState* scriptStateInUserScript, const char* className, const char* methodName, v8::Handle<v8::Value> holder, int argc, v8::Handle<v8::Value> argv[])
{
v8::Handle<v8::Object> classObject = classObjectOfPrivateScript(scriptState, className);
v8::Handle<v8::Value> method = classObject->Get(v8String(scriptState->isolate(), methodName));
if (method.IsEmpty() || !method->IsFunction()) {
fprintf(stderr, "Private script error: Target DOM method was not found. (Class name = %s, Method name = %s)\n", className, methodName);
RELEASE_ASSERT_NOT_REACHED();
}
initializeHolderIfNeeded(scriptState, classObject, holder);
v8::TryCatch block;
v8::Handle<v8::Value> result = V8ScriptRunner::callFunction(v8::Handle<v8::Function>::Cast(method), scriptState->executionContext(), holder, argc, argv, scriptState->isolate());
if (block.HasCaught()) {
rethrowExceptionInPrivateScript(scriptState->isolate(), block, scriptStateInUserScript, ExceptionState::ExecutionContext, methodName, className);
block.ReThrow();
return v8::Handle<v8::Value>();
}
return result;
}
} // namespace blink
|