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