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 #include "base/callback.h"
7 #include "extensions/common/event_matcher.h"
9 #include "extensions/common/event_filtering_info.h"
12 const char kUrlFiltersKey
[] = "url";
13 const char kWindowTypesKey
[] = "windowTypes";
16 namespace extensions
{
18 const char kEventFilterServiceTypeKey
[] = "serviceType";
20 EventMatcher::EventMatcher(scoped_ptr
<base::DictionaryValue
> filter
,
22 : filter_(filter
.Pass()),
23 routing_id_(routing_id
) {
26 EventMatcher::~EventMatcher() {
29 bool EventMatcher::MatchNonURLCriteria(
30 const EventFilteringInfo
& event_info
) const {
31 if (event_info
.has_instance_id()) {
32 return event_info
.instance_id() == GetInstanceID();
35 if (event_info
.has_window_type()) {
36 for (int i
= 0; i
< GetWindowTypeCount(); i
++) {
37 std::string window_type
;
38 if (GetWindowType(i
, &window_type
) &&
39 window_type
== event_info
.window_type())
45 if (event_info
.has_window_exposed_by_default()) {
46 // An event with a |window_exposed_by_default| set is only
47 // relevant to the listener if no window type filter is set.
50 return event_info
.window_exposed_by_default();
53 const std::string
& service_type_filter
= GetServiceTypeFilter();
54 return service_type_filter
.empty() ||
55 service_type_filter
== event_info
.service_type();
58 int EventMatcher::GetURLFilterCount() const {
59 base::ListValue
* url_filters
= nullptr;
60 if (filter_
->GetList(kUrlFiltersKey
, &url_filters
))
61 return url_filters
->GetSize();
65 bool EventMatcher::GetURLFilter(int i
, base::DictionaryValue
** url_filter_out
) {
66 base::ListValue
* url_filters
= nullptr;
67 if (filter_
->GetList(kUrlFiltersKey
, &url_filters
)) {
68 return url_filters
->GetDictionary(i
, url_filter_out
);
73 bool EventMatcher::HasURLFilters() const {
74 return GetURLFilterCount() != 0;
77 std::string
EventMatcher::GetServiceTypeFilter() const {
78 std::string service_type_filter
;
79 filter_
->GetStringASCII(kEventFilterServiceTypeKey
, &service_type_filter
);
80 return service_type_filter
;
83 int EventMatcher::GetInstanceID() const {
85 filter_
->GetInteger("instanceId", &instance_id
);
89 int EventMatcher::GetWindowTypeCount() const {
90 base::ListValue
* window_type_filters
= nullptr;
91 if (filter_
->GetList(kWindowTypesKey
, &window_type_filters
))
92 return window_type_filters
->GetSize();
96 bool EventMatcher::GetWindowType(int i
, std::string
* window_type_out
) const {
97 base::ListValue
* window_types
= nullptr;
98 if (filter_
->GetList(kWindowTypesKey
, &window_types
)) {
99 return window_types
->GetString(i
, window_type_out
);
104 bool EventMatcher::HasWindowTypes() const {
105 return GetWindowTypeCount() != 0;
108 int EventMatcher::GetRoutingID() const {
112 } // namespace extensions