Index: chrome/renderer/extensions/object_backed_native_handler.cc |
diff --git a/chrome/renderer/extensions/object_backed_native_handler.cc b/chrome/renderer/extensions/object_backed_native_handler.cc |
index f57d5796ad1640cd5e8ae1bf6260ae4393c105f6..98371e6872ee19e94aa424aca1d0d772773a6cdb 100644 |
--- a/chrome/renderer/extensions/object_backed_native_handler.cc |
+++ b/chrome/renderer/extensions/object_backed_native_handler.cc |
@@ -11,13 +11,26 @@ |
namespace extensions { |
+// Data to pass to ObjectBackedNativeHandler::Router. |
+struct ObjectBackedNativeHandler::RouterData { |
+ RouterData(ObjectBackedNativeHandler* self, HandlerFunction function) |
+ : self(self), function(function) {} |
+ |
+ ~RouterData() {} |
+ |
+ // The owner of the routed data. |
+ ObjectBackedNativeHandler* const self; |
+ |
+ // The function to route calls to. |
+ HandlerFunction function; |
+}; |
+ |
ObjectBackedNativeHandler::ObjectBackedNativeHandler( |
v8::Handle<v8::Context> context) |
: v8_context_(context), |
object_template_( |
v8::Persistent<v8::ObjectTemplate>::New(context->GetIsolate(), |
- v8::ObjectTemplate::New())), |
- is_valid_(true) { |
+ v8::ObjectTemplate::New())) { |
} |
ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { |
@@ -30,27 +43,22 @@ v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() { |
// static |
v8::Handle<v8::Value> ObjectBackedNativeHandler::Router( |
const v8::Arguments& args) { |
- // It is possible for JS code to execute after ModuleSystem has been deleted |
- // in which case the native handlers will also have been deleted, making |
- // HandlerFunction below point to freed memory. |
- if (!ModuleSystem::IsPresentInCurrentContext()) { |
- return v8::ThrowException(v8::Exception::Error( |
- v8::String::New("ModuleSystem has been deleted"))); |
- } |
- HandlerFunction* handler_function = static_cast<HandlerFunction*>( |
+ RouterData* router_data = static_cast<RouterData*>( |
args.Data().As<v8::External>()->Value()); |
- return handler_function->Run(args); |
+ // Router can be called during context destruction. Stop. |
+ if (!router_data->self->is_valid()) |
+ return v8::Handle<v8::Value>(); |
+ return router_data->function.Run(args); |
} |
void ObjectBackedNativeHandler::RouteFunction(const std::string& name, |
const HandlerFunction& handler_function) { |
- linked_ptr<HandlerFunction> function(new HandlerFunction(handler_function)); |
+ linked_ptr<RouterData> data(new RouterData(this, handler_function)); |
// TODO(koz): Investigate using v8's MakeWeak() function instead of holding |
// on to these pointers here. |
- handler_functions_.push_back(function); |
+ router_data_.push_back(data); |
v8::Handle<v8::FunctionTemplate> function_template = |
- v8::FunctionTemplate::New(Router, |
- v8::External::New(function.get())); |
+ v8::FunctionTemplate::New(Router, v8::External::New(data.get())); |
object_template_->Set(name.c_str(), function_template); |
} |
@@ -61,14 +69,12 @@ void ObjectBackedNativeHandler::RouteStaticFunction(const std::string& name, |
object_template_->Set(name.c_str(), function_template); |
} |
-bool ObjectBackedNativeHandler::Invalidate() { |
- if (!is_valid_) |
- return false; |
- |
- object_template_.Dispose(v8_context_->GetIsolate()); |
- |
- is_valid_ = false; |
- return true; |
+void ObjectBackedNativeHandler::Invalidate() { |
+ if (!is_valid()) |
+ return; |
+ object_template_.Clear(); |
+ v8_context_.Clear(); |
+ NativeHandler::Invalidate(); |
} |
} // extensions |