File: web_app_installer_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (536 lines) | stat: -rw-r--r-- 19,812 bytes parent folder | download | duplicates (5)
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/apps/app_service/app_install/web_app_installer.h"

#include <variant>

#include "base/functional/bind.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/test_future.h"
#include "chrome/browser/apps/app_service/app_install/app_install_types.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/services/app_service/public/cpp/app_registry_cache.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/services/app_service/public/cpp/app_update.h"
#include "components/webapps/browser/install_result_code.h"
#include "content/public/test/browser_test.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"

namespace apps {

class WebAppInstallerBrowserTest : public InProcessBrowserTest {
 public:
  void SetUpOnMainThread() override {
    InProcessBrowserTest::SetUpOnMainThread();

    https_server_.RegisterRequestHandler(base::BindRepeating(
        &WebAppInstallerBrowserTest::HandleRequest, base::Unretained(this)));
    https_server_.AddDefaultHandlers(GetChromeTestDataDir());

    ASSERT_TRUE(https_server_.Start());

    // Icon URLs should remap to the test server.
    host_resolver()->AddRule("meltingpot.googleusercontent.com", "127.0.0.1");
  }

  std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
      const net::test_server::HttpRequest& request) {
    if ((request.relative_url != "/manifest.json" &&
         request.relative_url != "/manifest2.json") ||
        manifest_.empty()) {
      return nullptr;
    }

    auto response = std::make_unique<net::test_server::BasicHttpResponse>();
    response->set_code(net::HTTP_OK);
    response->set_content_type("application/json");
    if (request.relative_url == "/manifest.json") {
      response->set_content(manifest_);

    } else {
      response->set_content(manifest2_);
    }
    return response;
  }

  std::string AddIconToManifest(const std::string& manifest_template) {
    GURL icon_url = https_server()->GetURL("meltingpot.googleusercontent.com",
                                           "/web_apps/blue-192.png");
    constexpr char kIconsBlock[] = R"([{
        "src": "$1",
        "sizes": "192x192",
        "type": "image/png"
      }])";
    std::string icon_value = base::ReplaceStringPlaceholders(
        kIconsBlock, {icon_url.spec()}, nullptr);
    return base::ReplaceStringPlaceholders(manifest_template, {icon_value},
                                           nullptr);
  }

  AppInstallData CreateInstallData(std::string_view name,
                                   std::string_view package_id,
                                   std::string_view original_manifest_url,
                                   std::string_view test_manifest_url) {
    AppInstallData data(PackageId::FromString(package_id).value());
    data.name = name;
    WebAppInstallData& web_app_data =
        data.app_type_data.emplace<WebAppInstallData>();
    web_app_data.original_manifest_url = GURL(original_manifest_url);
    web_app_data.proxied_manifest_url =
        https_server()->GetURL(test_manifest_url);
    web_app_data.document_url =
        web_app_data.original_manifest_url.GetWithEmptyPath();
    return data;
  }

  void VerifyAppInstalled(webapps::AppId app_id,
                          const std::string& app_name,
                          InstallReason expected_reason) {
    bool found = app_registry_cache().ForOneApp(
        app_id, [app_name, expected_reason](const AppUpdate& update) {
          EXPECT_EQ(update.Name(), app_name);
          EXPECT_EQ(update.InstallReason(), expected_reason);
        });
    ASSERT_TRUE(found);
  }

  void SetManifestResponse(std::string manifest) { manifest_ = manifest; }
  void SetManifest2Response(std::string manifest) { manifest2_ = manifest; }

  Profile* profile() { return browser()->profile(); }

  net::EmbeddedTestServer* https_server() { return &https_server_; }

  AppRegistryCache& app_registry_cache() {
    auto* proxy = AppServiceProxyFactory::GetForProfile(browser()->profile());
    return proxy->AppRegistryCache();
  }

 private:
  net::EmbeddedTestServer https_server_;
  std::string manifest_;
  std::string manifest2_;
};

IN_PROC_BROWSER_TEST_F(WebAppInstallerBrowserTest, InstallOneOemApp) {
  WebAppInstaller installer(profile());

  constexpr char kManifestTemplate[] = R"({
    "name": "Example App",
    "start_url": "/index.html",
    "scope": "/",
    "icons": $1
  })";
  SetManifestResponse(AddIconToManifest(kManifestTemplate));

  base::HistogramTester histograms;
  base::test::TestFuture<bool> result;
  installer.InstallApp(
      AppInstallSurface::kAppPreloadServiceOem,
      CreateInstallData("Example App", "web:https://www.example.com/index.html",
                        "https://www.example.com/manifest.json",
                        "/manifest.json"),
      result.GetCallback());
  ASSERT_TRUE(result.Get());

  auto app_id = web_app::GenerateAppId(
      std::nullopt, GURL("https://www.example.com/index.html"));
  VerifyAppInstalled(app_id, "Example App", InstallReason::kOem);

  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.InstallResult",
      WebAppInstallResult::kSuccess, 1);
  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.InstallResult."
      "AppPreloadServiceOem",
      WebAppInstallResult::kSuccess, 1);
  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.CommandResultCode",
      webapps::InstallResultCode::kSuccessNewInstall, 1);
  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.CommandResultCode."
      "AppPreloadServiceOem",
      webapps::InstallResultCode::kSuccessNewInstall, 1);
}

