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 #include "extensions/common/event_matcher.h"
6 
7 #include <utility>
8 
9 #include "base/callback.h"
10 #include "extensions/common/event_filtering_info.h"
11 
12 namespace {
13 const char kUrlFiltersKey[] = "url";
14 const char kWindowTypesKey[] = "windowTypes";
15 }
16 
17 namespace extensions {
18 
19 const char kEventFilterServiceTypeKey[] = "serviceType";
20 
EventMatcher(std::unique_ptr<base::DictionaryValue> filter,int routing_id)21 EventMatcher::EventMatcher(std::unique_ptr<base::DictionaryValue> filter,
22                            int routing_id)
23     : filter_(std::move(filter)), routing_id_(routing_id) {}
24 
~EventMatcher()25 EventMatcher::~EventMatcher() {
26 }
27 
MatchNonURLCriteria(const EventFilteringInfo & event_info) const28 bool EventMatcher::MatchNonURLCriteria(
29     const EventFilteringInfo& event_info) const {
30   if (event_info.instance_id) {
31     return *event_info.instance_id == GetInstanceID();
32   }
33 
34   if (event_info.window_type) {
35     int window_type_count = GetWindowTypeCount();
36     for (int i = 0; i < window_type_count; i++) {
37       std::string window_type;
38       if (GetWindowType(i, &window_type) &&
39           window_type == *event_info.window_type) {
40         return true;
41       }
42     }
43     return false;
44   }
45 
46   if (event_info.window_exposed_by_default) {
47     // An event with a |window_exposed_by_default| set is only
48     // relevant to the listener if no window type filter is set.
49     if (HasWindowTypes())
50       return false;
51     return *event_info.window_exposed_by_default;
52   }
53 
54   const std::string& service_type_filter = GetServiceTypeFilter();
55   return service_type_filter.empty() ||
56          (event_info.service_type &&
57           service_type_filter == *event_info.service_type);
58 }
59 
GetURLFilterCount() const60 int EventMatcher::GetURLFilterCount() const {
61   base::ListValue* url_filters = nullptr;
62   if (filter_->GetList(kUrlFiltersKey, &url_filters))
63     return url_filters->GetSize();
64   return 0;
65 }
66 
GetURLFilter(int i,base::DictionaryValue ** url_filter_out)67 bool EventMatcher::GetURLFilter(int i, base::DictionaryValue** url_filter_out) {
68   base::ListValue* url_filters = nullptr;
69   if (filter_->GetList(kUrlFiltersKey, &url_filters)) {
70     return url_filters->GetDictionary(i, url_filter_out);
71   }
72   return false;
73 }
74 
HasURLFilters() const75 bool EventMatcher::HasURLFilters() const {
76   return GetURLFilterCount() != 0;
77 }
78 
GetServiceTypeFilter() const79 std::string EventMatcher::GetServiceTypeFilter() const {
80   std::string service_type_filter;
81   filter_->GetStringASCII(kEventFilterServiceTypeKey, &service_type_filter);
82   return service_type_filter;
83 }
84 
GetInstanceID() const85 int EventMatcher::GetInstanceID() const {
86   int instance_id = 0;
87   filter_->GetInteger("instanceId", &instance_id);
88   return instance_id;
89 }
90 
GetWindowTypeCount() const91 int EventMatcher::GetWindowTypeCount() const {
92   base::ListValue* window_type_filters = nullptr;
93   if (filter_->GetList(kWindowTypesKey, &window_type_filters))
94     return window_type_filters->GetSize();
95   return 0;
96 }
97 
GetWindowType(int i,std::string * window_type_out) const98 bool EventMatcher::GetWindowType(int i, std::string* window_type_out) const {
99   base::ListValue* window_types = nullptr;
100   if (filter_->GetList(kWindowTypesKey, &window_types)) {
101     return window_types->GetString(i, window_type_out);
102   }
103   return false;
104 }
105 
HasWindowTypes() const106 bool EventMatcher::HasWindowTypes() const {
107   return GetWindowTypeCount() != 0;
108 }
109 
GetRoutingID() const110 int EventMatcher::GetRoutingID() const {
111   return routing_id_;
112 }
113 
114 }  // namespace extensions
115