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