Chromium Code Reviews| 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 #include "chrome/renderer/extensions/object_backed_native_handler.h" | 5 #include "chrome/renderer/extensions/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 "chrome/renderer/extensions/module_system.h" | 9 #include "chrome/renderer/extensions/module_system.h" |
| 10 #include "v8/include/v8.h" | 10 #include "v8/include/v8.h" |
| 11 | 11 |
| 12 namespace extensions { | 12 namespace extensions { |
| 13 | 13 |
| 14 // Data to pass to ObjectBackedNativeHandler::Router. | 14 namespace { |
| 15 struct ObjectBackedNativeHandler::RouterData { | |
| 16 RouterData(ObjectBackedNativeHandler* self, HandlerFunction function) | |
| 17 : self(self), function(function) {} | |
| 18 | 15 |
| 19 ~RouterData() {} | 16 // Keys for the router data objects. |
| 17 const char* kIsValid = "is_valid"; | |
| 18 const char* kHandlerFunction = "handler_function"; | |
| 20 | 19 |
| 21 // The owner of the routed data. | 20 } // namespace |
| 22 ObjectBackedNativeHandler* const self; | |
| 23 | |
| 24 // The function to route calls to. | |
| 25 HandlerFunction function; | |
| 26 }; | |
| 27 | 21 |
| 28 ObjectBackedNativeHandler::ObjectBackedNativeHandler( | 22 ObjectBackedNativeHandler::ObjectBackedNativeHandler( |
| 29 v8::Handle<v8::Context> context) | 23 v8::Handle<v8::Context> context) |
| 30 : v8_context_(context), | 24 : v8_context_(context), |
| 31 object_template_(v8::ObjectTemplate::New()) { | 25 object_template_(v8::ObjectTemplate::New()) { |
| 32 } | 26 } |
| 33 | 27 |
| 34 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { | 28 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { |
| 35 Invalidate(); | 29 Invalidate(); |
| 36 } | 30 } |
| 37 | 31 |
| 38 v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() { | 32 v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() { |
| 39 return object_template_->NewInstance(); | 33 return object_template_->NewInstance(); |
| 40 } | 34 } |
| 41 | 35 |
| 42 // static | 36 // static |
| 43 v8::Handle<v8::Value> ObjectBackedNativeHandler::Router( | 37 v8::Handle<v8::Value> ObjectBackedNativeHandler::Router( |
| 44 const v8::Arguments& args) { | 38 const v8::Arguments& args) { |
| 45 RouterData* router_data = static_cast<RouterData*>( | 39 v8::Handle<v8::Object> data = args.Data().As<v8::Object>(); |
| 46 args.Data().As<v8::External>()->Value()); | 40 if (!data->GetHiddenValue(v8::String::New(kIsValid))->BooleanValue()) { |
|
koz (OOO until 15th September)
2013/03/13 23:08:43
Another option would be to add an is_valid field t
not at google - send to devlin
2013/03/13 23:49:25
Done.
| |
| 47 // Router can be called during context destruction. Stop. | 41 return v8::ThrowException(v8::String::New( |
| 48 if (!router_data->self->is_valid()) | 42 "Extension view no longer exists")); |
| 49 return v8::Handle<v8::Value>(); | 43 } |
| 50 return router_data->function.Run(args); | 44 v8::Handle<v8::Value> handler_function = |
| 45 data->GetHiddenValue(v8::String::New(kHandlerFunction)); | |
| 46 CHECK(!handler_function.IsEmpty()); | |
| 47 return static_cast<HandlerFunction*>( | |
| 48 handler_function.As<v8::External>()->Value())->Run(args); | |
| 51 } | 49 } |
| 52 | 50 |
| 53 void ObjectBackedNativeHandler::RouteFunction( | 51 void ObjectBackedNativeHandler::RouteFunction( |
| 54 const std::string& name, | 52 const std::string& name, |
| 55 const HandlerFunction& handler_function) { | 53 const HandlerFunction& handler_function) { |
| 56 linked_ptr<RouterData> data(new RouterData(this, handler_function)); | 54 v8::HandleScope handle_scope; |
| 57 // TODO(koz): Investigate using v8's MakeWeak() function instead of holding | 55 v8::Persistent<v8::Object> data = v8::Persistent<v8::Object>::New( |
| 58 // on to these pointers here. | 56 v8_context_->GetIsolate(), v8::Object::New()); |
| 57 data->SetHiddenValue(v8::String::New(kIsValid), v8::Boolean::New(true)); | |
| 58 data->SetHiddenValue(v8::String::New(kHandlerFunction), | |
| 59 v8::External::New(new HandlerFunction(handler_function))); | |
| 59 router_data_.push_back(data); | 60 router_data_.push_back(data); |
| 60 v8::Handle<v8::FunctionTemplate> function_template = | 61 v8::Handle<v8::FunctionTemplate> function_template = |
| 61 v8::FunctionTemplate::New(Router, v8::External::New(data.get())); | 62 v8::FunctionTemplate::New(Router, data); |
| 62 object_template_->Set(name.c_str(), function_template); | 63 object_template_->Set(name.c_str(), function_template); |
| 63 } | 64 } |
| 64 | 65 |
| 65 void ObjectBackedNativeHandler::RouteStaticFunction( | 66 void ObjectBackedNativeHandler::RouteStaticFunction( |
| 66 const std::string& name, | 67 const std::string& name, |
| 67 const HandlerFunc handler_func) { | 68 const HandlerFunc& handler_func) { |
| 68 v8::Handle<v8::FunctionTemplate> function_template = | 69 v8::Handle<v8::FunctionTemplate> function_template = |
| 69 v8::FunctionTemplate::New(handler_func, v8::External::New(this)); | 70 v8::FunctionTemplate::New(handler_func, v8::External::New(this)); |
| 70 object_template_->Set(name.c_str(), function_template); | 71 object_template_->Set(name.c_str(), function_template); |
| 71 } | 72 } |
| 72 | 73 |
| 73 void ObjectBackedNativeHandler::Invalidate() { | 74 void ObjectBackedNativeHandler::Invalidate() { |
| 74 if (!is_valid()) | 75 if (!is_valid()) |
| 75 return; | 76 return; |
| 77 for (RouterData::iterator it = router_data_.begin(); | |
| 78 it != router_data_.end(); ++it) { | |
| 79 v8::Persistent<v8::Object> data = *it; | |
| 80 data->SetHiddenValue(v8::String::New(kIsValid), v8::Boolean::New(false)); | |
| 81 v8::Handle<v8::Value> handler_function = | |
| 82 data->GetHiddenValue(v8::String::New(kHandlerFunction)); | |
| 83 CHECK(!handler_function.IsEmpty()); | |
| 84 delete static_cast<HandlerFunction*>( | |
| 85 handler_function.As<v8::External>()->Value()); | |
| 86 data->DeleteHiddenValue(v8::String::New(kHandlerFunction)); | |
| 87 data.Dispose(v8_context_->GetIsolate()); | |
| 88 } | |
| 76 object_template_.reset(); | 89 object_template_.reset(); |
| 77 v8_context_.reset(); | 90 v8_context_.reset(); |
| 78 NativeHandler::Invalidate(); | 91 NativeHandler::Invalidate(); |
| 79 } | 92 } |
| 80 | 93 |
| 81 } // extensions | 94 } // extensions |
| OLD | NEW |