| 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 { | 15 // Key for the base::Bound routed function. |
| 16 RouterData(ObjectBackedNativeHandler* self, HandlerFunction function) | 16 const char* kHandlerFunction = "handler_function"; |
| 17 : self(self), function(function) {} | 17 } // namespace |
| 18 | |
| 19 ~RouterData() {} | |
| 20 | |
| 21 // The owner of the routed data. | |
| 22 ObjectBackedNativeHandler* const self; | |
| 23 | |
| 24 // The function to route calls to. | |
| 25 HandlerFunction function; | |
| 26 }; | |
| 27 | 18 |
| 28 ObjectBackedNativeHandler::ObjectBackedNativeHandler( | 19 ObjectBackedNativeHandler::ObjectBackedNativeHandler( |
| 29 v8::Handle<v8::Context> context) | 20 v8::Handle<v8::Context> context) |
| 30 : v8_context_(context), | 21 : v8_context_(context), |
| 31 object_template_(v8::ObjectTemplate::New()) { | 22 object_template_(v8::ObjectTemplate::New()) { |
| 32 } | 23 } |
| 33 | 24 |
| 34 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { | 25 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { |
| 35 Invalidate(); | 26 Invalidate(); |
| 36 } | 27 } |
| 37 | 28 |
| 38 v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() { | 29 v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() { |
| 39 return object_template_->NewInstance(); | 30 return object_template_->NewInstance(); |
| 40 } | 31 } |
| 41 | 32 |
| 42 // static | 33 // static |
| 43 v8::Handle<v8::Value> ObjectBackedNativeHandler::Router( | 34 v8::Handle<v8::Value> ObjectBackedNativeHandler::Router( |
| 44 const v8::Arguments& args) { | 35 const v8::Arguments& args) { |
| 45 RouterData* router_data = static_cast<RouterData*>( | 36 v8::HandleScope handle_scope; |
| 46 args.Data().As<v8::External>()->Value()); | 37 v8::Handle<v8::Object> data = args.Data().As<v8::Object>(); |
| 47 // Router can be called during context destruction. Stop. | 38 |
| 48 if (!router_data->self->is_valid()) | 39 v8::Handle<v8::Value> handler_function_value = |
| 49 return v8::Handle<v8::Value>(); | 40 data->Get(v8::String::New(kHandlerFunction)); |
| 50 return router_data->function.Run(args); | 41 // See comment in header file for why we do this. |
| 42 if (handler_function_value.IsEmpty()) { |
| 43 return v8::ThrowException(v8::String::New( |
| 44 "Extension view no longer exists")); |
| 45 } |
| 46 return handle_scope.Close(static_cast<HandlerFunction*>( |
| 47 handler_function_value.As<v8::External>()->Value())->Run(args)); |
| 51 } | 48 } |
| 52 | 49 |
| 53 void ObjectBackedNativeHandler::RouteFunction( | 50 void ObjectBackedNativeHandler::RouteFunction( |
| 54 const std::string& name, | 51 const std::string& name, |
| 55 const HandlerFunction& handler_function) { | 52 const HandlerFunction& handler_function) { |
| 56 linked_ptr<RouterData> data(new RouterData(this, handler_function)); | 53 v8::HandleScope handle_scope; |
| 57 // TODO(koz): Investigate using v8's MakeWeak() function instead of holding | 54 v8::Context::Scope context_scope(v8_context_.get()); |
| 58 // on to these pointers here. | 55 |
| 56 v8::Persistent<v8::Object> data = v8::Persistent<v8::Object>::New( |
| 57 v8_context_->GetIsolate(), v8::Object::New()); |
| 58 data->Set(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 v8::HandleScope handle_scope; |
| 78 v8::Context::Scope context_scope(v8_context_.get()); |
| 79 |
| 80 for (RouterData::iterator it = router_data_.begin(); |
| 81 it != router_data_.end(); ++it) { |
| 82 v8::Persistent<v8::Object> data = *it; |
| 83 v8::Handle<v8::Value> handler_function_value = |
| 84 data->Get(v8::String::New(kHandlerFunction)); |
| 85 CHECK(!handler_function_value.IsEmpty()); |
| 86 delete static_cast<HandlerFunction*>( |
| 87 handler_function_value.As<v8::External>()->Value()); |
| 88 data->Delete(v8::String::New(kHandlerFunction)); |
| 89 data.Dispose(v8_context_->GetIsolate()); |
| 90 } |
| 76 object_template_.reset(); | 91 object_template_.reset(); |
| 77 v8_context_.reset(); | 92 v8_context_.reset(); |
| 78 NativeHandler::Invalidate(); | 93 NativeHandler::Invalidate(); |
| 79 } | 94 } |
| 80 | 95 |
| 81 } // extensions | 96 } // extensions |
| OLD | NEW |