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