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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
// Copyright 2019 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_TEST_WEB_APP_TEST_OBSERVERS_H_
#define CHROME_BROWSER_WEB_APPLICATIONS_TEST_WEB_APP_TEST_OBSERVERS_H_
#include <set>
#include "base/functional/callback.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "chrome/browser/web_applications/web_app_install_manager.h"
#include "chrome/browser/web_applications/web_app_install_manager_observer.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/browser/web_applications/web_app_registrar_observer.h"
#include "components/webapps/common/web_app_id.h"
class Profile;
namespace web_app {
class WebApp;
class WebAppInstallManagerObserverAdapter
: public WebAppInstallManagerObserver {
public:
explicit WebAppInstallManagerObserverAdapter(
WebAppInstallManager* install_manager);
explicit WebAppInstallManagerObserverAdapter(Profile* profile);
~WebAppInstallManagerObserverAdapter() override;
using WebAppInstalledDelegate =
base::RepeatingCallback<void(const webapps::AppId& app_id)>;
void SetWebAppInstalledDelegate(WebAppInstalledDelegate delegate);
using WebAppInstalledWithOsHooksDelegate =
base::RepeatingCallback<void(const webapps::AppId& app_id)>;
void SetWebAppInstalledWithOsHooksDelegate(
WebAppInstalledWithOsHooksDelegate delegate);
using WebAppWillBeUninstalledDelegate =
base::RepeatingCallback<void(const webapps::AppId& app_id)>;
void SetWebAppWillBeUninstalledDelegate(
WebAppWillBeUninstalledDelegate delegate);
using WebAppUninstalledDelegate =
base::RepeatingCallback<void(const webapps::AppId& app_id)>;
void SetWebAppUninstalledDelegate(WebAppUninstalledDelegate delegate);
using WebAppManifestUpdateDelegate =
base::RepeatingCallback<void(const webapps::AppId& app_id)>;
void SetWebAppManifestUpdateDelegate(WebAppManifestUpdateDelegate delegate);
using WebAppSourceRemovedDelegate =
base::RepeatingCallback<void(const webapps::AppId& app_id)>;
void SetWebAppSourceRemovedDelegate(WebAppSourceRemovedDelegate delegate);
void OnWebAppInstalled(const webapps::AppId& app_id) override;
void OnWebAppInstalledWithOsHooks(const webapps::AppId& app_id) override;
void OnWebAppManifestUpdated(const webapps::AppId& app_id) override;
void OnWebAppWillBeUninstalled(const webapps::AppId& app_id) override;
void OnWebAppUninstalled(
const webapps::AppId& app_id,
webapps::WebappUninstallSource uninstall_source) override;
void OnWebAppInstallManagerDestroyed() override;
void OnWebAppSourceRemoved(const webapps::AppId& app_id) override;
protected:
// Helper method for subclasses to allow easy waiting on `wait_loop_`.
// Expects that the users set `is_listening_` to `true` and
// optionally set `optional_app_ids_`.
void SignalRunLoopAndStoreAppId(const webapps::AppId& app_id);
bool is_listening_ = false;
std::set<webapps::AppId> optional_app_ids_;
base::RunLoop wait_loop_;
webapps::AppId last_app_id_;
WebAppInstalledDelegate app_installed_delegate_;
WebAppInstalledWithOsHooksDelegate app_installed_with_os_hooks_delegate_;
WebAppManifestUpdateDelegate app_manifest_updated_delegate_;
WebAppUninstalledDelegate app_uninstalled_delegate_;
private:
WebAppWillBeUninstalledDelegate app_will_be_uninstalled_delegate_;
WebAppSourceRemovedDelegate app_source_removed_delegate_;
base::ScopedObservation<WebAppInstallManager, WebAppInstallManagerObserver>
observation_{this};
protected:
base::WeakPtrFactory<WebAppInstallManagerObserverAdapter> weak_factory_{this};
};
// This is an adapter for the WebAppRegistrarObserver. This class registers
// itself as an observer on construction, and will call the respective
// delegates (if set) for all WebAppRegistrarObserver calls.
class WebAppTestRegistryObserverAdapter : public WebAppRegistrarObserver {
public:
explicit WebAppTestRegistryObserverAdapter(WebAppRegistrar* registrar);
explicit WebAppTestRegistryObserverAdapter(Profile* profile);
WebAppTestRegistryObserverAdapter(const WebAppTestRegistryObserverAdapter&) =
delete;
WebAppTestRegistryObserverAdapter& operator=(
const WebAppTestRegistryObserverAdapter&) = delete;
~WebAppTestRegistryObserverAdapter() override;
using WebAppWillBeUpdatedFromSyncDelegate = base::RepeatingCallback<void(
const std::vector<const WebApp*>& new_apps_state)>;
void SetWebAppWillBeUpdatedFromSyncDelegate(
WebAppWillBeUpdatedFromSyncDelegate delegate);
using WebAppManifestUpdateDelegate =
base::RepeatingCallback<void(const webapps::AppId& app_id)>;
void SetWebAppManifestUpdateDelegate(WebAppManifestUpdateDelegate delegate);
using WebAppLastBadgingTimeChangedDelegate =
base::RepeatingCallback<void(const webapps::AppId& app_id,
const base::Time& time)>;
void SetWebAppLastBadgingTimeChangedDelegate(
WebAppLastBadgingTimeChangedDelegate delegate);
using WebAppProtocolSettingsChangedDelegate = base::RepeatingCallback<void()>;
void SetWebAppProtocolSettingsChangedDelegate(
WebAppProtocolSettingsChangedDelegate delegate);
// WebAppRegistrarObserver:
void OnWebAppsWillBeUpdatedFromSync(
const std::vector<const WebApp*>& new_apps_state) override;
void OnWebAppLastBadgingTimeChanged(const webapps::AppId& app_id,
const base::Time& time) override;
void OnWebAppProtocolSettingsChanged() override;
void OnAppRegistrarDestroyed() override;
protected:
// Helper method for subclasses to allow easy waiting on `wait_loop_`.
// Expects that the users set `is_listening_` to `true` and
// optionally set `optional_app_ids_`.
void SignalRunLoopAndStoreAppId(const webapps::AppId& app_id);
bool is_listening_ = false;
std::set<webapps::AppId> optional_app_ids_;
base::RunLoop wait_loop_;
webapps::AppId last_app_id_;
private:
WebAppWillBeUpdatedFromSyncDelegate app_will_be_updated_from_sync_delegate_;
WebAppLastBadgingTimeChangedDelegate app_last_badging_time_changed_delegate_;
WebAppProtocolSettingsChangedDelegate app_protocol_settings_changed_delegate_;
base::ScopedObservation<WebAppRegistrar, WebAppRegistrarObserver>
observation_{this};
protected:
base::WeakPtrFactory<WebAppTestRegistryObserverAdapter> weak_factory_{this};
};
class WebAppTestInstallObserver final
: public WebAppInstallManagerObserverAdapter {
public:
explicit WebAppTestInstallObserver(Profile* profile);
~WebAppTestInstallObserver() final;
// Restricts this observer to only listen for the given
// |optional_app_ids|. Settings these means that the
// WebAppInstalledDelegate doesn't get called until all of the ids in
// |optional_app_ids| are installed.
void BeginListening(const std::set<webapps::AppId>& optional_app_ids = {});
// Wait for the next observation (or, until all optional_app_ids are
// observed).
webapps::AppId Wait();
webapps::AppId BeginListeningAndWait(
const std::set<webapps::AppId>& optional_app_ids = {});
};
class WebAppTestInstallWithOsHooksObserver final
: public WebAppInstallManagerObserverAdapter {
public:
explicit WebAppTestInstallWithOsHooksObserver(Profile* profile);
~WebAppTestInstallWithOsHooksObserver() final;
// Restricts this observer to only listen for the given
// |optional_app_ids|. Settings these means that the
// WebAppInstalledWithOsHooksDelegate doesn't get called until all of the ids
// in |optional_app_ids| are installed.
void BeginListening(const std::set<webapps::AppId>& optional_app_ids = {});
// Wait for the next observation (or, until all optional_app_ids are
// observed).
webapps::AppId Wait();
webapps::AppId BeginListeningAndWait(
const std::set<webapps::AppId>& app_ids = {});
};
class WebAppTestManifestUpdatedObserver final
: public WebAppInstallManagerObserverAdapter {
public:
explicit WebAppTestManifestUpdatedObserver(
WebAppInstallManager* install_manager);
~WebAppTestManifestUpdatedObserver() final;
// Restricts this observer to only listen for the given
// |optional_app_ids|. Settings these means that the
// WebAppManifestUpdateDelegate doesn't get called until all of the ids in
// |optional_app_ids| are installed.
void BeginListening(const std::set<webapps::AppId>& optional_app_ids = {});
// Wait for the next observation (or, until all optional_app_ids are
// observed).
webapps::AppId Wait();
webapps::AppId BeginListeningAndWait(
const std::set<webapps::AppId>& app_ids = {});
};
class WebAppTestUninstallObserver final
: public WebAppInstallManagerObserverAdapter {
public:
explicit WebAppTestUninstallObserver(Profile* profile);
~WebAppTestUninstallObserver() final;
// Restricts this observer to only listen for the given
// |optional_app_ids|. Settings these means that the
// WebAppUninstalledDelegate doesn't get called until all of the ids in
// |optional_app_ids| are installed.
void BeginListening(const std::set<webapps::AppId>& optional_app_ids = {});
webapps::AppId Wait();
webapps::AppId BeginListeningAndWait(
const std::set<webapps::AppId>& app_ids = {});
};
} // namespace web_app
#endif // CHROME_BROWSER_WEB_APPLICATIONS_TEST_WEB_APP_TEST_OBSERVERS_H_
|