| // Copyright 2019 The Chromium Authors. All rights reserved. |
| // 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 "base/run_loop.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<AppId>& app_ids) { |
| for (const AppId& id : app_ids) { |
| if (id.empty()) |
| return true; |
| } |
| return false; |
| } |
| #endif |
| |
| } // namespace |
| |
| WebAppTestRegistryObserverAdapter::WebAppTestRegistryObserverAdapter( |
| WebAppRegistrar* registrar) { |
| observation_.Observe(registrar); |
| } |
| |
| WebAppTestRegistryObserverAdapter::WebAppTestRegistryObserverAdapter( |
| Profile* profile) |
| : WebAppTestRegistryObserverAdapter( |
| &WebAppProvider::GetForTest(profile)->registrar()) {} |
| |
| WebAppTestRegistryObserverAdapter::~WebAppTestRegistryObserverAdapter() = |
| default; |
| |
| void WebAppTestRegistryObserverAdapter::SetWebAppInstalledDelegate( |
| WebAppInstalledDelegate delegate) { |
| app_installed_delegate_ = delegate; |
| } |
| |
| void WebAppTestRegistryObserverAdapter::SetWebAppInstalledWithOsHooksDelegate( |
| WebAppInstalledWithOsHooksDelegate delegate) { |
| app_installed_with_os_hooks_delegate_ = delegate; |
| } |
| |
| void WebAppTestRegistryObserverAdapter::SetWebAppWillBeUninstalledDelegate( |
| WebAppWillBeUninstalledDelegate delegate) { |
| app_will_be_uninstalled_delegate_ = delegate; |
| } |
| |
| void WebAppTestRegistryObserverAdapter::SetWebAppUninstalledDelegate( |
| WebAppUninstalledDelegate delegate) { |
| app_uninstalled_delegate_ = delegate; |
| } |
| |
| void WebAppTestRegistryObserverAdapter::SetWebAppProfileWillBeDeletedDelegate( |
| WebAppProfileWillBeDeletedDelegate delegate) { |
| app_profile_will_be_deleted_delegate_ = delegate; |
| } |
| |
| void WebAppTestRegistryObserverAdapter::SetWebAppWillBeUpdatedFromSyncDelegate( |
| WebAppWillBeUpdatedFromSyncDelegate delegate) { |
| app_will_be_updated_from_sync_delegate_ = delegate; |
| } |
| |
| void WebAppTestRegistryObserverAdapter::SetWebAppManifestUpdateDelegate( |
| WebAppManifestUpdateDelegate delegate) { |
| app_manifest_updated_delegate_ = delegate; |
| } |
| |
| void WebAppTestRegistryObserverAdapter::SetWebAppLastBadgingTimeChangedDelegate( |
| WebAppLastBadgingTimeChangedDelegate delegate) { |
| app_last_badging_time_changed_delegate_ = delegate; |
| } |
| |
| void WebAppTestRegistryObserverAdapter:: |
| SetWebAppProtocolSettingsChangedDelegate( |
| WebAppProtocolSettingsChangedDelegate delegate) { |
| app_protocol_settings_changed_delegate_ = delegate; |
| } |
| |
| void WebAppTestRegistryObserverAdapter::OnWebAppInstalled(const AppId& app_id) { |
| if (app_installed_delegate_) |
| app_installed_delegate_.Run(app_id); |
| } |
| |
| void WebAppTestRegistryObserverAdapter::OnWebAppInstalledWithOsHooks( |
| const AppId& app_id) { |
| if (app_installed_with_os_hooks_delegate_) |
| app_installed_with_os_hooks_delegate_.Run(app_id); |
| } |
| |
| void WebAppTestRegistryObserverAdapter::OnWebAppManifestUpdated( |
| const AppId& app_id, |
| base::StringPiece old_name) { |
| if (app_manifest_updated_delegate_) |
| app_manifest_updated_delegate_.Run(app_id); |
| } |
| |
| 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::OnWebAppWillBeUninstalled( |
| const AppId& app_id) { |
| if (app_will_be_uninstalled_delegate_) |
| app_will_be_uninstalled_delegate_.Run(app_id); |
| } |
| |
| void WebAppTestRegistryObserverAdapter::OnWebAppUninstalled( |
| const AppId& app_id) { |
| if (app_uninstalled_delegate_) |
| app_uninstalled_delegate_.Run(app_id); |
| } |
| |
| void WebAppTestRegistryObserverAdapter::OnWebAppProfileWillBeDeleted( |
| const AppId& app_id) { |
| if (app_profile_will_be_deleted_delegate_) |
| app_profile_will_be_deleted_delegate_.Run(app_id); |
| } |
| |
| void WebAppTestRegistryObserverAdapter::OnWebAppLastBadgingTimeChanged( |
| const web_app::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::SignalRunLoopAndStoreAppId( |
| const AppId& app_id) { |
| if (!is_listening_) |
| return; |
| optional_app_ids_.erase(app_id); |
| if (!optional_app_ids_.empty()) |
| return; |
| last_app_id_ = app_id; |
| wait_loop_.Quit(); |
| is_listening_ = false; |
| } |
| |
| WebAppTestInstallObserver::WebAppTestInstallObserver(Profile* profile) |
| : WebAppTestRegistryObserverAdapter(profile) {} |
| WebAppTestInstallObserver::~WebAppTestInstallObserver() = default; |
| |
| void WebAppTestInstallObserver::BeginListening( |
| const std::set<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()); |
| } |
| |
| AppId WebAppTestInstallObserver::Wait() { |
| wait_loop_.Run(); |
| return last_app_id_; |
| } |
| |
| AppId WebAppTestInstallObserver::BeginListeningAndWait( |
| const std::set<AppId>& optional_app_ids) { |
| BeginListening(optional_app_ids); |
| AppId id = Wait(); |
| return id; |
| } |
| |
| WebAppTestInstallWithOsHooksObserver::WebAppTestInstallWithOsHooksObserver( |
| Profile* profile) |
| : WebAppTestRegistryObserverAdapter(profile) {} |
| WebAppTestInstallWithOsHooksObserver::~WebAppTestInstallWithOsHooksObserver() = |
| default; |
| |
| void WebAppTestInstallWithOsHooksObserver::BeginListening( |
| const std::set<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()); |
| } |
| |
| AppId WebAppTestInstallWithOsHooksObserver::Wait() { |
| wait_loop_.Run(); |
| return last_app_id_; |
| } |
| |
| AppId WebAppTestInstallWithOsHooksObserver::BeginListeningAndWait( |
| const std::set<AppId>& optional_app_ids) { |
| BeginListening(optional_app_ids); |
| AppId id = Wait(); |
| return id; |
| } |
| |
| WebAppTestManifestUpdatedObserver::WebAppTestManifestUpdatedObserver( |
| WebAppRegistrar* registrar) |
| : WebAppTestRegistryObserverAdapter(registrar) {} |
| WebAppTestManifestUpdatedObserver::~WebAppTestManifestUpdatedObserver() = |
| default; |
| |
| void WebAppTestManifestUpdatedObserver::BeginListening( |
| const std::set<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()); |
| } |
| |
| AppId WebAppTestManifestUpdatedObserver::Wait() { |
| wait_loop_.Run(); |
| return last_app_id_; |
| } |
| |
| AppId WebAppTestManifestUpdatedObserver::BeginListeningAndWait( |
| const std::set<AppId>& optional_app_ids) { |
| BeginListening(optional_app_ids); |
| AppId id = Wait(); |
| return id; |
| } |
| |
| WebAppTestUninstallObserver::WebAppTestUninstallObserver(Profile* profile) |
| : WebAppTestRegistryObserverAdapter(profile) {} |
| |
| WebAppTestUninstallObserver::~WebAppTestUninstallObserver() = default; |
| |
| void WebAppTestUninstallObserver::BeginListening( |
| const std::set<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()); |
| } |
| |
| AppId WebAppTestUninstallObserver::Wait() { |
| wait_loop_.Run(); |
| return last_app_id_; |
| } |
| |
| AppId WebAppTestUninstallObserver::BeginListeningAndWait( |
| const std::set<AppId>& optional_app_ids) { |
| BeginListening(optional_app_ids); |
| AppId id = Wait(); |
| return id; |
| } |
| |
| } // namespace web_app |