| // 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/test_os_integration_manager.h" |
| |
| #include "base/threading/sequenced_task_runner_handle.h" |
| #include "chrome/browser/web_applications/components/app_shortcut_manager.h" |
| #include "chrome/browser/web_applications/components/file_handler_manager.h" |
| #include "chrome/browser/web_applications/components/protocol_handler_manager.h" |
| #include "chrome/browser/web_applications/components/url_handler_manager.h" |
| #include "chrome/browser/web_applications/components/web_app_constants.h" |
| #include "chrome/browser/web_applications/components/web_app_ui_manager.h" |
| #include "chrome/browser/web_applications/test/fake_protocol_handler_manager.h" |
| #include "chrome/browser/web_applications/test/fake_url_handler_manager.h" |
| #include "chrome/browser/web_applications/test/test_file_handler_manager.h" |
| |
| namespace web_app { |
| TestOsIntegrationManager::TestOsIntegrationManager( |
| Profile* profile, |
| std::unique_ptr<AppShortcutManager> shortcut_manager, |
| std::unique_ptr<FileHandlerManager> file_handler_manager, |
| std::unique_ptr<ProtocolHandlerManager> protocol_handler_manager, |
| std::unique_ptr<UrlHandlerManager> url_handler_manager) |
| : OsIntegrationManager(profile, |
| std::move(shortcut_manager), |
| std::move(file_handler_manager), |
| std::move(protocol_handler_manager), |
| std::move(url_handler_manager)) { |
| if (!this->shortcut_manager()) { |
| set_shortcut_manager(std::make_unique<TestShortcutManager>(profile)); |
| } |
| if (!this->file_handler_manager()) { |
| set_file_handler_manager(std::make_unique<TestFileHandlerManager>(profile)); |
| } |
| if (!this->protocol_handler_manager()) { |
| set_protocol_handler_manager( |
| std::make_unique<FakeProtocolHandlerManager>(profile)); |
| } |
| if (!this->url_handler_manager()) { |
| set_url_handler_manager(std::make_unique<FakeUrlHandlerManager>(profile)); |
| } |
| } |
| |
| TestOsIntegrationManager::~TestOsIntegrationManager() = default; |
| |
| void TestOsIntegrationManager::SetNextCreateShortcutsResult(const AppId& app_id, |
| bool success) { |
| DCHECK(!base::Contains(next_create_shortcut_results_, app_id)); |
| next_create_shortcut_results_[app_id] = success; |
| } |
| |
| void TestOsIntegrationManager::InstallOsHooks( |
| const AppId& app_id, |
| InstallOsHooksCallback callback, |
| std::unique_ptr<WebApplicationInfo> web_app_info, |
| InstallOsHooksOptions options) { |
| OsHooksResults os_hooks_results{false}; |
| last_options_ = options; |
| |
| if (options.os_hooks[OsHookType::kFileHandlers]) { |
| ++num_create_file_handlers_calls_; |
| os_hooks_results[OsHookType::kFileHandlers] = true; |
| } |
| |
| did_add_to_desktop_ = options.add_to_desktop; |
| |
| if (options.os_hooks[OsHookType::kShortcuts] && can_create_shortcuts_) { |
| bool success = true; |
| ++num_create_shortcuts_calls_; |
| auto it = next_create_shortcut_results_.find(app_id); |
| if (it != next_create_shortcut_results_.end()) { |
| success = it->second; |
| next_create_shortcut_results_.erase(app_id); |
| } |
| if (success) |
| os_hooks_results[OsHookType::kShortcutsMenu] = true; |
| } |
| |
| if (options.os_hooks[OsHookType::kRunOnOsLogin]) { |
| ++num_register_run_on_os_login_calls_; |
| os_hooks_results[OsHookType::kRunOnOsLogin] = true; |
| } |
| |
| if (options.add_to_quick_launch_bar) |
| ++num_add_app_to_quick_launch_bar_calls_; |
| |
| if (options.os_hooks[OsHookType::kUrlHandlers]) { |
| ++num_register_url_handlers_calls_; |
| os_hooks_results[OsHookType::kUrlHandlers] = true; |
| } |
| |
| base::SequencedTaskRunnerHandle::Get()->PostTask( |
| FROM_HERE, |
| base::BindOnce(std::move(callback), std::move(os_hooks_results))); |
| } |
| |
| void TestOsIntegrationManager::UninstallOsHooks( |
| const AppId& app_id, |
| const OsHooksResults& os_hooks, |
| UninstallOsHooksCallback callback) { |
| base::SequencedTaskRunnerHandle::Get()->PostTask( |
| FROM_HERE, base::BindOnce(std::move(callback), os_hooks)); |
| } |
| |
| void TestOsIntegrationManager::UninstallAllOsHooks( |
| const AppId& app_id, |
| UninstallOsHooksCallback callback) { |
| OsHooksResults os_hooks_results{true}; |
| UninstallOsHooks(app_id, os_hooks_results, std::move(callback)); |
| } |
| |
| void TestOsIntegrationManager::UpdateOsHooks( |
| const AppId& app_id, |
| base::StringPiece old_name, |
| std::unique_ptr<ShortcutInfo> old_shortcut, |
| bool file_handlers_need_os_update, |
| const WebApplicationInfo& web_app_info) {} |
| |
| void TestOsIntegrationManager::SetFileHandlerManager( |
| std::unique_ptr<FileHandlerManager> file_handler_manager) { |
| set_file_handler_manager(std::move(file_handler_manager)); |
| } |
| |
| void TestOsIntegrationManager::SetProtocolHandlerManager( |
| std::unique_ptr<ProtocolHandlerManager> protocol_handler_manager) { |
| set_protocol_handler_manager(std::move(protocol_handler_manager)); |
| } |
| |
| void TestOsIntegrationManager::SetUrlHandlerManager( |
| std::unique_ptr<UrlHandlerManager> url_handler_manager) { |
| set_url_handler_manager(std::move(url_handler_manager)); |
| } |
| |
| TestOsIntegrationManager* |
| TestOsIntegrationManager::AsTestOsIntegrationManager() { |
| return this; |
| } |
| |
| TestShortcutManager::TestShortcutManager(Profile* profile) |
| : AppShortcutManager(profile) {} |
| |
| TestShortcutManager::~TestShortcutManager() = default; |
| |
| std::unique_ptr<ShortcutInfo> TestShortcutManager::BuildShortcutInfo( |
| const AppId& app_id) { |
| return nullptr; |
| } |
| |
| void TestShortcutManager::SetShortcutInfoForApp( |
| const AppId& app_id, |
| std::unique_ptr<ShortcutInfo> shortcut_info) { |
| shortcut_info_map_[app_id] = std::move(shortcut_info); |
| } |
| |
| void TestShortcutManager::GetShortcutInfoForApp( |
| const AppId& app_id, |
| GetShortcutInfoCallback callback) { |
| if (shortcut_info_map_.find(app_id) != shortcut_info_map_.end()) { |
| std::move(callback).Run(std::move(shortcut_info_map_[app_id])); |
| shortcut_info_map_.erase(app_id); |
| } else { |
| std::move(callback).Run(nullptr); |
| } |
| } |
| |
| void TestShortcutManager::GetAppExistingShortCutLocation( |
| ShortcutLocationCallback callback, |
| std::unique_ptr<ShortcutInfo> shortcut_info) { |
| ShortcutLocations locations; |
| if (existing_shortcut_locations_.find(shortcut_info->url) != |
| existing_shortcut_locations_.end()) { |
| locations = existing_shortcut_locations_[shortcut_info->url]; |
| } |
| std::move(callback).Run(locations); |
| } |
| |
| } // namespace web_app |