OLD | NEW |
---|---|
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 Loading... | |
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) { | |
battre
2012/06/18 12:29:17
opt: DCHECK(id_to_event_name_.find(id) != id_to_ev
koz (OOO until 15th September)
2012/06/19 00:07:57
Done.
| |
69 const std::string& event_name = id_to_event_name_[id]; | |
70 return event_matchers_[event_name][id]->event_matcher(); | |
71 } | |
72 | |
73 const std::string& EventFilter::GetEventName(MatcherID id) { | |
battre
2012/06/18 12:29:17
same here
koz (OOO until 15th September)
2012/06/19 00:07:57
Done.
| |
74 return id_to_event_name_[id]; | |
75 } | |
76 | |
68 bool EventFilter::CreateConditionSets( | 77 bool EventFilter::CreateConditionSets( |
69 MatcherID id, | 78 MatcherID id, |
70 base::ListValue* url_filters, | 79 EventMatcher* matcher, |
71 URLMatcherConditionSet::Vector* condition_sets) { | 80 URLMatcherConditionSet::Vector* condition_sets) { |
72 if (!url_filters || url_filters->GetSize() == 0) { | 81 if (matcher->GetURLFilterCount() == 0) { |
73 // If there are no url_filters then we want to match all events, so create a | 82 // If there are no URL filters then we want to match all events, so create a |
74 // URLFilter from an empty dictionary. | 83 // URLFilter from an empty dictionary. |
75 base::DictionaryValue empty_dict; | 84 base::DictionaryValue empty_dict; |
76 return AddDictionaryAsConditionSet(&empty_dict, condition_sets); | 85 return AddDictionaryAsConditionSet(&empty_dict, condition_sets); |
77 } | 86 } |
78 for (size_t i = 0; i < url_filters->GetSize(); i++) { | 87 for (int i = 0; i < matcher->GetURLFilterCount(); i++) { |
79 base::DictionaryValue* url_filter; | 88 base::DictionaryValue* url_filter; |
80 if (!url_filters->GetDictionary(i, &url_filter)) | 89 if (!matcher->GetURLFilter(i, &url_filter)) |
81 return false; | 90 return false; |
82 if (!AddDictionaryAsConditionSet(url_filter, condition_sets)) | 91 if (!AddDictionaryAsConditionSet(url_filter, condition_sets)) |
83 return false; | 92 return false; |
84 } | 93 } |
85 return true; | 94 return true; |
86 } | 95 } |
87 | 96 |
88 bool EventFilter::AddDictionaryAsConditionSet( | 97 bool EventFilter::AddDictionaryAsConditionSet( |
89 base::DictionaryValue* url_filter, | 98 base::DictionaryValue* url_filter, |
90 URLMatcherConditionSet::Vector* condition_sets) { | 99 URLMatcherConditionSet::Vector* condition_sets) { |
91 std::string error; | 100 std::string error; |
92 URLMatcherConditionSet::ID condition_set_id = next_condition_set_id_++; | 101 URLMatcherConditionSet::ID condition_set_id = next_condition_set_id_++; |
93 condition_sets->push_back(URLMatcherFactory::CreateFromURLFilterDictionary( | 102 condition_sets->push_back(URLMatcherFactory::CreateFromURLFilterDictionary( |
94 url_matcher_.condition_factory(), | 103 url_matcher_.condition_factory(), |
95 url_filter, | 104 url_filter, |
96 condition_set_id, | 105 condition_set_id, |
97 &error)); | 106 &error)); |
98 if (!error.empty()) { | 107 if (!error.empty()) { |
99 LOG(ERROR) << "CreateFromURLFilterDictionary failed: " << error; | 108 LOG(ERROR) << "CreateFromURLFilterDictionary failed: " << error; |
100 url_matcher_.ClearUnusedConditionSets(); | 109 url_matcher_.ClearUnusedConditionSets(); |
101 condition_sets->clear(); | 110 condition_sets->clear(); |
102 return false; | 111 return false; |
103 } | 112 } |
104 return true; | 113 return true; |
105 } | 114 } |
106 | 115 |
107 std::string EventFilter::RemoveEventMatcher(MatcherID id) { | 116 std::string EventFilter::RemoveEventMatcher(MatcherID id) { |
108 std::map<MatcherID, std::string>::iterator it = id_to_event_name_.find(id); | 117 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; | 118 std::string event_name = it->second; |
119 // EventMatcherEntry's destructor causes the condition set ids to be removed | |
120 // from url_matcher_. | |
121 event_matchers_[event_name].erase(id); | |
111 id_to_event_name_.erase(it); | 122 id_to_event_name_.erase(it); |
112 return event_name; | 123 return event_name; |
113 } | 124 } |
114 | 125 |
115 std::set<EventFilter::MatcherID> EventFilter::MatchEvent( | 126 std::set<EventFilter::MatcherID> EventFilter::MatchEvent( |
116 const std::string& event_name, const EventFilteringInfo& event_info) { | 127 const std::string& event_name, const EventFilteringInfo& event_info) { |
117 std::set<MatcherID> matchers; | 128 std::set<MatcherID> matchers; |
118 EventMatcherMultiMap::iterator it = event_matchers_.find(event_name); | 129 EventMatcherMultiMap::iterator it = event_matchers_.find(event_name); |
119 if (it == event_matchers_.end()) | 130 if (it == event_matchers_.end()) |
120 return matchers; | 131 return matchers; |
121 | 132 |
122 EventMatcherMap& matcher_map = it->second; | 133 EventMatcherMap& matcher_map = it->second; |
123 GURL url_to_match_against = event_info.has_url() ? event_info.url() : GURL(); | 134 GURL url_to_match_against = event_info.has_url() ? event_info.url() : GURL(); |
124 std::set<URLMatcherConditionSet::ID> matching_condition_set_ids = | 135 std::set<URLMatcherConditionSet::ID> matching_condition_set_ids = |
125 url_matcher_.MatchURL(url_to_match_against); | 136 url_matcher_.MatchURL(url_to_match_against); |
126 for (std::set<URLMatcherConditionSet::ID>::iterator it = | 137 for (std::set<URLMatcherConditionSet::ID>::iterator it = |
127 matching_condition_set_ids.begin(); | 138 matching_condition_set_ids.begin(); |
128 it != matching_condition_set_ids.end(); it++) { | 139 it != matching_condition_set_ids.end(); it++) { |
129 MatcherID id = condition_set_id_to_event_matcher_id_[*it]; | 140 std::map<URLMatcherConditionSet::ID, MatcherID>::iterator matcher_id = |
130 const EventMatcher& event_matcher = matcher_map[id]->event_matcher(); | 141 condition_set_id_to_event_matcher_id_.find(*it); |
131 if (event_matcher.MatchNonURLCriteria(event_info)) { | 142 if (matcher_id == condition_set_id_to_event_matcher_id_.end()) { |
132 CHECK(!event_matcher.url_filters() || event_info.has_url()); | 143 NOTREACHED() << "id not found in condition set map (" << (*it) << ")"; |
144 continue; | |
145 } | |
146 MatcherID id = matcher_id->second; | |
147 EventMatcherMap::iterator matcher_entry = matcher_map.find(id); | |
148 if (matcher_entry == matcher_map.end()) { | |
149 // Matcher must be for a different event. | |
150 continue; | |
151 } | |
152 const EventMatcher* event_matcher = matcher_entry->second->event_matcher(); | |
153 if (event_matcher->MatchNonURLCriteria(event_info)) { | |
154 CHECK(!event_matcher->HasURLFilters() || event_info.has_url()); | |
133 matchers.insert(id); | 155 matchers.insert(id); |
134 } | 156 } |
135 } | 157 } |
136 | 158 |
137 return matchers; | 159 return matchers; |
138 } | 160 } |
139 | 161 |
140 int EventFilter::GetMatcherCountForEvent(const std::string& name) { | 162 int EventFilter::GetMatcherCountForEvent(const std::string& name) { |
141 EventMatcherMultiMap::const_iterator it = event_matchers_.find(name); | 163 EventMatcherMultiMap::const_iterator it = event_matchers_.find(name); |
142 if (it == event_matchers_.end()) | 164 if (it == event_matchers_.end()) |
143 return 0; | 165 return 0; |
144 | 166 |
145 return it->second.size(); | 167 return it->second.size(); |
146 } | 168 } |
147 | 169 |
148 } // namespace extensions | 170 } // namespace extensions |
OLD | NEW |