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 // Prevents the removal of condition sets when this class is destroyed. We |
| 62 // call this in EventFilter's destructor so that we don't do the costly |
| 63 // removal of condition sets when the URLMatcher is going to be destroyed |
| 64 // and clean them up anyway. |
| 65 void DontRemoveConditionSetsInDestructor(); |
| 66 |
| 67 const EventMatcher& event_matcher() const { |
| 68 return *event_matcher_; |
| 69 } |
| 70 |
| 71 private: |
| 72 scoped_ptr<EventMatcher> event_matcher_; |
| 73 // The id sets in url_matcher_ that this EventMatcher owns. |
| 74 std::vector<URLMatcherConditionSet::ID> condition_set_ids_; |
| 75 URLMatcher* url_matcher_; |
| 76 |
| 77 DISALLOW_COPY_AND_ASSIGN(EventMatcherEntry); |
| 78 }; |
| 79 |
| 80 // Maps from a matcher id to an event matcher entry. |
| 81 typedef std::map<MatcherID, linked_ptr<EventMatcherEntry> > EventMatcherMap; |
| 82 |
| 83 // Maps from event name to the map of matchers that are registered for it. |
| 84 typedef std::map<std::string, EventMatcherMap> EventMatcherMultiMap; |
| 85 |
| 86 // Adds the list of filters to the URL matcher, having matches for those URLs |
| 87 // map to |id|. |
| 88 bool CreateConditionSets(MatcherID id, |
| 89 base::ListValue* url_filters, |
| 90 URLMatcherConditionSet::Vector* condition_sets); |
| 91 |
| 92 bool AddDictionaryAsConditionSet( |
| 93 base::DictionaryValue* url_filter, |
| 94 URLMatcherConditionSet::Vector* condition_sets); |
| 95 |
| 96 URLMatcher url_matcher_; |
| 97 EventMatcherMultiMap event_matchers_; |
| 98 |
| 99 // The next id to assign to an EventMatcher. |
| 100 MatcherID next_id_; |
| 101 |
| 102 // The next id to assign to a condition set passed to URLMatcher. |
| 103 URLMatcherConditionSet::ID next_condition_set_id_; |
| 104 |
| 105 // Maps condition set ids, which URLMatcher operates in, to event matcher |
| 106 // ids, which the interface to this class operates in. As each EventFilter |
| 107 // can specify many condition sets this is a many to one relationship. |
| 108 std::map<URLMatcherConditionSet::ID, MatcherID> |
| 109 condition_set_id_to_event_matcher_id_; |
| 110 |
| 111 // Maps from event matcher ids to the name of the event they match on. |
| 112 std::map<MatcherID, std::string> id_to_event_name_; |
| 113 |
| 114 DISALLOW_COPY_AND_ASSIGN(EventFilter); |
| 115 }; |
| 116 |
| 117 } // namespace extensions |
| 118 |
| 119 #endif // CHROME_COMMON_EXTENSIONS_EVENT_FILTER_H_ |
OLD | NEW |