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