Skip to content

Commit

Permalink
Settings Split: Initial commit of InputDeviceSettingsProvider interface
Browse files Browse the repository at this point in the history
Initial implementation of instantiating the InputDeviceSettingsProvider
interface. This will be used by the Settings SWA to retrieve and update
input device settings info.

Change-Id: I866e5b3ee38593c254794ae0ae491ceb1edf1d3e
Bug: b/241965700
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4234052
Reviewed-by: Zentaro Kavanagh <zentaro@chromium.org>
Reviewed-by: Emily Stark <estark@chromium.org>
Commit-Queue: David Padlipsky <dpad@google.com>
Cr-Commit-Position: refs/heads/main@{#1107561}
  • Loading branch information
dpadlipsky authored and Chromium LUCI CQ committed Feb 21, 2023
1 parent e54f862 commit ec6f43b
Show file tree
Hide file tree
Showing 14 changed files with 306 additions and 1 deletion.
1 change: 1 addition & 0 deletions chrome/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -5084,6 +5084,7 @@ static_library("browser") {
"//chrome/browser/ui/webui/ash/parent_access:mojo_bindings",
"//chrome/browser/ui/webui/ash/vm:mojo_bindings",
"//chrome/browser/ui/webui/nearby_share:mojom",
"//chrome/browser/ui/webui/settings/ash/input_device_settings:mojom",
"//chrome/browser/ui/webui/settings/ash/os_apps_page/mojom",
"//chrome/browser/ui/webui/settings/ash/search:mojo_bindings",
"//chrome/services/sharing/public/cpp",
Expand Down
1 change: 1 addition & 0 deletions chrome/browser/ash/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -4706,6 +4706,7 @@ source_set("unit_tests") {
"../ui/webui/settings/ash/device_storage_handler_unittest.cc",
"../ui/webui/settings/ash/fast_pair_saved_devices_handler_unittest.cc",
"../ui/webui/settings/ash/hierarchy_unittest.cc",
"../ui/webui/settings/ash/input_device_settings/input_device_settings_provider_unittest.cc",
"../ui/webui/settings/ash/internet_handler_unittest.cc",
"../ui/webui/settings/ash/metrics_consent_handler_unittest.cc",
"../ui/webui/settings/ash/multidevice_handler_unittest.cc",
Expand Down
5 changes: 5 additions & 0 deletions chrome/browser/chrome_browser_interface_binders.cc
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@
#include "chrome/browser/ui/webui/ash/vm/vm_ui.h"
#include "chrome/browser/ui/webui/nearby_share/nearby_share.mojom.h"
#include "chrome/browser/ui/webui/nearby_share/nearby_share_dialog_ui.h"
#include "chrome/browser/ui/webui/settings/ash/input_device_settings/input_device_settings_provider.mojom.h"
#include "chrome/browser/ui/webui/settings/ash/os_apps_page/mojom/app_notification_handler.mojom.h"
#include "chrome/browser/ui/webui/settings/ash/os_settings_ui.h"
#include "chrome/browser/ui/webui/settings/ash/search/search.mojom.h"
Expand Down Expand Up @@ -1141,6 +1142,10 @@ void PopulateChromeWebUIFrameBinders(
ash::settings::app_notification::mojom::AppNotificationsHandler,
ash::settings::OSSettingsUI>(map);

RegisterWebUIControllerInterfaceBinder<
ash::settings::mojom::InputDeviceSettingsProvider,
ash::settings::OSSettingsUI>(map);

RegisterWebUIControllerInterfaceBinder<
ash::cellular_setup::mojom::CellularSetup, ash::settings::OSSettingsUI>(
map);
Expand Down
5 changes: 5 additions & 0 deletions chrome/browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3048,6 +3048,8 @@ static_library("ui") {
"webui/settings/ash/guest_os_handler.h",
"webui/settings/ash/hierarchy.cc",
"webui/settings/ash/hierarchy.h",
"webui/settings/ash/input_device_settings/input_device_settings_provider.cc",
"webui/settings/ash/input_device_settings/input_device_settings_provider.h",
"webui/settings/ash/internet_handler.cc",
"webui/settings/ash/internet_handler.h",
"webui/settings/ash/internet_section.cc",
Expand Down Expand Up @@ -3156,6 +3158,7 @@ static_library("ui") {
"//ash/public/cpp",
"//ash/public/cpp/app_list/vector_icons",
"//ash/public/cpp/resources:ash_public_unscaled_resources",
"//ash/public/mojom",
"//ash/quick_pair/common",
"//ash/quick_pair/keyed_service",
"//ash/quick_pair/proto:fastpair_proto",
Expand Down Expand Up @@ -3242,6 +3245,7 @@ static_library("ui") {
"//chrome/browser/ui/webui/ash/parent_access:proto",
"//chrome/browser/ui/webui/ash/vm:mojo_bindings",
"//chrome/browser/ui/webui/nearby_share:mojom",
"//chrome/browser/ui/webui/settings/ash/input_device_settings:mojom",
"//chrome/browser/ui/webui/settings/ash/os_apps_page/mojom",
"//chrome/browser/ui/webui/settings/ash/search:mojo_bindings",
"//chrome/browser/web_applications",
Expand Down Expand Up @@ -6126,6 +6130,7 @@ static_library("test_support") {
]
deps += [
"//ash/public/cpp",
"//ash/public/mojom",
"//chrome/browser/ui/webui/settings/chromeos/constants:mojom",
"//chromeos/ash/components/dbus",
"//chromeos/ui/base",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2023 The Chromium Authors
# Use of this source code is governed by a BSD - style license that can be
# found in the LICENSE file.

import("//build/config/chromeos/ui_mode.gni")
import("//mojo/public/tools/bindings/mojom.gni")

assert(is_chromeos_ash)

mojom("mojom") {
disable_variants = true

sources = [ "input_device_settings_provider.mojom" ]

public_deps = [
"//ash/public/mojom:mojom",
"//mojo/public/mojom/base",
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
per-file *.mojom=set noparent
per-file *.mojom=file://ipc/SECURITY_OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2023 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/ui/webui/settings/ash/input_device_settings/input_device_settings_provider.h"
#include "ash/constants/ash_features.h"
#include "ash/public/cpp/input_device_settings_controller.h"
#include "ash/public/mojom/input_device_settings.mojom.h"

namespace ash::settings {

InputDeviceSettingsProvider::InputDeviceSettingsProvider(
InputDeviceSettingsController* controller)
: controller_(controller) {}
InputDeviceSettingsProvider::~InputDeviceSettingsProvider() = default;

void InputDeviceSettingsProvider::BindInterface(
mojo::PendingReceiver<mojom::InputDeviceSettingsProvider> receiver) {
DCHECK(features::IsInputDeviceSettingsSplitEnabled());
if (receiver_.is_bound()) {
receiver_.reset();
}
receiver_.Bind(std::move(receiver));
}

void InputDeviceSettingsProvider::GetConnectedKeyboards(
GetConnectedKeyboardsCallback callback) {
DCHECK(features::IsInputDeviceSettingsSplitEnabled());
std::move(callback).Run(controller_->GetConnectedKeyboards());
}

} // namespace ash::settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2023 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_UI_WEBUI_SETTINGS_ASH_INPUT_DEVICE_SETTINGS_INPUT_DEVICE_SETTINGS_PROVIDER_H_
#define CHROME_BROWSER_UI_WEBUI_SETTINGS_ASH_INPUT_DEVICE_SETTINGS_INPUT_DEVICE_SETTINGS_PROVIDER_H_

#include "ash/public/cpp/input_device_settings_controller.h"
#include "chrome/browser/ui/webui/settings/ash/input_device_settings/input_device_settings_provider.mojom.h"
#include "mojo/public/cpp/bindings/receiver.h"

namespace ash::settings {

class InputDeviceSettingsProvider : public mojom::InputDeviceSettingsProvider {
public:
explicit InputDeviceSettingsProvider(
InputDeviceSettingsController* controller);

~InputDeviceSettingsProvider() override;
InputDeviceSettingsProvider(const InputDeviceSettingsProvider& other) =
delete;
InputDeviceSettingsProvider& operator=(
const InputDeviceSettingsProvider& other) = delete;

void BindInterface(
mojo::PendingReceiver<mojom::InputDeviceSettingsProvider> receiver);

// mojom::InputDeviceSettingsProvider:
void GetConnectedKeyboards(GetConnectedKeyboardsCallback callback) override;

private:
InputDeviceSettingsController* controller_;
mojo::Receiver<mojom::InputDeviceSettingsProvider> receiver_{this};
};

} // namespace ash::settings

#endif // CHROME_BROWSER_UI_WEBUI_SETTINGS_ASH_INPUT_DEVICE_SETTINGS_INPUT_DEVICE_SETTINGS_PROVIDER_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

module ash.settings.mojom;

import "ash/public/mojom/input_device_settings.mojom";

// Interface for fetching and configuring input device settings in OSSettings.
interface InputDeviceSettingsProvider {
// Returns the list of currently connected keyboards.
GetConnectedKeyboards() => (array<ash.mojom.Keyboard> keyboards);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Copyright 2023 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/ui/webui/settings/ash/input_device_settings/input_device_settings_provider.h"

#include <memory>

#include "ash/constants/ash_features.h"
#include "ash/public/cpp/input_device_settings_controller.h"
#include "ash/public/mojom/input_device_settings.mojom-forward.h"
#include "ash/public/mojom/input_device_settings.mojom-shared.h"
#include "ash/public/mojom/input_device_settings.mojom.h"
#include "base/containers/cxx20_erase_vector.h"
#include "base/functional/bind.h"
#include "base/test/scoped_feature_list.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash::settings {
namespace {
const ::ash::mojom::Keyboard keyboard1 =
::ash::mojom::Keyboard(/*name=*/"AT Translated Set 2",
/*is_external=*/false,
/*id=*/0,
/*device_key=*/"fake-device-key1",
/*meta_key=*/::ash::mojom::MetaKey::kLauncher,
/*modifier_keys=*/{},
nullptr);
const ::ash::mojom::Keyboard keyboard2 =
::ash::mojom::Keyboard(/*name=*/"Logitech K580",
/*is_external=*/true,
/*id=*/1,
/*device_key=*/"fake-device-key2",
/*meta_key=*/::ash::mojom::MetaKey::kExternalMeta,
/*modifier_keys=*/{},
nullptr);

template <typename T>
void ExpectListsEqual(std::vector<T> expected_list,
std::vector<T> actual_list) {
ASSERT_EQ(expected_list.size(), actual_list.size());
for (size_t i = 0; i < actual_list.size(); i++) {
EXPECT_EQ(expected_list[i], actual_list[i]);
}
}

template <typename T>
std::vector<T> CloneMojomVector(const std::vector<T>& devices) {
std::vector<T> devices_copy;
devices_copy.reserve(devices.size());
for (const auto& device : devices) {
devices_copy.push_back(device->Clone());
}
return devices_copy;
}

class FakeInputDeviceSettingsController : public InputDeviceSettingsController {
public:
// InputDeviceSettingsController:
std::vector<::ash::mojom::KeyboardPtr> GetConnectedKeyboards() override {
return CloneMojomVector(keyboards_);
}
std::vector<::ash::mojom::TouchpadPtr> GetConnectedTouchpads() override {
return CloneMojomVector(touchpads_);
}
std::vector<::ash::mojom::MousePtr> GetConnectedMice() override {
return CloneMojomVector(mice_);
}
std::vector<::ash::mojom::PointingStickPtr> GetConnectedPointingSticks()
override {
return CloneMojomVector(pointing_sticks_);
}

void SetKeyboardSettings(
DeviceId id,
const ::ash::mojom::KeyboardSettings& settings) override {}
void AddObserver(Observer* observer) override {}
void RemoveObserver(Observer* observer) override {}

void AddKeyboard(::ash::mojom::KeyboardPtr keyboard) {
keyboards_.push_back(std::move(keyboard));
}
void RemoveKeyboard(uint32_t device_id) {
base::EraseIf(keyboards_, [device_id](const auto& keyboard) {
return keyboard->id == device_id;
});
}
void AddMouse(::ash::mojom::MousePtr mouse) {
mice_.push_back(std::move(mouse));
}
void RemoveMouse(uint32_t device_id) {
base::EraseIf(mice_, [device_id](const auto& mouse) {
return mouse->id == device_id;
});
}
void AddTouchpad(::ash::mojom::TouchpadPtr touchpad) {
touchpads_.push_back(std::move(touchpad));
}
void RemoveTouchpad(uint32_t device_id) {
base::EraseIf(touchpads_, [device_id](const auto& touchpad) {
return touchpad->id == device_id;
});
}
void AddPointingStick(::ash::mojom::PointingStickPtr pointing_stick) {
pointing_sticks_.push_back(std::move(pointing_stick));
}
void RemovePointingStick(uint32_t device_id) {
base::EraseIf(pointing_sticks_, [device_id](const auto& pointing_stick) {
return pointing_stick->id == device_id;
});
}

private:
std::vector<::ash::mojom::KeyboardPtr> keyboards_;
std::vector<::ash::mojom::TouchpadPtr> touchpads_;
std::vector<::ash::mojom::MousePtr> mice_;
std::vector<::ash::mojom::PointingStickPtr> pointing_sticks_;
};

} // namespace

class InputDeviceSettingsProviderTest : public testing::Test {
public:
InputDeviceSettingsProviderTest() = default;
~InputDeviceSettingsProviderTest() override = default;

void SetUp() override {
feature_list_ = std::make_unique<base::test::ScopedFeatureList>();
feature_list_->InitAndEnableFeature(features::kInputDeviceSettingsSplit);

controller_ = std::make_unique<FakeInputDeviceSettingsController>();
provider_ =
std::make_unique<InputDeviceSettingsProvider>(controller_.get());
}

void TearDown() override {
provider_.reset();
controller_.reset();
feature_list_.reset();
}

protected:
std::unique_ptr<FakeInputDeviceSettingsController> controller_;
std::unique_ptr<InputDeviceSettingsProvider> provider_;
std::unique_ptr<base::test::ScopedFeatureList> feature_list_;
};

TEST_F(InputDeviceSettingsProviderTest, TestGetConnectedKeyboards) {
std::vector<::ash::mojom::KeyboardPtr> expected_keyboards;
expected_keyboards.push_back(keyboard1.Clone());
controller_->AddKeyboard(keyboard1.Clone());
provider_->GetConnectedKeyboards(
base::BindOnce(ExpectListsEqual<::ash::mojom::KeyboardPtr>,
CloneMojomVector(expected_keyboards)));

expected_keyboards.push_back(keyboard2.Clone());
controller_->AddKeyboard(keyboard2.Clone());
provider_->GetConnectedKeyboards(
base::BindOnce(ExpectListsEqual<::ash::mojom::KeyboardPtr>,
CloneMojomVector(expected_keyboards)));
}

} // namespace ash::settings
8 changes: 7 additions & 1 deletion chrome/browser/ui/webui/settings/ash/os_settings_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include "chrome/browser/ui/webui/settings/ash/os_settings_manager.h"

#include "ash/constants/ash_features.h"
#include "ash/public/cpp/input_device_settings_controller.h"
#include "chrome/browser/ui/webui/settings/ash/hierarchy.h"
#include "chrome/browser/ui/webui/settings/ash/input_device_settings/input_device_settings_provider.h"
#include "chrome/browser/ui/webui/settings/ash/os_apps_page/app_notification_handler.h"
#include "chrome/browser/ui/webui/settings/ash/os_settings_sections.h"
#include "chrome/browser/ui/webui/settings/ash/search/search_handler.h"
Expand Down Expand Up @@ -56,7 +58,10 @@ OsSettingsManager::OsSettingsManager(
hierarchy_.get(),
local_search_service_proxy)),
app_notification_handler_(
std::make_unique<AppNotificationHandler>(app_service_proxy)) {}
std::make_unique<AppNotificationHandler>(app_service_proxy)),
input_device_settings_provider_(
std::make_unique<InputDeviceSettingsProvider>(
InputDeviceSettingsController::Get())) {}

OsSettingsManager::~OsSettingsManager() = default;

Expand All @@ -75,6 +80,7 @@ void OsSettingsManager::AddHandlers(content::WebUI* web_ui) {
void OsSettingsManager::Shutdown() {
// Note: These must be deleted in the opposite order of their creation to
// prevent against UAF violations.
input_device_settings_provider_.reset();
app_notification_handler_.reset();
search_handler_.reset();
settings_user_action_tracker_.reset();
Expand Down
6 changes: 6 additions & 0 deletions chrome/browser/ui/webui/settings/ash/os_settings_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "ash/webui/eche_app_ui/eche_app_manager.h"
#include "base/gtest_prod_util.h"
#include "chrome/browser/apps/app_service/app_service_proxy_forward.h"
#include "chrome/browser/ui/webui/settings/ash/input_device_settings/input_device_settings_provider.h"
#include "components/keyed_service/core/keyed_service.h"

class ArcAppListPrefs;
Expand Down Expand Up @@ -118,6 +119,10 @@ class OsSettingsManager : public KeyedService {
return app_notification_handler_.get();
}

InputDeviceSettingsProvider* input_device_settings_provider() {
return input_device_settings_provider_.get();
}

SearchHandler* search_handler() { return search_handler_.get(); }

SettingsUserActionTracker* settings_user_action_tracker() {
Expand All @@ -138,6 +143,7 @@ class OsSettingsManager : public KeyedService {
std::unique_ptr<SettingsUserActionTracker> settings_user_action_tracker_;
std::unique_ptr<SearchHandler> search_handler_;
std::unique_ptr<AppNotificationHandler> app_notification_handler_;
std::unique_ptr<InputDeviceSettingsProvider> input_device_settings_provider_;
};

} // namespace settings
Expand Down

0 comments on commit ec6f43b

Please sign in to comment.