OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/renderer/extensions/native_handler.h" | |
6 | |
7 #include "base/memory/linked_ptr.h" | |
8 #include "base/logging.h" | |
9 #include "chrome/renderer/extensions/module_system.h" | |
10 #include "v8/include/v8.h" | |
11 | |
12 namespace extensions { | |
13 | |
14 NativeHandler::NativeHandler() | |
15 : object_template_( | |
16 v8::Persistent<v8::ObjectTemplate>::New(v8::ObjectTemplate::New())) { | |
17 } | |
18 | |
19 NativeHandler::~NativeHandler() { | |
20 object_template_.Dispose(); | |
21 } | |
22 | |
23 v8::Handle<v8::Object> NativeHandler::NewInstance() { | |
24 return object_template_->NewInstance(); | |
25 } | |
26 | |
27 // static | |
28 v8::Handle<v8::Value> NativeHandler::Router(const v8::Arguments& args) { | |
29 // It is possible for JS code to execute after ModuleSystem has been deleted | |
30 // in which case the native handlers will also have been deleted, making | |
31 // HandlerFunction below point to freed memory. | |
32 if (!ModuleSystem::IsPresentInCurrentContext()) { | |
33 return v8::ThrowException(v8::Exception::Error( | |
34 v8::String::New("ModuleSystem has been deleted"))); | |
35 } | |
36 HandlerFunction* handler_function = static_cast<HandlerFunction*>( | |
37 args.Data().As<v8::External>()->Value()); | |
38 return handler_function->Run(args); | |
39 } | |
40 | |
41 void NativeHandler::RouteFunction(const std::string& name, | |
42 const HandlerFunction& handler_function) { | |
43 linked_ptr<HandlerFunction> function(new HandlerFunction(handler_function)); | |
44 // TODO(koz): Investigate using v8's MakeWeak() function instead of holding | |
45 // on to these pointers here. | |
46 handler_functions_.push_back(function); | |
47 v8::Handle<v8::FunctionTemplate> function_template = | |
48 v8::FunctionTemplate::New(Router, | |
49 v8::External::New(function.get())); | |
50 object_template_->Set(name.c_str(), function_template); | |
51 } | |
52 | |
53 void NativeHandler::RouteStaticFunction(const std::string& name, | |
54 const HandlerFunc handler_func) { | |
55 v8::Handle<v8::FunctionTemplate> function_template = | |
56 v8::FunctionTemplate::New(handler_func, v8::External::New(this)); | |
57 object_template_->Set(name.c_str(), function_template); | |
58 } | |
59 | |
60 } // extensions | |
OLD | NEW |