IN_PROC_BROWSER_TEST_F(WebAppInstallerBrowserTest, InstallOneDefaultApp) {
  WebAppInstaller installer(profile());

  constexpr char kManifestTemplate[] = R"({
    "name": "Example App",
    "start_url": "/index.html",
    "scope": "/",
    "icons": $1
  })";
  SetManifestResponse(AddIconToManifest(kManifestTemplate));

  base::HistogramTester histograms;
  base::test::TestFuture<bool> result;
  installer.InstallApp(
      AppInstallSurface::kAppPreloadServiceDefault,
      CreateInstallData("Example App", "web:https://www.example.com/index.html",
                        "https://www.example.com/manifest.json",
                        "/manifest.json"),
      result.GetCallback());
  ASSERT_TRUE(result.Get());

  auto app_id = web_app::GenerateAppId(
      std::nullopt, GURL("https://www.example.com/index.html"));
  VerifyAppInstalled(app_id, "Example App", InstallReason::kDefault);

  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.InstallResult",
      WebAppInstallResult::kSuccess, 1);
  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.InstallResult."
      "AppPreloadServiceDefault",
      WebAppInstallResult::kSuccess, 1);
  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.CommandResultCode",
      webapps::InstallResultCode::kSuccessNewInstall, 1);
  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.CommandResultCode."
      "AppPreloadServiceDefault",
      webapps::InstallResultCode::kSuccessNewInstall, 1);
}

IN_PROC_BROWSER_TEST_F(WebAppInstallerBrowserTest, InstallMultipleOemApps) {
  WebAppInstaller installer(profile());

  constexpr char kManifestTemplate[] = R"({
    "name": "Example App",
    "start_url": "/index.html",
    "scope": "/",
    "icons": $1
  })";
  constexpr char kManifestTemplate2[] = R"({
    "name": "Example App2",
    "start_url": "/index.html",
    "scope": "/",
    "icons": $1
  })";
  SetManifestResponse(AddIconToManifest(kManifestTemplate));
  SetManifest2Response(AddIconToManifest(kManifestTemplate2));

  base::HistogramTester histograms;
  base::test::TestFuture<bool> result;
  base::test::TestFuture<bool> result2;
  installer.InstallApp(
      AppInstallSurface::kAppPreloadServiceOem,
      CreateInstallData("Example App", "web:https://www.example.com/index.html",
                        "https://www.example.com/manifest.json",
                        "/manifest.json"),
      result.GetCallback());
  installer.InstallApp(
      AppInstallSurface::kAppPreloadServiceOem,
      CreateInstallData(
          "Example App2", "web:https://www.example2.com/index.html",
          "https://www.example2.com/manifest2.json", "/manifest2.json"),
      result2.GetCallback());
  ASSERT_TRUE(result.Get());
  ASSERT_TRUE(result2.Get());

  auto app_id = web_app::GenerateAppId(
      std::nullopt, GURL("https://www.example.com/index.html"));
  VerifyAppInstalled(app_id, "Example App", InstallReason::kOem);

  auto app_id2 = web_app::GenerateAppId(
      std::nullopt, GURL("https://www.example2.com/index.html"));
  VerifyAppInstalled(app_id2, "Example App2", InstallReason::kOem);

  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.InstallResult",
      WebAppInstallResult::kSuccess, 2);
  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.InstallResult."
      "AppPreloadServiceOem",
      WebAppInstallResult::kSuccess, 2);
  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.CommandResultCode",
      webapps::InstallResultCode::kSuccessNewInstall, 2);
  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.CommandResultCode."
      "AppPreloadServiceOem",
      webapps::InstallResultCode::kSuccessNewInstall, 2);
}

IN_PROC_BROWSER_TEST_F(WebAppInstallerBrowserTest, InstallWithManifestId) {
  WebAppInstaller installer(profile());

  SetManifestResponse(AddIconToManifest(R"({
    "id": "manifest_id",
    "name": "Example App",
    "start_url": "/index.html",
    "scope": "/",
    "icons": $1
  })"));

  base::test::TestFuture<bool> result;
  installer.InstallApp(
      AppInstallSurface::kAppPreloadServiceOem,
      CreateInstallData(
          "Example App", "web:https://www.example.com/manifest_id",
          "https://www.example.com/manifest.json", "/manifest.json"),
      result.GetCallback());
  ASSERT_TRUE(result.Get());

  // The generated app ID should take the manifest ID into account.
  auto app_id = web_app::GenerateAppId(
      "manifest_id", GURL("https://www.example.com/index.html"));
  ASSERT_TRUE(
      app_registry_cache().ForOneApp(app_id, [](const AppUpdate& update) {}));
}

