OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/renderer/extensions/object_backed_native_handler.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/memory/linked_ptr.h" |
| 9 #include "chrome/renderer/extensions/module_system.h" |
| 10 #include "v8/include/v8.h" |
| 11 |
| 12 namespace extensions { |
| 13 |
| 14 // Data to pass to ObjectBackedNativeHandler::Router. |
| 15 struct ObjectBackedNativeHandler::RouterData { |
| 16 RouterData(ObjectBackedNativeHandler* self, HandlerFunction function) |
| 17 : self(self), function(function) {} |
| 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 |
| 28 ObjectBackedNativeHandler::ObjectBackedNativeHandler( |
| 29 v8::Handle<v8::Context> context) |
| 30 : v8_context_(context), |
| 31 object_template_(v8::ObjectTemplate::New()) { |
| 32 } |
| 33 |
| 34 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { |
| 35 Invalidate(); |
| 36 } |
| 37 |
| 38 v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() { |
| 39 return object_template_->NewInstance(); |
| 40 } |
| 41 |
| 42 // static |
| 43 v8::Handle<v8::Value> ObjectBackedNativeHandler::Router( |
| 44 const v8::Arguments& args) { |
| 45 RouterData* router_data = static_cast<RouterData*>( |
| 46 args.Data().As<v8::External>()->Value()); |
| 47 // Router can be called during context destruction. Stop. |
| 48 if (!router_data->self->is_valid()) |
| 49 return v8::Handle<v8::Value>(); |
| 50 return router_data->function.Run(args); |
| 51 } |
| 52 |
| 53 void ObjectBackedNativeHandler::RouteFunction( |
| 54 const std::string& name, |
| 55 const HandlerFunction& handler_function) { |
| 56 linked_ptr<RouterData> data(new RouterData(this, handler_function)); |
| 57 // TODO(koz): Investigate using v8's MakeWeak() function instead of holding |
| 58 // on to these pointers here. |
| 59 router_data_.push_back(data); |
| 60 v8::Handle<v8::FunctionTemplate> function_template = |
| 61 v8::FunctionTemplate::New(Router, v8::External::New(data.get())); |
| 62 object_template_->Set(name.c_str(), function_template); |
| 63 } |
| 64 |
| 65 void ObjectBackedNativeHandler::RouteStaticFunction( |
| 66 const std::string& name, |
| 67 const HandlerFunc handler_func) { |
| 68 v8::Handle<v8::FunctionTemplate> function_template = |
| 69 v8::FunctionTemplate::New(handler_func, v8::External::New(this)); |
| 70 object_template_->Set(name.c_str(), function_template); |
| 71 } |
| 72 |
| 73 void ObjectBackedNativeHandler::Invalidate() { |
| 74 if (!is_valid()) |
| 75 return; |
| 76 object_template_.reset(); |
| 77 v8_context_.reset(); |
| 78 NativeHandler::Invalidate(); |
| 79 } |
| 80 |
| 81 } // extensions |
OLD | NEW |