File: installable_evaluator.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (296 lines) | stat: -rw-r--r-- 9,919 bytes parent folder | download
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright 2023 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/webapps/browser/installable/installable_evaluator.h"

#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "components/security_state/core/security_state.h"
#include "components/webapps/browser/webapps_client.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "content/public/common/url_constants.h"
#include "net/base/url_util.h"
#include "services/network/public/cpp/is_potentially_trustworthy.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/manifest/manifest_util.h"
#include "third_party/blink/public/mojom/favicon/favicon_url.mojom.h"

namespace webapps {

namespace {

using IconPurpose = blink::mojom::ManifestImageResource_Purpose;

// This constant is the icon size on Android (48dp) multiplied by the scale
// factor of a Nexus 5 device (3x). It is the currently advertised minimum icon
// size for triggering banners.
const int kMinimumPrimaryIconSizeInPx = 144;

struct ImageTypeDetails {
  const char* extension;
  const char* mimetype;
};

constexpr ImageTypeDetails kSupportedImageTypes[] = {
    {".png", "image/png"},
    {".svg", "image/svg+xml"},
    {".webp", "image/webp"},
};

bool HasValidStartUrl(const blink::mojom::Manifest& manifest,
                      const mojom::WebPageMetadata& metadata,
                      InstallableCriteria criteria) {
  if (manifest.start_url.is_valid()) {
    // If the start_url is valid, the id must be valid.
    CHECK(manifest.id.is_valid());
    return true;
  }
  return criteria == InstallableCriteria::kImplicitManifestFieldsHTML &&
         metadata.application_url.is_valid();
}

bool IsManifestNameValid(const blink::mojom::Manifest& manifest) {
  return (manifest.name && !manifest.name->empty()) ||
         (manifest.short_name && !manifest.short_name->empty());
}

bool IsWebPageMetadataContainValidName(const mojom::WebPageMetadata& metadata) {
  return !metadata.application_name.empty() || !metadata.title.empty();
}

bool HasValidName(const blink::mojom::Manifest& manifest,
                  const mojom::WebPageMetadata& metadata,
                  InstallableCriteria criteria) {
  if (IsManifestNameValid(manifest)) {
    return true;
  }
  return (criteria == InstallableCriteria::kImplicitManifestFieldsHTML &&
          IsWebPageMetadataContainValidName(metadata));
}

bool IsIconTypeSupported(const blink::Manifest::ImageResource& icon) {
  // The type field is optional. If it isn't present, fall back on checking
  // the src extension.
  if (icon.type.empty()) {
    std::string filename = icon.src.ExtractFileName();
    for (const ImageTypeDetails& details : kSupportedImageTypes) {
      if (base::EndsWith(filename, details.extension,
                         base::CompareCase::INSENSITIVE_ASCII)) {
        return true;
      }
    }
    return false;
  }

  for (const ImageTypeDetails& details : kSupportedImageTypes) {
    if (base::EqualsASCII(icon.type, details.mimetype)) {
      return true;
    }
  }
  return false;
}

// Returns whether |manifest| specifies an SVG or PNG icon that has
// IconPurpose::ANY, with size >= kMinimumPrimaryIconSizeInPx (or size "any").
bool DoesManifestContainRequiredIcon(const blink::mojom::Manifest& manifest) {
  for (const auto& icon : manifest.icons) {
    if (!IsIconTypeSupported(icon)) {
      continue;
    }

    if (!base::Contains(icon.purpose, IconPurpose::ANY)) {
      continue;
    }

    for (const auto& size : icon.sizes) {
      if (size.IsEmpty()) {  // "any"
        return true;
      }
      if (size.width() >= InstallableEvaluator::GetMinimumIconSizeInPx() &&
          size.height() >= InstallableEvaluator::GetMinimumIconSizeInPx() &&
          size.width() <= InstallableEvaluator::kMaximumIconSizeInPx &&
          size.height() <= InstallableEvaluator::kMaximumIconSizeInPx) {
        return true;
      }
    }
  }

  return false;
}

bool HasNonDefaultFavicon(content::WebContents* web_contents) {
  if (!web_contents) {
    return false;
  }
  for (const auto& favicon_url : web_contents->GetFaviconURLs()) {
    if (!favicon_url->is_default_icon) {
      return true;
    }
  }
  return false;
}

bool HasValidIcon(content::WebContents* web_contents,
                  const blink::mojom::Manifest& manifest,
                  InstallableCriteria criteria) {
  if (DoesManifestContainRequiredIcon(manifest)) {
    return true;
  }
  return (criteria == InstallableCriteria::kImplicitManifestFieldsHTML &&
          HasNonDefaultFavicon(web_contents));
}

bool IsInstallableDisplayMode(blink::mojom::DisplayMode display_mode) {
  return display_mode == blink::mojom::DisplayMode::kStandalone ||
         display_mode == blink::mojom::DisplayMode::kFullscreen ||
         display_mode == blink::mojom::DisplayMode::kMinimalUi ||
         display_mode == blink::mojom::DisplayMode::kWindowControlsOverlay ||
         (display_mode == blink::mojom::DisplayMode::kBorderless &&
          base::FeatureList::IsEnabled(blink::features::kWebAppBorderless)) ||
         (display_mode == blink::mojom::DisplayMode::kTabbed &&
          base::FeatureList::IsEnabled(blink::features::kDesktopPWAsTabStrip));
}

InstallableStatusCode GetDisplayError(const blink::mojom::Manifest& manifest,
                                      InstallableCriteria criteria) {
  blink::mojom::DisplayMode display_mode_to_evaluate = manifest.display;
  InstallableStatusCode error_type_if_invalid = MANIFEST_DISPLAY_NOT_SUPPORTED;

  // Unsupported values are ignored when we parse the manifest, and
  // consequently aren't in the manifest.display_override array.
  // If this array is not empty, the first value will "win", so validate
  // this value is installable.
  if (!manifest.display_override.empty()) {
    display_mode_to_evaluate = manifest.display_override[0];
    error_type_if_invalid = MANIFEST_DISPLAY_OVERRIDE_NOT_SUPPORTED;
  }

  switch (criteria) {
    case InstallableCriteria::kValidManifestWithIcons:
      if (!IsInstallableDisplayMode(display_mode_to_evaluate)) {
        return error_type_if_invalid;
      }
      break;
    case InstallableCriteria::kImplicitManifestFieldsHTML:
      if (display_mode_to_evaluate == blink::mojom::DisplayMode::kBrowser) {
        return error_type_if_invalid;
      }
      break;
    case InstallableCriteria::kValidManifestIgnoreDisplay:
      break;
    case InstallableCriteria::kDoNotCheck:
      NOTREACHED();
      break;
  }
  return NO_ERROR_DETECTED;
}

}  // namespace

InstallableEvaluator::InstallableEvaluator(content::WebContents* web_contents,
                                           const InstallablePageData& data,
                                           InstallableCriteria criteria)
    : web_contents_(web_contents->GetWeakPtr()),
      page_data_(data),
      criteria_(criteria) {}

InstallableEvaluator::~InstallableEvaluator() = default;

// static
int InstallableEvaluator::GetMinimumIconSizeInPx() {
  return kMinimumPrimaryIconSizeInPx;
}

absl::optional<std::vector<InstallableStatusCode>>
InstallableEvaluator::CheckInstallability() const {
  if (criteria_ == InstallableCriteria::kDoNotCheck) {
    return absl::nullopt;
  }

  std::vector<InstallableStatusCode> errors;
  if (blink::IsEmptyManifest(page_data_->GetManifest())) {
    errors.push_back(MANIFEST_EMPTY);
    return errors;
  }

  if (!HasValidStartUrl(page_data_->GetManifest(),
                        page_data_->WebPageMetadata(), criteria_)) {
    errors.push_back(START_URL_NOT_VALID);
  }

  if (!HasValidName(page_data_->GetManifest(), page_data_->WebPageMetadata(),
                    criteria_)) {
    errors.push_back(MANIFEST_MISSING_NAME_OR_SHORT_NAME);
  }

  InstallableStatusCode display_error =
      GetDisplayError(page_data_->GetManifest(), criteria_);
  if (display_error != NO_ERROR_DETECTED) {
    errors.push_back(display_error);
  }

  if (!HasValidIcon(web_contents_.get(), page_data_->GetManifest(),
                    criteria_)) {
    errors.push_back(MANIFEST_MISSING_SUITABLE_ICON);
  }

  return errors;
}

std::vector<InstallableStatusCode> InstallableEvaluator::CheckEligibility(
    content::WebContents* web_contents) const {
  std::vector<InstallableStatusCode> errors;
  if (web_contents->GetBrowserContext()->IsOffTheRecord()) {
    errors.push_back(IN_INCOGNITO);
  }
  if (!IsContentSecure(web_contents)) {
    errors.push_back(NOT_FROM_SECURE_ORIGIN);
  }
  return errors;
}

// static
bool InstallableEvaluator::IsContentSecure(content::WebContents* web_contents) {
  if (!web_contents) {
    return false;
  }

  // chrome:// URLs are considered secure.
  const GURL& url = web_contents->GetLastCommittedURL();
  if (url.scheme() == content::kChromeUIScheme) {
    return true;
  }

  // chrome-untrusted:// URLs are shipped with Chrome, so they are considered
  // secure in this context.
  if (url.scheme() == content::kChromeUIUntrustedScheme) {
    return true;
  }

  if (IsOriginConsideredSecure(url)) {
    return true;
  }

  // This can be null in unit tests but should be non-null in production.
  if (!webapps::WebappsClient::Get()) {
    return false;
  }

  return security_state::IsSslCertificateValid(
      WebappsClient::Get()->GetSecurityLevelForWebContents(web_contents));
}

// static
bool InstallableEvaluator::IsOriginConsideredSecure(const GURL& url) {
  auto origin = url::Origin::Create(url);
  auto* webapps_client = webapps::WebappsClient::Get();
  return (webapps_client && webapps_client->IsOriginConsideredSecure(origin)) ||
         net::IsLocalhost(url) ||
         network::SecureOriginAllowlist::GetInstance().IsOriginAllowlisted(
             origin);
}

}  // namespace webapps