File: manifest_update_check_command.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 (602 lines) | stat: -rw-r--r-- 22,929 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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
// 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 "chrome/browser/web_applications/commands/manifest_update_check_command.h"

#include "base/feature_list.h"
#include "base/functional/callback_forward.h"
#include "base/i18n/time_formatting.h"
#include "base/notreached.h"
#include "base/strings/to_string.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/web_applications/callback_utils.h"
#include "chrome/browser/web_applications/generated_icon_fix_util.h"
#include "chrome/browser/web_applications/locks/app_lock.h"
#include "chrome/browser/web_applications/manifest_update_manager.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_icon_operations.h"
#include "chrome/browser/web_applications/web_app_install_utils.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/browser/web_applications/web_app_registry_update.h"
#include "chrome/browser/web_applications/web_app_sync_bridge.h"
#include "chrome/browser/web_applications/web_app_ui_manager.h"
#include "chrome/browser/web_applications/web_contents/web_app_icon_downloader.h"
#include "chrome/common/chrome_features.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace web_app {

ManifestUpdateCheckCommand::ManifestUpdateCheckCommand(
    const GURL& url,
    const webapps::AppId& app_id,
    base::Time check_time,
    base::WeakPtr<content::WebContents> web_contents,
    CompletedCallback callback,
    std::unique_ptr<WebAppDataRetriever> data_retriever,
    std::unique_ptr<WebAppIconDownloader> icon_downloader)
    : WebAppCommand<AppLock,
                    ManifestUpdateCheckResult,
                    std::unique_ptr<WebAppInstallInfo>>(
          "ManifestUpdateCheckCommand",
          AppLockDescription(app_id),
          std::move(callback),
          /*args_for_shutdown=*/
          std::make_tuple(ManifestUpdateCheckResult::kSystemShutdown,
                          /*new_install_info=*/nullptr)),
      url_(url),
      app_id_(app_id),
      check_time_(check_time),
      web_contents_(web_contents),
      data_retriever_(std::move(data_retriever)),
      icon_downloader_(std::move(icon_downloader)) {
  GetMutableDebugValue().Set("app_id", app_id_);
  GetMutableDebugValue().Set("url", url_.spec());
  GetMutableDebugValue().Set("stage", base::ToString(stage_));
  GetMutableDebugValue().Set("check_time",
                             base::TimeFormatFriendlyDateAndTime(check_time_));
}

ManifestUpdateCheckCommand::~ManifestUpdateCheckCommand() = default;

void ManifestUpdateCheckCommand::StartWithLock(std::unique_ptr<AppLock> lock) {
  lock_ = std::move(lock);

  if (IsWebContentsDestroyed()) {
    CompleteCommandAndSelfDestruct(
        ManifestUpdateCheckResult::kWebContentsDestroyed);
    return;
  }
  Observe(web_contents_.get());

  // Runs a linear sequence of asynchronous and synchronous steps.
  // This sequence can be early exited at any point by a call to
  // CompleteCommandAndSelfDestruct().
  RunChainedCallbacks(
      base::BindOnce(&ManifestUpdateCheckCommand::DownloadNewManifestData,
                     GetWeakPtr()),

      base::BindOnce(&ManifestUpdateCheckCommand::LoadExistingManifestData,
                     GetWeakPtr()),

      base::BindOnce(&ManifestUpdateCheckCommand::CompareManifestData,
                     GetWeakPtr()),

      base::BindOnce(&ManifestUpdateCheckCommand::ResolveIdentityChanges,
                     GetWeakPtr()),

      base::BindOnce(&ManifestUpdateCheckCommand::CheckComplete, GetWeakPtr()));
}

void ManifestUpdateCheckCommand::DidFinishNavigation(
    content::NavigationHandle* navigation_handle) {
  if (!navigation_handle->IsInPrimaryMainFrame() ||
      !navigation_handle->HasCommitted() ||
      navigation_handle->IsSameDocument()) {
    return;
  }

  if (url::IsSameOriginWith(navigation_handle->GetPreviousPrimaryMainFrameURL(),
                            navigation_handle->GetURL())) {
    return;
  }

  CompleteCommandAndSelfDestruct(
      ManifestUpdateCheckResult::kCancelledDueToMainFrameNavigation);
}

