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 "v8/include/v8.h" | 12 #include "v8/include/v8.h" |
13 | 13 |
14 namespace extensions { | 14 namespace extensions { |
15 | 15 |
16 namespace { | 16 namespace { |
17 // Key for the base::Bound routed function. | 17 // Key for the base::Bound routed function. |
18 const char* kHandlerFunction = "handler_function"; | 18 const char* kHandlerFunction = "handler_function"; |
19 } // namespace | 19 } // namespace |
20 | 20 |
21 ObjectBackedNativeHandler::ObjectBackedNativeHandler(ScriptContext* context) | 21 ObjectBackedNativeHandler::ObjectBackedNativeHandler(ScriptContext* context) |
22 : router_data_(context->isolate()), | 22 : router_data_(context->isolate()), |
23 context_(context), | 23 context_(context), |
24 object_template_(context->isolate(), | 24 object_template_(context->isolate(), |
25 v8::ObjectTemplate::New(context->isolate())) { | 25 v8::ObjectTemplate::New(context->isolate())) { |
26 } | 26 } |
27 | 27 |
28 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { | 28 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { Invalidate(); } |
29 } | |
30 | 29 |
31 v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() { | 30 v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() { |
32 return v8::Local<v8::ObjectTemplate>::New(GetIsolate(), object_template_) | 31 return v8::Local<v8::ObjectTemplate>::New(GetIsolate(), object_template_) |
33 ->NewInstance(); | 32 ->NewInstance(); |
34 } | 33 } |
35 | 34 |
36 // static | 35 // static |
37 void ObjectBackedNativeHandler::Router( | 36 void ObjectBackedNativeHandler::Router( |
38 const v8::FunctionCallbackInfo<v8::Value>& args) { | 37 const v8::FunctionCallbackInfo<v8::Value>& args) { |
39 v8::HandleScope handle_scope(args.GetIsolate()); | 38 v8::HandleScope handle_scope(args.GetIsolate()); |
(...skipping 29 matching lines...) Expand all Loading... |
69 v8::Local<v8::ObjectTemplate>::New(isolate, object_template_) | 68 v8::Local<v8::ObjectTemplate>::New(isolate, object_template_) |
70 ->Set(isolate, name.c_str(), function_template); | 69 ->Set(isolate, name.c_str(), function_template); |
71 router_data_.Append(data); | 70 router_data_.Append(data); |
72 } | 71 } |
73 | 72 |
74 v8::Isolate* ObjectBackedNativeHandler::GetIsolate() const { | 73 v8::Isolate* ObjectBackedNativeHandler::GetIsolate() const { |
75 return context_->isolate(); | 74 return context_->isolate(); |
76 } | 75 } |
77 | 76 |
78 void ObjectBackedNativeHandler::Invalidate() { | 77 void ObjectBackedNativeHandler::Invalidate() { |
79 v8::Isolate* isolate = GetIsolate(); | 78 if (!is_valid()) |
| 79 return; |
| 80 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
80 v8::HandleScope handle_scope(isolate); | 81 v8::HandleScope handle_scope(isolate); |
81 v8::Context::Scope context_scope(context_->v8_context()); | 82 v8::Context::Scope context_scope(context_->v8_context()); |
82 | 83 |
83 for (size_t i = 0; i < router_data_.Size(); i++) { | 84 for (size_t i = 0; i < router_data_.Size(); i++) { |
84 v8::Handle<v8::Object> data = router_data_.Get(i); | 85 v8::Handle<v8::Object> data = router_data_.Get(i); |
85 v8::Handle<v8::Value> handler_function_value = | 86 v8::Handle<v8::Value> handler_function_value = |
86 data->Get(v8::String::NewFromUtf8(isolate, kHandlerFunction)); | 87 data->Get(v8::String::NewFromUtf8(isolate, kHandlerFunction)); |
87 CHECK(!handler_function_value.IsEmpty()); | 88 CHECK(!handler_function_value.IsEmpty()); |
88 delete static_cast<HandlerFunction*>( | 89 delete static_cast<HandlerFunction*>( |
89 handler_function_value.As<v8::External>()->Value()); | 90 handler_function_value.As<v8::External>()->Value()); |
90 data->Delete(v8::String::NewFromUtf8(isolate, kHandlerFunction)); | 91 data->Delete(v8::String::NewFromUtf8(isolate, kHandlerFunction)); |
91 } | 92 } |
92 | |
93 router_data_.Clear(); | 93 router_data_.Clear(); |
94 object_template_.Reset(); | 94 object_template_.Reset(); |
95 | 95 context_ = NULL; |
96 NativeHandler::Invalidate(); | 96 NativeHandler::Invalidate(); |
97 } | 97 } |
98 | 98 |
99 } // namespace extensions | 99 } // namespace extensions |
OLD | NEW |