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