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

Unified Diff: chrome/common/extensions/filtered_event_router.cc

Issue 10388135: Add common code for extensions event filtering. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: self review Created 8 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: chrome/common/extensions/filtered_event_router.cc
diff --git a/chrome/common/extensions/filtered_event_router.cc b/chrome/common/extensions/filtered_event_router.cc
new file mode 100644
index 0000000000000000000000000000000000000000..cad738f044a9d6181214f090dedab9ec9812e32b
--- /dev/null
+++ b/chrome/common/extensions/filtered_event_router.cc
@@ -0,0 +1,99 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/common/extensions/filtered_event_router.h"
+
+#include "chrome/common/extensions/matcher/url_matcher_factory.h"
+
+namespace extensions {
+
+FilteredEventRouter::FilteredEventRouter()
+ : next_id_(0),
+ next_condition_set_id_(0) {
+}
+
+FilteredEventRouter::~FilteredEventRouter() {
+}
+
+int FilteredEventRouter::AddEventMatcher(const std::string& event_name,
+ scoped_ptr<EventMatcher> matcher) {
battre 2012/05/15 13:12:32 nit: indentation
koz (OOO until 15th September) 2012/05/16 00:14:00 Done.
+ int id = next_id_++;
+ event_matchers_[event_name];
battre 2012/05/15 13:12:32 what is this? Move it down by two lines to move it
koz (OOO until 15th September) 2012/05/16 00:14:00 With your next comment this becomes unnecessary.
+ id_to_event_name_[id] = event_name;
+ AddURLConditionsToURLMatcher(id, matcher->url_filters());
+ EventMatcherMultiMap::iterator matcher_map =
+ event_matchers_.find(event_name);
+ matcher_map->second[id] = linked_ptr<EventMatcher>(matcher.release());
battre 2012/05/15 13:12:32 could you simplify lines 22, 25-27 to event_matche
koz (OOO until 15th September) 2012/05/16 00:14:00 True! Done.
+ return id;
+}
+
+void FilteredEventRouter::AddURLConditionsToURLMatcher(int id,
battre 2012/05/15 13:12:32 nit: indentation (move parameter to next line or l
koz (OOO until 15th September) 2012/05/16 00:14:00 Done.
+ base::ListValue* url_filters) {
+ if (!url_filters)
+ return;
+ URLMatcherConditionSet::Vector condition_sets;
+ for (size_t i = 0; i < url_filters->GetSize(); i++) {
+ base::DictionaryValue* url_filter;
+ CHECK(url_filters->GetDictionary(i, &url_filter));
battre 2012/05/15 13:12:32 This is passed by an extension, right? I think we
koz (OOO until 15th September) 2012/05/16 00:14:00 Good point, I've handled the error cases now.
+ std::string error;
+ int condition_set_id = next_condition_set_id_++;
+ condition_sets.push_back(URLMatcherFactory::CreateFromURLFilterDictionary(
+ url_matcher_.condition_factory(),
+ url_filter,
+ condition_set_id,
+ &error));
battre 2012/05/15 13:12:32 Handle error?
koz (OOO until 15th September) 2012/05/16 00:14:00 Done.
+ condition_set_id_to_event_matcher_id_.insert(std::make_pair(
+ condition_set_id, id));
+ }
+ url_matcher_.AddConditionSets(condition_sets);
+}
+
+std::string FilteredEventRouter::RemoveEventMatcher(int id) {
+ std::map<int, std::string>::iterator it = id_to_event_name_.find(id);
+ event_matchers_[it->second].erase(id);
+ std::string event_name = it->second;
+ id_to_event_name_.erase(it);
+ return event_name;
+}
+
+std::set<int> FilteredEventRouter::MatchEvent(const std::string& event_name,
battre 2012/05/15 13:12:32 nit: indentation
koz (OOO until 15th September) 2012/05/16 00:14:00 Done.
+ const EventFilteringInfo& event_info) {
+ std::set<int> matchers;
+ EventMatcherMultiMap::const_iterator matcher_map =
+ event_matchers_.find(event_name);
+ if (matcher_map == event_matchers_.end())
+ return matchers;
+
+ std::set<int> url_matched_matchers;
+ if (event_info.has_url()) {
+ std::set<int> matching_condition_set_ids =
+ url_matcher_.MatchURL(event_info.url());
+ for (std::set<int>::iterator it = matching_condition_set_ids.begin();
+ it != matching_condition_set_ids.end(); it++) {
+ url_matched_matchers.insert(condition_set_id_to_event_matcher_id_[*it]);
+ }
+ }
+
+ for (EventMatcherMap::const_iterator it = matcher_map->second.begin();
+ it != matcher_map->second.end(); it++) {
+ CHECK(!it->second->url_filters() || event_info.has_url()) <<
+ "matcher has URL filters, but event provided no URL";
+ bool did_matcher_match_url = !event_info.has_url() ||
+ !it->second->url_filters() || url_matched_matchers.count(it->first);
+ if (did_matcher_match_url && it->second->MatchNonURLCriteria(event_info)) {
+ matchers.insert(it->first);
+ }
+ }
+ return matchers;
+}
+
+int FilteredEventRouter::GetMatcherCountForEvent(const std::string& name) {
+ EventMatcherMultiMap::const_iterator it = event_matchers_.find(name);
+ if (it == event_matchers_.end())
+ return 0;
+
+ return it->second.size();
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698