File: external_app_resolution_command.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 (188 lines) | stat: -rw-r--r-- 7,718 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
// 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.

#ifndef CHROME_BROWSER_WEB_APPLICATIONS_COMMANDS_EXTERNAL_APP_RESOLUTION_COMMAND_H_
#define CHROME_BROWSER_WEB_APPLICATIONS_COMMANDS_EXTERNAL_APP_RESOLUTION_COMMAND_H_

#include <memory>
#include <optional>
#include <vector>

#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/web_applications/commands/web_app_command.h"
#include "chrome/browser/web_applications/external_install_options.h"
#include "chrome/browser/web_applications/externally_managed_app_manager.h"
#include "chrome/browser/web_applications/install_bounce_metric.h"
#include "chrome/browser/web_applications/jobs/install_from_info_job.h"
#include "chrome/browser/web_applications/jobs/install_placeholder_job.h"
#include "chrome/browser/web_applications/jobs/uninstall/remove_install_source_job.h"
#include "chrome/browser/web_applications/locks/all_apps_lock.h"
#include "chrome/browser/web_applications/locks/shared_web_contents_lock.h"
#include "chrome/browser/web_applications/locks/shared_web_contents_with_app_lock.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_install_utils.h"
#include "chrome/browser/web_applications/web_app_logging.h"
#include "components/services/app_service/public/cpp/app_launch_util.h"
#include "components/webapps/browser/installable/installable_logging.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "components/webapps/browser/uninstall_result_code.h"

class Browser;
class Profile;

namespace content {
class WebContents;
}

namespace webapps {
class WebAppUrlLoader;
enum class WebAppUrlLoaderResult;
}  // namespace webapps

namespace web_app {

class InstallPlaceholderJob;
class WebAppDataRetriever;
class WebAppUninstallAndReplaceJob;

struct WebAppInstallInfo;

// Invariant: This command assumes that a new placeholder app for this
// install_url cannot be installed between the scheduling and running of this
// command.
class ExternalAppResolutionCommand
    : public WebAppCommand<SharedWebContentsLock,
                           ExternallyManagedAppManagerInstallResult> {
 public:
  using InstallResult = ExternallyManagedAppManagerInstallResult;
  using InstalledCallback = base::OnceCallback<void(InstallResult)>;

  ExternalAppResolutionCommand(Profile& profile,
                               const ExternalInstallOptions& install_options,
                               std::optional<webapps::AppId> placeholder_app_id,
                               InstalledCallback installed_callback);
  ~ExternalAppResolutionCommand() override;

  void SetDataRetrieverForTesting(
      std::unique_ptr<WebAppDataRetriever> data_retriever);
  void SetOnLockUpgradedCallbackForTesting(base::OnceClosure callback);

  // WebAppCommand:
  void OnShutdown(base::PassKey<WebAppCommandManager>) const override;

 protected:
  // WebAppCommand:
  void StartWithLock(std::unique_ptr<SharedWebContentsLock> lock) override;

 private:
  WebAppProvider& provider() const;

  void Abort(webapps::InstallResultCode code);

  // This function loads the URL and based on the result branches off to try
  // either
  // * regular web app installation (if loading was successful).
  // * placeholder installation (in case loading was successful but the user
  // agent got redirected to a different origin and a placeholder installation
  // is configured as fallback).
  // * offline installation from install info (in all other cases).
  void OnUrlLoadedAndBranchInstallation(webapps::WebAppUrlLoaderResult result);

  // Regular installation path:
  void OnGetWebAppInstallInfoInCommand(
      std::unique_ptr<WebAppInstallInfo> web_app_info);
  void OnDidPerformInstallableCheck(blink::mojom::ManifestPtr opt_manifest,
                                    bool valid_manifest_for_web_app,
                                    webapps::InstallableStatusCode error_code);
  void OnPreparedForIconRetrieving(IconUrlSizeSet icon_urls,
                                   bool skip_page_favicons,
                                   webapps::WebAppUrlLoaderResult result);
  void OnIconsRetrievedUpgradeLockDescription(
      IconsDownloadedResult result,
      IconsMap icons_map,
      DownloadedIconsHttpResults icons_http_results);
  void OnLockUpgradedFinalizeInstall(bool icon_download_failed);
  void OnInstallFinalized(const webapps::AppId& app_id,
                          webapps::InstallResultCode code);
  void OnUninstallAndReplaceCompletedUninstallPlaceholder(
      bool uninstall_triggered);
  void OnAllAppsLockGrantedRemovePlaceholder();
  void OnPlaceholderUninstalledMaybeRelaunch(
      webapps::UninstallResultCode result);

  void OnLaunch(base::WeakPtr<Browser> browser,
                base::WeakPtr<content::WebContents> web_contents,
                apps::LaunchContainer container,
                base::Value debug_value);

  // Placeholder installation path:
  void OnPlaceHolderAppLockAcquired();
  void OnPlaceHolderInstalled(webapps::InstallResultCode code,
                              webapps::AppId app_id);

  // Offline installation path:
  void InstallFromInfo();
  void OnInstallFromInfoAppLockAcquired();
  void OnInstallFromInfoCompleted(webapps::AppId app_id,
                                  webapps::InstallResultCode code);
  void OnUninstallAndReplaceCompleted(bool is_offline_install,
                                      bool uninstall_triggered);

  void TryAppInfoFactoryOnFailure(
      ExternallyManagedAppManager::InstallResult result);

  ExternallyManagedAppManager::InstallResult PrepareResult(
      bool is_offline_install,
      ExternallyManagedAppManager::InstallResult result);

  // SharedWebContentsLock is held while loading the app contents (and manifest,
  // if possible).
  std::unique_ptr<SharedWebContentsLock> web_contents_lock_;

  // SharedWebContentsWithAppLock is held when the affected app ids are known
  // (i.e. after deciding whether a placeholder install is needed or when the
  // manifest is loaded).
  std::unique_ptr<SharedWebContentsWithAppLock> apps_lock_;

  std::unique_ptr<AllAppsLock> all_apps_lock_;
  std::unique_ptr<AllAppsLockDescription> all_apps_lock_description_;

  // Populated after an install completes.
  webapps::AppId app_id_;
  webapps::InstallResultCode install_code_;
  bool uninstalled_for_replace_ = false;
  bool relaunch_app_after_placeholder_uninstall_ = false;

  // `this` must be owned by `profile_`.
  raw_ref<Profile> profile_;

  raw_ptr<content::WebContents> web_contents_ = nullptr;
  std::unique_ptr<webapps::WebAppUrlLoader> url_loader_;
  std::unique_ptr<WebAppDataRetriever> data_retriever_;
  std::unique_ptr<WebAppInstallInfo> web_app_info_;

  ExternalInstallOptions install_options_;

  std::optional<webapps::AppId> installed_placeholder_app_id_;

  webapps::WebappInstallSource install_surface_;
  std::optional<WebAppInstallParams> install_params_;

  std::optional<WebAppUninstallAndReplaceJob> uninstall_and_replace_job_;
  std::optional<InstallPlaceholderJob> install_placeholder_job_;
  std::optional<InstallFromInfoJob> install_from_info_job_;
  std::optional<RemoveInstallSourceJob> remove_placeholder_job_;

  InstallErrorLogEntry install_error_log_entry_;

  base::OnceClosure on_lock_upgraded_callback_for_testing_;

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

}  // namespace web_app

#endif  // CHROME_BROWSER_WEB_APPLICATIONS_COMMANDS_EXTERNAL_APP_RESOLUTION_COMMAND_H_