Commit 5bee35ff authored by Phillis Tang's avatar Phillis Tang Committed by Commit Bot

DPWA: improve barrier callback for os hooks

Adds a CallbackFactory that creates callback for each os hook, and
DCHECK on destruction to make sure that all os hooks type have created
callback.

Bug: 1129193
Change-Id: I82a44dc7fa8a1c40c7caba8fcaa6134620e80d4f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2517438
Commit-Queue: Phillis Tang <phillis@chromium.org>
Reviewed-by: default avatarDaniel Murphy <dmurph@chromium.org>
Cr-Commit-Position: refs/heads/master@{#826457}
parent 968b98ed
......@@ -162,6 +162,7 @@ source_set("unit_tests") {
sources = [
"file_handler_manager_unittest.cc",
"install_finalizer_unittest.cc",
"os_integration_manager_unittest.cc",
"pending_app_manager_unittest.cc",
"web_app_constants_unittest.cc",
"web_app_data_retriever_unittest.cc",
......
......@@ -57,6 +57,9 @@ using UninstallOsHooksCallback =
// Used to suppress OS hooks within this object's lifetime.
using ScopedOsHooksSuppress = std::unique_ptr<base::AutoReset<bool>>;
using BarrierCallback =
base::RepeatingCallback<void(OsHookType::Type os_hook, bool completed)>;
// OsIntegrationManager is responsible of creating/updating/deleting
// all OS hooks during Web App lifecycle.
// It contains individual OS integration managers and takes
......@@ -128,6 +131,9 @@ class OsIntegrationManager {
static ScopedOsHooksSuppress ScopedSuppressOsHooksForTesting();
// Suppress calling individual OS managers for testing.
void SuppressOsManagersForTesting();
protected:
AppShortcutManager* shortcut_manager() { return shortcut_manager_.get(); }
FileHandlerManager* file_handler_manager() {
......@@ -142,22 +148,37 @@ class OsIntegrationManager {
file_handler_manager_ = std::move(file_handler_manager);
}
virtual void CreateShortcuts(const AppId& app_id,
bool add_to_desktop,
CreateShortcutsCallback callback);
virtual void RegisterFileHandlers(
const AppId& app_id,
base::OnceCallback<void(bool success)> callback);
virtual void RegisterShortcutsMenu(
const AppId& app_id,
const std::vector<WebApplicationShortcutsMenuItemInfo>&
shortcuts_menu_item_infos,
const ShortcutsMenuIconsBitmaps& shortcuts_menu_icons_bitmaps,
base::OnceCallback<void(bool success)> callback);
virtual void ReadAllShortcutsMenuIconsAndRegisterShortcutsMenu(
const AppId& app_id,
base::OnceCallback<void(bool success)> callback);
virtual void RegisterRunOnOsLogin(const AppId& app_id,
RegisterRunOnOsLoginCallback callback);
virtual void MacAppShimOnAppInstalledForProfile(const AppId& app_id);
virtual void AddAppToQuickLaunchBar(const AppId& app_id);
private:
void OnShortcutsCreated(const AppId& app_id,
std::unique_ptr<WebApplicationInfo> web_app_info,
InstallOsHooksOptions options,
base::RepeatingCallback<void(OsHookType::Type os_hook,
bool created)> callback,
BarrierCallback barrier,
bool shortcuts_created);
void OnShortcutsDeleted(
const AppId& app_id,
base::RepeatingCallback<void(OsHookType::Type os_hook, bool deleted)>
barrier_callback,
bool shortcuts_deleted);
void RegisterRunOnOsLogin(const AppId& app_id,
RegisterRunOnOsLoginCallback callback);
void OnShortcutsDeleted(const AppId& app_id,
DeleteShortcutsCallback callback,
bool shortcuts_deleted);
void OnShortcutInfoRetrievedRegisterRunOnOsLogin(
RegisterRunOnOsLoginCallback callback,
......@@ -169,6 +190,7 @@ class OsIntegrationManager {
std::unique_ptr<AppShortcutManager> shortcut_manager_;
std::unique_ptr<FileHandlerManager> file_handler_manager_;
bool suppress_os_managers_for_testing_ = false;
base::WeakPtrFactory<OsIntegrationManager> weak_ptr_factory_{this};
};
......
// Copyright 2020 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/components/os_integration_manager.h"
#include <memory>
#include "base/bind.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "chrome/browser/web_applications/components/web_app_constants.h"
#include "chrome/browser/web_applications/components/web_app_helpers.h"
#include "chrome/browser/web_applications/components/web_app_ui_manager.h"
#include "chrome/browser/web_applications/test/test_file_handler_manager.h"
#include "chrome/browser/web_applications/test/test_os_integration_manager.h"
#include "chrome/browser/web_applications/test/web_app_test.h"
#include "chrome/browser/web_applications/web_app.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
namespace web_app {
class OsIntegrationManagerTest : public WebAppTest {
public:
void SetUp() override {
WebAppTest::SetUp();
auto shortcut_manager = std::make_unique<TestShortcutManager>(profile());
auto file_handler_manager =
std::make_unique<TestFileHandlerManager>(profile());
os_integration_manager_ = std::make_unique<OsIntegrationManager>(
profile(), std::move(shortcut_manager),
std::move(file_handler_manager));
os_integration_manager_->SuppressOsManagersForTesting();
}
OsIntegrationManager* os_integration_manager() {
return os_integration_manager_.get();
}
private:
std::unique_ptr<WebAppUiManager> ui_manager_;
std::unique_ptr<OsIntegrationManager> os_integration_manager_;
};
TEST_F(OsIntegrationManagerTest, InstallOsHooksCallbackCalled) {
base::RunLoop run_loop;
OsHooksResults install_results;
InstallOsHooksCallback callback =
base::BindLambdaForTesting([&](OsHooksResults results) {
install_results = results;
run_loop.Quit();
});
const AppId app_id = "test";
InstallOsHooksOptions options;
options.os_hooks = OsHooksResults{true};
os_integration_manager()->InstallOsHooks(app_id, std::move(callback), nullptr,
options);
run_loop.Run();
ASSERT_TRUE(install_results[0]);
}
} // namespace web_app
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment