File: event_matcher.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (113 lines) | stat: -rw-r--r-- 3,232 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/common/event_matcher.h"

#include <utility>

#include "base/functional/callback.h"
#include "base/strings/string_util.h"
#include "extensions/common/mojom/event_dispatcher.mojom.h"

namespace {
const char kUrlFiltersKey[] = "url";
const char kWindowTypesKey[] = "windowTypes";
}

namespace extensions {

const char kEventFilterServiceTypeKey[] = "serviceType";

EventMatcher::EventMatcher(std::unique_ptr<base::Value::Dict> filter,
                           int routing_id)
    : filter_(std::move(filter)), routing_id_(routing_id) {}

EventMatcher::~EventMatcher() {
}

bool EventMatcher::MatchNonURLCriteria(
    const mojom::EventFilteringInfo& event_info) const {
  if (event_info.has_instance_id) {
    return event_info.instance_id == GetInstanceID();
  }

  if (event_info.window_type) {
    int window_type_count = GetWindowTypeCount();
    for (int i = 0; i < window_type_count; i++) {
      std::string window_type;
      if (GetWindowType(i, &window_type) &&
          window_type == *event_info.window_type) {
        return true;
      }
    }
    return false;
  }

  if (event_info.has_window_exposed_by_default) {
    // An event with a |window_exposed_by_default| set is only
    // relevant to the listener if no window type filter is set.
    if (GetWindowTypeCount() > 0) {
      return false;
    }
    return event_info.window_exposed_by_default;
  }

  const std::string& service_type_filter = GetServiceTypeFilter();
  return service_type_filter.empty() ||
         (event_info.service_type &&
          service_type_filter == *event_info.service_type);
}

int EventMatcher::GetURLFilterCount() const {
  base::Value::List* url_filters = filter_->FindList(kUrlFiltersKey);
  if (url_filters)
    return url_filters->size();
  return 0;
}

const base::Value::Dict* EventMatcher::GetURLFilter(int i) {
  base::Value::List* url_filters = filter_->FindList(kUrlFiltersKey);
  if (url_filters)
    return (*url_filters)[i].GetIfDict();
  return nullptr;
}

bool EventMatcher::HasURLFilters() const {
  return GetURLFilterCount() != 0;
}

std::string EventMatcher::GetServiceTypeFilter() const {
  std::string service_type_filter;
  if (const std::string* ptr =
          filter_->FindString(kEventFilterServiceTypeKey)) {
    if (base::IsStringASCII(*ptr))
      service_type_filter = *ptr;
  }
  return service_type_filter;
}

int EventMatcher::GetInstanceID() const {
  return filter_->FindInt("instanceId").value_or(0);
}

int EventMatcher::GetWindowTypeCount() const {
  base::Value::List* window_type_filters = filter_->FindList(kWindowTypesKey);
  if (window_type_filters)
    return window_type_filters->size();
  return 0;
}

bool EventMatcher::GetWindowType(int i, std::string* window_type_out) const {
  base::Value::List* window_types = filter_->FindList(kWindowTypesKey);
  if (window_types) {
    if (i >= 0 && static_cast<size_t>(i) < window_types->size() &&
        (*window_types)[i].is_string()) {
      *window_type_out = (*window_types)[i].GetString();
      return true;
    }
  }
  return false;
}

}  // namespace extensions