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