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