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.
5 #ifndef EXTENSIONS_COMMON_EVENT_FILTER_H_
6 #define EXTENSIONS_COMMON_EVENT_FILTER_H_
11 #include "base/memory/linked_ptr.h"
12 #include "extensions/common/event_filtering_info.h"
13 #include "extensions/common/event_matcher.h"
14 #include "extensions/common/matcher/url_matcher.h"
16 namespace extensions {
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.
23 typedef int MatcherID;
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);
32 // Retrieve the EventMatcher with the given id.
33 EventMatcher* GetEventMatcher(MatcherID id);
35 // Retrieve the name of the event that the EventMatcher specified by |id| is
37 const std::string& GetEventName(MatcherID id);
39 // Removes an event matcher, returning the name of the event that it was for.
40 std::string RemoveEventMatcher(MatcherID id);
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,
50 int GetMatcherCountForEvent(const std::string& event_name);
53 bool IsURLMatcherEmpty() const {
54 return url_matcher_.IsEmpty();
58 class EventMatcherEntry {
60 // Adds |condition_sets| to |url_matcher| on construction and removes them
61 // again on destruction. |condition_sets| should be the
62 // URLMatcherConditionSets that match the URL constraints specified by
64 EventMatcherEntry(scoped_ptr<EventMatcher> event_matcher,
65 URLMatcher* url_matcher,
66 const URLMatcherConditionSet::Vector& condition_sets);
69 // Prevents the removal of condition sets when this class is destroyed. We
70 // call this in EventFilter's destructor so that we don't do the costly
71 // removal of condition sets when the URLMatcher is going to be destroyed
72 // and clean them up anyway.
73 void DontRemoveConditionSetsInDestructor();
75 EventMatcher* event_matcher() {
76 return event_matcher_.get();
80 scoped_ptr<EventMatcher> event_matcher_;
81 // The id sets in url_matcher_ that this EventMatcher owns.
82 std::vector<URLMatcherConditionSet::ID> condition_set_ids_;
83 URLMatcher* url_matcher_;
85 DISALLOW_COPY_AND_ASSIGN(EventMatcherEntry);
88 // Maps from a matcher id to an event matcher entry.
89 typedef std::map<MatcherID, linked_ptr<EventMatcherEntry> > EventMatcherMap;
91 // Maps from event name to the map of matchers that are registered for it.
92 typedef std::map<std::string, EventMatcherMap> EventMatcherMultiMap;
94 // Adds the list of URL filters in |matcher| to the URL matcher, having
95 // matches for those URLs map to |id|.
96 bool CreateConditionSets(MatcherID id,
97 EventMatcher* matcher,
98 URLMatcherConditionSet::Vector* condition_sets);
100 bool AddDictionaryAsConditionSet(
101 base::DictionaryValue* url_filter,
102 URLMatcherConditionSet::Vector* condition_sets);
104 URLMatcher url_matcher_;
105 EventMatcherMultiMap event_matchers_;
107 // The next id to assign to an EventMatcher.
110 // The next id to assign to a condition set passed to URLMatcher.
111 URLMatcherConditionSet::ID next_condition_set_id_;
113 // Maps condition set ids, which URLMatcher operates in, to event matcher
114 // ids, which the interface to this class operates in. As each EventFilter
115 // can specify many condition sets this is a many to one relationship.
116 std::map<URLMatcherConditionSet::ID, MatcherID>
117 condition_set_id_to_event_matcher_id_;
119 // Maps from event matcher ids to the name of the event they match on.
120 std::map<MatcherID, std::string> id_to_event_name_;
122 DISALLOW_COPY_AND_ASSIGN(EventFilter);
125 } // namespace extensions
127 #endif // EXTENSIONS_COMMON_EVENT_FILTER_H_