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_
|