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/native_handler.h" | 5 #include "chrome/renderer/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/module_system.h" | |
9 #include "v8/include/v8.h" | 10 #include "v8/include/v8.h" |
10 | 11 |
11 NativeHandler::NativeHandler() | 12 NativeHandler::NativeHandler() |
12 : object_template_( | 13 : object_template_( |
13 v8::Persistent<v8::ObjectTemplate>::New(v8::ObjectTemplate::New())) { | 14 v8::Persistent<v8::ObjectTemplate>::New(v8::ObjectTemplate::New())) { |
14 } | 15 } |
15 | 16 |
16 NativeHandler::~NativeHandler() { | 17 NativeHandler::~NativeHandler() { |
17 object_template_.Dispose(); | 18 object_template_.Dispose(); |
18 } | 19 } |
19 | 20 |
20 v8::Handle<v8::Object> NativeHandler::NewInstance() { | 21 v8::Handle<v8::Object> NativeHandler::NewInstance() { |
21 return object_template_->NewInstance(); | 22 return object_template_->NewInstance(); |
22 } | 23 } |
23 | 24 |
24 // static | 25 // static |
25 v8::Handle<v8::Value> NativeHandler::Router(const v8::Arguments& args) { | 26 v8::Handle<v8::Value> NativeHandler::Router(const v8::Arguments& args) { |
26 HandlerFunction* handler_function = static_cast<HandlerFunction*>( | 27 HandlerFunction* handler_function = static_cast<HandlerFunction*>( |
27 args.Data().As<v8::External>()->Value()); | 28 args.Data().As<v8::External>()->Value()); |
29 if (!ModuleSystem::IsPresentInCurrentContext()) | |
30 return v8::ThrowException(v8::Exception::Error( | |
31 v8::String::New("ModuleSystem has been deleted"))); | |
not at google - send to devlin
2012/04/24 05:15:29
nit: braces around multi-line-single-statement if
koz (OOO until 15th September)
2012/04/24 05:35:20
Done.
| |
28 return handler_function->Run(args); | 32 return handler_function->Run(args); |
29 } | 33 } |
30 | 34 |
31 void NativeHandler::RouteFunction(const std::string& name, | 35 void NativeHandler::RouteFunction(const std::string& name, |
32 const HandlerFunction& handler_function) { | 36 const HandlerFunction& handler_function) { |
33 linked_ptr<HandlerFunction> function(new HandlerFunction(handler_function)); | 37 linked_ptr<HandlerFunction> function(new HandlerFunction(handler_function)); |
34 // TODO(koz): Investigate using v8's MakeWeak() function instead of holding | 38 // TODO(koz): Investigate using v8's MakeWeak() function instead of holding |
35 // on to these pointers here. | 39 // on to these pointers here. |
36 handler_functions_.push_back(function); | 40 handler_functions_.push_back(function); |
37 v8::Handle<v8::FunctionTemplate> function_template = | 41 v8::Handle<v8::FunctionTemplate> function_template = |
38 v8::FunctionTemplate::New(Router, | 42 v8::FunctionTemplate::New(Router, |
39 v8::External::New(function.get())); | 43 v8::External::New(function.get())); |
40 object_template_->Set(name.c_str(), function_template); | 44 object_template_->Set(name.c_str(), function_template); |
41 } | 45 } |
42 | 46 |
43 void NativeHandler::RouteStaticFunction(const std::string& name, | 47 void NativeHandler::RouteStaticFunction(const std::string& name, |
44 const HandlerFunc handler_func) { | 48 const HandlerFunc handler_func) { |
45 v8::Handle<v8::FunctionTemplate> function_template = | 49 v8::Handle<v8::FunctionTemplate> function_template = |
46 v8::FunctionTemplate::New(handler_func, v8::External::New(this)); | 50 v8::FunctionTemplate::New(handler_func, v8::External::New(this)); |
47 object_template_->Set(name.c_str(), function_template); | 51 object_template_->Set(name.c_str(), function_template); |
48 } | 52 } |
OLD | NEW |