File: intent_test_util.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (142 lines) | stat: -rw-r--r-- 5,792 bytes parent folder | download | duplicates (9)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/services/app_service/public/cpp/intent_test_util.h"

#include <utility>
#include <vector>

#include "components/services/app_service/public/cpp/intent_filter_util.h"
#include "components/services/app_service/public/cpp/intent_util.h"

namespace {

apps::IntentFilterPtr MakeIntentFilter(const std::string& action,
                                       const std::string& mime_type,
                                       const std::string& file_extension,
                                       const std::string& url_pattern,
                                       const std::string& activity_label) {
  DCHECK(!mime_type.empty() || !file_extension.empty() || !url_pattern.empty());
  auto intent_filter = std::make_unique<apps::IntentFilter>();

  intent_filter->AddSingleValueCondition(apps::ConditionType::kAction, action,
                                         apps::PatternMatchType::kLiteral);

  apps::ConditionValues condition_values;
  if (!mime_type.empty()) {
    condition_values.push_back(std::make_unique<apps::ConditionValue>(
        mime_type, apps::PatternMatchType::kMimeType));
  }
  if (!file_extension.empty()) {
    condition_values.push_back(std::make_unique<apps::ConditionValue>(
        file_extension, apps::PatternMatchType::kFileExtension));
  }
  if (!url_pattern.empty()) {
    condition_values.push_back(std::make_unique<apps::ConditionValue>(
        url_pattern, apps::PatternMatchType::kGlob));
  }
  intent_filter->conditions.push_back(std::make_unique<apps::Condition>(
      apps::ConditionType::kFile, std::move(condition_values)));

  intent_filter->activity_label = activity_label;
  return intent_filter;
}

}  // namespace

namespace apps_util {

apps::IntentFilterPtr MakeIntentFilterForMimeType(
    const std::string& mime_type) {
  auto intent_filter = std::make_unique<apps::IntentFilter>();

  intent_filter->AddSingleValueCondition(apps::ConditionType::kAction,
                                         kIntentActionSend,
                                         apps::PatternMatchType::kLiteral);

  std::vector<apps::ConditionValuePtr> condition_values;
  condition_values.push_back(std::make_unique<apps::ConditionValue>(
      mime_type, apps::PatternMatchType::kMimeType));
  intent_filter->conditions.push_back(std::make_unique<apps::Condition>(
      apps::ConditionType::kMimeType, std::move(condition_values)));

  return intent_filter;
}

apps::IntentFilterPtr MakeIntentFilterForSend(
    const std::string& mime_type,
    const std::string& activity_label) {
  return MakeIntentFilter(kIntentActionSend, mime_type, "", "", activity_label);
}

apps::IntentFilterPtr MakeIntentFilterForSendMultiple(
    const std::string& mime_type,
    const std::string& activity_label) {
  return MakeIntentFilter(kIntentActionSendMultiple, mime_type, "", "",
                          activity_label);
}

apps::IntentFilterPtr MakeFileFilterForView(const std::string& mime_type,
                                            const std::string& file_extension,
                                            const std::string& activity_label) {
  return MakeIntentFilter(kIntentActionView, mime_type, file_extension, "",
                          activity_label);
}

apps::IntentFilterPtr MakeURLFilterForView(const std::string& url_pattern,
                                           const std::string& activity_label) {
  return MakeIntentFilter(kIntentActionView, "", "", url_pattern,
                          activity_label);
}

apps::IntentFilterPtr MakeSchemeOnlyFilter(const std::string& scheme) {
  apps::ConditionValues condition_values;
  condition_values.push_back(std::make_unique<apps::ConditionValue>(
      scheme, apps::PatternMatchType::kLiteral));
  auto condition = std::make_unique<apps::Condition>(
      apps::ConditionType::kScheme, std::move(condition_values));

  auto intent_filter = std::make_unique<apps::IntentFilter>();
  intent_filter->conditions.push_back(std::move(condition));

  return intent_filter;
}

apps::IntentFilterPtr MakeSchemeAndHostOnlyFilter(const std::string& scheme,
                                                  const std::string& host) {
  apps::ConditionValues scheme_condition_values;
  scheme_condition_values.push_back(std::make_unique<apps::ConditionValue>(
      scheme, apps::PatternMatchType::kLiteral));
  auto scheme_condition = std::make_unique<apps::Condition>(
      apps::ConditionType::kScheme, std::move(scheme_condition_values));

  apps::ConditionValues host_condition_values;
  host_condition_values.push_back(std::make_unique<apps::ConditionValue>(
      host, apps::PatternMatchType::kLiteral));
  auto host_condition = std::make_unique<apps::Condition>(
      apps::ConditionType::kAuthority, std::move(host_condition_values));

  auto intent_filter = std::make_unique<apps::IntentFilter>();
  intent_filter->conditions.push_back(std::move(scheme_condition));
  intent_filter->conditions.push_back(std::move(host_condition));

  return intent_filter;
}

void AddConditionValue(apps::ConditionType condition_type,
                       const std::string& value,
                       apps::PatternMatchType pattern_match_type,
                       apps::IntentFilterPtr& intent_filter) {
  for (auto& condition : intent_filter->conditions) {
    if (condition->condition_type == condition_type) {
      condition->condition_values.push_back(
          std::make_unique<apps::ConditionValue>(value, pattern_match_type));
      return;
    }
  }
  intent_filter->AddSingleValueCondition(condition_type, value,
                                         pattern_match_type);
}

}  // namespace apps_util