blob: 3c41ea53928bfe7fc71747dce604c0413e5b2822 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/renderer/extensions/native_handler.h"
#include "base/memory/linked_ptr.h"
#include "base/logging.h"
#include "chrome/renderer/extensions/module_system.h"
#include "v8/include/v8.h"
namespace extensions {
NativeHandler::NativeHandler(v8::Isolate* isolate)
: isolate_(isolate),
object_template_(
v8::Persistent<v8::ObjectTemplate>::New(isolate,
v8::ObjectTemplate::New())) {
}
NativeHandler::~NativeHandler() {
object_template_.Dispose(isolate_);
}
v8::Handle<v8::Object> NativeHandler::NewInstance() {
return object_template_->NewInstance();
}
// static
v8::Handle<v8::Value> NativeHandler::Router(const v8::Arguments& args) {
// It is possible for JS code to execute after ModuleSystem has been deleted
// in which case the native handlers will also have been deleted, making
// HandlerFunction below point to freed memory.
if (!ModuleSystem::IsPresentInCurrentContext()) {
return v8::ThrowException(v8::Exception::Error(
v8::String::New("ModuleSystem has been deleted")));
}
HandlerFunction* handler_function = static_cast<HandlerFunction*>(
args.Data().As<v8::External>()->Value());
return handler_function->Run(args);
}
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);
v8::Handle<v8::FunctionTemplate> function_template =
v8::FunctionTemplate::New(Router,
v8::External::New(function.get()));
object_template_->Set(name.c_str(), function_template);
}
void NativeHandler::RouteStaticFunction(const std::string& name,
const HandlerFunc handler_func) {
v8::Handle<v8::FunctionTemplate> function_template =
v8::FunctionTemplate::New(handler_func, v8::External::New(this));
object_template_->Set(name.c_str(), function_template);
}
} // extensions
|