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 EXTENSIONS_COMMON_EVENT_FILTER_H_ 6 #define EXTENSIONS_COMMON_EVENT_FILTER_H_ 7 8 #include <map> 9 #include <set> 10 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" 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 int routing_id); 49 50 int GetMatcherCountForEvent(const std::string& event_name); 51 52 // For testing. 53 bool IsURLMatcherEmpty() const { 54 return url_matcher_.IsEmpty(); 55 } 56 57 private: 58 class EventMatcherEntry { 59 public: 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 63 // |event_matcher|. 64 EventMatcherEntry(scoped_ptr<EventMatcher> event_matcher, 65 URLMatcher* url_matcher, 66 const URLMatcherConditionSet::Vector& condition_sets); 67 ~EventMatcherEntry(); 68 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(); 74 75 EventMatcher* event_matcher() { 76 return event_matcher_.get(); 77 } 78 79 private: 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_; 84 85 DISALLOW_COPY_AND_ASSIGN(EventMatcherEntry); 86 }; 87 88 // Maps from a matcher id to an event matcher entry. 89 typedef std::map<MatcherID, linked_ptr<EventMatcherEntry> > EventMatcherMap; 90 91 // Maps from event name to the map of matchers that are registered for it. 92 typedef std::map<std::string, EventMatcherMap> EventMatcherMultiMap; 93 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); 99 100 bool AddDictionaryAsConditionSet( 101 base::DictionaryValue* url_filter, 102 URLMatcherConditionSet::Vector* condition_sets); 103 104 URLMatcher url_matcher_; 105 EventMatcherMultiMap event_matchers_; 106 107 // The next id to assign to an EventMatcher. 108 MatcherID next_id_; 109 110 // The next id to assign to a condition set passed to URLMatcher. 111 URLMatcherConditionSet::ID next_condition_set_id_; 112 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_; 118 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_; 121 122 DISALLOW_COPY_AND_ASSIGN(EventFilter); 123 }; 124 125 } // namespace extensions 126 127 #endif // EXTENSIONS_COMMON_EVENT_FILTER_H_ 128