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

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

Issue 9657026: Revert 125801 - Implement a module system for the extension bindings JS. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 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
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/extensions/event_bindings.h" 5 #include "chrome/renderer/extensions/event_bindings.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
(...skipping 21 matching lines...) Expand all
32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
33 #include "v8/include/v8.h" 33 #include "v8/include/v8.h"
34 34
35 using WebKit::WebFrame; 35 using WebKit::WebFrame;
36 using WebKit::WebSecurityOrigin; 36 using WebKit::WebSecurityOrigin;
37 using WebKit::WebURL; 37 using WebKit::WebURL;
38 using content::RenderThread; 38 using content::RenderThread;
39 39
40 namespace { 40 namespace {
41 41
42 // A map of event names to the number of contexts listening to that event.
43 // We notify the browser about event listeners when we transition between 0
44 // and 1.
45 typedef std::map<std::string, int> EventListenerCounts;
46
47 // A map of extension IDs to listener counts for that extension.
48 base::LazyInstance<std::map<std::string, EventListenerCounts> >
49 g_listener_counts = LAZY_INSTANCE_INITIALIZER;
50
51 // TODO(koz): Merge this into EventBindings.
52 class ExtensionImpl : public ChromeV8Extension { 42 class ExtensionImpl : public ChromeV8Extension {
53 public: 43 public:
54
55 explicit ExtensionImpl(ExtensionDispatcher* dispatcher) 44 explicit ExtensionImpl(ExtensionDispatcher* dispatcher)
56 : ChromeV8Extension(dispatcher) { 45 : ChromeV8Extension("extensions/event.js",
57 RouteStaticFunction("AttachEvent", &AttachEvent); 46 IDR_EVENT_BINDINGS_JS,
58 RouteStaticFunction("DetachEvent", &DetachEvent); 47 dispatcher) {
59 } 48 }
60 ~ExtensionImpl() {} 49 ~ExtensionImpl() {}
61 50
51 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
52 v8::Handle<v8::String> name) {
53 if (name->Equals(v8::String::New("AttachEvent"))) {
54 return v8::FunctionTemplate::New(AttachEvent, v8::External::New(this));
55 } else if (name->Equals(v8::String::New("DetachEvent"))) {
56 return v8::FunctionTemplate::New(DetachEvent, v8::External::New(this));
57 }
58 return ChromeV8Extension::GetNativeFunction(name);
59 }
60
62 // Attach an event name to an object. 61 // Attach an event name to an object.
63 static v8::Handle<v8::Value> AttachEvent(const v8::Arguments& args) { 62 static v8::Handle<v8::Value> AttachEvent(const v8::Arguments& args) {
64 DCHECK(args.Length() == 1); 63 DCHECK(args.Length() == 1);
65 // TODO(erikkay) should enforce that event name is a string in the bindings 64 // TODO(erikkay) should enforce that event name is a string in the bindings
66 DCHECK(args[0]->IsString() || args[0]->IsUndefined()); 65 DCHECK(args[0]->IsString() || args[0]->IsUndefined());
67 66
68 if (args[0]->IsString()) { 67 if (args[0]->IsString()) {
69 ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args); 68 ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args);
70 const ChromeV8ContextSet& context_set = 69 const ChromeV8ContextSet& context_set =
71 self->extension_dispatcher()->v8_context_set(); 70 self->extension_dispatcher()->v8_context_set();
72 ChromeV8Context* context = context_set.GetCurrent(); 71 ChromeV8Context* context = context_set.GetCurrent();
73 CHECK(context); 72 CHECK(context);
74 std::string event_name(*v8::String::AsciiValue(args[0])); 73 std::string event_name(*v8::String::AsciiValue(args[0]));
75 74
76 if (!self->CheckCurrentContextAccessToExtensionAPI(event_name)) 75 if (!self->CheckCurrentContextAccessToExtensionAPI(event_name))
77 return v8::Undefined(); 76 return v8::Undefined();
78 77
79 EventListenerCounts& listener_counts = 78 EventListenerCounts& listener_counts =
80 g_listener_counts.Get()[context->extension_id()]; 79 self->listener_counts_[context->extension_id()];
81 if (++listener_counts[event_name] == 1) { 80 if (++listener_counts[event_name] == 1) {
82 content::RenderThread::Get()->Send( 81 content::RenderThread::Get()->Send(
83 new ExtensionHostMsg_AddListener(context->extension_id(), 82 new ExtensionHostMsg_AddListener(context->extension_id(),
84 event_name)); 83 event_name));
85 } 84 }
86 85
87 // This is called the first time the page has added a listener. Since 86 // This is called the first time the page has added a listener. Since
88 // the background page is the only lazy page, we know this is the first 87 // the background page is the only lazy page, we know this is the first
89 // time this listener has been registered. 88 // time this listener has been registered.
90 if (self->IsLazyBackgroundPage(context->extension_id())) { 89 if (self->IsLazyBackgroundPage(context->extension_id())) {
(...skipping 13 matching lines...) Expand all
104 103
105 if (args[0]->IsString() && args[1]->IsBoolean()) { 104 if (args[0]->IsString() && args[1]->IsBoolean()) {
106 ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args); 105 ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args);
107 const ChromeV8ContextSet& context_set = 106 const ChromeV8ContextSet& context_set =
108 self->extension_dispatcher()->v8_context_set(); 107 self->extension_dispatcher()->v8_context_set();
109 ChromeV8Context* context = context_set.GetCurrent(); 108 ChromeV8Context* context = context_set.GetCurrent();
110 if (!context) 109 if (!context)
111 return v8::Undefined(); 110 return v8::Undefined();
112 111
113 EventListenerCounts& listener_counts = 112 EventListenerCounts& listener_counts =
114 g_listener_counts.Get()[context->extension_id()]; 113 self->listener_counts_[context->extension_id()];
115 std::string event_name(*v8::String::AsciiValue(args[0])); 114 std::string event_name(*v8::String::AsciiValue(args[0]));
116 bool is_manual = args[1]->BooleanValue(); 115 bool is_manual = args[1]->BooleanValue();
117 116
118 if (--listener_counts[event_name] == 0) { 117 if (--listener_counts[event_name] == 0) {
119 content::RenderThread::Get()->Send( 118 content::RenderThread::Get()->Send(
120 new ExtensionHostMsg_RemoveListener(context->extension_id(), 119 new ExtensionHostMsg_RemoveListener(context->extension_id(),
121 event_name)); 120 event_name));
122 } 121 }
123 122
124 // DetachEvent is called when the last listener for the context is 123 // DetachEvent is called when the last listener for the context is
125 // removed. If the context is the background page, and it removes the 124 // removed. If the context is the background page, and it removes the
126 // last listener manually, then we assume that it is no longer interested 125 // last listener manually, then we assume that it is no longer interested
127 // in being awakened for this event. 126 // in being awakened for this event.
128 if (is_manual && self->IsLazyBackgroundPage(context->extension_id())) { 127 if (is_manual && self->IsLazyBackgroundPage(context->extension_id())) {
129 content::RenderThread::Get()->Send( 128 content::RenderThread::Get()->Send(
130 new ExtensionHostMsg_RemoveLazyListener(context->extension_id(), 129 new ExtensionHostMsg_RemoveLazyListener(context->extension_id(),
131 event_name)); 130 event_name));
132 } 131 }
133 } 132 }
134 133
135 return v8::Undefined(); 134 return v8::Undefined();
136 } 135 }
137 136
138 private: 137 private:
138 // A map of event names to the number of contexts listening to that event.
139 // We notify the browser about event listeners when we transition between 0
140 // and 1.
141 typedef std::map<std::string, int> EventListenerCounts;
139 142
140 bool IsLazyBackgroundPage(const std::string& extension_id) { 143 bool IsLazyBackgroundPage(const std::string& extension_id) {
141 content::RenderView* render_view = GetCurrentRenderView(); 144 content::RenderView* render_view = GetCurrentRenderView();
142 if (!render_view) 145 if (!render_view)
143 return false; 146 return false;
144 147
145 ExtensionHelper* helper = ExtensionHelper::Get(render_view); 148 ExtensionHelper* helper = ExtensionHelper::Get(render_view);
146 const ::Extension* extension = 149 const ::Extension* extension =
147 extension_dispatcher()->extensions()->GetByID(extension_id); 150 extension_dispatcher()->extensions()->GetByID(extension_id);
148 return (extension && !extension->background_page_persists() && 151 return (extension && !extension->background_page_persists() &&
149 helper->view_type() == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); 152 helper->view_type() == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE);
150 } 153 }
154
155 // A map of extension IDs to listener counts for that extension.
156 std::map<std::string, EventListenerCounts> listener_counts_;
151 }; 157 };
152 158
153 } // namespace 159 } // namespace
154 160
155 ChromeV8Extension* EventBindings::Get(ExtensionDispatcher* dispatcher) { 161 v8::Extension* EventBindings::Get(ExtensionDispatcher* dispatcher) {
156 return new ExtensionImpl(dispatcher); 162 static v8::Extension* extension = new ExtensionImpl(dispatcher);
163 return extension;
157 } 164 }
OLDNEW
« no previous file with comments | « chrome/renderer/extensions/event_bindings.h ('k') | chrome/renderer/extensions/experimental.socket_custom_bindings.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698