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/object_backed_native_handler.h" | 5 #include "extensions/renderer/object_backed_native_handler.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/linked_ptr.h" | 8 #include "base/memory/linked_ptr.h" |
9 #include "extensions/renderer/console.h" | 9 #include "extensions/renderer/console.h" |
10 #include "extensions/renderer/module_system.h" | 10 #include "extensions/renderer/module_system.h" |
11 #include "extensions/renderer/script_context.h" | 11 #include "extensions/renderer/script_context.h" |
| 12 #include "extensions/renderer/v8_maybe_helpers.h" |
12 #include "v8/include/v8.h" | 13 #include "v8/include/v8.h" |
13 | 14 |
14 namespace extensions { | 15 namespace extensions { |
15 | 16 |
16 namespace { | 17 namespace { |
17 // Key for the base::Bound routed function. | 18 // Key for the base::Bound routed function. |
18 const char* kHandlerFunction = "handler_function"; | 19 const char* kHandlerFunction = "handler_function"; |
19 } // namespace | 20 } // namespace |
20 | 21 |
21 ObjectBackedNativeHandler::ObjectBackedNativeHandler(ScriptContext* context) | 22 ObjectBackedNativeHandler::ObjectBackedNativeHandler(ScriptContext* context) |
22 : router_data_(context->isolate()), | 23 : router_data_(context->isolate()), |
23 context_(context), | 24 context_(context), |
24 object_template_(context->isolate(), | 25 object_template_(context->isolate(), |
25 v8::ObjectTemplate::New(context->isolate())) { | 26 v8::ObjectTemplate::New(context->isolate())) { |
26 } | 27 } |
27 | 28 |
28 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { | 29 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { |
29 } | 30 } |
30 | 31 |
31 v8::Local<v8::Object> ObjectBackedNativeHandler::NewInstance() { | 32 v8::MaybeLocal<v8::Object> ObjectBackedNativeHandler::NewInstance() { |
32 return v8::Local<v8::ObjectTemplate>::New(GetIsolate(), object_template_) | 33 return v8::Local<v8::ObjectTemplate>::New(GetIsolate(), object_template_) |
33 ->NewInstance(); | 34 ->NewInstance(context_->v8_context()); |
34 } | 35 } |
35 | 36 |
36 // static | 37 // static |
37 void ObjectBackedNativeHandler::Router( | 38 void ObjectBackedNativeHandler::Router( |
38 const v8::FunctionCallbackInfo<v8::Value>& args) { | 39 const v8::FunctionCallbackInfo<v8::Value>& args) { |
39 v8::HandleScope handle_scope(args.GetIsolate()); | 40 v8::HandleScope handle_scope(args.GetIsolate()); |
40 v8::Local<v8::Object> data = args.Data().As<v8::Object>(); | 41 v8::Local<v8::Object> data = args.Data().As<v8::Object>(); |
| 42 v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext(); |
41 | 43 |
42 v8::Local<v8::Value> handler_function_value = | 44 v8::Local<v8::Value> handler_function_value; |
43 data->Get(v8::String::NewFromUtf8(args.GetIsolate(), kHandlerFunction)); | |
44 // See comment in header file for why we do this. | 45 // See comment in header file for why we do this. |
45 if (handler_function_value.IsEmpty() || | 46 if (!data->Get(context, ToV8String(args.GetIsolate(), kHandlerFunction)) |
| 47 .ToLocal(&handler_function_value) || |
46 handler_function_value->IsUndefined()) { | 48 handler_function_value->IsUndefined()) { |
47 console::Error(args.GetIsolate()->GetCallingContext(), | 49 console::Error(args.GetIsolate()->GetCallingContext(), |
48 "Extension view no longer exists"); | 50 "Extension view no longer exists"); |
49 return; | 51 return; |
50 } | 52 } |
51 DCHECK(handler_function_value->IsExternal()); | 53 DCHECK(handler_function_value->IsExternal()); |
52 static_cast<HandlerFunction*>( | 54 static_cast<HandlerFunction*>( |
53 handler_function_value.As<v8::External>()->Value())->Run(args); | 55 handler_function_value.As<v8::External>()->Value())->Run(args); |
54 } | 56 } |
55 | 57 |
56 void ObjectBackedNativeHandler::RouteFunction( | 58 void ObjectBackedNativeHandler::RouteFunction( |
57 const std::string& name, | 59 const std::string& name, |
58 const HandlerFunction& handler_function) { | 60 const HandlerFunction& handler_function) { |
59 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 61 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
60 v8::HandleScope handle_scope(isolate); | 62 v8::HandleScope handle_scope(isolate); |
61 v8::Context::Scope context_scope(context_->v8_context()); | 63 v8::Local<v8::Context> v8_context = context_->v8_context(); |
| 64 v8::Context::Scope context_scope(v8_context); |
62 | 65 |
63 v8::Local<v8::Object> data = v8::Object::New(isolate); | 66 v8::Local<v8::Object> data = v8::Object::New(isolate); |
64 data->Set( | 67 SetProperty( |
65 v8::String::NewFromUtf8(isolate, kHandlerFunction), | 68 v8_context, data, ToV8String(isolate, kHandlerFunction), |
66 v8::External::New(isolate, new HandlerFunction(handler_function))); | 69 v8::External::New(isolate, new HandlerFunction(handler_function))); |
67 v8::Local<v8::FunctionTemplate> function_template = | 70 v8::Local<v8::FunctionTemplate> function_template = |
68 v8::FunctionTemplate::New(isolate, Router, data); | 71 v8::FunctionTemplate::New(isolate, Router, data); |
69 v8::Local<v8::ObjectTemplate>::New(isolate, object_template_) | 72 v8::Local<v8::ObjectTemplate>::New(isolate, object_template_) |
70 ->Set(isolate, name.c_str(), function_template); | 73 ->Set(isolate, name.c_str(), function_template); |
71 router_data_.Append(data); | 74 router_data_.Append(data); |
72 } | 75 } |
73 | 76 |
74 v8::Isolate* ObjectBackedNativeHandler::GetIsolate() const { | 77 v8::Isolate* ObjectBackedNativeHandler::GetIsolate() const { |
75 return context_->isolate(); | 78 return context_->isolate(); |
76 } | 79 } |
77 | 80 |
78 void ObjectBackedNativeHandler::Invalidate() { | 81 void ObjectBackedNativeHandler::Invalidate() { |
79 v8::Isolate* isolate = GetIsolate(); | 82 v8::Isolate* isolate = GetIsolate(); |
80 v8::HandleScope handle_scope(isolate); | 83 v8::HandleScope handle_scope(isolate); |
81 v8::Context::Scope context_scope(context_->v8_context()); | 84 v8::Local<v8::Context> v8_context = context_->v8_context(); |
| 85 v8::Context::Scope context_scope(v8_context); |
82 | 86 |
83 for (size_t i = 0; i < router_data_.Size(); i++) { | 87 for (size_t i = 0; i < router_data_.Size(); i++) { |
84 v8::Local<v8::Object> data = router_data_.Get(i); | 88 v8::Local<v8::Object> data = router_data_.Get(i); |
85 v8::Local<v8::Value> handler_function_value = | 89 v8::Local<v8::Value> handler_function_value = |
86 data->Get(v8::String::NewFromUtf8(isolate, kHandlerFunction)); | 90 UnsafeGet(v8_context, data, ToV8String(isolate, kHandlerFunction)); |
87 CHECK(!handler_function_value.IsEmpty()); | |
88 delete static_cast<HandlerFunction*>( | 91 delete static_cast<HandlerFunction*>( |
89 handler_function_value.As<v8::External>()->Value()); | 92 handler_function_value.As<v8::External>()->Value()); |
90 data->Delete(v8::String::NewFromUtf8(isolate, kHandlerFunction)); | 93 data->Delete(v8_context, ToV8String(isolate, kHandlerFunction)); |
91 } | 94 } |
92 | 95 |
93 router_data_.Clear(); | 96 router_data_.Clear(); |
94 object_template_.Reset(); | 97 object_template_.Reset(); |
95 | 98 |
96 NativeHandler::Invalidate(); | 99 NativeHandler::Invalidate(); |
97 } | 100 } |
98 | 101 |
99 } // namespace extensions | 102 } // namespace extensions |
OLD | NEW |