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

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: respond to comments 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..9382f35e0d069527006891c4a2ba6a483bc0d7d2
--- /dev/null
+++ b/chrome/common/extensions/filtered_event_router.cc
@@ -0,0 +1,104 @@
+// 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() {
+}
+
+FilteredEventRouter::MatcherID
+FilteredEventRouter::AddEventMatcher(const std::string& event_name,
+ scoped_ptr<EventMatcher> matcher) {
+ MatcherID id = next_id_++;
+ if (!AddURLConditionsToURLMatcher(id, matcher->url_filters()))
+ return -1;
+ id_to_event_name_[id] = event_name;
+ event_matchers_[event_name][id] = linked_ptr<EventMatcher>(matcher.release());
+ return id;
+}
+
+bool FilteredEventRouter::AddURLConditionsToURLMatcher(
+ MatcherID id, base::ListValue* url_filters) {
+ if (!url_filters)
+ return true;
+ URLMatcherConditionSet::Vector condition_sets;
+ for (size_t i = 0; i < url_filters->GetSize(); i++) {
+ base::DictionaryValue* url_filter;
+ if (!url_filters->GetDictionary(i, &url_filter))
+ return false;
+ std::string error;
+ URLMatcherConditionSet::ID condition_set_id = next_condition_set_id_++;
+ condition_sets.push_back(URLMatcherFactory::CreateFromURLFilterDictionary(
+ url_matcher_.condition_factory(),
+ url_filter,
+ condition_set_id,
+ &error));
+ if (!error.empty()) {
+ LOG(ERROR) << "CreateFromURLFilterDictionary failed: " << error;
battre 2012/05/16 13:35:12 Here we have to add url_matcher_.ClearUnusedCondit
koz (OOO until 15th September) 2012/05/17 05:08:42 Done.
+ return false;
+ }
+ condition_set_id_to_event_matcher_id_.insert(std::make_pair(
+ condition_set_id, id));
+ }
+ url_matcher_.AddConditionSets(condition_sets);
+ return true;
+}
+
+std::string FilteredEventRouter::RemoveEventMatcher(MatcherID id) {
battre 2012/05/16 13:35:12 you should remove the event condition sets from ur
koz (OOO until 15th September) 2012/05/17 05:08:42 Done. I've added an EventMatcherEntry class that h
+ 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<FilteredEventRouter::MatcherID> FilteredEventRouter::MatchEvent(
+ const std::string& event_name, const EventFilteringInfo& event_info) {
+ std::set<MatcherID> matchers;
+ EventMatcherMultiMap::const_iterator matcher_map =
+ event_matchers_.find(event_name);
+ if (matcher_map == event_matchers_.end())
+ return matchers;
+
+ std::set<MatcherID> url_matched_matchers;
+ if (event_info.has_url()) {
+ std::set<MatcherID> matching_condition_set_ids =
battre 2012/05/16 13:35:12 this should be std::set<URLMatcherConditionSet::ID
koz (OOO until 15th September) 2012/05/17 05:08:42 Done.
+ url_matcher_.MatchURL(event_info.url());
+ for (std::set<MatcherID>::iterator it = matching_condition_set_ids.begin();
battre 2012/05/16 13:35:12 same here
koz (OOO until 15th September) 2012/05/17 05:08:42 Done.
+ it != matching_condition_set_ids.end(); it++) {
battre 2012/05/16 13:35:12 nit: +1 space
koz (OOO until 15th September) 2012/05/17 05:08:42 Fixed here and elsewhere.
+ 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++) {
battre 2012/05/16 13:35:12 nit: +1 space
battre 2012/05/16 13:35:12 This looks inefficient and defeats the purpose of
koz (OOO until 15th September) 2012/05/17 05:08:42 Done.
koz (OOO until 15th September) 2012/05/17 05:08:42 That's a good idea! Done.
+ 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->has_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