File: webapk_restore_task.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 (128 lines) | stat: -rw-r--r-- 4,535 bytes parent folder | download | duplicates (3)
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
// 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.

#ifndef CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_RESTORE_TASK_H_
#define CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_RESTORE_TASK_H_

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/types/pass_key.h"
#include "components/webapps/browser/android/add_to_homescreen_data_fetcher.h"
#include "components/webapps/browser/android/shortcut_info.h"
#include "components/webapps/common/web_app_id.h"
#include "third_party/skia/include/core/SkBitmap.h"

class WebApkInstallService;

namespace webapps {
enum class InstallableStatusCode;
enum class WebAppUrlLoaderResult;
enum class WebApkInstallResult;
}  // namespace webapps

namespace webapk {

class WebApkRestoreManager;
class WebApkRestoreWebContentsManager;

struct WebApkRestoreData {
  WebApkRestoreData() = delete;
  explicit WebApkRestoreData(
      webapps::AppId app_id,
      std::unique_ptr<webapps::ShortcutInfo> shortcut_info,
      base::Time);
  ~WebApkRestoreData();
  WebApkRestoreData(WebApkRestoreData&& other);
  WebApkRestoreData(const WebApkRestoreData&) = delete;
  WebApkRestoreData& operator=(const WebApkRestoreData&) = delete;

  webapps::AppId app_id;
  // Fallback shortcut info
  std::unique_ptr<webapps::ShortcutInfo> shortcut_info;
  // Time when this WebApk was last used or installed
  base::Time last_used_time;
};

// Task for installing previously synced WebAPK on new devices. Each instance
// represents a WebAPK to be install.
class WebApkRestoreTask : public webapps::AddToHomescreenDataFetcher::Observer {
 public:
  explicit WebApkRestoreTask(
      base::PassKey<WebApkRestoreManager>,
      WebApkInstallService* web_apk_install_service,
      WebApkRestoreWebContentsManager* web_contents_manager,
      std::unique_ptr<webapps::ShortcutInfo> fallback_info,
      base::Time last_used_time);

  WebApkRestoreTask(const WebApkRestoreTask&) = delete;
  WebApkRestoreTask& operator=(const WebApkRestoreTask&) = delete;
  ~WebApkRestoreTask() override;

  using CompleteCallback =
      base::OnceCallback<void(const GURL&, webapps::WebApkInstallResult)>;

  // LINT.IfChange(WebApkRestoreFallbackReason)
  enum class FallbackReason {
    kNone = 0,
    kLoadUrl = 1,
    kManifestIdMismatch = 2,
    kNotWebApkCompatible = 3,
    kMaxValue = kNotWebApkCompatible,
  };
  // LINT.ThenChange(/tools/metrics/histograms/metadata/web_apk/enums.xml:WebApkRestoreFallbackReason)

  virtual void Start(CompleteCallback complete_callback);

  void DownloadIcon(base::OnceClosure fetch_icon_callback);

  GURL manifest_id() const;
  std::u16string app_name() const;
  const SkBitmap& app_icon() const { return app_icon_; }
  base::Time last_used_time() const { return last_used_time_; }

 private:
  void OnIconDownloaded(base::OnceClosure fetch_icon_callback,
                        const SkBitmap& bitmap);
  void OnIconCreated(base::OnceClosure fetch_icon_callback,
                     const SkBitmap& bitmap,
                     bool is_generated);

  void OnWebAppUrlLoaded(webapps::WebAppUrlLoaderResult result);

  // AddToHomescreenDataFetcher::Observer:
  void OnUserTitleAvailable(
      const std::u16string& user_title,
      const GURL& url,
      webapps::AddToHomescreenParams::AppType app_type) override;
  void OnDataAvailable(const webapps::ShortcutInfo& info,
                       const SkBitmap& display_icon,
                       webapps::AddToHomescreenParams::AppType app_type,
                       webapps::InstallableStatusCode status_code) override;

  std::unique_ptr<webapps::ShortcutInfo> UpdateFetchedInfoWithFallbackInfo(
      const webapps::ShortcutInfo& fetched_info);

  void Install(const webapps::ShortcutInfo& restore_info,
               const SkBitmap& display_icon,
               FallbackReason fallback_reason);
  void OnFinishedInstall(bool is_fallback, webapps::WebApkInstallResult result);

  raw_ptr<WebApkInstallService> web_apk_install_service_;
  base::WeakPtr<WebApkRestoreWebContentsManager> web_contents_manager_;

  CompleteCallback complete_callback_;

  std::unique_ptr<webapps::ShortcutInfo> fallback_info_;
  SkBitmap app_icon_;
  base::Time last_used_time_;

  std::unique_ptr<webapps::AddToHomescreenDataFetcher> data_fetcher_;

  base::WeakPtrFactory<WebApkRestoreTask> weak_factory_{this};
};

}  // namespace webapk

#endif  // CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_RESTORE_TASK_H_