OLD | NEW |
---|---|
(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 #ifndef CHROME_COMMON_EXTENSIONS_EVENT_FILTER_H_ | |
6 #define CHROME_COMMON_EXTENSIONS_EVENT_FILTER_H_ | |
7 #pragma once | |
8 | |
9 #include "base/memory/linked_ptr.h" | |
10 #include "chrome/common/extensions/event_matcher.h" | |
11 #include "chrome/common/extensions/event_filtering_info.h" | |
12 #include "chrome/common/extensions/matcher/url_matcher.h" | |
13 | |
14 #include <map> | |
15 #include <set> | |
16 | |
17 namespace extensions { | |
18 | |
19 // Matches incoming events against a collection of EventMatchers. Each added | |
20 // EventMatcher is given an id which is returned by MatchEvent() when it is | |
21 // passed a matching event. | |
22 class EventFilter { | |
23 public: | |
24 typedef int MatcherID; | |
25 EventFilter(); | |
26 ~EventFilter(); | |
27 | |
28 // Adds an event matcher that will be used in calls to MatchEvent(). Returns | |
29 // the id of the matcher, or -1 if there was an error. | |
30 MatcherID AddEventMatcher(const std::string& event_name, | |
31 scoped_ptr<EventMatcher> matcher); | |
32 // Removes an event matcher, returning the name of the event that it was for. | |
33 std::string RemoveEventMatcher(MatcherID id); | |
34 | |
35 // Match an event named |event_name| with filtering info |event_info| against | |
36 // our set of event matchers. Returns a set of ids that correspond to the | |
37 // event matchers that matched the event. | |
38 // TODO(koz): Add a std::string* parameter for retrieving error messages. | |
39 std::set<MatcherID> MatchEvent(const std::string& event_name, | |
40 const EventFilteringInfo& event_info); | |
41 | |
42 int GetMatcherCountForEvent(const std::string& event_name); | |
43 | |
44 // For testing. | |
45 bool IsURLMatcherEmpty() const { | |
46 return url_matcher_.IsEmpty(); | |
47 } | |
48 | |
49 private: | |
50 class EventMatcherEntry { | |
51 public: | |
52 // Adds |condition_sets| to |url_matcher| on construction and removes them | |
53 // again on destruction. |condition_sets| should be the | |
54 // URLMatcherConditionSets that match the URL constraints specified by | |
55 // |event_matcher|. | |
56 EventMatcherEntry(scoped_ptr<EventMatcher> event_matcher, | |
57 URLMatcher* url_matcher, | |
58 const URLMatcherConditionSet::Vector& condition_sets); | |
59 ~EventMatcherEntry(); | |
60 | |
61 const EventMatcher& event_matcher() const { | |
62 return *event_matcher_; | |
63 } | |
64 | |
65 private: | |
66 scoped_ptr<EventMatcher> event_matcher_; | |
67 // The id sets in url_matcher_ that this EventMatcher owns. | |
68 std::vector<URLMatcherConditionSet::ID> condition_set_ids_; | |
69 URLMatcher* url_matcher_; | |
battre
2012/05/17 08:50:51
DISALLOW_COPY_AND_ASSIGN
koz (OOO until 15th September)
2012/05/18 01:13:50
Done.
| |
70 }; | |
71 | |
72 // Maps from a matcher id to an event matcher entry. | |
73 typedef std::map<MatcherID, linked_ptr<EventMatcherEntry> > EventMatcherMap; | |
74 | |
75 // Maps from event name to the map of matchers that are registered for it. | |
76 typedef std::map<std::string, EventMatcherMap> EventMatcherMultiMap; | |
77 | |
78 // Adds the list of filters to the URL matcher, having matches for those URLs | |
79 // map to |id|. | |
80 bool CreateConditionSets(MatcherID id, | |
81 base::ListValue* url_filters, | |
82 URLMatcherConditionSet::Vector* condition_sets); | |
83 | |
84 bool AddDictionaryAsConditionSet( | |
85 base::DictionaryValue* url_filter, | |
86 URLMatcherConditionSet::Vector* condition_sets); | |
87 | |
88 URLMatcher url_matcher_; | |
89 EventMatcherMultiMap event_matchers_; | |
battre
2012/05/17 08:50:51
Add a comment that event_matchers_ must appear aft
koz (OOO until 15th September)
2012/05/18 01:13:50
I added the comment in ~EventFilter().
| |
90 | |
91 // The next id to assign to an EventMatcher. | |
92 MatcherID next_id_; | |
93 | |
94 // The next id to assign to a condition set passed to URLMatcher. | |
95 URLMatcherConditionSet::ID next_condition_set_id_; | |
96 | |
97 // Maps condition set ids, which URLMatcher operates in, to event matcher | |
98 // ids, which the interface to this class operates in. As each EventFilter | |
99 // can specify many condition sets this is a many to one relationship. | |
100 std::map<URLMatcherConditionSet::ID, MatcherID> | |
101 condition_set_id_to_event_matcher_id_; | |
102 | |
103 // Maps from event matcher ids to the name of the event they match on. | |
104 std::map<MatcherID, std::string> id_to_event_name_; | |
105 | |
106 DISALLOW_COPY_AND_ASSIGN(EventFilter); | |
107 }; | |
108 | |
109 } // namespace extensions | |
110 | |
111 #endif // CHROME_COMMON_EXTENSIONS_EVENT_FILTER_H_ | |
OLD | NEW |