// Reinstalling an existing user-installed app should not overwrite manifest
// data, but will add the OEM install reason.
IN_PROC_BROWSER_TEST_F(WebAppInstallerBrowserTest, InstallOverUserApp) {
  constexpr char kStartUrl[] = "https://www.example.com/";
  constexpr char kOriginalManifestUrl[] =
      "https://www.example.com/manifest.json";
  constexpr char kUserAppName[] = "User Installed App";

  WebAppInstaller installer(profile());

  auto app_id = web_app::test::InstallDummyWebApp(profile(), kUserAppName,
                                                  GURL(kStartUrl));

  SetManifestResponse(AddIconToManifest(R"({
    "name": "OEM Installed app",
    "start_url": "/",
    "icons": $1
  })"));

  base::test::TestFuture<bool> result;
  installer.InstallApp(
      AppInstallSurface::kAppPreloadServiceOem,
      CreateInstallData("OEM Installed app", base::StrCat({"web:", kStartUrl}),
                        kOriginalManifestUrl, "/manifest.json"),
      result.GetCallback());
  ASSERT_TRUE(result.Get());

  VerifyAppInstalled(app_id, kUserAppName, InstallReason::kOem);
}

// The manifest id in the proto does not match the calculated manifest id.
IN_PROC_BROWSER_TEST_F(WebAppInstallerBrowserTest,
                       InstallMismatchedDataManifestId) {
  WebAppInstaller installer(profile());

  SetManifestResponse(AddIconToManifest(R"({
    "name": "Example App",
    "start_url": "/index.html",
    "scope": "/",
    "icons": $1
  })"));

  base::HistogramTester histograms;
  base::test::TestFuture<bool> result;
  installer.InstallApp(
      AppInstallSurface::kAppPreloadServiceOem,
      CreateInstallData(
          "Example App", "web:https://www.example.com/manifest_id",
          "https://www.example.com/manifest.json", "/manifest.json"),
      result.GetCallback());
  ASSERT_FALSE(result.Get());

  auto app_id = web_app::GenerateAppId(
      std::nullopt, GURL("https://www.example.com/index.html"));
  bool found =
      app_registry_cache().ForOneApp(app_id, [](const AppUpdate& update) {});
  ASSERT_FALSE(found);

  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.InstallResult",
      WebAppInstallResult::kWebAppInstallError, 1);
  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.InstallResult."
      "AppPreloadServiceOem",
      WebAppInstallResult::kWebAppInstallError, 1);
  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.CommandResultCode",
      webapps::InstallResultCode::kExpectedAppIdCheckFailed, 1);
  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.CommandResultCode."
      "AppPreloadServiceOem",
      webapps::InstallResultCode::kExpectedAppIdCheckFailed, 1);
}

IN_PROC_BROWSER_TEST_F(WebAppInstallerBrowserTest, ManifestFileIsNotJSON) {
  WebAppInstaller installer(profile());

  SetManifestResponse("INVALID");

  base::test::TestFuture<bool> result;
  installer.InstallApp(
      AppInstallSurface::kAppPreloadServiceOem,
      CreateInstallData(
          "Example App", "web:https://www.example.com/manifest_id",
          "https://www.example.com/manifest.json", "/manifest.json"),
      result.GetCallback());
  ASSERT_FALSE(result.Get());

  auto app_id = web_app::GenerateAppId(
      std::nullopt, GURL("https://www.example.com/index.html"));
  bool found =
      app_registry_cache().ForOneApp(app_id, [](const AppUpdate& update) {});
  ASSERT_FALSE(found);
}

IN_PROC_BROWSER_TEST_F(WebAppInstallerBrowserTest,
                       ManifestFileIsHasMissingFields) {
  WebAppInstaller installer(profile());

  SetManifestResponse(R"({
    "is_valid": "no."
  })");

  base::HistogramTester histograms;
  base::test::TestFuture<bool> result;
  installer.InstallApp(
      AppInstallSurface::kAppPreloadServiceOem,
      CreateInstallData(
          "Example App", "web:https://www.example.com/manifest_id",
          "https://www.example.com/manifest.json", "/manifest.json"),
      result.GetCallback());
  ASSERT_FALSE(result.Get());

  auto app_id = web_app::GenerateAppId(
      std::nullopt, GURL("https://www.example.com/index.html"));
  bool found =
      app_registry_cache().ForOneApp(app_id, [](const AppUpdate& update) {});
  ASSERT_FALSE(found);

  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.CommandResultCode",
      webapps::InstallResultCode::kNotValidManifestForWebApp, 1);
  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.CommandResultCode."
      "AppPreloadServiceOem",
      webapps::InstallResultCode::kNotValidManifestForWebApp, 1);
}

