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

Side by Side Diff: chrome/common/extensions/event_filter.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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/common/extensions/event_filter.h"
6
7 #include "chrome/common/extensions/matcher/url_matcher_factory.h"
8
9 namespace extensions {
10
11 EventFilter::EventMatcherEntry::EventMatcherEntry(
12 scoped_ptr<EventMatcher> event_matcher,
13 URLMatcher* url_matcher,
14 const URLMatcherConditionSet::Vector& condition_sets)
15 : event_matcher_(event_matcher.Pass()),
16 url_matcher_(url_matcher) {
17 for (URLMatcherConditionSet::Vector::const_iterator it =
18 condition_sets.begin(); it != condition_sets.end(); it++)
19 condition_set_ids_.push_back((*it)->id());
20 url_matcher_->AddConditionSets(condition_sets);
21 }
22
23 EventFilter::EventMatcherEntry::~EventMatcherEntry() {
24 url_matcher_->RemoveConditionSets(condition_set_ids_);
battre 2012/05/17 08:50:51 This architecture makes the destruction of the Eve
koz (OOO until 15th September) 2012/05/18 01:13:50 Good point. Done.
25 }
26
27 EventFilter::EventFilter()
28 : next_id_(0),
29 next_condition_set_id_(0) {
30 }
31
32 EventFilter::~EventFilter() {
33 }
34
35 EventFilter::MatcherID
36 EventFilter::AddEventMatcher(const std::string& event_name,
37 scoped_ptr<EventMatcher> matcher) {
battre 2012/05/17 08:50:51 nit: indent +1
koz (OOO until 15th September) 2012/05/18 01:13:50 Done.
38 MatcherID id = next_id_++;
39 URLMatcherConditionSet::Vector condition_sets;
40 if (!CreateConditionSets(id, matcher->url_filters(), &condition_sets))
41 return -1;
42
43 for (URLMatcherConditionSet::Vector::iterator it = condition_sets.begin();
44 it != condition_sets.end(); it++) {
45 condition_set_id_to_event_matcher_id_.insert(
46 std::make_pair((*it)->id(), id));
47 }
48 id_to_event_name_[id] = event_name;
49 event_matchers_[event_name][id] = linked_ptr<EventMatcherEntry>(
50 new EventMatcherEntry(matcher.Pass(), &url_matcher_, condition_sets));
51 return id;
52 }
53
54 bool EventFilter::CreateConditionSets(
55 MatcherID id,
56 base::ListValue* url_filters,
57 URLMatcherConditionSet::Vector* condition_sets) {
58 if (!url_filters || url_filters->GetSize() == 0) {
59 // If there are no url_filters then we want to match all events, so create a
60 // URLFilter from an empty dictionary.
61 base::DictionaryValue empty_dict;
62 return AddDictionaryAsConditionSet(&empty_dict, condition_sets);
63 }
64 for (size_t i = 0; i < url_filters->GetSize(); i++) {
65 base::DictionaryValue* url_filter;
66 if (!url_filters->GetDictionary(i, &url_filter))
67 return false;
68 if (!AddDictionaryAsConditionSet(url_filter, condition_sets))
69 return false;
70 }
71 return true;
72 }
73
74 bool EventFilter::AddDictionaryAsConditionSet(
75 base::DictionaryValue* url_filter,
76 URLMatcherConditionSet::Vector* condition_sets) {
77 std::string error;
78 URLMatcherConditionSet::ID condition_set_id = next_condition_set_id_++;
79 condition_sets->push_back(URLMatcherFactory::CreateFromURLFilterDictionary(
80 url_matcher_.condition_factory(),
81 url_filter,
82 condition_set_id,
83 &error));
84 if (!error.empty()) {
85 LOG(ERROR) << "CreateFromURLFilterDictionary failed: " << error;
86 url_matcher_.ClearUnusedConditionSets();
87 condition_sets->clear();
88 return false;
89 }
90 return true;
91 }
92
93 std::string EventFilter::RemoveEventMatcher(MatcherID id) {
94 std::map<int, std::string>::iterator it = id_to_event_name_.find(id);
95 event_matchers_[it->second].erase(id);
96 std::string event_name = it->second;
97 id_to_event_name_.erase(it);
98 return event_name;
99 }
100
101 std::set<EventFilter::MatcherID> EventFilter::MatchEvent(
102 const std::string& event_name, const EventFilteringInfo& event_info) {
103 std::set<MatcherID> matchers;
104 EventMatcherMultiMap::iterator it = event_matchers_.find(event_name);
105 if (it == event_matchers_.end())
106 return matchers;
107
108 EventMatcherMap& matcher_map = it->second;
109 std::set<MatcherID> url_matched_matchers;
110 GURL url_to_match_against = event_info.has_url() ? event_info.url() : GURL();
111 std::set<URLMatcherConditionSet::ID> matching_condition_set_ids =
112 url_matcher_.MatchURL(url_to_match_against);
113 for (std::set<URLMatcherConditionSet::ID>::iterator it =
battre 2012/05/17 08:50:51 I would recommend to combine these two loop. I don
koz (OOO until 15th September) 2012/05/18 01:13:50 I merged the loops but there is no reserve method
114 matching_condition_set_ids.begin();
115 it != matching_condition_set_ids.end(); it++) {
116 url_matched_matchers.insert(condition_set_id_to_event_matcher_id_[*it]);
117 }
118
119 for (std::set<MatcherID>::const_iterator it = url_matched_matchers.begin();
120 it != url_matched_matchers.end(); it++) {
121 const EventMatcher& event_matcher = matcher_map[*it]->event_matcher();
122 if (event_matcher.MatchNonURLCriteria(event_info)) {
123 CHECK(!event_matcher.url_filters() || event_info.has_url());
124 matchers.insert(*it);
125 }
126 }
127
128 return matchers;
129 }
130
131 int EventFilter::GetMatcherCountForEvent(const std::string& name) {
132 EventMatcherMultiMap::const_iterator it = event_matchers_.find(name);
133 if (it == event_matchers_.end())
134 return 0;
135
136 return it->second.size();
137 }
138
139 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698