| 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 ObjectBackedNativeHandler::ObjectBackedNativeHandler( | |
| 15 v8::Handle<v8::Context> context) | |
| 16 : v8_context_(context), | |
| 17 object_template_( | |
| 18 v8::Persistent<v8::ObjectTemplate>::New(context->GetIsolate(), | |
| 19 v8::ObjectTemplate::New())), | |
| 20 is_valid_(true) { | |
| 21 } | |
| 22 | |
| 23 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { | |
| 24 } | |
| 25 | |
| 26 v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() { | |
| 27 return object_template_->NewInstance(); | |
| 28 } | |
| 29 | |
| 30 // static | |
| 31 v8::Handle<v8::Value> ObjectBackedNativeHandler::Router( | |
| 32 const v8::Arguments& args) { | |
| 33 // It is possible for JS code to execute after ModuleSystem has been deleted | |
| 34 // in which case the native handlers will also have been deleted, making | |
| 35 // HandlerFunction below point to freed memory. | |
| 36 if (!ModuleSystem::IsPresentInCurrentContext()) { | |
| 37 return v8::ThrowException(v8::Exception::Error( | |
| 38 v8::String::New("ModuleSystem has been deleted"))); | |
| 39 } | |
| 40 HandlerFunction* handler_function = static_cast<HandlerFunction*>( | |
| 41 args.Data().As<v8::External>()->Value()); | |
| 42 return handler_function->Run(args); | |
| 43 } | |
| 44 | |
| 45 void ObjectBackedNativeHandler::RouteFunction(const std::string& name, | |
| 46 const HandlerFunction& handler_function) { | |
| 47 linked_ptr<HandlerFunction> function(new HandlerFunction(handler_function)); | |
| 48 // TODO(koz): Investigate using v8's MakeWeak() function instead of holding | |
| 49 // on to these pointers here. | |
| 50 handler_functions_.push_back(function); | |
| 51 v8::Handle<v8::FunctionTemplate> function_template = | |
| 52 v8::FunctionTemplate::New(Router, | |
| 53 v8::External::New(function.get())); | |
| 54 object_template_->Set(name.c_str(), function_template); | |
| 55 } | |
| 56 | |
| 57 void ObjectBackedNativeHandler::RouteStaticFunction(const std::string& name, | |
| 58 const HandlerFunc handler_func) { | |
| 59 v8::Handle<v8::FunctionTemplate> function_template = | |
| 60 v8::FunctionTemplate::New(handler_func, v8::External::New(this)); | |
| 61 object_template_->Set(name.c_str(), function_template); | |
| 62 } | |
| 63 | |
| 64 bool ObjectBackedNativeHandler::Invalidate() { | |
| 65 if (!is_valid_) | |
| 66 return false; | |
| 67 | |
| 68 object_template_.Dispose(v8_context_->GetIsolate()); | |
| 69 | |
| 70 is_valid_ = false; | |
| 71 return true; | |
| 72 } | |
| 73 | |
| 74 } // extensions | |
| OLD | NEW |