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" |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 handler_function_value.As<v8::External>()->Value())->Run(args); | 50 handler_function_value.As<v8::External>()->Value())->Run(args); |
51 } | 51 } |
52 | 52 |
53 void ObjectBackedNativeHandler::RouteFunction( | 53 void ObjectBackedNativeHandler::RouteFunction( |
54 const std::string& name, | 54 const std::string& name, |
55 const HandlerFunction& handler_function) { | 55 const HandlerFunction& handler_function) { |
56 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 56 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
57 v8::HandleScope handle_scope(isolate); | 57 v8::HandleScope handle_scope(isolate); |
58 v8::Context::Scope context_scope(context_->v8_context()); | 58 v8::Context::Scope context_scope(context_->v8_context()); |
59 | 59 |
60 v8::Persistent<v8::Object> data(isolate, v8::Object::New(isolate)); | 60 v8::Local<v8::Object> data = v8::Object::New(isolate); |
61 v8::Local<v8::Object> local_data = v8::Local<v8::Object>::New(isolate, data); | 61 data->Set( |
62 local_data->Set( | |
63 v8::String::NewFromUtf8(isolate, kHandlerFunction), | 62 v8::String::NewFromUtf8(isolate, kHandlerFunction), |
64 v8::External::New(isolate, new HandlerFunction(handler_function))); | 63 v8::External::New(isolate, new HandlerFunction(handler_function))); |
65 v8::Handle<v8::FunctionTemplate> function_template = | 64 v8::Handle<v8::FunctionTemplate> function_template = |
66 v8::FunctionTemplate::New(isolate, Router, local_data); | 65 v8::FunctionTemplate::New(isolate, Router, data); |
67 object_template_.NewHandle(isolate) | 66 object_template_.NewHandle(isolate) |
68 ->Set(isolate, name.c_str(), function_template); | 67 ->Set(isolate, name.c_str(), function_template); |
69 router_data_.Append(local_data); | 68 router_data_.Append(data); |
70 } | 69 } |
71 | 70 |
72 v8::Isolate* ObjectBackedNativeHandler::GetIsolate() const { | 71 v8::Isolate* ObjectBackedNativeHandler::GetIsolate() const { |
73 return context_->isolate(); | 72 return context_->isolate(); |
74 } | 73 } |
75 | 74 |
76 void ObjectBackedNativeHandler::Invalidate() { | 75 void ObjectBackedNativeHandler::Invalidate() { |
77 if (!is_valid()) | 76 if (!is_valid()) |
78 return; | 77 return; |
79 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 78 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
80 v8::HandleScope handle_scope(isolate); | 79 v8::HandleScope handle_scope(isolate); |
81 v8::Context::Scope context_scope(context_->v8_context()); | 80 v8::Context::Scope context_scope(context_->v8_context()); |
82 | 81 |
83 for (size_t i = 0; i < router_data_.Size(); i++) { | 82 for (size_t i = 0; i < router_data_.Size(); i++) { |
84 v8::Handle<v8::Object> data = router_data_.Get(i); | 83 v8::Handle<v8::Object> data = router_data_.Get(i); |
85 v8::Handle<v8::Value> handler_function_value = | 84 v8::Handle<v8::Value> handler_function_value = |
86 data->Get(v8::String::NewFromUtf8(isolate, kHandlerFunction)); | 85 data->Get(v8::String::NewFromUtf8(isolate, kHandlerFunction)); |
87 CHECK(!handler_function_value.IsEmpty()); | 86 CHECK(!handler_function_value.IsEmpty()); |
88 delete static_cast<HandlerFunction*>( | 87 delete static_cast<HandlerFunction*>( |
89 handler_function_value.As<v8::External>()->Value()); | 88 handler_function_value.As<v8::External>()->Value()); |
90 data->Delete(v8::String::NewFromUtf8(isolate, kHandlerFunction)); | 89 data->Delete(v8::String::NewFromUtf8(isolate, kHandlerFunction)); |
91 } | 90 } |
92 router_data_.Clear(); | 91 router_data_.Clear(); |
93 object_template_.reset(); | 92 object_template_.reset(); |
94 context_ = NULL; | 93 context_ = NULL; |
95 NativeHandler::Invalidate(); | 94 NativeHandler::Invalidate(); |
96 } | 95 } |
97 | 96 |
98 } // namespace extensions | 97 } // namespace extensions |
OLD | NEW |