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/native_handler.h" |
6 | 6 |
7 #include "base/memory/linked_ptr.h" | 7 #include "base/memory/linked_ptr.h" |
8 #include "base/logging.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" |
(...skipping 20 matching lines...) Expand all Loading... |
31 // HandlerFunction below point to freed memory. | 31 // HandlerFunction below point to freed memory. |
32 if (!ModuleSystem::IsPresentInCurrentContext()) { | 32 if (!ModuleSystem::IsPresentInCurrentContext()) { |
33 return v8::ThrowException(v8::Exception::Error( | 33 return v8::ThrowException(v8::Exception::Error( |
34 v8::String::New("ModuleSystem has been deleted"))); | 34 v8::String::New("ModuleSystem has been deleted"))); |
35 } | 35 } |
36 HandlerFunction* handler_function = static_cast<HandlerFunction*>( | 36 HandlerFunction* handler_function = static_cast<HandlerFunction*>( |
37 args.Data().As<v8::External>()->Value()); | 37 args.Data().As<v8::External>()->Value()); |
38 return handler_function->Run(args); | 38 return handler_function->Run(args); |
39 } | 39 } |
40 | 40 |
| 41 // static |
| 42 void NativeHandler::DisposeFunction(v8::Persistent<v8::Value> object, |
| 43 void* parameter) { |
| 44 HandlerFunction* handler_function = |
| 45 reinterpret_cast<HandlerFunction*>(parameter); |
| 46 |
| 47 object.Dispose(); |
| 48 delete handler_function; |
| 49 } |
| 50 |
41 void NativeHandler::RouteFunction(const std::string& name, | 51 void NativeHandler::RouteFunction(const std::string& name, |
42 const HandlerFunction& handler_function) { | 52 const HandlerFunction& handler_function) { |
43 linked_ptr<HandlerFunction> function(new HandlerFunction(handler_function)); | 53 HandlerFunction* function = new HandlerFunction(handler_function); |
44 // TODO(koz): Investigate using v8's MakeWeak() function instead of holding | 54 // Deleted in DisposeFunction once v8 garbage collects function_template. |
45 // on to these pointers here. | 55 v8::Persistent<v8::External> function_value = |
46 handler_functions_.push_back(function); | 56 v8::Persistent<v8::External>::New(v8::External::New(function)); |
| 57 function_value.MakeWeak(function, DisposeFunction); |
47 v8::Handle<v8::FunctionTemplate> function_template = | 58 v8::Handle<v8::FunctionTemplate> function_template = |
48 v8::FunctionTemplate::New(Router, | 59 v8::FunctionTemplate::New(Router, function_value); |
49 v8::External::New(function.get())); | |
50 object_template_->Set(name.c_str(), function_template); | 60 object_template_->Set(name.c_str(), function_template); |
51 } | 61 } |
52 | 62 |
53 void NativeHandler::RouteStaticFunction(const std::string& name, | 63 void NativeHandler::RouteStaticFunction(const std::string& name, |
54 const HandlerFunc handler_func) { | 64 const HandlerFunc handler_func) { |
55 v8::Handle<v8::FunctionTemplate> function_template = | 65 v8::Handle<v8::FunctionTemplate> function_template = |
56 v8::FunctionTemplate::New(handler_func, v8::External::New(this)); | 66 v8::FunctionTemplate::New(handler_func, v8::External::New(this)); |
57 object_template_->Set(name.c_str(), function_template); | 67 object_template_->Set(name.c_str(), function_template); |
58 } | 68 } |
59 | 69 |
60 } // extensions | 70 } // extensions |
OLD | NEW |