Add missing 'gpu_tests' config to MB, fix typo in MB.
[chromium-blink-merge.git] / extensions / renderer / object_backed_native_handler.cc
blobf93610de3b97ed40eb1c7f788e1071490ba04fa5
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/object_backed_native_handler.h"
7 #include "base/logging.h"
8 #include "base/memory/linked_ptr.h"
9 #include "extensions/renderer/console.h"
10 #include "extensions/renderer/module_system.h"
11 #include "extensions/renderer/script_context.h"
12 #include "extensions/renderer/script_context_set.h"
13 #include "extensions/renderer/v8_helpers.h"
14 #include "v8/include/v8.h"
16 namespace extensions {
18 using namespace v8_helpers;
20 namespace {
21 // Key for the base::Bound routed function.
22 const char* kHandlerFunction = "handler_function";
23 } // namespace
25 ObjectBackedNativeHandler::ObjectBackedNativeHandler(ScriptContext* context)
26 : router_data_(context->isolate()),
27 context_(context),
28 object_template_(context->isolate(),
29 v8::ObjectTemplate::New(context->isolate())) {
32 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() {
35 v8::Local<v8::Object> ObjectBackedNativeHandler::NewInstance() {
36 return v8::Local<v8::ObjectTemplate>::New(GetIsolate(), object_template_)
37 ->NewInstance();
40 // static
41 void ObjectBackedNativeHandler::Router(
42 const v8::FunctionCallbackInfo<v8::Value>& args) {
43 v8::HandleScope handle_scope(args.GetIsolate());
44 v8::Local<v8::Object> data = args.Data().As<v8::Object>();
46 v8::Local<v8::Value> handler_function_value =
47 data->Get(v8::String::NewFromUtf8(args.GetIsolate(), kHandlerFunction));
48 // See comment in header file for why we do this.
49 if (IsEmptyOrUndefined(handler_function_value)) {
50 ScriptContext* script_context = ScriptContextSet::GetContextByV8Context(
51 args.GetIsolate()->GetCallingContext());
52 console::Error(script_context ? script_context->GetRenderFrame() : nullptr,
53 "Extension view no longer exists");
54 return;
56 DCHECK(handler_function_value->IsExternal());
57 static_cast<HandlerFunction*>(
58 handler_function_value.As<v8::External>()->Value())->Run(args);
61 void ObjectBackedNativeHandler::RouteFunction(
62 const std::string& name,
63 const HandlerFunction& handler_function) {
64 v8::Isolate* isolate = v8::Isolate::GetCurrent();
65 v8::HandleScope handle_scope(isolate);
66 v8::Local<v8::Context> v8_context = context_->v8_context();
67 v8::Context::Scope context_scope(v8_context);
69 v8::Local<v8::Object> data = v8::Object::New(isolate);
70 SetProperty(
71 v8_context, data, kHandlerFunction,
72 v8::External::New(isolate, new HandlerFunction(handler_function)));
74 // TODO(kalman): Cache these. See https://crbug.com/478744.
75 v8::Local<v8::FunctionTemplate> function_template =
76 v8::FunctionTemplate::New(isolate, Router, data);
77 v8::Local<v8::ObjectTemplate>::New(isolate, object_template_)
78 ->Set(isolate, name.c_str(), function_template);
79 router_data_.Append(data);
82 v8::Isolate* ObjectBackedNativeHandler::GetIsolate() const {
83 return context_->isolate();
86 void ObjectBackedNativeHandler::Invalidate() {
87 v8::Isolate* isolate = GetIsolate();
88 v8::HandleScope handle_scope(isolate);
89 v8::Local<v8::Context> v8_context = context_->v8_context();
90 v8::Context::Scope context_scope(v8_context);
92 for (size_t i = 0; i < router_data_.Size(); i++) {
93 v8::Local<v8::Object> data = router_data_.Get(i);
94 v8::Local<v8::Value> handler_function_value =
95 GetPropertyUnsafe(v8_context, data, kHandlerFunction);
96 CHECK(!handler_function_value.IsEmpty());
97 delete static_cast<HandlerFunction*>(
98 handler_function_value.As<v8::External>()->Value());
99 DeletePropertyUnsafe(v8_context, data, kHandlerFunction);
102 router_data_.Clear();
103 object_template_.Reset();
105 NativeHandler::Invalidate();
108 } // namespace extensions