////////////////////////////////////////////////////////////////////////////////
// ManifestUpdateCheckStage::kDownloadingNewManifestData:
////////////////////////////////////////////////////////////////////////////////

void ManifestUpdateCheckCommand::DownloadNewManifestData(
    base::OnceClosure next_step_callback) {
  DCHECK_EQ(stage_, ManifestUpdateCheckStage::kPendingAppLock);
  stage_ = ManifestUpdateCheckStage::kDownloadingNewManifestData;

  RunChainedCallbacks(
      base::BindOnce(&ManifestUpdateCheckCommand::DownloadNewManifestJson,
                     GetWeakPtr()),

      base::BindOnce(&ManifestUpdateCheckCommand::StashNewManifestJson,
                     GetWeakPtr()),

      base::BindOnce(&ManifestUpdateCheckCommand::DownloadNewIconBitmaps,
                     GetWeakPtr()),

      base::BindOnce(&ManifestUpdateCheckCommand::StashNewIconBitmaps,
                     GetWeakPtr()),

      base::BindOnce(&ManifestUpdateCheckCommand::ValidateNewScopeExtensions,
                     GetWeakPtr()),

      base::BindOnce(&ManifestUpdateCheckCommand::StashValidatedScopeExtensions,
                     GetWeakPtr()),

      std::move(next_step_callback));
}

void ManifestUpdateCheckCommand::DownloadNewManifestJson(
    WebAppDataRetriever::CheckInstallabilityCallback next_step_callback) {
  DCHECK_EQ(stage_, ManifestUpdateCheckStage::kDownloadingNewManifestData);

  if (IsWebContentsDestroyed()) {
    CompleteCommandAndSelfDestruct(
        ManifestUpdateCheckResult::kWebContentsDestroyed);
    return;
  }

  webapps::InstallableParams params;
  params.valid_primary_icon = true;
  params.installable_criteria =
      webapps::InstallableCriteria::kValidManifestIgnoreDisplay;
  data_retriever_->CheckInstallabilityAndRetrieveManifest(
      web_contents_.get(), std::move(next_step_callback), params);
}

void ManifestUpdateCheckCommand::StashNewManifestJson(
    base::OnceClosure next_step_callback,
    blink::mojom::ManifestPtr opt_manifest,
    bool valid_manifest_for_web_app,
    webapps::InstallableStatusCode installable_status) {
  DCHECK_EQ(stage_, ManifestUpdateCheckStage::kDownloadingNewManifestData);

  GetMutableDebugValue().Set(
      "manifest_url", opt_manifest ? opt_manifest->manifest_url.spec() : "");
  GetMutableDebugValue().Set("manifest_installable_result",
                             base::ToString(installable_status));

  if (installable_status != webapps::InstallableStatusCode::NO_ERROR_DETECTED) {
    CompleteCommandAndSelfDestruct(ManifestUpdateCheckResult::kAppNotEligible);
    return;
  }
  DCHECK(opt_manifest);
  CHECK(!new_install_info_);

  new_install_info_ = std::make_unique<WebAppInstallInfo>(
      CreateWebAppInfoFromManifest(*opt_manifest));

  if (app_id_ !=
      GenerateAppIdFromManifestId(new_install_info_->manifest_id())) {
    CompleteCommandAndSelfDestruct(ManifestUpdateCheckResult::kAppIdMismatch);
    return;
  }

  std::move(next_step_callback).Run();
}

void ManifestUpdateCheckCommand::DownloadNewIconBitmaps(
    WebAppIconDownloader::WebAppIconDownloaderCallback next_step_callback) {
  DCHECK_EQ(stage_, ManifestUpdateCheckStage::kDownloadingNewManifestData);

  if (IsWebContentsDestroyed()) {
    CompleteCommandAndSelfDestruct(
        ManifestUpdateCheckResult::kWebContentsDestroyed);
    return;
  }

  CHECK(new_install_info_);
  IconUrlSizeSet icon_urls = GetValidIconUrlsToDownload(*new_install_info_);

  IconDownloaderOptions options = {.skip_page_favicons = true,
                                   .fail_all_if_any_fail = true};
  icon_downloader_->Start(web_contents_.get(), icon_urls,
                          std::move(next_step_callback), options);
}

