| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/renderer/event_bindings.h" | 5 #include "extensions/renderer/event_bindings.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <memory> |
| 10 #include <utility> | 11 #include <utility> |
| 11 | 12 |
| 12 #include "base/bind.h" | 13 #include "base/bind.h" |
| 13 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
| 14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/ptr_util.h" |
| 15 #include "components/crx_file/id_util.h" | 16 #include "components/crx_file/id_util.h" |
| 16 #include "content/public/child/v8_value_converter.h" | 17 #include "content/public/child/v8_value_converter.h" |
| 17 #include "content/public/renderer/render_frame.h" | 18 #include "content/public/renderer/render_frame.h" |
| 18 #include "content/public/renderer/render_thread.h" | 19 #include "content/public/renderer/render_thread.h" |
| 19 #include "content/public/renderer/render_view.h" | 20 #include "content/public/renderer/render_view.h" |
| 20 #include "extensions/common/event_filter.h" | 21 #include "extensions/common/event_filter.h" |
| 21 #include "extensions/common/extension.h" | 22 #include "extensions/common/extension.h" |
| 22 #include "extensions/common/extension_messages.h" | 23 #include "extensions/common/extension_messages.h" |
| 23 #include "extensions/common/value_counter.h" | 24 #include "extensions/common/value_counter.h" |
| 24 #include "extensions/renderer/extension_frame_helper.h" | 25 #include "extensions/renderer/extension_frame_helper.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 37 // A map of extension IDs to listener counts for that extension. | 38 // A map of extension IDs to listener counts for that extension. |
| 38 base::LazyInstance<std::map<std::string, EventListenerCounts>> | 39 base::LazyInstance<std::map<std::string, EventListenerCounts>> |
| 39 g_listener_counts = LAZY_INSTANCE_INITIALIZER; | 40 g_listener_counts = LAZY_INSTANCE_INITIALIZER; |
| 40 | 41 |
| 41 // A map of (extension ID, event name) pairs to the filtered listener counts | 42 // A map of (extension ID, event name) pairs to the filtered listener counts |
| 42 // for that pair. The map is used to keep track of which filters are in effect | 43 // for that pair. The map is used to keep track of which filters are in effect |
| 43 // for which events. We notify the browser about filtered event listeners when | 44 // for which events. We notify the browser about filtered event listeners when |
| 44 // we transition between 0 and 1. | 45 // we transition between 0 and 1. |
| 45 using FilteredEventListenerKey = std::pair<std::string, std::string>; | 46 using FilteredEventListenerKey = std::pair<std::string, std::string>; |
| 46 using FilteredEventListenerCounts = | 47 using FilteredEventListenerCounts = |
| 47 std::map<FilteredEventListenerKey, scoped_ptr<ValueCounter>>; | 48 std::map<FilteredEventListenerKey, std::unique_ptr<ValueCounter>>; |
| 48 base::LazyInstance<FilteredEventListenerCounts> g_filtered_listener_counts = | 49 base::LazyInstance<FilteredEventListenerCounts> g_filtered_listener_counts = |
| 49 LAZY_INSTANCE_INITIALIZER; | 50 LAZY_INSTANCE_INITIALIZER; |
| 50 | 51 |
| 51 base::LazyInstance<EventFilter> g_event_filter = LAZY_INSTANCE_INITIALIZER; | 52 base::LazyInstance<EventFilter> g_event_filter = LAZY_INSTANCE_INITIALIZER; |
| 52 | 53 |
| 53 // Gets a unique string key identifier for a ScriptContext. | 54 // Gets a unique string key identifier for a ScriptContext. |
| 54 // TODO(kalman): Just use pointer equality...? | 55 // TODO(kalman): Just use pointer equality...? |
| 55 std::string GetKeyForScriptContext(ScriptContext* script_context) { | 56 std::string GetKeyForScriptContext(ScriptContext* script_context) { |
| 56 const std::string& extension_id = script_context->GetExtensionID(); | 57 const std::string& extension_id = script_context->GetExtensionID(); |
| 57 CHECK(crx_file::id_util::IdIsValid(extension_id) || | 58 CHECK(crx_file::id_util::IdIsValid(extension_id) || |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 | 118 |
| 118 // Add a filter to |event_name| in |extension_id|, returning true if it | 119 // Add a filter to |event_name| in |extension_id|, returning true if it |
| 119 // was the first filter for that event in that extension. | 120 // was the first filter for that event in that extension. |
| 120 bool AddFilter(const std::string& event_name, | 121 bool AddFilter(const std::string& event_name, |
| 121 const std::string& extension_id, | 122 const std::string& extension_id, |
| 122 const base::DictionaryValue& filter) { | 123 const base::DictionaryValue& filter) { |
| 123 FilteredEventListenerKey key(extension_id, event_name); | 124 FilteredEventListenerKey key(extension_id, event_name); |
| 124 FilteredEventListenerCounts& all_counts = g_filtered_listener_counts.Get(); | 125 FilteredEventListenerCounts& all_counts = g_filtered_listener_counts.Get(); |
| 125 FilteredEventListenerCounts::const_iterator counts = all_counts.find(key); | 126 FilteredEventListenerCounts::const_iterator counts = all_counts.find(key); |
| 126 if (counts == all_counts.end()) { | 127 if (counts == all_counts.end()) { |
| 127 counts = all_counts.insert(std::make_pair( | 128 counts = |
| 128 key, make_scoped_ptr(new ValueCounter()))) | 129 all_counts |
| 129 .first; | 130 .insert(std::make_pair(key, base::WrapUnique(new ValueCounter()))) |
| 131 .first; |
| 130 } | 132 } |
| 131 return counts->second->Add(filter); | 133 return counts->second->Add(filter); |
| 132 } | 134 } |
| 133 | 135 |
| 134 // Remove a filter from |event_name| in |extension_id|, returning true if it | 136 // Remove a filter from |event_name| in |extension_id|, returning true if it |
| 135 // was the last filter for that event in that extension. | 137 // was the last filter for that event in that extension. |
| 136 bool RemoveFilter(const std::string& event_name, | 138 bool RemoveFilter(const std::string& event_name, |
| 137 const std::string& extension_id, | 139 const std::string& extension_id, |
| 138 base::DictionaryValue* filter) { | 140 base::DictionaryValue* filter) { |
| 139 FilteredEventListenerKey key(extension_id, event_name); | 141 FilteredEventListenerKey key(extension_id, event_name); |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 void EventBindings::AttachFilteredEvent( | 252 void EventBindings::AttachFilteredEvent( |
| 251 const v8::FunctionCallbackInfo<v8::Value>& args) { | 253 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 252 CHECK_EQ(2, args.Length()); | 254 CHECK_EQ(2, args.Length()); |
| 253 CHECK(args[0]->IsString()); | 255 CHECK(args[0]->IsString()); |
| 254 CHECK(args[1]->IsObject()); | 256 CHECK(args[1]->IsObject()); |
| 255 | 257 |
| 256 std::string event_name = *v8::String::Utf8Value(args[0]); | 258 std::string event_name = *v8::String::Utf8Value(args[0]); |
| 257 if (!context()->HasAccessOrThrowError(event_name)) | 259 if (!context()->HasAccessOrThrowError(event_name)) |
| 258 return; | 260 return; |
| 259 | 261 |
| 260 scoped_ptr<base::DictionaryValue> filter; | 262 std::unique_ptr<base::DictionaryValue> filter; |
| 261 { | 263 { |
| 262 scoped_ptr<content::V8ValueConverter> converter( | 264 std::unique_ptr<content::V8ValueConverter> converter( |
| 263 content::V8ValueConverter::create()); | 265 content::V8ValueConverter::create()); |
| 264 scoped_ptr<base::Value> filter_value(converter->FromV8Value( | 266 std::unique_ptr<base::Value> filter_value(converter->FromV8Value( |
| 265 v8::Local<v8::Object>::Cast(args[1]), context()->v8_context())); | 267 v8::Local<v8::Object>::Cast(args[1]), context()->v8_context())); |
| 266 if (!filter_value || !filter_value->IsType(base::Value::TYPE_DICTIONARY)) { | 268 if (!filter_value || !filter_value->IsType(base::Value::TYPE_DICTIONARY)) { |
| 267 args.GetReturnValue().Set(static_cast<int32_t>(-1)); | 269 args.GetReturnValue().Set(static_cast<int32_t>(-1)); |
| 268 return; | 270 return; |
| 269 } | 271 } |
| 270 filter = base::DictionaryValue::From(std::move(filter_value)); | 272 filter = base::DictionaryValue::From(std::move(filter_value)); |
| 271 } | 273 } |
| 272 | 274 |
| 273 // Hold onto a weak reference to |filter| so that it can be used after passing | 275 // Hold onto a weak reference to |filter| so that it can be used after passing |
| 274 // ownership to |event_filter|. | 276 // ownership to |event_filter|. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 v8::Array::New(isolate, matched_event_filters.size())); | 334 v8::Array::New(isolate, matched_event_filters.size())); |
| 333 int i = 0; | 335 int i = 0; |
| 334 for (MatcherIDs::iterator it = matched_event_filters.begin(); | 336 for (MatcherIDs::iterator it = matched_event_filters.begin(); |
| 335 it != matched_event_filters.end(); | 337 it != matched_event_filters.end(); |
| 336 ++it) { | 338 ++it) { |
| 337 array->Set(v8::Integer::New(isolate, i++), v8::Integer::New(isolate, *it)); | 339 array->Set(v8::Integer::New(isolate, i++), v8::Integer::New(isolate, *it)); |
| 338 } | 340 } |
| 339 args.GetReturnValue().Set(array); | 341 args.GetReturnValue().Set(array); |
| 340 } | 342 } |
| 341 | 343 |
| 342 scoped_ptr<EventMatcher> EventBindings::ParseEventMatcher( | 344 std::unique_ptr<EventMatcher> EventBindings::ParseEventMatcher( |
| 343 scoped_ptr<base::DictionaryValue> filter) { | 345 std::unique_ptr<base::DictionaryValue> filter) { |
| 344 return make_scoped_ptr(new EventMatcher( | 346 return base::WrapUnique(new EventMatcher( |
| 345 std::move(filter), context()->GetRenderFrame()->GetRoutingID())); | 347 std::move(filter), context()->GetRenderFrame()->GetRoutingID())); |
| 346 } | 348 } |
| 347 | 349 |
| 348 void EventBindings::OnInvalidated() { | 350 void EventBindings::OnInvalidated() { |
| 349 // Detach all attached events that weren't attached. Iterate over a copy | 351 // Detach all attached events that weren't attached. Iterate over a copy |
| 350 // because it will be mutated. | 352 // because it will be mutated. |
| 351 std::set<std::string> attached_event_names_safe = attached_event_names_; | 353 std::set<std::string> attached_event_names_safe = attached_event_names_; |
| 352 for (const std::string& event_name : attached_event_names_safe) { | 354 for (const std::string& event_name : attached_event_names_safe) { |
| 353 DetachEvent(event_name, false /* is_manual */); | 355 DetachEvent(event_name, false /* is_manual */); |
| 354 } | 356 } |
| 355 DCHECK(attached_event_names_.empty()) | 357 DCHECK(attached_event_names_.empty()) |
| 356 << "Events cannot be attached during invalidation"; | 358 << "Events cannot be attached during invalidation"; |
| 357 | 359 |
| 358 // Same for filtered events. | 360 // Same for filtered events. |
| 359 std::set<int> attached_matcher_ids_safe = attached_matcher_ids_; | 361 std::set<int> attached_matcher_ids_safe = attached_matcher_ids_; |
| 360 for (int matcher_id : attached_matcher_ids_safe) { | 362 for (int matcher_id : attached_matcher_ids_safe) { |
| 361 DetachFilteredEvent(matcher_id, false /* is_manual */); | 363 DetachFilteredEvent(matcher_id, false /* is_manual */); |
| 362 } | 364 } |
| 363 DCHECK(attached_matcher_ids_.empty()) | 365 DCHECK(attached_matcher_ids_.empty()) |
| 364 << "Filtered events cannot be attached during invalidation"; | 366 << "Filtered events cannot be attached during invalidation"; |
| 365 } | 367 } |
| 366 | 368 |
| 367 } // namespace extensions | 369 } // namespace extensions |
| OLD | NEW |