OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // TODO(dcarney): Remove this when UnsafePersistent is removed. | 5 // TODO(dcarney): Remove this when UnsafePersistent is removed. |
6 #define V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR | 6 #define V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR |
7 | 7 |
8 #include "chrome/renderer/extensions/object_backed_native_handler.h" | 8 #include "chrome/renderer/extensions/object_backed_native_handler.h" |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 ChromeV8Context* context) | 25 ChromeV8Context* context) |
26 : context_(context), | 26 : context_(context), |
27 object_template_(v8::ObjectTemplate::New()) { | 27 object_template_(v8::ObjectTemplate::New()) { |
28 } | 28 } |
29 | 29 |
30 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { | 30 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { |
31 Invalidate(); | 31 Invalidate(); |
32 } | 32 } |
33 | 33 |
34 v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() { | 34 v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() { |
35 return object_template_.NewHandle(v8::Isolate::GetCurrent())->NewInstance(); | 35 return object_template_->NewInstance(); |
36 } | 36 } |
37 | 37 |
38 // static | 38 // static |
39 void ObjectBackedNativeHandler::Router( | 39 void ObjectBackedNativeHandler::Router( |
40 const v8::FunctionCallbackInfo<v8::Value>& args) { | 40 const v8::FunctionCallbackInfo<v8::Value>& args) { |
41 v8::HandleScope handle_scope(args.GetIsolate()); | 41 v8::HandleScope handle_scope(args.GetIsolate()); |
42 v8::Handle<v8::Object> data = args.Data().As<v8::Object>(); | 42 v8::Handle<v8::Object> data = args.Data().As<v8::Object>(); |
43 | 43 |
44 v8::Handle<v8::Value> handler_function_value = | 44 v8::Handle<v8::Value> handler_function_value = |
45 data->Get(v8::String::New(kHandlerFunction)); | 45 data->Get(v8::String::New(kHandlerFunction)); |
(...skipping 15 matching lines...) Expand all Loading... |
61 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 61 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
62 v8::HandleScope handle_scope(isolate); | 62 v8::HandleScope handle_scope(isolate); |
63 v8::Context::Scope context_scope(context_->v8_context()); | 63 v8::Context::Scope context_scope(context_->v8_context()); |
64 | 64 |
65 v8::Persistent<v8::Object> data(isolate, v8::Object::New()); | 65 v8::Persistent<v8::Object> data(isolate, v8::Object::New()); |
66 v8::Local<v8::Object> local_data = v8::Local<v8::Object>::New(isolate, data); | 66 v8::Local<v8::Object> local_data = v8::Local<v8::Object>::New(isolate, data); |
67 local_data->Set(v8::String::New(kHandlerFunction), | 67 local_data->Set(v8::String::New(kHandlerFunction), |
68 v8::External::New(new HandlerFunction(handler_function))); | 68 v8::External::New(new HandlerFunction(handler_function))); |
69 v8::Handle<v8::FunctionTemplate> function_template = | 69 v8::Handle<v8::FunctionTemplate> function_template = |
70 v8::FunctionTemplate::New(Router, local_data); | 70 v8::FunctionTemplate::New(Router, local_data); |
71 object_template_.NewHandle(isolate)->Set(name.c_str(), function_template); | 71 object_template_->Set(name.c_str(), function_template); |
72 router_data_.push_back(UnsafePersistent<v8::Object>(&data)); | 72 router_data_.push_back(UnsafePersistent<v8::Object>(&data)); |
73 } | 73 } |
74 | 74 |
75 void ObjectBackedNativeHandler::Invalidate() { | 75 void ObjectBackedNativeHandler::Invalidate() { |
76 if (!is_valid()) | 76 if (!is_valid()) |
77 return; | 77 return; |
78 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 78 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
79 v8::HandleScope handle_scope(isolate); | 79 v8::HandleScope handle_scope(isolate); |
80 v8::Context::Scope context_scope(context_->v8_context()); | 80 v8::Context::Scope context_scope(context_->v8_context()); |
81 | 81 |
82 for (RouterData::iterator it = router_data_.begin(); | 82 for (RouterData::iterator it = router_data_.begin(); |
83 it != router_data_.end(); ++it) { | 83 it != router_data_.end(); ++it) { |
84 v8::Handle<v8::Object> data = it->newLocal(isolate); | 84 v8::Handle<v8::Object> data = it->newLocal(isolate); |
85 v8::Handle<v8::Value> handler_function_value = | 85 v8::Handle<v8::Value> handler_function_value = |
86 data->Get(v8::String::New(kHandlerFunction)); | 86 data->Get(v8::String::New(kHandlerFunction)); |
87 CHECK(!handler_function_value.IsEmpty()); | 87 CHECK(!handler_function_value.IsEmpty()); |
88 delete static_cast<HandlerFunction*>( | 88 delete static_cast<HandlerFunction*>( |
89 handler_function_value.As<v8::External>()->Value()); | 89 handler_function_value.As<v8::External>()->Value()); |
90 data->Delete(v8::String::New(kHandlerFunction)); | 90 data->Delete(v8::String::New(kHandlerFunction)); |
91 it->dispose(); | 91 it->dispose(); |
92 } | 92 } |
93 object_template_.reset(); | 93 object_template_.reset(); |
94 context_ = NULL; | 94 context_ = NULL; |
95 NativeHandler::Invalidate(); | 95 NativeHandler::Invalidate(); |
96 } | 96 } |
97 | 97 |
98 } // namespace extensions | 98 } // namespace extensions |
OLD | NEW |