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