void ManifestUpdateCheckCommand::StashNewIconBitmaps(
    base::OnceClosure next_step_callback,
    IconsDownloadedResult result,
    IconsMap icons_map,
    DownloadedIconsHttpResults icons_http_results) {
  DCHECK_EQ(stage_, ManifestUpdateCheckStage::kDownloadingNewManifestData);

  GetMutableDebugValue().Set("icon_download_result", base::ToString(result));

  RecordIconDownloadMetrics(result, icons_http_results);

  if (result != IconsDownloadedResult::kCompleted) {
    CompleteCommandAndSelfDestruct(
        ManifestUpdateCheckResult::kIconDownloadFailed);
    return;
  }

  PopulateOtherIcons(new_install_info_.get(), icons_map);
  PopulateProductIcons(new_install_info_.get(), &icons_map);

  std::move(next_step_callback).Run();
}

void ManifestUpdateCheckCommand::ValidateNewScopeExtensions(
    OnDidGetWebAppOriginAssociations next_step_callback) {
  DCHECK_EQ(stage_, ManifestUpdateCheckStage::kDownloadingNewManifestData);

  if (IsWebContentsDestroyed()) {
    CompleteCommandAndSelfDestruct(
        ManifestUpdateCheckResult::kWebContentsDestroyed);
    return;
  }

  CHECK(new_install_info_);
  ScopeExtensions new_scope_extensions = new_install_info_->scope_extensions;

  lock_->origin_association_manager().GetWebAppOriginAssociations(
      new_install_info_->manifest_id(), std::move(new_scope_extensions),
      std::move(next_step_callback));
}

void ManifestUpdateCheckCommand::StashValidatedScopeExtensions(
    base::OnceClosure next_step_callback,
    ScopeExtensions validated_scope_extensions) {
  DCHECK_EQ(stage_, ManifestUpdateCheckStage::kDownloadingNewManifestData);

  if (IsWebContentsDestroyed()) {
    CompleteCommandAndSelfDestruct(
        ManifestUpdateCheckResult::kWebContentsDestroyed);
    return;
  }

  new_install_info_->validated_scope_extensions =
      std::make_optional(std::move(validated_scope_extensions));
  std::move(next_step_callback).Run();
}

////////////////////////////////////////////////////////////////////////////////
// ManifestUpdateCheckStage::kLoadingExistingManifestData:
////////////////////////////////////////////////////////////////////////////////

void ManifestUpdateCheckCommand::LoadExistingManifestData(
    base::OnceClosure next_step_callback) {
  DCHECK_EQ(stage_, ManifestUpdateCheckStage::kDownloadingNewManifestData);
  stage_ = ManifestUpdateCheckStage::kLoadingExistingManifestData;

  RunChainedCallbacks(
      base::BindOnce(&ManifestUpdateCheckCommand::LoadExistingAppIcons,
                     GetWeakPtr()),

      base::BindOnce(&ManifestUpdateCheckCommand::StashExistingAppIcons,
                     GetWeakPtr()),

      base::BindOnce(
          &ManifestUpdateCheckCommand::LoadExistingShortcutsMenuIcons,
          GetWeakPtr()),

      base::BindOnce(
          &ManifestUpdateCheckCommand::StashExistingShortcutsMenuIcons,
          GetWeakPtr()),

      std::move(next_step_callback));
}

void ManifestUpdateCheckCommand::LoadExistingAppIcons(
    WebAppIconManager::ReadIconBitmapsCallback next_step_callback) {
  DCHECK_EQ(stage_, ManifestUpdateCheckStage::kLoadingExistingManifestData);

  lock_->icon_manager().ReadAllIcons(app_id_, std::move(next_step_callback));
}

void ManifestUpdateCheckCommand::StashExistingAppIcons(
    base::OnceClosure next_step_callback,
    IconBitmaps icon_bitmaps) {
  DCHECK_EQ(stage_, ManifestUpdateCheckStage::kLoadingExistingManifestData);

  if (icon_bitmaps.empty()) {
    CompleteCommandAndSelfDestruct(
        ManifestUpdateCheckResult::kIconReadFromDiskFailed);
    return;
  }

  existing_app_icon_bitmaps_ = std::move(icon_bitmaps);
  std::move(next_step_callback).Run();
}

