Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: chrome/renderer/extensions/object_backed_native_handler.cc

Issue 12632004: Revert 186643 - Caused a 10% regression on SunSpider benchmark (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/object_backed_native_handler.h"
6
7 #include "base/logging.h"
8 #include "base/memory/linked_ptr.h"
9 #include "chrome/renderer/extensions/module_system.h"
10 #include "v8/include/v8.h"
11
12 namespace extensions {
13
14 // Data to pass to ObjectBackedNativeHandler::Router.
15 struct ObjectBackedNativeHandler::RouterData {
16 RouterData(ObjectBackedNativeHandler* self, HandlerFunction function)
17 : self(self), function(function) {}
18
19 ~RouterData() {}
20
21 // The owner of the routed data.
22 ObjectBackedNativeHandler* const self;
23
24 // The function to route calls to.
25 HandlerFunction function;
26 };
27
28 ObjectBackedNativeHandler::ObjectBackedNativeHandler(
29 v8::Handle<v8::Context> context)
30 : v8_context_(context),
31 object_template_(
32 v8::Persistent<v8::ObjectTemplate>::New(context->GetIsolate(),
33 v8::ObjectTemplate::New())) {
34 }
35
36 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() {
37 }
38
39 v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() {
40 return object_template_->NewInstance();
41 }
42
43 // static
44 v8::Handle<v8::Value> ObjectBackedNativeHandler::Router(
45 const v8::Arguments& args) {
46 RouterData* router_data = static_cast<RouterData*>(
47 args.Data().As<v8::External>()->Value());
48 // Router can be called during context destruction. Stop.
49 if (!router_data->self->is_valid())
50 return v8::Handle<v8::Value>();
51 return router_data->function.Run(args);
52 }
53
54 void ObjectBackedNativeHandler::RouteFunction(const std::string& name,
55 const HandlerFunction& handler_function) {
56 linked_ptr<RouterData> data(new RouterData(this, handler_function));
57 // TODO(koz): Investigate using v8's MakeWeak() function instead of holding
58 // on to these pointers here.
59 router_data_.push_back(data);
60 v8::Handle<v8::FunctionTemplate> function_template =
61 v8::FunctionTemplate::New(Router, v8::External::New(data.get()));
62 object_template_->Set(name.c_str(), function_template);
63 }
64
65 void ObjectBackedNativeHandler::RouteStaticFunction(const std::string& name,
66 const HandlerFunc handler_func) {
67 v8::Handle<v8::FunctionTemplate> function_template =
68 v8::FunctionTemplate::New(handler_func, v8::External::New(this));
69 object_template_->Set(name.c_str(), function_template);
70 }
71
72 void ObjectBackedNativeHandler::Invalidate() {
73 if (!is_valid())
74 return;
75 object_template_.Clear();
76 v8_context_.Clear();
77 NativeHandler::Invalidate();
78 }
79
80 } // extensions
OLDNEW
« no previous file with comments | « chrome/renderer/extensions/object_backed_native_handler.h ('k') | chrome/renderer/extensions/page_actions_custom_bindings.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698