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 "extensions/renderer/utils_native_handler.h" | 5 #include "extensions/renderer/utils_native_handler.h" |
6 | 6 |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "extensions/renderer/script_context.h" | 8 #include "extensions/renderer/script_context.h" |
| 9 #include "extensions/renderer/v8_maybe_helpers.h" |
9 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" | 10 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" |
10 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" | 11 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" |
11 | 12 |
12 namespace extensions { | 13 namespace extensions { |
13 | 14 |
14 UtilsNativeHandler::UtilsNativeHandler(ScriptContext* context) | 15 UtilsNativeHandler::UtilsNativeHandler(ScriptContext* context) |
15 : ObjectBackedNativeHandler(context) { | 16 : ObjectBackedNativeHandler(context) { |
16 RouteFunction("createClassWrapper", | 17 RouteFunction("createClassWrapper", |
17 base::Bind(&UtilsNativeHandler::CreateClassWrapper, | 18 base::Bind(&UtilsNativeHandler::CreateClassWrapper, |
18 base::Unretained(this))); | 19 base::Unretained(this))); |
19 RouteFunction( | 20 RouteFunction( |
20 "deepCopy", | 21 "deepCopy", |
21 base::Bind(&UtilsNativeHandler::DeepCopy, base::Unretained(this))); | 22 base::Bind(&UtilsNativeHandler::DeepCopy, base::Unretained(this))); |
22 } | 23 } |
23 | 24 |
24 UtilsNativeHandler::~UtilsNativeHandler() {} | 25 UtilsNativeHandler::~UtilsNativeHandler() {} |
25 | 26 |
26 void UtilsNativeHandler::CreateClassWrapper( | 27 void UtilsNativeHandler::CreateClassWrapper( |
27 const v8::FunctionCallbackInfo<v8::Value>& args) { | 28 const v8::FunctionCallbackInfo<v8::Value>& args) { |
28 CHECK_EQ(3, args.Length()); | 29 CHECK_EQ(3, args.Length()); |
29 CHECK(args[0]->IsString()); | 30 CHECK(args[0]->IsString()); |
30 std::string name = *v8::String::Utf8Value(args[0]); | 31 std::string name = *v8::String::Utf8Value(args[0]); |
31 CHECK(args[1]->IsObject()); | 32 CHECK(args[1]->IsObject()); |
32 v8::Local<v8::Object> cls = args[1].As<v8::Object>(); | 33 v8::Local<v8::Object> cls = args[1].As<v8::Object>(); |
33 CHECK(args[2]->IsObject() || args[2]->IsUndefined()); | 34 CHECK(args[2]->IsObject() || args[2]->IsUndefined()); |
34 v8::Local<v8::Value> superclass = args[2]; | 35 v8::Local<v8::Value> superclass = args[2]; |
35 | 36 |
36 v8::HandleScope handle_scope(GetIsolate()); | 37 v8::HandleScope handle_scope(GetIsolate()); |
37 // TODO(fsamuel): Consider moving the source wrapping to ModuleSystem. | 38 // TODO(fsamuel): Consider moving the source wrapping to ModuleSystem. |
38 v8::Local<v8::String> source = v8::String::NewFromUtf8( | 39 v8::Local<v8::String> source = ToV8String( |
39 GetIsolate(), | 40 GetIsolate(), |
40 base::StringPrintf( | 41 base::StringPrintf( |
41 "(function($Object, $Function, privates, cls, superclass) {" | 42 "(function($Object, $Function, privates, cls, superclass) {" |
42 "'use strict';\n" | 43 "'use strict';\n" |
43 " function %s() {\n" | 44 " function %s() {\n" |
44 " var privateObj = $Object.create(cls.prototype);\n" | 45 " var privateObj = $Object.create(cls.prototype);\n" |
45 " $Function.apply(cls, privateObj, arguments);\n" | 46 " $Function.apply(cls, privateObj, arguments);\n" |
46 " privateObj.wrapper = this;\n" | 47 " privateObj.wrapper = this;\n" |
47 " privates(this).impl = privateObj;\n" | 48 " privates(this).impl = privateObj;\n" |
48 " };\n" | 49 " };\n" |
49 " if (superclass) {\n" | 50 " if (superclass) {\n" |
50 " %s.prototype = Object.create(superclass.prototype);\n" | 51 " %s.prototype = Object.create(superclass.prototype);\n" |
51 " }\n" | 52 " }\n" |
52 " return %s;\n" | 53 " return %s;\n" |
53 "})", | 54 "})", |
54 name.c_str(), name.c_str(), name.c_str()).c_str()); | 55 name.c_str(), name.c_str(), name.c_str()).c_str()); |
55 v8::Local<v8::Value> func_as_value = context()->module_system()->RunString( | 56 v8::Local<v8::Value> func_as_value = context()->module_system()->RunString( |
56 source, v8::String::NewFromUtf8(GetIsolate(), name.c_str())); | 57 source, ToV8String(GetIsolate(), name.c_str())); |
57 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) { | 58 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) { |
58 args.GetReturnValue().SetUndefined(); | 59 args.GetReturnValue().SetUndefined(); |
59 return; | 60 return; |
60 } | 61 } |
61 | 62 |
62 // TODO(fsamuel): Move privates from ModuleSystem to a shared location. | 63 // TODO(fsamuel): Move privates from ModuleSystem to a shared location. |
63 v8::Local<v8::Object> natives(context()->module_system()->NewInstance()); | 64 v8::Local<v8::Object> natives; |
64 CHECK(!natives.IsEmpty()); // this can happen if v8 has issues | 65 if (!context()->module_system()->NewInstance().ToLocal(&natives)) |
| 66 NOTREACHED(); // this can happen if v8 has issues |
65 v8::Local<v8::Function> func = func_as_value.As<v8::Function>(); | 67 v8::Local<v8::Function> func = func_as_value.As<v8::Function>(); |
| 68 v8::Local<v8::Context> v8_context = context()->v8_context(); |
| 69 v8::Local<v8::Value> privates = UnsafeGet( |
| 70 v8_context, natives, |
| 71 ToV8String(GetIsolate(), "privates", v8::NewStringType::kInternalized)); |
| 72 |
66 v8::Local<v8::Value> func_args[] = { | 73 v8::Local<v8::Value> func_args[] = { |
67 context()->safe_builtins()->GetObjekt(), | 74 context()->safe_builtins()->GetObjekt(), |
68 context()->safe_builtins()->GetFunction(), | 75 context()->safe_builtins()->GetFunction(), |
69 natives->Get(v8::String::NewFromUtf8(GetIsolate(), "privates", | 76 privates, |
70 v8::String::kInternalizedString)), | |
71 cls, | 77 cls, |
72 superclass}; | 78 superclass}; |
73 v8::Local<v8::Value> result; | 79 v8::Local<v8::Value> result; |
74 { | 80 { |
75 v8::TryCatch try_catch; | 81 v8::TryCatch try_catch(GetIsolate()); |
76 try_catch.SetCaptureMessage(true); | 82 try_catch.SetCaptureMessage(true); |
77 result = context()->CallFunction(func, arraysize(func_args), func_args); | 83 result = context()->CallFunction(func, arraysize(func_args), func_args); |
78 if (try_catch.HasCaught()) { | 84 if (try_catch.HasCaught()) { |
79 args.GetReturnValue().SetUndefined(); | 85 args.GetReturnValue().SetUndefined(); |
80 return; | 86 return; |
81 } | 87 } |
82 } | 88 } |
83 args.GetReturnValue().Set(result); | 89 args.GetReturnValue().Set(result); |
84 } | 90 } |
85 | 91 |
86 void UtilsNativeHandler::DeepCopy( | 92 void UtilsNativeHandler::DeepCopy( |
87 const v8::FunctionCallbackInfo<v8::Value>& args) { | 93 const v8::FunctionCallbackInfo<v8::Value>& args) { |
88 CHECK_EQ(1, args.Length()); | 94 CHECK_EQ(1, args.Length()); |
89 args.GetReturnValue().Set( | 95 args.GetReturnValue().Set( |
90 blink::WebSerializedScriptValue::serialize(args[0]).deserialize()); | 96 blink::WebSerializedScriptValue::serialize(args[0]).deserialize()); |
91 } | 97 } |
92 | 98 |
93 } // namespace extensions | 99 } // namespace extensions |
OLD | NEW |