void ManifestUpdateCheckCommand::LoadExistingShortcutsMenuIcons(
    WebAppIconManager::ReadShortcutsMenuIconsCallback next_step_callback) {
  DCHECK_EQ(stage_, ManifestUpdateCheckStage::kLoadingExistingManifestData);

  lock_->icon_manager().ReadAllShortcutsMenuIcons(
      app_id_, std::move(next_step_callback));
}

void ManifestUpdateCheckCommand::StashExistingShortcutsMenuIcons(
    base::OnceClosure next_step_callback,
    ShortcutsMenuIconBitmaps shortcuts_menu_icon_bitmaps) {
  DCHECK_EQ(stage_, ManifestUpdateCheckStage::kLoadingExistingManifestData);

  existing_shortcuts_menu_icon_bitmaps_ =
      std::move(shortcuts_menu_icon_bitmaps);
  std::move(next_step_callback).Run();
}

////////////////////////////////////////////////////////////////////////////////
// ManifestUpdateCheckStage::kComparingManifestData:
////////////////////////////////////////////////////////////////////////////////

void ManifestUpdateCheckCommand::CompareManifestData(
    base::OnceClosure next_step_callback) {
  DCHECK_EQ(stage_, ManifestUpdateCheckStage::kLoadingExistingManifestData);
  stage_ = ManifestUpdateCheckStage::kComparingManifestData;

  const WebApp* web_app = lock_->registrar().GetAppById(app_id_);
  DCHECK(web_app);

  CHECK(new_install_info_);
  manifest_data_changes_ = GetManifestDataChanges(
      GetWebApp(), &existing_app_icon_bitmaps_,
      &existing_shortcuts_menu_icon_bitmaps_, *new_install_info_);

  std::move(next_step_callback).Run();
}

////////////////////////////////////////////////////////////////////////////////
// ManifestUpdateCheckStage::kResolvingIdentityChanges:
////////////////////////////////////////////////////////////////////////////////

void ManifestUpdateCheckCommand::ResolveIdentityChanges(
    base::OnceClosure next_step_callback) {
  DCHECK_EQ(stage_, ManifestUpdateCheckStage::kComparingManifestData);
  stage_ = ManifestUpdateCheckStage::kResolvingIdentityChanges;

  if (manifest_data_changes_.app_name_changed) {
    manifest_data_changes_.app_name_identity_update_decision =
        MakeAppNameIdentityUpdateDecision();
  }
  if (manifest_data_changes_.app_icon_identity_change) {
    manifest_data_changes_.app_icon_identity_update_decision =
        MakeAppIconIdentityUpdateDecision();
  }

  // Record metrics prior to reverts to capture attempts to change name/icons.
  RecordIdentityConfirmationMetrics(manifest_data_changes_, GetWebApp());

  // Apply reverts before showing the confirmation dialog to avoid showing
  // rejected changes in the preview.
  RevertIdentityChangesIfNeeded();

  if (manifest_data_changes_.RequiresConfirmation()) {
    ConfirmAppIdentityUpdate(std::move(next_step_callback));
    return;
  }

  std::move(next_step_callback).Run();
}

IdentityUpdateDecision
ManifestUpdateCheckCommand::MakeAppNameIdentityUpdateDecision() const {
  DCHECK_EQ(stage_, ManifestUpdateCheckStage::kResolvingIdentityChanges);
  DCHECK(manifest_data_changes_.app_name_changed);

  const WebApp& web_app = GetWebApp();
  if (CanWebAppSilentlyUpdateIdentity(web_app)) {
    return IdentityUpdateDecision::kSilentlyAllow;
  }

  if (CanShowIdentityUpdateConfirmationDialog(lock_->registrar(), web_app)) {
    return IdentityUpdateDecision::kGetUserConfirmation;
  }

  return IdentityUpdateDecision::kRevert;
}

