File: web_app_install_finalizer.h

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 (216 lines) | stat: -rw-r--r-- 8,469 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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_INSTALL_FINALIZER_H_
#define CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_INSTALL_FINALIZER_H_

#include <map>
#include <memory>
#include <vector>

#include "base/containers/flat_set.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/clock.h"
#include "base/time/default_clock.h"
#include "build/build_config.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_integrity_block_data.h"
#include "chrome/browser/web_applications/os_integration/os_integration_manager.h"
#include "chrome/browser/web_applications/proto/web_app_install_state.pb.h"
#include "chrome/browser/web_applications/scope_extension_info.h"
#include "chrome/browser/web_applications/web_app_chromeos_data.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_management_type.h"
#include "components/webapps/browser/install_result_code.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "components/webapps/common/web_app_id.h"
#include "third_party/skia/include/core/SkBitmap.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/ash/experiences/system_web_apps/types/system_web_app_data.h"
#endif

class Profile;

namespace webapps {
enum class UninstallResultCode;
enum class WebappUninstallSource;
}  // namespace webapps

namespace web_app {

class IsolatedWebAppStorageLocation;
class WebApp;
class WebAppProvider;

// An finalizer for the installation process, represents the last step.
// Takes WebAppInstallInfo as input, writes data to disk (e.g icons, shortcuts)
// and registers an app.
class WebAppInstallFinalizer {
 public:
  using InstallFinalizedCallback =
      base::OnceCallback<void(const webapps::AppId& app_id,
                              webapps::InstallResultCode code)>;
  using UninstallWebAppCallback =
      base::OnceCallback<void(webapps::UninstallResultCode code)>;
  using RepeatingUninstallCallback =
      base::RepeatingCallback<void(const webapps::AppId& app_id,
                                   webapps::UninstallResultCode code)>;

  struct FinalizeOptions {
    struct IwaOptions {
      IwaOptions(
          IsolatedWebAppStorageLocation location,
          std::optional<IsolatedWebAppIntegrityBlockData> integrity_block_data);
      ~IwaOptions();
      IwaOptions(const IwaOptions&);

      IsolatedWebAppStorageLocation location;
      std::optional<IsolatedWebAppIntegrityBlockData> integrity_block_data;
    };

    explicit FinalizeOptions(webapps::WebappInstallSource install_surface);
    ~FinalizeOptions();
    FinalizeOptions(const FinalizeOptions&);

    const WebAppManagement::Type source;
    const webapps::WebappInstallSource install_surface;
    proto::InstallState install_state =
        proto::InstallState::INSTALLED_WITH_OS_INTEGRATION;
    bool overwrite_existing_manifest_fields = true;
    bool skip_icon_writes_on_download_failure = false;

    std::optional<WebAppChromeOsData> chromeos_data;
#if BUILDFLAG(IS_CHROMEOS)
    std::optional<ash::SystemWebAppData> system_web_app_data;
#endif

    // If set, will propagate `IsolatedWebAppStorageLocation` and
    // `IntegrityBlockData` to `WebApp::isolation_data()` with the given values,
    // as well as the version from
    // `WebAppInstallInfo::isolated_web_app_version`. Will `CHECK` if
    // `web_app_info.isolated_web_app_version` is invalid.
    std::optional<IwaOptions> iwa_options;

    // These are required to be false if `install_state` is not
    // proto::INSTALLED_WITH_OS_INTEGRATION.
    bool add_to_applications_menu = true;
    bool add_to_desktop = true;
    bool add_to_quick_launch_bar = true;

    // Controls fetching and validation of web_app_origin_association data
    // from web origins found in manifest scope_extensions entries. If true,
    // do not validate even if scope_extensions has valid entries.
    bool skip_origin_association_validation = false;
  };

  static bool& DisableUserDisplayModeSyncMitigationsForTesting();

  explicit WebAppInstallFinalizer(Profile* profile);
  WebAppInstallFinalizer(const WebAppInstallFinalizer&) = delete;
  WebAppInstallFinalizer& operator=(const WebAppInstallFinalizer&) = delete;
  virtual ~WebAppInstallFinalizer();

