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

Side by Side Diff: chrome/common/extensions/event_filter.cc

Issue 12208078: Move chrome/common/extensions/event_* to extensions/common/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: v8 Created 7 years, 10 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
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 "extensions/common/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_);
25 }
26
27 void EventFilter::EventMatcherEntry::DontRemoveConditionSetsInDestructor() {
28 condition_set_ids_.clear();
29 }
30
31 EventFilter::EventFilter()
32 : next_id_(0),
33 next_condition_set_id_(0) {
34 }
35
36 EventFilter::~EventFilter() {
37 // Normally when an event matcher entry is removed from event_matchers_ it
38 // will remove its condition sets from url_matcher_, but as url_matcher_ is
39 // being destroyed anyway there is no need to do that step here.
40 for (EventMatcherMultiMap::iterator it = event_matchers_.begin();
41 it != event_matchers_.end(); it++) {
42 for (EventMatcherMap::iterator it2 = it->second.begin();
43 it2 != it->second.end(); it2++) {
44 it2->second->DontRemoveConditionSetsInDestructor();
45 }
46 }
47 }
48
49 EventFilter::MatcherID
50 EventFilter::AddEventMatcher(const std::string& event_name,
51 scoped_ptr<EventMatcher> matcher) {
52 MatcherID id = next_id_++;
53 URLMatcherConditionSet::Vector condition_sets;
54 if (!CreateConditionSets(id, matcher.get(), &condition_sets))
55 return -1;
56
57 for (URLMatcherConditionSet::Vector::iterator it = condition_sets.begin();
58 it != condition_sets.end(); it++) {
59 condition_set_id_to_event_matcher_id_.insert(
60 std::make_pair((*it)->id(), id));
61 }
62 id_to_event_name_[id] = event_name;
63 event_matchers_[event_name][id] = linked_ptr<EventMatcherEntry>(
64 new EventMatcherEntry(matcher.Pass(), &url_matcher_, condition_sets));
65 return id;
66 }
67
68 EventMatcher* EventFilter::GetEventMatcher(MatcherID id) {
69 DCHECK(id_to_event_name_.find(id) != id_to_event_name_.end());
70 const std::string& event_name = id_to_event_name_[id];
71 return event_matchers_[event_name][id]->event_matcher();
72 }
73
74 const std::string& EventFilter::GetEventName(MatcherID id) {
75 DCHECK(id_to_event_name_.find(id) != id_to_event_name_.end());
76 return id_to_event_name_[id];
77 }
78
79 bool EventFilter::CreateConditionSets(
80 MatcherID id,
81 EventMatcher* matcher,
82 URLMatcherConditionSet::Vector* condition_sets) {
83 if (matcher->GetURLFilterCount() == 0) {
84 // If there are no URL filters then we want to match all events, so create a
85 // URLFilter from an empty dictionary.
86 base::DictionaryValue empty_dict;
87 return AddDictionaryAsConditionSet(&empty_dict, condition_sets);
88 }
89 for (int i = 0; i < matcher->GetURLFilterCount(); i++) {
90 base::DictionaryValue* url_filter;
91 if (!matcher->GetURLFilter(i, &url_filter))
92 return false;
93 if (!AddDictionaryAsConditionSet(url_filter, condition_sets))
94 return false;
95 }
96 return true;
97 }
98
99 bool EventFilter::AddDictionaryAsConditionSet(
100 base::DictionaryValue* url_filter,
101 URLMatcherConditionSet::Vector* condition_sets) {
102 std::string error;
103 URLMatcherConditionSet::ID condition_set_id = next_condition_set_id_++;
104 condition_sets->push_back(URLMatcherFactory::CreateFromURLFilterDictionary(
105 url_matcher_.condition_factory(),
106 url_filter,
107 condition_set_id,
108 &error));
109 if (!error.empty()) {
110 LOG(ERROR) << "CreateFromURLFilterDictionary failed: " << error;
111 url_matcher_.ClearUnusedConditionSets();
112 condition_sets->clear();
113 return false;
114 }
115 return true;
116 }
117
118 std::string EventFilter::RemoveEventMatcher(MatcherID id) {
119 std::map<MatcherID, std::string>::iterator it = id_to_event_name_.find(id);
120 std::string event_name = it->second;
121 // EventMatcherEntry's destructor causes the condition set ids to be removed
122 // from url_matcher_.
123 event_matchers_[event_name].erase(id);
124 id_to_event_name_.erase(it);
125 return event_name;
126 }
127
128 std::set<EventFilter::MatcherID> EventFilter::MatchEvent(
129 const std::string& event_name, const EventFilteringInfo& event_info) {
130 std::set<MatcherID> matchers;
131 EventMatcherMultiMap::iterator it = event_matchers_.find(event_name);
132 if (it == event_matchers_.end())
133 return matchers;
134
135 EventMatcherMap& matcher_map = it->second;
136 GURL url_to_match_against = event_info.has_url() ? event_info.url() : GURL();
137 std::set<URLMatcherConditionSet::ID> matching_condition_set_ids =
138 url_matcher_.MatchURL(url_to_match_against);
139 for (std::set<URLMatcherConditionSet::ID>::iterator it =
140 matching_condition_set_ids.begin();
141 it != matching_condition_set_ids.end(); it++) {
142 std::map<URLMatcherConditionSet::ID, MatcherID>::iterator matcher_id =
143 condition_set_id_to_event_matcher_id_.find(*it);
144 if (matcher_id == condition_set_id_to_event_matcher_id_.end()) {
145 NOTREACHED() << "id not found in condition set map (" << (*it) << ")";
146 continue;
147 }
148 MatcherID id = matcher_id->second;
149 EventMatcherMap::iterator matcher_entry = matcher_map.find(id);
150 if (matcher_entry == matcher_map.end()) {
151 // Matcher must be for a different event.
152 continue;
153 }
154 const EventMatcher* event_matcher = matcher_entry->second->event_matcher();
155 if (event_matcher->MatchNonURLCriteria(event_info)) {
156 CHECK(!event_matcher->HasURLFilters() || event_info.has_url());
157 matchers.insert(id);
158 }
159 }
160
161 return matchers;
162 }
163
164 int EventFilter::GetMatcherCountForEvent(const std::string& name) {
165 EventMatcherMultiMap::const_iterator it = event_matchers_.find(name);
166 if (it == event_matchers_.end())
167 return 0;
168
169 return it->second.size();
170 }
171
172 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/common/extensions/event_filter.h ('k') | chrome/common/extensions/event_filter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698