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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
// 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.
#include "chrome/browser/web_applications/test/web_app_test_observers.h"
#include <sstream>
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/bind.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_provider.h"
namespace web_app {
namespace {
#if DCHECK_IS_ON()
bool IsAnyIdEmpty(const std::set<webapps::AppId>& app_ids) {
for (const webapps::AppId& id : app_ids) {
if (id.empty())
return true;
}
return false;
}
#endif
template <typename Container>
std::string ContainerToString(const Container& container) {
std::ostringstream ss;
ss << "[";
bool first_item = true;
for (const auto& item : container) {
if (!first_item) {
ss << ", ";
}
ss << base::ToString(item);
first_item = false;
}
ss << "]";
return ss.str();
}
} // namespace
WebAppInstallManagerObserverAdapter::WebAppInstallManagerObserverAdapter(
WebAppInstallManager* install_manager) {
observation_.Observe(install_manager);
}
WebAppInstallManagerObserverAdapter::WebAppInstallManagerObserverAdapter(
Profile* profile)
: WebAppInstallManagerObserverAdapter(
&WebAppProvider::GetForWebApps(profile)->install_manager()) {}
WebAppInstallManagerObserverAdapter::~WebAppInstallManagerObserverAdapter() =
default;
void WebAppInstallManagerObserverAdapter::SetWebAppInstalledDelegate(
WebAppInstalledDelegate delegate) {
app_installed_delegate_ = std::move(delegate);
}
void WebAppInstallManagerObserverAdapter::SetWebAppInstalledWithOsHooksDelegate(
WebAppInstalledWithOsHooksDelegate delegate) {
app_installed_with_os_hooks_delegate_ = std::move(delegate);
}
void WebAppInstallManagerObserverAdapter::SetWebAppWillBeUninstalledDelegate(
WebAppWillBeUninstalledDelegate delegate) {
app_will_be_uninstalled_delegate_ = std::move(delegate);
}
void WebAppInstallManagerObserverAdapter::SetWebAppUninstalledDelegate(
WebAppUninstalledDelegate delegate) {
app_uninstalled_delegate_ = std::move(delegate);
}
void WebAppInstallManagerObserverAdapter::SetWebAppManifestUpdateDelegate(
WebAppManifestUpdateDelegate delegate) {
app_manifest_updated_delegate_ = std::move(delegate);
}
void WebAppInstallManagerObserverAdapter::SetWebAppSourceRemovedDelegate(
WebAppSourceRemovedDelegate delegate) {
app_source_removed_delegate_ = std::move(delegate);
}
void WebAppInstallManagerObserverAdapter::OnWebAppInstalled(
const webapps::AppId& app_id) {
if (app_installed_delegate_)
app_installed_delegate_.Run(app_id);
}
void WebAppInstallManagerObserverAdapter::OnWebAppInstalledWithOsHooks(
const webapps::AppId& app_id) {
if (app_installed_with_os_hooks_delegate_)
app_installed_with_os_hooks_delegate_.Run(app_id);
}
void WebAppInstallManagerObserverAdapter::OnWebAppManifestUpdated(
const webapps::AppId& app_id) {
if (app_manifest_updated_delegate_)
app_manifest_updated_delegate_.Run(app_id);
}
void WebAppInstallManagerObserverAdapter::OnWebAppWillBeUninstalled(
const webapps::AppId& app_id) {
if (app_will_be_uninstalled_delegate_)
app_will_be_uninstalled_delegate_.Run(app_id);
}
void WebAppInstallManagerObserverAdapter::OnWebAppUninstalled(
const webapps::AppId& app_id,
webapps::WebappUninstallSource uninstall_source) {
if (app_uninstalled_delegate_)
app_uninstalled_delegate_.Run(app_id);
}
void WebAppInstallManagerObserverAdapter::OnWebAppInstallManagerDestroyed() {
observation_.Reset();
}
void WebAppInstallManagerObserverAdapter::OnWebAppSourceRemoved(
const webapps::AppId& app_id) {
if (app_source_removed_delegate_) {
app_source_removed_delegate_.Run(app_id);
}
}
void WebAppInstallManagerObserverAdapter::SignalRunLoopAndStoreAppId(
const webapps::AppId& app_id) {
if (!is_listening_)
return;
optional_app_ids_.erase(app_id);
if (!optional_app_ids_.empty())
return;
last_app_id_ = app_id;
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, wait_loop_.QuitClosure());
is_listening_ = false;
}
WebAppTestRegistryObserverAdapter::WebAppTestRegistryObserverAdapter(
WebAppRegistrar* registrar) {
observation_.Observe(registrar);
}
WebAppTestRegistryObserverAdapter::WebAppTestRegistryObserverAdapter(
Profile* profile)
: WebAppTestRegistryObserverAdapter(
&WebAppProvider::GetForTest(profile)->registrar_unsafe()) {}
WebAppTestRegistryObserverAdapter::~WebAppTestRegistryObserverAdapter() =
default;
void WebAppTestRegistryObserverAdapter::SetWebAppWillBeUpdatedFromSyncDelegate(
WebAppWillBeUpdatedFromSyncDelegate delegate) {
app_will_be_updated_from_sync_delegate_ = std::move(delegate);
}
void WebAppTestRegistryObserverAdapter::SetWebAppLastBadgingTimeChangedDelegate(
WebAppLastBadgingTimeChangedDelegate delegate) {
app_last_badging_time_changed_delegate_ = std::move(delegate);
}
void WebAppTestRegistryObserverAdapter::
SetWebAppProtocolSettingsChangedDelegate(
WebAppProtocolSettingsChangedDelegate delegate) {
app_protocol_settings_changed_delegate_ = std::move(delegate);
}
void WebAppTestRegistryObserverAdapter::OnWebAppsWillBeUpdatedFromSync(
const std::vector<const WebApp*>& new_apps_state) {
if (app_will_be_updated_from_sync_delegate_)
app_will_be_updated_from_sync_delegate_.Run(new_apps_state);
}
void WebAppTestRegistryObserverAdapter::OnWebAppLastBadgingTimeChanged(
const webapps::AppId& app_id,
const base::Time& time) {
if (app_last_badging_time_changed_delegate_)
app_last_badging_time_changed_delegate_.Run(app_id, time);
}
void WebAppTestRegistryObserverAdapter::OnWebAppProtocolSettingsChanged() {
if (app_protocol_settings_changed_delegate_)
app_protocol_settings_changed_delegate_.Run();
}
void WebAppTestRegistryObserverAdapter::OnAppRegistrarDestroyed() {
observation_.Reset();
}
void WebAppTestRegistryObserverAdapter::SignalRunLoopAndStoreAppId(
const webapps::AppId& app_id) {
if (!is_listening_)
return;
optional_app_ids_.erase(app_id);
if (!optional_app_ids_.empty())
return;
last_app_id_ = app_id;
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, wait_loop_.QuitClosure());
is_listening_ = false;
}
WebAppTestInstallObserver::WebAppTestInstallObserver(Profile* profile)
: WebAppInstallManagerObserverAdapter(profile) {}
WebAppTestInstallObserver::~WebAppTestInstallObserver() = default;
void WebAppTestInstallObserver::BeginListening(
const std::set<webapps::AppId>& optional_app_ids) {
optional_app_ids_ = optional_app_ids;
#if DCHECK_IS_ON()
DCHECK(!IsAnyIdEmpty(optional_app_ids_)) << "Cannot listen for empty ids.";
#endif
is_listening_ = true;
app_installed_delegate_ = base::BindRepeating(
&WebAppTestInstallObserver::SignalRunLoopAndStoreAppId,
weak_factory_.GetWeakPtr());
}
webapps::AppId WebAppTestInstallObserver::Wait() {
wait_loop_.Run();
if (last_app_id_.empty()) {
LOG(ERROR) << "Could not find any of "
<< ContainerToString(optional_app_ids_);
}
return last_app_id_;
}
webapps::AppId WebAppTestInstallObserver::BeginListeningAndWait(
const std::set<webapps::AppId>& optional_app_ids) {
BeginListening(optional_app_ids);
webapps::AppId id = Wait();
return id;
}
WebAppTestInstallWithOsHooksObserver::WebAppTestInstallWithOsHooksObserver(
Profile* profile)
: WebAppInstallManagerObserverAdapter(profile) {}
WebAppTestInstallWithOsHooksObserver::~WebAppTestInstallWithOsHooksObserver() =
default;
void WebAppTestInstallWithOsHooksObserver::BeginListening(
const std::set<webapps::AppId>& optional_app_ids) {
optional_app_ids_ = optional_app_ids;
#if DCHECK_IS_ON()
DCHECK(!IsAnyIdEmpty(optional_app_ids_)) << "Cannot listen for empty ids.";
#endif
is_listening_ = true;
app_installed_with_os_hooks_delegate_ = base::BindRepeating(
&WebAppTestInstallWithOsHooksObserver::SignalRunLoopAndStoreAppId,
weak_factory_.GetWeakPtr());
}
webapps::AppId WebAppTestInstallWithOsHooksObserver::Wait() {
wait_loop_.Run();
if (last_app_id_.empty()) {
LOG(ERROR) << "Could not find any of "
<< ContainerToString(optional_app_ids_);
}
return last_app_id_;
}
webapps::AppId WebAppTestInstallWithOsHooksObserver::BeginListeningAndWait(
const std::set<webapps::AppId>& optional_app_ids) {
BeginListening(optional_app_ids);
webapps::AppId id = Wait();
return id;
}
WebAppTestManifestUpdatedObserver::WebAppTestManifestUpdatedObserver(
WebAppInstallManager* install_manager)
: WebAppInstallManagerObserverAdapter(install_manager) {}
WebAppTestManifestUpdatedObserver::~WebAppTestManifestUpdatedObserver() =
default;
void WebAppTestManifestUpdatedObserver::BeginListening(
const std::set<webapps::AppId>& optional_app_ids) {
optional_app_ids_ = optional_app_ids;
#if DCHECK_IS_ON()
DCHECK(!IsAnyIdEmpty(optional_app_ids_)) << "Cannot listen for empty ids.";
#endif
is_listening_ = true;
app_manifest_updated_delegate_ = base::BindRepeating(
&WebAppTestManifestUpdatedObserver::SignalRunLoopAndStoreAppId,
weak_factory_.GetWeakPtr());
}
webapps::AppId WebAppTestManifestUpdatedObserver::Wait() {
wait_loop_.Run();
if (last_app_id_.empty()) {
LOG(ERROR) << "Could not find any of "
<< ContainerToString(optional_app_ids_);
}
return last_app_id_;
}
webapps::AppId WebAppTestManifestUpdatedObserver::BeginListeningAndWait(
const std::set<webapps::AppId>& optional_app_ids) {
BeginListening(optional_app_ids);
webapps::AppId id = Wait();
return id;
}
WebAppTestUninstallObserver::WebAppTestUninstallObserver(Profile* profile)
: WebAppInstallManagerObserverAdapter(profile) {}
WebAppTestUninstallObserver::~WebAppTestUninstallObserver() = default;
void WebAppTestUninstallObserver::BeginListening(
const std::set<webapps::AppId>& optional_app_ids) {
optional_app_ids_ = optional_app_ids;
#if DCHECK_IS_ON()
DCHECK(!IsAnyIdEmpty(optional_app_ids_)) << "Cannot listen for empty ids.";
#endif
is_listening_ = true;
app_uninstalled_delegate_ = base::BindRepeating(
&WebAppTestUninstallObserver::SignalRunLoopAndStoreAppId,
weak_factory_.GetWeakPtr());
}
webapps::AppId WebAppTestUninstallObserver::Wait() {
wait_loop_.Run();
if (last_app_id_.empty()) {
LOG(ERROR) << "Could not find any of "
<< ContainerToString(optional_app_ids_);
}
return last_app_id_;
}
webapps::AppId WebAppTestUninstallObserver::BeginListeningAndWait(
const std::set<webapps::AppId>& optional_app_ids) {
BeginListening(optional_app_ids);
webapps::AppId id = Wait();
return id;
}
} // namespace web_app
|