  // Write the WebApp data to disk and register the app.
  void FinalizeInstall(const WebAppInstallInfo& web_app_info,
                       const FinalizeOptions& options,
                       InstallFinalizedCallback callback);

  // Write the new WebApp data to disk and update the app.
  // TODO(crbug.com/40759394): Chrome fails to update the manifest
  // if the app window needing update closes at the same time as Chrome.
  // Therefore, the manifest may not always update as expected.
  // Virtual for testing.
  virtual void FinalizeUpdate(const WebAppInstallInfo& web_app_info,
                              InstallFinalizedCallback callback);

  void SetProvider(base::PassKey<WebAppProvider>, WebAppProvider& provider);
  void Start();
  void Shutdown();

  Profile* profile() { return profile_; }

  // Writes external config data to the web_app DB, mapped per source.
  void WriteExternalConfigMapInfo(
      WebApp& web_app,
      WebAppManagement::Type source,
      bool is_placeholder,
      GURL install_url,
      std::vector<std::string> additional_policy_ids);

  void SetClockForTesting(base::Clock* clock);

 private:
  using CommitCallback = base::OnceCallback<void(bool success)>;

  void UpdateIsolationDataAndResetPendingUpdateInfo(
      WebApp* web_app,
      const IsolatedWebAppStorageLocation& location,
      const base::Version& version,
      std::optional<IsolatedWebAppIntegrityBlockData> integrity_block_data);

  void SetWebAppManifestFieldsAndWriteData(
      const WebAppInstallInfo& web_app_info,
      std::unique_ptr<WebApp> web_app,
      CommitCallback commit_callback,
      bool skip_icon_writes_on_download_failure);

  void WriteTranslations(
      const webapps::AppId& app_id,
      const base::flat_map<std::string, blink::Manifest::TranslationItem>&
          translations,
      CommitCallback commit_callback,
      bool success);

  void CommitToSyncBridge(std::unique_ptr<WebApp> web_app,
                          CommitCallback commit_callback,
                          bool success);

  void OnOriginAssociationValidated(WebAppInstallInfo web_app_info,
                                    FinalizeOptions options,
                                    InstallFinalizedCallback callback,
                                    webapps::AppId app_id,
                                    ScopeExtensions validated_scope_extensions);

  void OnDatabaseCommitCompletedForInstall(InstallFinalizedCallback callback,
                                           webapps::AppId app_id,
                                           FinalizeOptions finalize_options,
                                           bool success);

  void OnInstallHooksFinished(InstallFinalizedCallback callback,
                              webapps::AppId app_id);
  void NotifyWebAppInstalledWithOsHooks(webapps::AppId app_id);

  void OnDatabaseCommitCompletedForUpdate(
      InstallFinalizedCallback callback,
      webapps::AppId app_id,
      std::string old_name,
      FileHandlerUpdateAction file_handlers_need_os_update,
      const WebAppInstallInfo& web_app_info,
      bool success);

  void OnUpdateHooksFinished(InstallFinalizedCallback callback,
                             webapps::AppId app_id);

  // Returns a value indicating whether the file handlers registered with the OS
  // should be updated. Used to avoid unnecessary updates. TODO(estade): why
  // does this optimization exist when other OS hooks don't have similar
  // optimizations?
  FileHandlerUpdateAction GetFileHandlerUpdateAction(
      const webapps::AppId& app_id,
      const WebAppInstallInfo& new_web_app_info);

  const raw_ptr<Profile> profile_;
  raw_ptr<WebAppProvider> provider_ = nullptr;
  raw_ptr<base::Clock> clock_{base::DefaultClock::GetInstance()};

  bool started_ = false;

  base::WeakPtrFactory<WebAppInstallFinalizer> weak_ptr_factory_{this};
};

}  // namespace web_app

#endif  // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_INSTALL_FINALIZER_H_