event_filter.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #include "extensions/common/event_filter.h"
  5. #include <string>
  6. #include <utility>
  7. #include "base/logging.h"
  8. #include "base/notreached.h"
  9. #include "components/url_matcher/url_matcher_factory.h"
  10. #include "extensions/common/mojom/event_dispatcher.mojom.h"
  11. #include "ipc/ipc_message.h"
  12. using url_matcher::URLMatcher;
  13. using url_matcher::URLMatcherConditionSet;
  14. using url_matcher::URLMatcherFactory;
  15. namespace extensions {
  16. EventFilter::EventMatcherEntry::EventMatcherEntry(
  17. std::unique_ptr<EventMatcher> event_matcher,
  18. URLMatcher* url_matcher,
  19. const URLMatcherConditionSet::Vector& condition_sets)
  20. : event_matcher_(std::move(event_matcher)), url_matcher_(url_matcher) {
  21. condition_set_ids_.reserve(condition_sets.size());
  22. for (const scoped_refptr<URLMatcherConditionSet>& condition_set :
  23. condition_sets) {
  24. condition_set_ids_.push_back(condition_set->id());
  25. }
  26. url_matcher_->AddConditionSets(condition_sets);
  27. }
  28. EventFilter::EventMatcherEntry::~EventMatcherEntry() {
  29. url_matcher_->RemoveConditionSets(condition_set_ids_);
  30. }
  31. void EventFilter::EventMatcherEntry::DontRemoveConditionSetsInDestructor() {
  32. condition_set_ids_.clear();
  33. }
  34. EventFilter::EventFilter()
  35. : next_id_(0),
  36. next_condition_set_id_(0) {
  37. }
  38. EventFilter::~EventFilter() {
  39. // Normally when an event matcher entry is removed from event_matchers_ it
  40. // will remove its condition sets from url_matcher_, but as url_matcher_ is
  41. // being destroyed anyway there is no need to do that step here.
  42. for (auto& matcher_map : event_matchers_) {
  43. for (auto& matcher : matcher_map.second)
  44. matcher.second->DontRemoveConditionSetsInDestructor();
  45. }
  46. }
  47. EventFilter::MatcherID EventFilter::AddEventMatcher(
  48. const std::string& event_name,
  49. std::unique_ptr<EventMatcher> matcher) {
  50. URLMatcherConditionSet::Vector condition_sets;
  51. if (!CreateConditionSets(matcher.get(), &condition_sets))
  52. return -1;
  53. MatcherID id = next_id_++;
  54. for (const scoped_refptr<URLMatcherConditionSet>& condition_set :
  55. condition_sets) {
  56. condition_set_id_to_event_matcher_id_.insert(
  57. std::make_pair(condition_set->id(), id));
  58. }
  59. id_to_event_name_[id] = event_name;
  60. event_matchers_[event_name][id] = std::make_unique<EventMatcherEntry>(
  61. std::move(matcher), &url_matcher_, condition_sets);
  62. return id;
  63. }
  64. EventMatcher* EventFilter::GetEventMatcher(MatcherID id) {
  65. const std::string& event_name = GetEventName(id);
  66. return event_matchers_[event_name][id]->event_matcher();
  67. }
  68. const std::string& EventFilter::GetEventName(MatcherID id) const {
  69. auto it = id_to_event_name_.find(id);
  70. DCHECK(it != id_to_event_name_.end());
  71. return it->second;
  72. }
  73. bool EventFilter::CreateConditionSets(
  74. EventMatcher* matcher,
  75. URLMatcherConditionSet::Vector* condition_sets) {
  76. int url_filter_count = matcher->GetURLFilterCount();
  77. if (url_filter_count == 0) {
  78. // If there are no URL filters then we want to match all events, so create a
  79. // URLFilter from an empty dictionary.
  80. base::Value::Dict empty_dict;
  81. return AddDictionaryAsConditionSet(empty_dict, condition_sets);
  82. }
  83. for (int i = 0; i < url_filter_count; i++) {
  84. const base::Value::Dict* url_filter = matcher->GetURLFilter(i);
  85. if (!url_filter)
  86. return false;
  87. if (!AddDictionaryAsConditionSet(*url_filter, condition_sets))
  88. return false;
  89. }
  90. return true;
  91. }
  92. bool EventFilter::AddDictionaryAsConditionSet(
  93. const base::Value::Dict& url_filter,
  94. URLMatcherConditionSet::Vector* condition_sets) {
  95. std::string error;
  96. base::MatcherStringPattern::ID condition_set_id = next_condition_set_id_++;
  97. condition_sets->push_back(URLMatcherFactory::CreateFromURLFilterDictionary(
  98. url_matcher_.condition_factory(),
  99. url_filter,
  100. condition_set_id,
  101. &error));
  102. if (!error.empty()) {
  103. LOG(ERROR) << "CreateFromURLFilterDictionary failed: " << error;
  104. url_matcher_.ClearUnusedConditionSets();
  105. condition_sets->clear();
  106. return false;
  107. }
  108. return true;
  109. }
  110. std::string EventFilter::RemoveEventMatcher(MatcherID id) {
  111. auto it = id_to_event_name_.find(id);
  112. std::string event_name = it->second;
  113. // EventMatcherEntry's destructor causes the condition set ids to be removed
  114. // from url_matcher_.
  115. event_matchers_[event_name].erase(id);
  116. id_to_event_name_.erase(it);
  117. return event_name;
  118. }
  119. std::set<EventFilter::MatcherID> EventFilter::MatchEvent(
  120. const std::string& event_name,
  121. const mojom::EventFilteringInfo& event_info,
  122. int routing_id) const {
  123. std::set<MatcherID> matchers;
  124. auto it = event_matchers_.find(event_name);
  125. if (it == event_matchers_.end())
  126. return matchers;
  127. const EventMatcherMap& matcher_map = it->second;
  128. const GURL& url_to_match_against =
  129. event_info.url ? *event_info.url : GURL::EmptyGURL();
  130. std::set<base::MatcherStringPattern::ID> matching_condition_set_ids =
  131. url_matcher_.MatchURL(url_to_match_against);
  132. for (const auto& id_key : matching_condition_set_ids) {
  133. auto matcher_id = condition_set_id_to_event_matcher_id_.find(id_key);
  134. if (matcher_id == condition_set_id_to_event_matcher_id_.end()) {
  135. NOTREACHED() << "id not found in condition set map (" << id_key << ")";
  136. continue;
  137. }
  138. MatcherID id = matcher_id->second;
  139. auto matcher_entry = matcher_map.find(id);
  140. if (matcher_entry == matcher_map.end()) {
  141. // Matcher must be for a different event.
  142. continue;
  143. }
  144. const EventMatcher* event_matcher = matcher_entry->second->event_matcher();
  145. // The context that installed the event listener should be the same context
  146. // as the one where the event listener is called.
  147. if (routing_id != MSG_ROUTING_NONE &&
  148. event_matcher->GetRoutingID() != routing_id) {
  149. continue;
  150. }
  151. if (event_matcher->MatchNonURLCriteria(event_info)) {
  152. CHECK(!event_matcher->HasURLFilters() || event_info.url);
  153. matchers.insert(id);
  154. }
  155. }
  156. return matchers;
  157. }
  158. int EventFilter::GetMatcherCountForEventForTesting(
  159. const std::string& name) const {
  160. auto it = event_matchers_.find(name);
  161. return it != event_matchers_.end() ? it->second.size() : 0;
  162. }
  163. } // namespace extensions