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