IdentityUpdateDecision
ManifestUpdateCheckCommand::MakeAppIconIdentityUpdateDecision() const {
  DCHECK_EQ(stage_, ManifestUpdateCheckStage::kResolvingIdentityChanges);
  DCHECK(manifest_data_changes_.app_icon_identity_change);

  const WebApp& web_app = GetWebApp();
  if (CanWebAppSilentlyUpdateIdentity(web_app) ||
      base::FeatureList::IsEnabled(features::kWebAppManifestIconUpdating)) {
    return IdentityUpdateDecision::kSilentlyAllow;
  }

  // Web apps that were installed by sync but have generated icons get a window
  // of time where they can "fix" themselves silently to use the site provided
  // icons.
  if (web_app.is_generated_icon() &&
      web_app.latest_install_source() == webapps::WebappInstallSource::SYNC &&
      generated_icon_fix_util::IsWithinFixTimeWindow(web_app)) {
    ScopedRegistryUpdate update = lock_->sync_bridge().BeginUpdate();
    generated_icon_fix_util::EnsureFixTimeWindowStarted(
        *lock_, update, app_id_,
        proto::GENERATED_ICON_FIX_SOURCE_MANIFEST_UPDATE);
    return IdentityUpdateDecision::kSilentlyAllow;
  }

  if (CanShowIdentityUpdateConfirmationDialog(lock_->registrar(), web_app) &&
      base::FeatureList::IsEnabled(features::kPwaUpdateDialogForIcon)) {
    return IdentityUpdateDecision::kGetUserConfirmation;
  }

  return IdentityUpdateDecision::kRevert;
}

void ManifestUpdateCheckCommand::ConfirmAppIdentityUpdate(
    base::OnceClosure next_step_callback) {
  DCHECK_EQ(stage_, ManifestUpdateCheckStage::kResolvingIdentityChanges);

  DCHECK(
      CanShowIdentityUpdateConfirmationDialog(lock_->registrar(), GetWebApp()));
  DCHECK(manifest_data_changes_.RequiresConfirmation());

  if (IsWebContentsDestroyed()) {
    CompleteCommandAndSelfDestruct(
        ManifestUpdateCheckResult::kWebContentsDestroyed);
    return;
  }

  const SkBitmap* before_icon = nullptr;
  const SkBitmap* after_icon = nullptr;
  if (manifest_data_changes_.app_icon_identity_change) {
    before_icon = &manifest_data_changes_.app_icon_identity_change->before;
    after_icon = &manifest_data_changes_.app_icon_identity_change->after;
  } else {
    // Even though this is just a name update an icon still needs to be shown
    // to the user, find an existing icon to show in the before/after.
    for (SquareSizePx size : kIdentitySizes) {
      auto it = existing_app_icon_bitmaps_.any.find(size);
      if (it != existing_app_icon_bitmaps_.any.end()) {
        before_icon = &it->second;
        after_icon = before_icon;
        break;
      }
    }
    // TODO(crbug.com/40254036): Try other sizes if the above sizes fail.
  }

  if (before_icon == nullptr || after_icon == nullptr ||
      before_icon->drawsNothing() || after_icon->drawsNothing()) {
    OnIdentityUpdateConfirmationComplete(std::move(next_step_callback),
                                         AppIdentityUpdate::kSkipped);
    return;
  }

  lock_->ui_manager().ShowWebAppIdentityUpdateDialog(
      app_id_,
      /*title_change=*/manifest_data_changes_.app_name_changed,
      /*icon_change=*/
      manifest_data_changes_.app_icon_identity_change.has_value(),
      /*old_title=*/base::UTF8ToUTF16(GetWebApp().untranslated_name()),
      /*new_title=*/new_install_info_->title,
      /*old_icon=*/*before_icon,
      /*new_icon=*/*after_icon, web_contents_.get(),
      base::BindOnce(
          &ManifestUpdateCheckCommand::OnIdentityUpdateConfirmationComplete,
          GetWeakPtr(), std::move(next_step_callback)));
}

void ManifestUpdateCheckCommand::OnIdentityUpdateConfirmationComplete(
    base::OnceClosure next_step_callback,
    AppIdentityUpdate app_identity_update) {
  DCHECK_EQ(stage_, ManifestUpdateCheckStage::kResolvingIdentityChanges);

  switch (app_identity_update) {
    case AppIdentityUpdate::kAllowed:
      break;

    case AppIdentityUpdate::kUninstall:
      CompleteCommandAndSelfDestruct(
          ManifestUpdateCheckResult::kAppIdentityUpdateRejectedAndUninstalled);
      return;

    case AppIdentityUpdate::kSkipped: {
      manifest_data_changes_.app_name_identity_update_decision =
          IdentityUpdateDecision::kRevert;
      manifest_data_changes_.app_icon_identity_update_decision =
          IdentityUpdateDecision::kRevert;
      RevertIdentityChangesIfNeeded();
      break;
    }
  }

  std::move(next_step_callback).Run();
}

