File: generated_icon_fix_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 (149 lines) | stat: -rw-r--r-- 5,759 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
// 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/generated_icon_fix_command.h"

#include <memory>
#include <string>

#include "base/check.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/web_applications/generated_icon_fix_util.h"
#include "chrome/browser/web_applications/locks/shared_web_contents_with_app_lock.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_command_manager.h"
#include "chrome/browser/web_applications/web_app_icon_generator.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_install_manager.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_contents/web_app_icon_downloader.h"
#include "chrome/browser/web_applications/web_contents/web_contents_manager.h"
#include "chrome/common/chrome_features.h"
#include "ui/gfx/geometry/size.h"

namespace web_app {

GeneratedIconFixCommand::GeneratedIconFixCommand(
    webapps::AppId app_id,
    proto::GeneratedIconFixSource source,
    base::OnceCallback<void(GeneratedIconFixResult)> callback)
    : WebAppCommand<SharedWebContentsWithAppLock, GeneratedIconFixResult>(
          "GeneratedIconFixCommand",
          SharedWebContentsWithAppLockDescription({app_id}),
          std::move(callback),
          GeneratedIconFixResult::kShutdown),
      app_id_(std::move(app_id)),
      source_(source) {
  GetMutableDebugValue().Set("app_id", app_id_);
  GetMutableDebugValue().Set("source", base::ToString(source_));
  GetMutableDebugValue().Set("stop_location", stop_location_.ToString());
}

GeneratedIconFixCommand::~GeneratedIconFixCommand() = default;

void GeneratedIconFixCommand::StartWithLock(
    std::unique_ptr<SharedWebContentsWithAppLock> lock) {
  lock_ = std::move(lock);

  const WebApp* app = lock_->registrar().GetAppById(app_id_);
  if (!app) {
    Stop(GeneratedIconFixResult::kAppUninstalled, FROM_HERE);
    return;
  }

  // DCHECK instead of CHECK to avoid crashing at start up.
  DCHECK(generated_icon_fix_util::HasRemainingAttempts(*app));

  {
    ScopedRegistryUpdate update = lock_->sync_bridge().BeginUpdate();
    generated_icon_fix_util::RecordFixAttempt(*lock_, update, app_id_, source_);
  }

  // Refresh WebApp pointer as the above mutation destroys the previous one.
  app = lock_->registrar().GetAppById(app_id_);

  install_info_ =
      std::make_unique<WebAppInstallInfo>(app->manifest_id(), app->start_url());
  icon_downloader_ = lock_->web_contents_manager().CreateIconDownloader();
  IconUrlSizeSet icon_urls;
  install_info_->manifest_icons = app->manifest_icons();
  // Set title and start_url for PopulateProductIcons() in case it tries to
  // generate icons again.
  install_info_->title = base::UTF8ToUTF16(app->untranslated_name());
  for (const apps::IconInfo& icon_info : install_info_->manifest_icons) {
    icon_urls.emplace(IconUrlWithSize::CreateForUnspecifiedSize(icon_info.url));
  }
  icon_downloader_->Start(
      &lock_->shared_web_contents(), icon_urls,
      base::BindOnce(&GeneratedIconFixCommand::OnIconsDownloaded,
                     weak_factory_.GetWeakPtr()));
}

void GeneratedIconFixCommand::OnIconsDownloaded(
    IconsDownloadedResult result,
    IconsMap icons_map,
    DownloadedIconsHttpResults icons_http_results) {
  if (result != IconsDownloadedResult::kCompleted) {
    Stop(GeneratedIconFixResult::kDownloadFailure, FROM_HERE);
    return;
  }

  PopulateProductIcons(install_info_.get(), &icons_map);
  if (install_info_->is_generated_icon) {
    Stop(GeneratedIconFixResult::kStillGenerated, FROM_HERE);
    return;
  }

  // Note: Empty params are noops, WriteData() never deletes icons.
  lock_->icon_manager().WriteData(
      app_id_, install_info_->icon_bitmaps, /*shortcuts_menu_icons=*/{},
      /*other_icons_map=*/{},
      base::BindOnce(&GeneratedIconFixCommand::OnIconsWritten,
                     weak_factory_.GetWeakPtr()));
}

void GeneratedIconFixCommand::OnIconsWritten(bool success) {
  if (!success) {
    Stop(GeneratedIconFixResult::kWriteFailure, FROM_HERE);
    return;
  }

  {
    ScopedRegistryUpdate update = lock_->sync_bridge().BeginUpdate();
    SetWebAppProductIconFields(*install_info_, *update->UpdateApp(app_id_));
  }
  lock_->install_manager().NotifyWebAppManifestUpdated(app_id_);
  Stop(GeneratedIconFixResult::kSuccess, FROM_HERE);
}

void GeneratedIconFixCommand::Stop(GeneratedIconFixResult result,
                                   base::Location location) {
  CHECK(result != GeneratedIconFixResult::kShutdown);
  stop_location_ = std::move(location);

  // TODO(crbug.com/40185008): Record the attempt and call
  // GeneratedIconFixManager::MaybeScheduleNextFix() if !success.

  CompleteAndSelfDestruct(
      [&] {
        switch (result) {
          case GeneratedIconFixResult::kDownloadFailure:
          case GeneratedIconFixResult::kStillGenerated:
          case GeneratedIconFixResult::kWriteFailure:
            return CommandResult::kFailure;
          case GeneratedIconFixResult::kAppUninstalled:
          case GeneratedIconFixResult::kShutdown:
          case GeneratedIconFixResult::kSuccess:
            return CommandResult::kSuccess;
        }
      }(),
      result);
}

}  // namespace web_app