| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/native_handler.h" | 5 #include "chrome/renderer/extensions/native_handler.h" |
| 6 | 6 |
| 7 #include "base/memory/linked_ptr.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "chrome/renderer/extensions/module_system.h" | |
| 10 #include "v8/include/v8.h" | |
| 11 | |
| 12 namespace extensions { | 7 namespace extensions { |
| 13 | 8 |
| 14 NativeHandler::NativeHandler(v8::Isolate* isolate) | 9 NativeHandler::NativeHandler() : is_valid_(true) {} |
| 15 : object_template_(v8::ObjectTemplate::New()) {} | |
| 16 | 10 |
| 17 NativeHandler::~NativeHandler() {} | 11 NativeHandler::~NativeHandler() {} |
| 18 | 12 |
| 19 v8::Handle<v8::Object> NativeHandler::NewInstance() { | 13 void NativeHandler::Invalidate() { |
| 20 return object_template_->NewInstance(); | 14 is_valid_ = false; |
| 21 } | 15 } |
| 22 | 16 |
| 23 // static | 17 } // namespace extensions |
| 24 v8::Handle<v8::Value> NativeHandler::Router(const v8::Arguments& args) { | |
| 25 // It is possible for JS code to execute after ModuleSystem has been deleted | |
| 26 // in which case the native handlers will also have been deleted, making | |
| 27 // HandlerFunction below point to freed memory. | |
| 28 if (!ModuleSystem::IsPresentInCurrentContext()) { | |
| 29 return v8::ThrowException(v8::Exception::Error( | |
| 30 v8::String::New("ModuleSystem has been deleted"))); | |
| 31 } | |
| 32 HandlerFunction* handler_function = static_cast<HandlerFunction*>( | |
| 33 args.Data().As<v8::External>()->Value()); | |
| 34 return handler_function->Run(args); | |
| 35 } | |
| 36 | |
| 37 void NativeHandler::RouteFunction(const std::string& name, | |
| 38 const HandlerFunction& handler_function) { | |
| 39 linked_ptr<HandlerFunction> function(new HandlerFunction(handler_function)); | |
| 40 // TODO(koz): Investigate using v8's MakeWeak() function instead of holding | |
| 41 // on to these pointers here. | |
| 42 handler_functions_.push_back(function); | |
| 43 v8::Handle<v8::FunctionTemplate> function_template = | |
| 44 v8::FunctionTemplate::New(Router, | |
| 45 v8::External::New(function.get())); | |
| 46 object_template_->Set(name.c_str(), function_template); | |
| 47 } | |
| 48 | |
| 49 void NativeHandler::RouteStaticFunction(const std::string& name, | |
| 50 const HandlerFunc handler_func) { | |
| 51 v8::Handle<v8::FunctionTemplate> function_template = | |
| 52 v8::FunctionTemplate::New(handler_func, v8::External::New(this)); | |
| 53 object_template_->Set(name.c_str(), function_template); | |
| 54 } | |
| 55 | |
| 56 } // extensions | |
| OLD | NEW |