| 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_( | |
| 32 v8::Persistent<v8::ObjectTemplate>::New(context->GetIsolate(), | |
| 33 v8::ObjectTemplate::New())) { | |
| 34 } | |
| 35 | |
| 36 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { | |
| 37 } | |
| 38 | |
| 39 v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() { | |
| 40 return object_template_->NewInstance(); | |
| 41 } | |
| 42 | |
| 43 // static | |
| 44 v8::Handle<v8::Value> ObjectBackedNativeHandler::Router( | |
| 45 const v8::Arguments& args) { | |
| 46 RouterData* router_data = static_cast<RouterData*>( | |
| 47 args.Data().As<v8::External>()->Value()); | |
| 48 // Router can be called during context destruction. Stop. | |
| 49 if (!router_data->self->is_valid()) | |
| 50 return v8::Handle<v8::Value>(); | |
| 51 return router_data->function.Run(args); | |
| 52 } | |
| 53 | |
| 54 void ObjectBackedNativeHandler::RouteFunction(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(const std::string& name, | |
| 66 const HandlerFunc handler_func) { | |
| 67 v8::Handle<v8::FunctionTemplate> function_template = | |
| 68 v8::FunctionTemplate::New(handler_func, v8::External::New(this)); | |
| 69 object_template_->Set(name.c_str(), function_template); | |
| 70 } | |
| 71 | |
| 72 void ObjectBackedNativeHandler::Invalidate() { | |
| 73 if (!is_valid()) | |
| 74 return; | |
| 75 object_template_.Clear(); | |
| 76 v8_context_.Clear(); | |
| 77 NativeHandler::Invalidate(); | |
| 78 } | |
| 79 | |
| 80 } // extensions | |
| OLD | NEW |