void ManifestUpdateCheckCommand::RevertIdentityChangesIfNeeded() {
  if (manifest_data_changes_.app_name_identity_update_decision ==
          IdentityUpdateDecision::kRevert &&
      manifest_data_changes_.app_name_changed) {
    // Revert to WebApp::untranslated_name() instead of
    // WebAppRegistrar::GetAppShortName() because that's the field
    // WebAppInstallInfo::title gets written to (see SetWebAppManifestFields()).
    new_install_info_->title =
        base::UTF8ToUTF16(GetWebApp().untranslated_name());
    manifest_data_changes_.app_name_changed = false;
  }

  if (manifest_data_changes_.app_icon_identity_update_decision ==
          IdentityUpdateDecision::kRevert &&
      manifest_data_changes_.app_icon_identity_change) {
    const WebApp& web_app = GetWebApp();
    // TODO(crbug.com/40282537): Bundle up product icon data into a single
    // struct to make this a single assignment and less likely to miss fields as
    // they get added in future.
    new_install_info_->manifest_icons = web_app.manifest_icons();
    new_install_info_->icon_bitmaps = existing_app_icon_bitmaps_;
    new_install_info_->is_generated_icon = web_app.is_generated_icon();
    new_install_info_->generated_icon_fix = web_app.generated_icon_fix();
    manifest_data_changes_.app_icon_identity_change.reset();
    manifest_data_changes_.any_app_icon_changed = false;
  }
}

////////////////////////////////////////////////////////////////////////////////
// ManifestUpdateCheckStage::kComplete:
////////////////////////////////////////////////////////////////////////////////

void ManifestUpdateCheckCommand::CheckComplete() {
  DCHECK_EQ(stage_, ManifestUpdateCheckStage::kResolvingIdentityChanges);
  stage_ = ManifestUpdateCheckStage::kComplete;

  ManifestUpdateCheckResult check_result =
      manifest_data_changes_ ? ManifestUpdateCheckResult::kAppUpdateNeeded
                             : ManifestUpdateCheckResult::kAppUpToDate;
  CompleteCommandAndSelfDestruct(check_result);
}

const WebApp& ManifestUpdateCheckCommand::GetWebApp() const {
  const WebApp* web_app = lock_->registrar().GetAppById(app_id_);
  DCHECK(web_app);
  return *web_app;
}

bool ManifestUpdateCheckCommand::IsWebContentsDestroyed() {
  return !web_contents_ || web_contents_->IsBeingDestroyed();
}

void ManifestUpdateCheckCommand::CompleteCommandAndSelfDestruct(
    ManifestUpdateCheckResult check_result) {
  GetMutableDebugValue().Set("result", base::ToString(check_result));

  CommandResult command_result = [&] {
    switch (check_result) {
      case ManifestUpdateCheckResult::kAppUpdateNeeded:
      case ManifestUpdateCheckResult::kAppIdentityUpdateRejectedAndUninstalled:
      case ManifestUpdateCheckResult::kAppUpToDate:
        return CommandResult::kSuccess;
      case ManifestUpdateCheckResult::kAppIdMismatch:
      case ManifestUpdateCheckResult::kAppNotEligible:
      case ManifestUpdateCheckResult::kIconDownloadFailed:
      case ManifestUpdateCheckResult::kIconReadFromDiskFailed:
      case ManifestUpdateCheckResult::kWebContentsDestroyed:
      case ManifestUpdateCheckResult::kCancelledDueToMainFrameNavigation:
        return CommandResult::kFailure;
      case ManifestUpdateCheckResult::kSystemShutdown:
        NOTREACHED() << "This should be handled by OnShutdown()";
    }
  }();

  Observe(nullptr);
  CompleteAndSelfDestruct(
      command_result, check_result,
      check_result == ManifestUpdateCheckResult::kAppUpdateNeeded
          ? std::move(new_install_info_)
          : nullptr);
}

}  // namespace web_app