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