Commit 25dba9dc authored by koz@chromium.org's avatar koz@chromium.org

Use MakeWeak() to clean up function pointers in NativeHandler.


Review URL: https://chromiumcodereview.appspot.com/10918042

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@176155 0039d316-1c4b-4281-b951-d872f2087c98
parent da891ec5
......@@ -14,7 +14,7 @@ const char* kModuleName = "module_name";
const char* kModuleField = "module_field";
const char* kModulesField = "modules";
} // namespace
} // namespace
namespace extensions {
......
......@@ -38,15 +38,25 @@ v8::Handle<v8::Value> NativeHandler::Router(const v8::Arguments& args) {
return handler_function->Run(args);
}
// static
void NativeHandler::DisposeFunction(v8::Persistent<v8::Value> object,
void* parameter) {
HandlerFunction* handler_function =
reinterpret_cast<HandlerFunction*>(parameter);
object.Dispose();
delete handler_function;
}
void NativeHandler::RouteFunction(const std::string& name,
const HandlerFunction& handler_function) {
linked_ptr<HandlerFunction> function(new HandlerFunction(handler_function));
// TODO(koz): Investigate using v8's MakeWeak() function instead of holding
// on to these pointers here.
handler_functions_.push_back(function);
HandlerFunction* function = new HandlerFunction(handler_function);
// Deleted in DisposeFunction once v8 garbage collects function_template.
v8::Persistent<v8::External> function_value =
v8::Persistent<v8::External>::New(v8::External::New(function));
function_value.MakeWeak(function, DisposeFunction);
v8::Handle<v8::FunctionTemplate> function_template =
v8::FunctionTemplate::New(Router,
v8::External::New(function.get()));
v8::FunctionTemplate::New(Router, function_value);
object_template_->Set(name.c_str(), function_template);
}
......
......@@ -6,11 +6,9 @@
#define CHROME_RENDERER_EXTENSIONS_NATIVE_HANDLER_H_
#include "base/bind.h"
#include "base/memory/linked_ptr.h"
#include "v8/include/v8.h"
#include <string>
#include <vector>
namespace extensions {
......@@ -48,8 +46,9 @@ class NativeHandler {
private:
static v8::Handle<v8::Value> Router(const v8::Arguments& args);
static void DisposeFunction(v8::Persistent<v8::Value> object,
void* parameter);
std::vector<linked_ptr<HandlerFunction> > handler_functions_;
v8::Persistent<v8::ObjectTemplate> object_template_;
DISALLOW_COPY_AND_ASSIGN(NativeHandler);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment