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/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/callback.h" |
9 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/memory/scoped_ptr.h" |
10 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/memory/scoped_ptr.h" |
11 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 15 #include "chrome/common/extensions/matcher/url_matcher.h" |
12 #include "chrome/common/extensions/extension_messages.h" | 16 #include "chrome/common/extensions/extension_messages.h" |
13 #include "chrome/common/extensions/extension_set.h" | 17 #include "chrome/common/extensions/extension_set.h" |
| 18 #include "chrome/common/extensions/event_filter.h" |
| 19 #include "chrome/common/extensions/value_set.h" |
14 #include "chrome/common/url_constants.h" | 20 #include "chrome/common/url_constants.h" |
15 #include "chrome/common/view_type.h" | 21 #include "chrome/common/view_type.h" |
16 #include "chrome/renderer/extensions/chrome_v8_context.h" | 22 #include "chrome/renderer/extensions/chrome_v8_context.h" |
17 #include "chrome/renderer/extensions/chrome_v8_context_set.h" | 23 #include "chrome/renderer/extensions/chrome_v8_context_set.h" |
18 #include "chrome/renderer/extensions/chrome_v8_extension.h" | 24 #include "chrome/renderer/extensions/chrome_v8_extension.h" |
19 #include "chrome/renderer/extensions/event_bindings.h" | 25 #include "chrome/renderer/extensions/event_bindings.h" |
20 #include "chrome/renderer/extensions/extension_dispatcher.h" | 26 #include "chrome/renderer/extensions/extension_dispatcher.h" |
21 #include "chrome/renderer/extensions/extension_helper.h" | 27 #include "chrome/renderer/extensions/extension_helper.h" |
22 #include "chrome/renderer/extensions/user_script_slave.h" | 28 #include "chrome/renderer/extensions/user_script_slave.h" |
23 #include "content/public/renderer/render_thread.h" | 29 #include "content/public/renderer/render_thread.h" |
| 30 #include "content/public/renderer/v8_value_converter.h" |
24 #include "googleurl/src/gurl.h" | 31 #include "googleurl/src/gurl.h" |
25 #include "grit/renderer_resources.h" | 32 #include "grit/renderer_resources.h" |
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | 35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
29 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" | 36 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" |
30 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" | 37 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" |
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 38 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
32 #include "v8/include/v8.h" | 39 #include "v8/include/v8.h" |
33 | 40 |
34 using WebKit::WebFrame; | 41 using WebKit::WebFrame; |
35 using WebKit::WebSecurityOrigin; | 42 using WebKit::WebSecurityOrigin; |
36 using WebKit::WebURL; | 43 using WebKit::WebURL; |
37 using content::RenderThread; | 44 using content::RenderThread; |
38 using extensions::Extension; | 45 using extensions::Extension; |
39 | 46 |
40 namespace { | 47 namespace { |
41 | 48 |
42 // A map of event names to the number of contexts listening to that event. | 49 // 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 | 50 // We notify the browser about event listeners when we transition between 0 |
44 // and 1. | 51 // and 1. |
45 typedef std::map<std::string, int> EventListenerCounts; | 52 typedef std::map<std::string, int> EventListenerCounts; |
46 | 53 |
47 // A map of extension IDs to listener counts for that extension. | 54 // A map of extension IDs to listener counts for that extension. |
48 base::LazyInstance<std::map<std::string, EventListenerCounts> > | 55 base::LazyInstance<std::map<std::string, EventListenerCounts> > |
49 g_listener_counts = LAZY_INSTANCE_INITIALIZER; | 56 g_listener_counts = LAZY_INSTANCE_INITIALIZER; |
50 | 57 |
| 58 // A map of event names to a (filter -> count) map. The map is used to keep |
| 59 // track of which filters are in effect for which events. |
| 60 // We notify the browser about filtered event listeners when we transition |
| 61 // between 0 and 1. |
| 62 typedef std::map<std::string, linked_ptr<extensions::ValueSet> > |
| 63 FilteredEventListenerCounts; |
| 64 |
| 65 // A map of extension IDs to filtered listener counts for that extension. |
| 66 base::LazyInstance<std::map<std::string, FilteredEventListenerCounts> > |
| 67 g_filtered_listener_counts = LAZY_INSTANCE_INITIALIZER; |
| 68 |
51 // TODO(koz): Merge this into EventBindings. | 69 // TODO(koz): Merge this into EventBindings. |
52 class ExtensionImpl : public ChromeV8Extension { | 70 class ExtensionImpl : public ChromeV8Extension { |
53 public: | 71 public: |
54 | 72 |
55 explicit ExtensionImpl(ExtensionDispatcher* dispatcher) | 73 ExtensionImpl(ExtensionDispatcher* dispatcher, |
56 : ChromeV8Extension(dispatcher) { | 74 extensions::EventFilter* event_filter) |
57 RouteStaticFunction("AttachEvent", &AttachEvent); | 75 : ChromeV8Extension(dispatcher), |
58 RouteStaticFunction("DetachEvent", &DetachEvent); | 76 event_filter_(event_filter) { |
| 77 RouteFunction("AttachEvent", |
| 78 base::Bind(&ExtensionImpl::AttachEvent, |
| 79 base::Unretained(this))); |
| 80 RouteFunction("DetachEvent", |
| 81 base::Bind(&ExtensionImpl::DetachEvent, |
| 82 base::Unretained(this))); |
| 83 RouteFunction("AttachFilteredEvent", |
| 84 base::Bind(&ExtensionImpl::AttachFilteredEvent, |
| 85 base::Unretained(this))); |
| 86 RouteFunction("DetachFilteredEvent", |
| 87 base::Bind(&ExtensionImpl::DetachFilteredEvent, |
| 88 base::Unretained(this))); |
| 89 RouteFunction("MatchAgainstEventFilter", |
| 90 base::Bind(&ExtensionImpl::MatchAgainstEventFilter, |
| 91 base::Unretained(this))); |
59 } | 92 } |
60 ~ExtensionImpl() {} | 93 ~ExtensionImpl() {} |
61 | 94 |
62 // Attach an event name to an object. | 95 // Attach an event name to an object. |
63 static v8::Handle<v8::Value> AttachEvent(const v8::Arguments& args) { | 96 v8::Handle<v8::Value> AttachEvent(const v8::Arguments& args) { |
64 DCHECK(args.Length() == 1); | 97 DCHECK(args.Length() == 1); |
65 // TODO(erikkay) should enforce that event name is a string in the bindings | 98 // TODO(erikkay) should enforce that event name is a string in the bindings |
66 DCHECK(args[0]->IsString() || args[0]->IsUndefined()); | 99 DCHECK(args[0]->IsString() || args[0]->IsUndefined()); |
67 | 100 |
68 if (args[0]->IsString()) { | 101 if (args[0]->IsString()) { |
69 ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args); | 102 std::string event_name = *v8::String::AsciiValue(args[0]->ToString()); |
70 const ChromeV8ContextSet& context_set = | 103 const ChromeV8ContextSet& context_set = |
71 self->extension_dispatcher()->v8_context_set(); | 104 extension_dispatcher()->v8_context_set(); |
72 ChromeV8Context* context = context_set.GetCurrent(); | 105 ChromeV8Context* context = context_set.GetCurrent(); |
73 CHECK(context); | 106 CHECK(context); |
74 std::string event_name(*v8::String::AsciiValue(args[0])); | |
75 | 107 |
76 ExtensionDispatcher* extension_dispatcher = self->extension_dispatcher(); | 108 if (!extension_dispatcher()->CheckCurrentContextAccessToExtensionAPI( |
77 if (!extension_dispatcher->CheckCurrentContextAccessToExtensionAPI( | |
78 event_name)) | 109 event_name)) |
79 return v8::Undefined(); | 110 return v8::Undefined(); |
80 | 111 |
81 std::string extension_id = context->GetExtensionID(); | 112 std::string extension_id = context->GetExtensionID(); |
82 EventListenerCounts& listener_counts = | 113 EventListenerCounts& listener_counts = |
83 g_listener_counts.Get()[extension_id]; | 114 g_listener_counts.Get()[extension_id]; |
84 if (++listener_counts[event_name] == 1) { | 115 if (++listener_counts[event_name] == 1) { |
85 content::RenderThread::Get()->Send( | 116 content::RenderThread::Get()->Send( |
86 new ExtensionHostMsg_AddListener(extension_id, event_name)); | 117 new ExtensionHostMsg_AddListener(extension_id, event_name)); |
87 } | 118 } |
88 | 119 |
89 // This is called the first time the page has added a listener. Since | 120 // This is called the first time the page has added a listener. Since |
90 // the background page is the only lazy page, we know this is the first | 121 // the background page is the only lazy page, we know this is the first |
91 // time this listener has been registered. | 122 // time this listener has been registered. |
92 if (self->IsLazyBackgroundPage(context->extension())) { | 123 if (IsLazyBackgroundPage(context->extension())) { |
93 content::RenderThread::Get()->Send( | 124 content::RenderThread::Get()->Send( |
94 new ExtensionHostMsg_AddLazyListener(extension_id, event_name)); | 125 new ExtensionHostMsg_AddLazyListener(extension_id, event_name)); |
95 } | 126 } |
96 } | 127 } |
97 | |
98 return v8::Undefined(); | 128 return v8::Undefined(); |
99 } | 129 } |
100 | 130 |
101 static v8::Handle<v8::Value> DetachEvent(const v8::Arguments& args) { | 131 v8::Handle<v8::Value> DetachEvent(const v8::Arguments& args) { |
102 DCHECK(args.Length() == 2); | 132 DCHECK(args.Length() == 2); |
103 // TODO(erikkay) should enforce that event name is a string in the bindings | 133 // TODO(erikkay) should enforce that event name is a string in the bindings |
104 DCHECK(args[0]->IsString() || args[0]->IsUndefined()); | 134 DCHECK(args[0]->IsString() || args[0]->IsUndefined()); |
105 | 135 |
106 if (args[0]->IsString() && args[1]->IsBoolean()) { | 136 if (args[0]->IsString() && args[1]->IsBoolean()) { |
107 ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args); | 137 std::string event_name = *v8::String::AsciiValue(args[0]->ToString()); |
| 138 bool is_manual = args[1]->BooleanValue(); |
| 139 |
108 const ChromeV8ContextSet& context_set = | 140 const ChromeV8ContextSet& context_set = |
109 self->extension_dispatcher()->v8_context_set(); | 141 extension_dispatcher()->v8_context_set(); |
110 ChromeV8Context* context = context_set.GetCurrent(); | 142 ChromeV8Context* context = context_set.GetCurrent(); |
111 if (!context) | 143 if (!context) |
112 return v8::Undefined(); | 144 return v8::Undefined(); |
113 | 145 |
114 std::string extension_id = context->GetExtensionID(); | 146 std::string extension_id = context->GetExtensionID(); |
115 EventListenerCounts& listener_counts = | 147 EventListenerCounts& listener_counts = |
116 g_listener_counts.Get()[extension_id]; | 148 g_listener_counts.Get()[extension_id]; |
117 std::string event_name(*v8::String::AsciiValue(args[0])); | |
118 bool is_manual = args[1]->BooleanValue(); | |
119 | 149 |
120 if (--listener_counts[event_name] == 0) { | 150 if (--listener_counts[event_name] == 0) { |
121 content::RenderThread::Get()->Send( | 151 content::RenderThread::Get()->Send( |
122 new ExtensionHostMsg_RemoveListener(extension_id, event_name)); | 152 new ExtensionHostMsg_RemoveListener(extension_id, event_name)); |
123 } | 153 } |
124 | 154 |
125 // DetachEvent is called when the last listener for the context is | 155 // DetachEvent is called when the last listener for the context is |
126 // removed. If the context is the background page, and it removes the | 156 // removed. If the context is the background page, and it removes the |
127 // last listener manually, then we assume that it is no longer interested | 157 // last listener manually, then we assume that it is no longer interested |
128 // in being awakened for this event. | 158 // in being awakened for this event. |
129 if (is_manual && self->IsLazyBackgroundPage(context->extension())) { | 159 if (is_manual && IsLazyBackgroundPage(context->extension())) { |
130 content::RenderThread::Get()->Send( | 160 content::RenderThread::Get()->Send( |
131 new ExtensionHostMsg_RemoveLazyListener(extension_id, event_name)); | 161 new ExtensionHostMsg_RemoveLazyListener(extension_id, event_name)); |
132 } | 162 } |
133 } | 163 } |
| 164 return v8::Undefined(); |
| 165 } |
| 166 |
| 167 // void AttachFilteredEvent(string event_name, object filter) |
| 168 // event_name - Name of the event to attach. |
| 169 // filter - Which instances of the named event are we interested in. |
| 170 v8::Handle<v8::Value> AttachFilteredEvent(const v8::Arguments& args) { |
| 171 DCHECK_EQ(2, args.Length()); |
| 172 DCHECK(args[0]->IsString()); |
| 173 DCHECK(args[1]->IsObject()); |
| 174 |
| 175 const ChromeV8ContextSet& context_set = |
| 176 extension_dispatcher()->v8_context_set(); |
| 177 ChromeV8Context* context = context_set.GetCurrent(); |
| 178 DCHECK(context); |
| 179 if (!context) |
| 180 return v8::Integer::New(-1); |
| 181 |
| 182 std::string event_name = *v8::String::AsciiValue(args[0]); |
| 183 // This method throws an exception if it returns false. |
| 184 if (!extension_dispatcher()->CheckCurrentContextAccessToExtensionAPI( |
| 185 event_name)) |
| 186 return v8::Undefined(); |
| 187 |
| 188 std::string extension_id = context->GetExtensionID(); |
| 189 if (extension_id.empty()) |
| 190 return v8::Undefined(); |
| 191 |
| 192 |
| 193 scoped_ptr<base::DictionaryValue> filter; |
| 194 { |
| 195 scoped_ptr<content::V8ValueConverter> converter( |
| 196 content::V8ValueConverter::create()); |
| 197 |
| 198 base::DictionaryValue* filter_dict; |
| 199 base::Value* filter_value = |
| 200 converter->FromV8Value(args[1]->ToObject(), |
| 201 v8::Context::GetCurrent()); |
| 202 CHECK(filter_value->GetAsDictionary(&filter_dict)); |
| 203 filter.reset(filter_dict); |
| 204 } |
| 205 int id = event_filter_->AddEventMatcher(event_name, ParseEventMatcher( |
| 206 filter.get())); |
| 207 |
| 208 // Only send IPCs the first time a filter gets added. |
| 209 if (AddFilter(event_name, extension_id, filter.get())) { |
| 210 bool lazy = IsLazyBackgroundPage(context->extension()); |
| 211 content::RenderThread::Get()->Send( |
| 212 new ExtensionHostMsg_AddFilteredListener(extension_id, event_name, |
| 213 *filter, lazy)); |
| 214 } |
| 215 |
| 216 return v8::Integer::New(id); |
| 217 } |
| 218 |
| 219 bool AddFilter(const std::string& event_name, |
| 220 const std::string& extension_id, |
| 221 base::DictionaryValue* filter) { |
| 222 FilteredEventListenerCounts::iterator it = |
| 223 g_filtered_listener_counts.Get()[extension_id].find(event_name); |
| 224 if (it == g_filtered_listener_counts.Get()[extension_id].end()) |
| 225 g_filtered_listener_counts.Get()[extension_id][event_name].reset( |
| 226 new extensions::ValueSet); |
| 227 |
| 228 int result = g_filtered_listener_counts.Get()[extension_id][event_name]-> |
| 229 Add(filter); |
| 230 return 1 == result; |
| 231 } |
| 232 |
| 233 bool RemoveFilter(const std::string& event_name, |
| 234 const std::string& extension_id, |
| 235 base::DictionaryValue* filter) { |
| 236 int result = g_filtered_listener_counts.Get()[extension_id][event_name]-> |
| 237 Remove(filter); |
| 238 return 0 == result; |
| 239 } |
| 240 |
| 241 // void DetachFilteredEvent(int id, bool manual) |
| 242 // id - Id of the event to detach. |
| 243 // manual - false if this is part of the extension unload process where all |
| 244 // listeners are automatically detached. |
| 245 v8::Handle<v8::Value> DetachFilteredEvent(const v8::Arguments& args) { |
| 246 DCHECK_EQ(2, args.Length()); |
| 247 DCHECK(args[0]->IsInt32()); |
| 248 DCHECK(args[1]->IsBoolean()); |
| 249 bool is_manual = args[1]->BooleanValue(); |
| 250 const ChromeV8ContextSet& context_set = |
| 251 extension_dispatcher()->v8_context_set(); |
| 252 ChromeV8Context* context = context_set.GetCurrent(); |
| 253 if (!context) |
| 254 return v8::Integer::New(-1); |
| 255 |
| 256 std::string extension_id = context->GetExtensionID(); |
| 257 if (extension_id.empty()) |
| 258 return v8::Undefined(); |
| 259 |
| 260 int matcher_id = args[0]->Int32Value(); |
| 261 extensions::EventMatcher* event_matcher = |
| 262 event_filter_->GetEventMatcher(matcher_id); |
| 263 |
| 264 const std::string& event_name = event_filter_->GetEventName(matcher_id); |
| 265 |
| 266 // Only send IPCs the last time a filter gets removed. |
| 267 if (RemoveFilter(event_name, extension_id, event_matcher->value())) { |
| 268 bool lazy = is_manual && IsLazyBackgroundPage(context->extension()); |
| 269 content::RenderThread::Get()->Send( |
| 270 new ExtensionHostMsg_RemoveFilteredListener(extension_id, event_name, |
| 271 *event_matcher->value(), |
| 272 lazy)); |
| 273 } |
| 274 |
| 275 event_filter_->RemoveEventMatcher(matcher_id); |
134 | 276 |
135 return v8::Undefined(); | 277 return v8::Undefined(); |
136 } | 278 } |
137 | 279 |
| 280 v8::Handle<v8::Value> MatchAgainstEventFilter(const v8::Arguments& args) { |
| 281 typedef std::set<extensions::EventFilter::MatcherID> MatcherIDs; |
| 282 |
| 283 std::string event_name = *v8::String::AsciiValue(args[0]->ToString()); |
| 284 extensions::EventFilteringInfo info = ParseFromObject(args[1]->ToObject()); |
| 285 MatcherIDs matched_event_filters = event_filter_->MatchEvent( |
| 286 event_name, info); |
| 287 v8::Handle<v8::Array> array(v8::Array::New(matched_event_filters.size())); |
| 288 int i = 0; |
| 289 for (MatcherIDs::iterator it = matched_event_filters.begin(); |
| 290 it != matched_event_filters.end(); ++it) { |
| 291 array->Set(v8::Integer::New(i++), v8::Integer::New(*it)); |
| 292 } |
| 293 return array; |
| 294 } |
| 295 |
| 296 extensions::EventFilteringInfo ParseFromObject( |
| 297 v8::Handle<v8::Object> object) { |
| 298 v8::Handle<v8::Value> url_value(object->Get(v8::String::New("url"))); |
| 299 extensions::EventFilteringInfo info; |
| 300 info.SetURL(GURL(*v8::String::AsciiValue(url_value))); |
| 301 return info; |
| 302 } |
| 303 |
138 private: | 304 private: |
139 | 305 extensions::EventFilter* event_filter_; |
140 bool IsLazyBackgroundPage(const Extension* extension) { | 306 bool IsLazyBackgroundPage(const Extension* extension) { |
141 content::RenderView* render_view = GetCurrentRenderView(); | 307 content::RenderView* render_view = GetCurrentRenderView(); |
142 if (!render_view) | 308 if (!render_view) |
143 return false; | 309 return false; |
144 | 310 |
145 ExtensionHelper* helper = ExtensionHelper::Get(render_view); | 311 ExtensionHelper* helper = ExtensionHelper::Get(render_view); |
146 return (extension && extension->has_lazy_background_page() && | 312 return (extension && extension->has_lazy_background_page() && |
147 helper->view_type() == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); | 313 helper->view_type() == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); |
148 } | 314 } |
| 315 |
| 316 scoped_ptr<extensions::EventMatcher> ParseEventMatcher( |
| 317 base::DictionaryValue* filter_dict) { |
| 318 return scoped_ptr<extensions::EventMatcher>(new extensions::EventMatcher( |
| 319 scoped_ptr<base::DictionaryValue>(filter_dict->DeepCopy()))); |
| 320 } |
149 }; | 321 }; |
150 | 322 |
151 } // namespace | 323 } // namespace |
152 | 324 |
153 ChromeV8Extension* EventBindings::Get(ExtensionDispatcher* dispatcher) { | 325 // static |
154 return new ExtensionImpl(dispatcher); | 326 ChromeV8Extension* EventBindings::Get(ExtensionDispatcher* dispatcher, |
| 327 extensions::EventFilter* event_filter) { |
| 328 return new ExtensionImpl(dispatcher, event_filter); |
155 } | 329 } |
OLD | NEW |