IN_PROC_BROWSER_TEST_F(WebAppInstallerBrowserTest, ManifestWithFailingIcons) {
  WebAppInstaller installer(profile());

  constexpr char kManifestTemplate[] = R"({
    "name": "Example App",
    "start_url": "/index.html",
    "scope": "/",
    "icons": [{
      "src": "$1",
      "sizes": "96x96",
      "type": "image/png"
    }]
  })";

  // The image will fail to download, which will cause the installation to fail.
  GURL image_url =
      https_server()->GetURL("meltingpot.googleusercontent.com", "/404");

  SetManifestResponse(base::ReplaceStringPlaceholders(
      kManifestTemplate, {image_url.spec()}, nullptr));

  base::HistogramTester histograms;
  base::test::TestFuture<bool> result;
  installer.InstallApp(
      AppInstallSurface::kAppPreloadServiceOem,
      CreateInstallData(
          "Example App", "web:https://www.example.com/manifest_id",
          "https://www.example.com/manifest.json", "/manifest.json"),
      result.GetCallback());
  ASSERT_FALSE(result.Get());

  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.CommandResultCode",
      webapps::InstallResultCode::kIconDownloadingFailed, 1);
  histograms.ExpectBucketCount(
      "Apps.AppInstallService.WebAppInstaller.CommandResultCode."
      "AppPreloadServiceOem",
      webapps::InstallResultCode::kIconDownloadingFailed, 1);
}

IN_PROC_BROWSER_TEST_F(WebAppInstallerBrowserTest, InstallWebsite) {
  WebAppInstaller installer(profile());
  SetManifestResponse(AddIconToManifest(R"({
    "name": "Example App",
    "start_url": "/",
    "icons": $1
  })"));

  AppInstallData data = CreateInstallData(
      "Example App", "website:https://www.example.com/",
      "https://www.example.com/manifest.json", "/manifest.json");
  // Unset user_window_override to request UserDisplayMode::kBrowser.
  std::get<WebAppInstallData>(data.app_type_data).open_as_window = false;

  base::test::TestFuture<bool> result;
  installer.InstallApp(AppInstallSurface::kAppInstallUriUnknown, data,
                       result.GetCallback());
  ASSERT_TRUE(result.Get());

  // Verify that the app is set to open in a browser in App Service.
  auto app_id =
      web_app::GenerateAppId(std::nullopt, GURL("https://www.example.com/"));
  bool found =
      app_registry_cache().ForOneApp(app_id, [](const AppUpdate& update) {
        EXPECT_EQ(update.WindowMode(), apps::WindowMode::kBrowser);
      });
  ASSERT_TRUE(found);

  EXPECT_TRUE(web_app::WebAppProvider::GetForWebApps(profile())
                  ->registrar_unsafe()
                  .IsDiyApp(app_id));
}

IN_PROC_BROWSER_TEST_F(WebAppInstallerBrowserTest,
                       InstallWebsiteWithOpenInWindowOverride) {
  WebAppInstaller installer(profile());

  constexpr char kManifestTemplate[] = R"({
    "name": "Example App",
    "start_url": "/",
    "scope": "/",
    "icons": $1
  })";
  SetManifestResponse(AddIconToManifest(kManifestTemplate));

  AppInstallData data = CreateInstallData(
      "Example App", "website:https://www.example.com/",
      "https://www.example.com/manifest.json", "/manifest.json");
  // Unset user_window_override to request UserDisplayMode::kStandalone.
  std::get<WebAppInstallData>(data.app_type_data).open_as_window = true;

  base::test::TestFuture<bool> result;
  installer.InstallApp(AppInstallSurface::kAppInstallUriUnknown, data,
                       result.GetCallback());
  ASSERT_TRUE(result.Get());

  // Verify that the app is set to open in a window in App Service.
  auto app_id =
      web_app::GenerateAppId(std::nullopt, GURL("https://www.example.com/"));
  bool found =
      app_registry_cache().ForOneApp(app_id, [](const AppUpdate& update) {
        EXPECT_EQ(update.WindowMode(), apps::WindowMode::kWindow);
      });
  ASSERT_TRUE(found);

  EXPECT_TRUE(web_app::WebAppProvider::GetForWebApps(profile())
                  ->registrar_unsafe()
                  .IsDiyApp(app_id));
}

}  // namespace apps