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

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: rebase 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_);
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->url_filters(), &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 bool EventFilter::CreateConditionSets(
69 MatcherID id,
70 base::ListValue* url_filters,
71 URLMatcherConditionSet::Vector* condition_sets) {
72 if (!url_filters || url_filters->GetSize() == 0) {
73 // If there are no url_filters then we want to match all events, so create a
74 // URLFilter from an empty dictionary.
75 base::DictionaryValue empty_dict;
76 return AddDictionaryAsConditionSet(&empty_dict, condition_sets);
77 }
78 for (size_t i = 0; i < url_filters->GetSize(); i++) {
79 base::DictionaryValue* url_filter;
80 if (!url_filters->GetDictionary(i, &url_filter))
81 return false;
82 if (!AddDictionaryAsConditionSet(url_filter, condition_sets))
83 return false;
84 }
85 return true;
86 }
87
88 bool EventFilter::AddDictionaryAsConditionSet(
89 base::DictionaryValue* url_filter,
90 URLMatcherConditionSet::Vector* condition_sets) {
91 std::string error;
92 URLMatcherConditionSet::ID condition_set_id = next_condition_set_id_++;
93 condition_sets->push_back(URLMatcherFactory::CreateFromURLFilterDictionary(
94 url_matcher_.condition_factory(),
95 url_filter,
96 condition_set_id,
97 &error));
98 if (!error.empty()) {
99 LOG(ERROR) << "CreateFromURLFilterDictionary failed: " << error;
100 url_matcher_.ClearUnusedConditionSets();
101 condition_sets->clear();
102 return false;
103 }
104 return true;
105 }
106
107 std::string EventFilter::RemoveEventMatcher(MatcherID id) {
108 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;
111 id_to_event_name_.erase(it);
112 return event_name;
113 }
114
115 std::set<EventFilter::MatcherID> EventFilter::MatchEvent(
116 const std::string& event_name, const EventFilteringInfo& event_info) {
117 std::set<MatcherID> matchers;
118 EventMatcherMultiMap::iterator it = event_matchers_.find(event_name);
119 if (it == event_matchers_.end())
120 return matchers;
121
122 EventMatcherMap& matcher_map = it->second;
123 GURL url_to_match_against = event_info.has_url() ? event_info.url() : GURL();
124 std::set<URLMatcherConditionSet::ID> matching_condition_set_ids =
125 url_matcher_.MatchURL(url_to_match_against);
126 for (std::set<URLMatcherConditionSet::ID>::iterator it =
127 matching_condition_set_ids.begin();
128 it != matching_condition_set_ids.end(); it++) {
129 MatcherID id = condition_set_id_to_event_matcher_id_[*it];
130 const EventMatcher& event_matcher = matcher_map[id]->event_matcher();
131 if (event_matcher.MatchNonURLCriteria(event_info)) {
132 CHECK(!event_matcher.url_filters() || event_info.has_url());
133 matchers.insert(id);
134 }
135 }
136
137 return matchers;
138 }
139
140 int EventFilter::GetMatcherCountForEvent(const std::string& name) {
141 EventMatcherMultiMap::const_iterator it = event_matchers_.find(name);
142 if (it == event_matchers_.end())
143 return 0;
144
145 return it->second.size();
146 }
147
148 } // 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