Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(517)

Side by Side Diff: chrome/renderer/extensions/object_backed_native_handler.cc

Issue 12378077: Attempting to fix problems in 11571014. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/object_backed_native_handler.h" 5 #include "chrome/renderer/extensions/object_backed_native_handler.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/linked_ptr.h" 8 #include "base/memory/linked_ptr.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 // Data to pass to ObjectBackedNativeHandler::Router.
15 struct ObjectBackedNativeHandler::RouterData {
16 RouterData(ObjectBackedNativeHandler* self, HandlerFunction function)
17 : self(self), function(function) {}
18
19 ~RouterData() {}
20
21 // The owner of the routed data.
22 ObjectBackedNativeHandler* const self;
23
24 // The function to route calls to.
25 HandlerFunction function;
26 };
27
14 ObjectBackedNativeHandler::ObjectBackedNativeHandler( 28 ObjectBackedNativeHandler::ObjectBackedNativeHandler(
15 v8::Handle<v8::Context> context) 29 v8::Handle<v8::Context> context)
16 : v8_context_(context), 30 : v8_context_(context),
17 object_template_( 31 object_template_(
18 v8::Persistent<v8::ObjectTemplate>::New(context->GetIsolate(), 32 v8::Persistent<v8::ObjectTemplate>::New(context->GetIsolate(),
19 v8::ObjectTemplate::New())), 33 v8::ObjectTemplate::New())) {
20 is_valid_(true) {
21 } 34 }
22 35
23 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { 36 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() {
24 } 37 }
25 38
26 v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() { 39 v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() {
27 return object_template_->NewInstance(); 40 return object_template_->NewInstance();
28 } 41 }
29 42
30 // static 43 // static
31 v8::Handle<v8::Value> ObjectBackedNativeHandler::Router( 44 v8::Handle<v8::Value> ObjectBackedNativeHandler::Router(
32 const v8::Arguments& args) { 45 const v8::Arguments& args) {
33 // It is possible for JS code to execute after ModuleSystem has been deleted 46 RouterData* router_data = static_cast<RouterData*>(
34 // in which case the native handlers will also have been deleted, making
35 // HandlerFunction below point to freed memory.
36 if (!ModuleSystem::IsPresentInCurrentContext()) {
37 return v8::ThrowException(v8::Exception::Error(
38 v8::String::New("ModuleSystem has been deleted")));
39 }
40 HandlerFunction* handler_function = static_cast<HandlerFunction*>(
41 args.Data().As<v8::External>()->Value()); 47 args.Data().As<v8::External>()->Value());
42 return handler_function->Run(args); 48 // Router can be called during context destruction. Stop.
49 if (!router_data->self->is_valid())
50 return v8::Handle<v8::Value>();
51 return router_data->function.Run(args);
43 } 52 }
44 53
45 void ObjectBackedNativeHandler::RouteFunction(const std::string& name, 54 void ObjectBackedNativeHandler::RouteFunction(const std::string& name,
46 const HandlerFunction& handler_function) { 55 const HandlerFunction& handler_function) {
47 linked_ptr<HandlerFunction> function(new HandlerFunction(handler_function)); 56 linked_ptr<RouterData> data(new RouterData(this, handler_function));
48 // TODO(koz): Investigate using v8's MakeWeak() function instead of holding 57 // TODO(koz): Investigate using v8's MakeWeak() function instead of holding
49 // on to these pointers here. 58 // on to these pointers here.
50 handler_functions_.push_back(function); 59 router_data_.push_back(data);
51 v8::Handle<v8::FunctionTemplate> function_template = 60 v8::Handle<v8::FunctionTemplate> function_template =
52 v8::FunctionTemplate::New(Router, 61 v8::FunctionTemplate::New(Router, v8::External::New(data.get()));
53 v8::External::New(function.get()));
54 object_template_->Set(name.c_str(), function_template); 62 object_template_->Set(name.c_str(), function_template);
55 } 63 }
56 64
57 void ObjectBackedNativeHandler::RouteStaticFunction(const std::string& name, 65 void ObjectBackedNativeHandler::RouteStaticFunction(const std::string& name,
58 const HandlerFunc handler_func) { 66 const HandlerFunc handler_func) {
59 v8::Handle<v8::FunctionTemplate> function_template = 67 v8::Handle<v8::FunctionTemplate> function_template =
60 v8::FunctionTemplate::New(handler_func, v8::External::New(this)); 68 v8::FunctionTemplate::New(handler_func, v8::External::New(this));
61 object_template_->Set(name.c_str(), function_template); 69 object_template_->Set(name.c_str(), function_template);
62 } 70 }
63 71
64 bool ObjectBackedNativeHandler::Invalidate() { 72 void ObjectBackedNativeHandler::Invalidate() {
65 if (!is_valid_) 73 if (!is_valid())
66 return false; 74 return;
67 75 object_template_.Clear();
68 object_template_.Dispose(v8_context_->GetIsolate()); 76 v8_context_.Clear();
69 77 NativeHandler::Invalidate();
70 is_valid_ = false;
71 return true;
72 } 78 }
73 79
74 } // extensions 80 } // extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698