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

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

Issue 237723004: Move EventBindings to //extensions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 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
« no previous file with comments | « extensions/renderer/event_bindings.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "extensions/renderer/event_bindings.h"
6 6
7 #include <map> 7 #include <map>
8 #include <set> 8 #include <set>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/lazy_instance.h" 14 #include "base/lazy_instance.h"
15 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
16 #include "base/message_loop/message_loop.h"
17 #include "chrome/common/extensions/value_counter.h"
18 #include "chrome/common/url_constants.h"
19 #include "chrome/renderer/extensions/chrome_v8_context.h"
20 #include "chrome/renderer/extensions/chrome_v8_context_set.h"
21 #include "chrome/renderer/extensions/chrome_v8_extension.h"
22 #include "chrome/renderer/extensions/dispatcher.h" 16 #include "chrome/renderer/extensions/dispatcher.h"
23 #include "chrome/renderer/extensions/extension_helper.h" 17 #include "chrome/renderer/extensions/extension_helper.h"
24 #include "chrome/renderer/extensions/user_script_slave.h"
25 #include "content/public/renderer/render_thread.h" 18 #include "content/public/renderer/render_thread.h"
26 #include "content/public/renderer/render_view.h" 19 #include "content/public/renderer/render_view.h"
27 #include "content/public/renderer/v8_value_converter.h" 20 #include "content/public/renderer/v8_value_converter.h"
28 #include "extensions/common/event_filter.h" 21 #include "extensions/common/event_filter.h"
29 #include "extensions/common/extension.h" 22 #include "extensions/common/extension.h"
30 #include "extensions/common/extension_messages.h" 23 #include "extensions/common/extension_messages.h"
31 #include "extensions/common/manifest_handlers/background_info.h" 24 #include "extensions/common/manifest_handlers/background_info.h"
32 #include "extensions/common/view_type.h" 25 #include "extensions/common/value_counter.h"
33 #include "grit/renderer_resources.h" 26 #include "extensions/renderer/object_backed_native_handler.h"
34 #include "third_party/WebKit/public/platform/WebURL.h"
35 #include "third_party/WebKit/public/platform/WebURLRequest.h"
36 #include "third_party/WebKit/public/web/WebDocument.h"
37 #include "third_party/WebKit/public/web/WebFrame.h"
38 #include "third_party/WebKit/public/web/WebView.h"
39 #include "url/gurl.h" 27 #include "url/gurl.h"
40 #include "v8/include/v8.h" 28 #include "v8/include/v8.h"
41 29
42 using blink::WebFrame;
43 using blink::WebURL;
44 using content::RenderThread;
45
46 namespace extensions { 30 namespace extensions {
47 31
48 namespace { 32 namespace {
49 33
50 // A map of event names to the number of contexts listening to that event. 34 // A map of event names to the number of contexts listening to that event.
51 // We notify the browser about event listeners when we transition between 0 35 // We notify the browser about event listeners when we transition between 0
52 // and 1. 36 // and 1.
53 typedef std::map<std::string, int> EventListenerCounts; 37 typedef std::map<std::string, int> EventListenerCounts;
54 38
55 // A map of extension IDs to listener counts for that extension. 39 // A map of extension IDs to listener counts for that extension.
56 base::LazyInstance<std::map<std::string, EventListenerCounts> > 40 base::LazyInstance<std::map<std::string, EventListenerCounts> >
57 g_listener_counts = LAZY_INSTANCE_INITIALIZER; 41 g_listener_counts = LAZY_INSTANCE_INITIALIZER;
58 42
59 // A map of event names to a (filter -> count) map. The map is used to keep 43 // A map of event names to a (filter -> count) map. The map is used to keep
60 // track of which filters are in effect for which events. 44 // track of which filters are in effect for which events.
61 // We notify the browser about filtered event listeners when we transition 45 // We notify the browser about filtered event listeners when we transition
62 // between 0 and 1. 46 // between 0 and 1.
63 typedef std::map<std::string, linked_ptr<ValueCounter> > 47 typedef std::map<std::string, linked_ptr<ValueCounter> >
64 FilteredEventListenerCounts; 48 FilteredEventListenerCounts;
65 49
66 // A map of extension IDs to filtered listener counts for that extension. 50 // A map of extension IDs to filtered listener counts for that extension.
67 base::LazyInstance<std::map<std::string, FilteredEventListenerCounts> > 51 base::LazyInstance<std::map<std::string, FilteredEventListenerCounts> >
68 g_filtered_listener_counts = LAZY_INSTANCE_INITIALIZER; 52 g_filtered_listener_counts = LAZY_INSTANCE_INITIALIZER;
69 53
70 base::LazyInstance<EventFilter> g_event_filter = LAZY_INSTANCE_INITIALIZER; 54 base::LazyInstance<EventFilter> g_event_filter = LAZY_INSTANCE_INITIALIZER;
71 55
72 // TODO(koz): Merge this into EventBindings. 56 // TODO(koz): Merge this into EventBindings.
73 class ExtensionImpl : public ChromeV8Extension { 57 class ExtensionImpl : public ObjectBackedNativeHandler {
74 public: 58 public:
75 explicit ExtensionImpl(Dispatcher* dispatcher, ChromeV8Context* context) 59 explicit ExtensionImpl(Dispatcher* dispatcher, ScriptContext* context)
76 : ChromeV8Extension(dispatcher, context) { 60 : ObjectBackedNativeHandler(context), dispatcher_(dispatcher) {
77 RouteFunction("AttachEvent", 61 RouteFunction(
62 "AttachEvent",
78 base::Bind(&ExtensionImpl::AttachEvent, base::Unretained(this))); 63 base::Bind(&ExtensionImpl::AttachEvent, base::Unretained(this)));
79 RouteFunction("DetachEvent", 64 RouteFunction(
65 "DetachEvent",
80 base::Bind(&ExtensionImpl::DetachEvent, base::Unretained(this))); 66 base::Bind(&ExtensionImpl::DetachEvent, base::Unretained(this)));
81 RouteFunction("AttachFilteredEvent", 67 RouteFunction("AttachFilteredEvent",
82 base::Bind(&ExtensionImpl::AttachFilteredEvent, 68 base::Bind(&ExtensionImpl::AttachFilteredEvent,
83 base::Unretained(this))); 69 base::Unretained(this)));
84 RouteFunction("DetachFilteredEvent", 70 RouteFunction("DetachFilteredEvent",
85 base::Bind(&ExtensionImpl::DetachFilteredEvent, 71 base::Bind(&ExtensionImpl::DetachFilteredEvent,
86 base::Unretained(this))); 72 base::Unretained(this)));
87 RouteFunction("MatchAgainstEventFilter", 73 RouteFunction("MatchAgainstEventFilter",
88 base::Bind(&ExtensionImpl::MatchAgainstEventFilter, 74 base::Bind(&ExtensionImpl::MatchAgainstEventFilter,
89 base::Unretained(this))); 75 base::Unretained(this)));
90 } 76 }
91 77
92 virtual ~ExtensionImpl() {} 78 virtual ~ExtensionImpl() {}
93 79
94 // Attach an event name to an object. 80 // Attach an event name to an object.
95 void AttachEvent(const v8::FunctionCallbackInfo<v8::Value>& args) { 81 void AttachEvent(const v8::FunctionCallbackInfo<v8::Value>& args) {
96 CHECK_EQ(1, args.Length()); 82 CHECK_EQ(1, args.Length());
97 CHECK(args[0]->IsString()); 83 CHECK(args[0]->IsString());
98 84
99 std::string event_name = *v8::String::Utf8Value(args[0]->ToString()); 85 std::string event_name = *v8::String::Utf8Value(args[0]->ToString());
100 86
101 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context())) 87 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context()))
102 return; 88 return;
103 89
104 std::string extension_id = context()->GetExtensionID(); 90 std::string extension_id = context()->GetExtensionID();
105 EventListenerCounts& listener_counts = 91 EventListenerCounts& listener_counts =
106 g_listener_counts.Get()[extension_id]; 92 g_listener_counts.Get()[extension_id];
107 if (++listener_counts[event_name] == 1) { 93 if (++listener_counts[event_name] == 1) {
108 content::RenderThread::Get()->Send( 94 content::RenderThread::Get()->Send(
109 new ExtensionHostMsg_AddListener(extension_id, event_name)); 95 new ExtensionHostMsg_AddListener(extension_id, event_name));
110 } 96 }
111 97
112 // This is called the first time the page has added a listener. Since 98 // This is called the first time the page has added a listener. Since
113 // the background page is the only lazy page, we know this is the first 99 // the background page is the only lazy page, we know this is the first
114 // time this listener has been registered. 100 // time this listener has been registered.
115 if (IsLazyBackgroundPage(GetRenderView(), context()->extension())) { 101 if (IsLazyBackgroundContext(context())) {
116 content::RenderThread::Get()->Send( 102 content::RenderThread::Get()->Send(
117 new ExtensionHostMsg_AddLazyListener(extension_id, event_name)); 103 new ExtensionHostMsg_AddLazyListener(extension_id, event_name));
118 } 104 }
119 } 105 }
120 106
121 void DetachEvent(const v8::FunctionCallbackInfo<v8::Value>& args) { 107 void DetachEvent(const v8::FunctionCallbackInfo<v8::Value>& args) {
122 CHECK_EQ(2, args.Length()); 108 CHECK_EQ(2, args.Length());
123 CHECK(args[0]->IsString()); 109 CHECK(args[0]->IsString());
124 CHECK(args[1]->IsBoolean()); 110 CHECK(args[1]->IsBoolean());
125 111
126 std::string event_name = *v8::String::Utf8Value(args[0]); 112 std::string event_name = *v8::String::Utf8Value(args[0]);
127 bool is_manual = args[1]->BooleanValue(); 113 bool is_manual = args[1]->BooleanValue();
128 114
129 std::string extension_id = context()->GetExtensionID(); 115 std::string extension_id = context()->GetExtensionID();
130 EventListenerCounts& listener_counts = 116 EventListenerCounts& listener_counts =
131 g_listener_counts.Get()[extension_id]; 117 g_listener_counts.Get()[extension_id];
132 118
133 if (--listener_counts[event_name] == 0) { 119 if (--listener_counts[event_name] == 0) {
134 content::RenderThread::Get()->Send( 120 content::RenderThread::Get()->Send(
135 new ExtensionHostMsg_RemoveListener(extension_id, event_name)); 121 new ExtensionHostMsg_RemoveListener(extension_id, event_name));
136 } 122 }
137 123
138 // DetachEvent is called when the last listener for the context is 124 // DetachEvent is called when the last listener for the context is
139 // removed. If the context is the background page, and it removes the 125 // removed. If the context is the background page, and it removes the
140 // last listener manually, then we assume that it is no longer interested 126 // last listener manually, then we assume that it is no longer interested
141 // in being awakened for this event. 127 // in being awakened for this event.
142 if (is_manual && IsLazyBackgroundPage(GetRenderView(), 128 if (is_manual && IsLazyBackgroundContext(context())) {
143 context()->extension())) {
144 content::RenderThread::Get()->Send( 129 content::RenderThread::Get()->Send(
145 new ExtensionHostMsg_RemoveLazyListener(extension_id, event_name)); 130 new ExtensionHostMsg_RemoveLazyListener(extension_id, event_name));
146 } 131 }
147 } 132 }
148 133
149 // MatcherID AttachFilteredEvent(string event_name, object filter) 134 // MatcherID AttachFilteredEvent(string event_name, object filter)
150 // event_name - Name of the event to attach. 135 // event_name - Name of the event to attach.
151 // filter - Which instances of the named event are we interested in. 136 // filter - Which instances of the named event are we interested in.
152 // returns the id assigned to the listener, which will be returned from calls 137 // returns the id assigned to the listener, which will be returned from calls
153 // to MatchAgainstEventFilter where this listener matches. 138 // to MatchAgainstEventFilter where this listener matches.
(...skipping 26 matching lines...) Expand all
180 return; 165 return;
181 } 166 }
182 if (!filter_value->GetAsDictionary(&filter_dict)) { 167 if (!filter_value->GetAsDictionary(&filter_dict)) {
183 delete filter_value; 168 delete filter_value;
184 args.GetReturnValue().Set(static_cast<int32_t>(-1)); 169 args.GetReturnValue().Set(static_cast<int32_t>(-1));
185 return; 170 return;
186 } 171 }
187 172
188 filter.reset(filter_dict); 173 filter.reset(filter_dict);
189 EventFilter& event_filter = g_event_filter.Get(); 174 EventFilter& event_filter = g_event_filter.Get();
190 int id = event_filter.AddEventMatcher(event_name, ParseEventMatcher( 175 int id = event_filter.AddEventMatcher(event_name,
191 filter.get())); 176 ParseEventMatcher(filter.get()));
192 177
193 // Only send IPCs the first time a filter gets added. 178 // Only send IPCs the first time a filter gets added.
194 if (AddFilter(event_name, extension_id, filter.get())) { 179 if (AddFilter(event_name, extension_id, filter.get())) {
195 bool lazy = IsLazyBackgroundPage(GetRenderView(), context()->extension()); 180 bool lazy = IsLazyBackgroundContext(context());
196 content::RenderThread::Get()->Send( 181 content::RenderThread::Get()->Send(
197 new ExtensionHostMsg_AddFilteredListener(extension_id, event_name, 182 new ExtensionHostMsg_AddFilteredListener(
198 *filter, lazy)); 183 extension_id, event_name, *filter, lazy));
199 } 184 }
200 185
201 args.GetReturnValue().Set(static_cast<int32_t>(id)); 186 args.GetReturnValue().Set(static_cast<int32_t>(id));
202 } 187 }
203 188
204 // Add a filter to |event_name| in |extension_id|, returning true if it 189 // Add a filter to |event_name| in |extension_id|, returning true if it
205 // was the first filter for that event in that extension. 190 // was the first filter for that event in that extension.
206 static bool AddFilter(const std::string& event_name, 191 static bool AddFilter(const std::string& event_name,
207 const std::string& extension_id, 192 const std::string& extension_id,
208 base::DictionaryValue* filter) { 193 base::DictionaryValue* filter) {
(...skipping 29 matching lines...) Expand all
238 CHECK(args[0]->IsInt32()); 223 CHECK(args[0]->IsInt32());
239 CHECK(args[1]->IsBoolean()); 224 CHECK(args[1]->IsBoolean());
240 bool is_manual = args[1]->BooleanValue(); 225 bool is_manual = args[1]->BooleanValue();
241 226
242 std::string extension_id = context()->GetExtensionID(); 227 std::string extension_id = context()->GetExtensionID();
243 if (extension_id.empty()) 228 if (extension_id.empty())
244 return; 229 return;
245 230
246 int matcher_id = args[0]->Int32Value(); 231 int matcher_id = args[0]->Int32Value();
247 EventFilter& event_filter = g_event_filter.Get(); 232 EventFilter& event_filter = g_event_filter.Get();
248 EventMatcher* event_matcher = 233 EventMatcher* event_matcher = event_filter.GetEventMatcher(matcher_id);
249 event_filter.GetEventMatcher(matcher_id);
250 234
251 const std::string& event_name = event_filter.GetEventName(matcher_id); 235 const std::string& event_name = event_filter.GetEventName(matcher_id);
252 236
253 // Only send IPCs the last time a filter gets removed. 237 // Only send IPCs the last time a filter gets removed.
254 if (RemoveFilter(event_name, extension_id, event_matcher->value())) { 238 if (RemoveFilter(event_name, extension_id, event_matcher->value())) {
255 bool lazy = is_manual && IsLazyBackgroundPage(GetRenderView(), 239 bool lazy = is_manual && IsLazyBackgroundContext(context());
256 context()->extension());
257 content::RenderThread::Get()->Send( 240 content::RenderThread::Get()->Send(
258 new ExtensionHostMsg_RemoveFilteredListener(extension_id, event_name, 241 new ExtensionHostMsg_RemoveFilteredListener(
259 *event_matcher->value(), 242 extension_id, event_name, *event_matcher->value(), lazy));
260 lazy));
261 } 243 }
262 244
263 event_filter.RemoveEventMatcher(matcher_id); 245 event_filter.RemoveEventMatcher(matcher_id);
264 } 246 }
265 247
266 void MatchAgainstEventFilter( 248 void MatchAgainstEventFilter(
267 const v8::FunctionCallbackInfo<v8::Value>& args) { 249 const v8::FunctionCallbackInfo<v8::Value>& args) {
268 v8::Isolate* isolate = args.GetIsolate(); 250 v8::Isolate* isolate = args.GetIsolate();
269 typedef std::set<EventFilter::MatcherID> MatcherIDs; 251 typedef std::set<EventFilter::MatcherID> MatcherIDs;
270 EventFilter& event_filter = g_event_filter.Get(); 252 EventFilter& event_filter = g_event_filter.Get();
271 std::string event_name = *v8::String::Utf8Value(args[0]->ToString()); 253 std::string event_name = *v8::String::Utf8Value(args[0]->ToString());
272 EventFilteringInfo info = 254 EventFilteringInfo info = ParseFromObject(args[1]->ToObject(), isolate);
273 ParseFromObject(args[1]->ToObject(), isolate);
274 // Only match events routed to this context's RenderView or ones that don't 255 // Only match events routed to this context's RenderView or ones that don't
275 // have a routingId in their filter. 256 // have a routingId in their filter.
276 MatcherIDs matched_event_filters = event_filter.MatchEvent( 257 MatcherIDs matched_event_filters = event_filter.MatchEvent(
277 event_name, info, context()->GetRenderView()->GetRoutingID()); 258 event_name, info, context()->GetRenderView()->GetRoutingID());
278 v8::Handle<v8::Array> array( 259 v8::Handle<v8::Array> array(
279 v8::Array::New(isolate, matched_event_filters.size())); 260 v8::Array::New(isolate, matched_event_filters.size()));
280 int i = 0; 261 int i = 0;
281 for (MatcherIDs::iterator it = matched_event_filters.begin(); 262 for (MatcherIDs::iterator it = matched_event_filters.begin();
282 it != matched_event_filters.end(); ++it) { 263 it != matched_event_filters.end();
264 ++it) {
283 array->Set(v8::Integer::New(isolate, i++), 265 array->Set(v8::Integer::New(isolate, i++),
284 v8::Integer::New(isolate, *it)); 266 v8::Integer::New(isolate, *it));
285 } 267 }
286 args.GetReturnValue().Set(array); 268 args.GetReturnValue().Set(array);
287 } 269 }
288 270
289 static EventFilteringInfo ParseFromObject(v8::Handle<v8::Object> object, 271 static EventFilteringInfo ParseFromObject(v8::Handle<v8::Object> object,
290 v8::Isolate* isolate) { 272 v8::Isolate* isolate) {
291 EventFilteringInfo info; 273 EventFilteringInfo info;
292 v8::Handle<v8::String> url(v8::String::NewFromUtf8(isolate, "url")); 274 v8::Handle<v8::String> url(v8::String::NewFromUtf8(isolate, "url"));
(...skipping 10 matching lines...) Expand all
303 v8::Handle<v8::String> service_type( 285 v8::Handle<v8::String> service_type(
304 v8::String::NewFromUtf8(isolate, "serviceType")); 286 v8::String::NewFromUtf8(isolate, "serviceType"));
305 if (object->Has(service_type)) { 287 if (object->Has(service_type)) {
306 v8::Handle<v8::Value> service_type_value(object->Get(service_type)); 288 v8::Handle<v8::Value> service_type_value(object->Get(service_type));
307 info.SetServiceType(*v8::String::Utf8Value(service_type_value)); 289 info.SetServiceType(*v8::String::Utf8Value(service_type_value));
308 } 290 }
309 return info; 291 return info;
310 } 292 }
311 293
312 private: 294 private:
313 static bool IsLazyBackgroundPage(content::RenderView* render_view, 295 static bool IsLazyBackgroundContext(ScriptContext* context) {
314 const Extension* extension) { 296 content::RenderView* render_view = context->GetRenderView();
315 if (!render_view) 297 if (!render_view)
316 return false; 298 return false;
317 ExtensionHelper* helper = ExtensionHelper::Get(render_view); 299 ExtensionHelper* helper = ExtensionHelper::Get(render_view);
300 const Extension* extension = context->extension();
318 return (extension && BackgroundInfo::HasLazyBackgroundPage(extension) && 301 return (extension && BackgroundInfo::HasLazyBackgroundPage(extension) &&
319 helper->view_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); 302 helper->view_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE);
320 } 303 }
321 304
322 scoped_ptr<EventMatcher> ParseEventMatcher( 305 scoped_ptr<EventMatcher> ParseEventMatcher(
323 base::DictionaryValue* filter_dict) { 306 base::DictionaryValue* filter_dict) {
324 return scoped_ptr<EventMatcher>(new EventMatcher( 307 return scoped_ptr<EventMatcher>(new EventMatcher(
325 scoped_ptr<base::DictionaryValue>(filter_dict->DeepCopy()), 308 scoped_ptr<base::DictionaryValue>(filter_dict->DeepCopy()),
326 context()->GetRenderView()->GetRoutingID())); 309 context()->GetRenderView()->GetRoutingID()));
327 } 310 }
311
312 // Not owned.
313 Dispatcher* dispatcher_;
328 }; 314 };
329 315
330 } // namespace 316 } // namespace
331 317
332 // static 318 // static
333 ChromeV8Extension* EventBindings::Create(Dispatcher* dispatcher, 319 ObjectBackedNativeHandler* EventBindings::Create(Dispatcher* dispatcher,
334 ChromeV8Context* context) { 320 ScriptContext* context) {
335 return new ExtensionImpl(dispatcher, context); 321 return new ExtensionImpl(dispatcher, context);
336 } 322 }
337 323
338 } // namespace extensions 324 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/event_bindings.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698