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

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

Issue 10534125: Extend EventFilter to handle the case where EventMatchers from event X get (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: rebase Created 8 years, 6 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "chrome/common/extensions/event_filter.h" 5 #include "chrome/common/extensions/event_filter.h"
6 6
7 #include "chrome/common/extensions/matcher/url_matcher_factory.h" 7 #include "chrome/common/extensions/matcher/url_matcher_factory.h"
8 8
9 namespace extensions { 9 namespace extensions {
10 10
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 it2->second->DontRemoveConditionSetsInDestructor(); 44 it2->second->DontRemoveConditionSetsInDestructor();
45 } 45 }
46 } 46 }
47 } 47 }
48 48
49 EventFilter::MatcherID 49 EventFilter::MatcherID
50 EventFilter::AddEventMatcher(const std::string& event_name, 50 EventFilter::AddEventMatcher(const std::string& event_name,
51 scoped_ptr<EventMatcher> matcher) { 51 scoped_ptr<EventMatcher> matcher) {
52 MatcherID id = next_id_++; 52 MatcherID id = next_id_++;
53 URLMatcherConditionSet::Vector condition_sets; 53 URLMatcherConditionSet::Vector condition_sets;
54 if (!CreateConditionSets(id, matcher->url_filters(), &condition_sets)) 54 if (!CreateConditionSets(id, matcher.get(), &condition_sets))
55 return -1; 55 return -1;
56 56
57 for (URLMatcherConditionSet::Vector::iterator it = condition_sets.begin(); 57 for (URLMatcherConditionSet::Vector::iterator it = condition_sets.begin();
58 it != condition_sets.end(); it++) { 58 it != condition_sets.end(); it++) {
59 condition_set_id_to_event_matcher_id_.insert( 59 condition_set_id_to_event_matcher_id_.insert(
60 std::make_pair((*it)->id(), id)); 60 std::make_pair((*it)->id(), id));
61 } 61 }
62 id_to_event_name_[id] = event_name; 62 id_to_event_name_[id] = event_name;
63 event_matchers_[event_name][id] = linked_ptr<EventMatcherEntry>( 63 event_matchers_[event_name][id] = linked_ptr<EventMatcherEntry>(
64 new EventMatcherEntry(matcher.Pass(), &url_matcher_, condition_sets)); 64 new EventMatcherEntry(matcher.Pass(), &url_matcher_, condition_sets));
65 return id; 65 return id;
66 } 66 }
67 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
68 bool EventFilter::CreateConditionSets( 79 bool EventFilter::CreateConditionSets(
69 MatcherID id, 80 MatcherID id,
70 base::ListValue* url_filters, 81 EventMatcher* matcher,
71 URLMatcherConditionSet::Vector* condition_sets) { 82 URLMatcherConditionSet::Vector* condition_sets) {
72 if (!url_filters || url_filters->GetSize() == 0) { 83 if (matcher->GetURLFilterCount() == 0) {
73 // If there are no url_filters then we want to match all events, so create a 84 // If there are no URL filters then we want to match all events, so create a
74 // URLFilter from an empty dictionary. 85 // URLFilter from an empty dictionary.
75 base::DictionaryValue empty_dict; 86 base::DictionaryValue empty_dict;
76 return AddDictionaryAsConditionSet(&empty_dict, condition_sets); 87 return AddDictionaryAsConditionSet(&empty_dict, condition_sets);
77 } 88 }
78 for (size_t i = 0; i < url_filters->GetSize(); i++) { 89 for (int i = 0; i < matcher->GetURLFilterCount(); i++) {
79 base::DictionaryValue* url_filter; 90 base::DictionaryValue* url_filter;
80 if (!url_filters->GetDictionary(i, &url_filter)) 91 if (!matcher->GetURLFilter(i, &url_filter))
81 return false; 92 return false;
82 if (!AddDictionaryAsConditionSet(url_filter, condition_sets)) 93 if (!AddDictionaryAsConditionSet(url_filter, condition_sets))
83 return false; 94 return false;
84 } 95 }
85 return true; 96 return true;
86 } 97 }
87 98
88 bool EventFilter::AddDictionaryAsConditionSet( 99 bool EventFilter::AddDictionaryAsConditionSet(
89 base::DictionaryValue* url_filter, 100 base::DictionaryValue* url_filter,
90 URLMatcherConditionSet::Vector* condition_sets) { 101 URLMatcherConditionSet::Vector* condition_sets) {
91 std::string error; 102 std::string error;
92 URLMatcherConditionSet::ID condition_set_id = next_condition_set_id_++; 103 URLMatcherConditionSet::ID condition_set_id = next_condition_set_id_++;
93 condition_sets->push_back(URLMatcherFactory::CreateFromURLFilterDictionary( 104 condition_sets->push_back(URLMatcherFactory::CreateFromURLFilterDictionary(
94 url_matcher_.condition_factory(), 105 url_matcher_.condition_factory(),
95 url_filter, 106 url_filter,
96 condition_set_id, 107 condition_set_id,
97 &error)); 108 &error));
98 if (!error.empty()) { 109 if (!error.empty()) {
99 LOG(ERROR) << "CreateFromURLFilterDictionary failed: " << error; 110 LOG(ERROR) << "CreateFromURLFilterDictionary failed: " << error;
100 url_matcher_.ClearUnusedConditionSets(); 111 url_matcher_.ClearUnusedConditionSets();
101 condition_sets->clear(); 112 condition_sets->clear();
102 return false; 113 return false;
103 } 114 }
104 return true; 115 return true;
105 } 116 }
106 117
107 std::string EventFilter::RemoveEventMatcher(MatcherID id) { 118 std::string EventFilter::RemoveEventMatcher(MatcherID id) {
108 std::map<MatcherID, std::string>::iterator it = id_to_event_name_.find(id); 119 std::map<MatcherID, std::string>::iterator it = id_to_event_name_.find(id);
109 event_matchers_[it->second].erase(id);
110 std::string event_name = it->second; 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);
111 id_to_event_name_.erase(it); 124 id_to_event_name_.erase(it);
112 return event_name; 125 return event_name;
113 } 126 }
114 127
115 std::set<EventFilter::MatcherID> EventFilter::MatchEvent( 128 std::set<EventFilter::MatcherID> EventFilter::MatchEvent(
116 const std::string& event_name, const EventFilteringInfo& event_info) { 129 const std::string& event_name, const EventFilteringInfo& event_info) {
117 std::set<MatcherID> matchers; 130 std::set<MatcherID> matchers;
118 EventMatcherMultiMap::iterator it = event_matchers_.find(event_name); 131 EventMatcherMultiMap::iterator it = event_matchers_.find(event_name);
119 if (it == event_matchers_.end()) 132 if (it == event_matchers_.end())
120 return matchers; 133 return matchers;
121 134
122 EventMatcherMap& matcher_map = it->second; 135 EventMatcherMap& matcher_map = it->second;
123 GURL url_to_match_against = event_info.has_url() ? event_info.url() : GURL(); 136 GURL url_to_match_against = event_info.has_url() ? event_info.url() : GURL();
124 std::set<URLMatcherConditionSet::ID> matching_condition_set_ids = 137 std::set<URLMatcherConditionSet::ID> matching_condition_set_ids =
125 url_matcher_.MatchURL(url_to_match_against); 138 url_matcher_.MatchURL(url_to_match_against);
126 for (std::set<URLMatcherConditionSet::ID>::iterator it = 139 for (std::set<URLMatcherConditionSet::ID>::iterator it =
127 matching_condition_set_ids.begin(); 140 matching_condition_set_ids.begin();
128 it != matching_condition_set_ids.end(); it++) { 141 it != matching_condition_set_ids.end(); it++) {
129 MatcherID id = condition_set_id_to_event_matcher_id_[*it]; 142 std::map<URLMatcherConditionSet::ID, MatcherID>::iterator matcher_id =
130 const EventMatcher& event_matcher = matcher_map[id]->event_matcher(); 143 condition_set_id_to_event_matcher_id_.find(*it);
131 if (event_matcher.MatchNonURLCriteria(event_info)) { 144 if (matcher_id == condition_set_id_to_event_matcher_id_.end()) {
132 CHECK(!event_matcher.url_filters() || event_info.has_url()); 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());
133 matchers.insert(id); 157 matchers.insert(id);
134 } 158 }
135 } 159 }
136 160
137 return matchers; 161 return matchers;
138 } 162 }
139 163
140 int EventFilter::GetMatcherCountForEvent(const std::string& name) { 164 int EventFilter::GetMatcherCountForEvent(const std::string& name) {
141 EventMatcherMultiMap::const_iterator it = event_matchers_.find(name); 165 EventMatcherMultiMap::const_iterator it = event_matchers_.find(name);
142 if (it == event_matchers_.end()) 166 if (it == event_matchers_.end())
143 return 0; 167 return 0;
144 168
145 return it->second.size(); 169 return it->second.size();
146 } 170 }
147 171
148 } // namespace extensions 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