Commit 0ebd9e48 authored by jamescook's avatar jamescook Committed by Commit bot

chromeos: Introduce SetClient() on ash::mojom::SystemTray interface

Previously ash would make a direct connection to Chrome (content_browser) to
handle showing system tray menu UI. This is a layering violation and makes
it more difficult to run ash without chrome.

Chrome's SystemTrayClient now explicitly registers itself with ash on startup.
It no longer attempts to reconnect on error; my understanding is that chrome
tolerating an ash crash is not a goal.

BUG=none
TEST=manual, can still open settings via system tray menu

Review-Url: https://codereview.chromium.org/2525813004
Cr-Commit-Position: refs/heads/master@{#434736}
parent baa32fe2
...@@ -6,117 +6,112 @@ ...@@ -6,117 +6,112 @@
#include "ash/common/system/tray/system_tray_notifier.h" #include "ash/common/system/tray/system_tray_notifier.h"
#include "ash/common/wm_shell.h" #include "ash/common/wm_shell.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "content/public/common/service_names.mojom.h"
#include "services/service_manager/public/cpp/connector.h"
namespace ash { namespace ash {
SystemTrayController::SystemTrayController( SystemTrayController::SystemTrayController()
service_manager::Connector* connector) : hour_clock_type_(base::GetHourClockType()) {}
: connector_(connector), hour_clock_type_(base::GetHourClockType()) {}
SystemTrayController::~SystemTrayController() {} SystemTrayController::~SystemTrayController() {}
void SystemTrayController::ShowSettings() { void SystemTrayController::ShowSettings() {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->ShowSettings(); system_tray_client_->ShowSettings();
} }
void SystemTrayController::ShowDateSettings() { void SystemTrayController::ShowDateSettings() {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->ShowDateSettings(); system_tray_client_->ShowDateSettings();
} }
void SystemTrayController::ShowSetTimeDialog() { void SystemTrayController::ShowSetTimeDialog() {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->ShowSetTimeDialog(); system_tray_client_->ShowSetTimeDialog();
} }
void SystemTrayController::ShowDisplaySettings() { void SystemTrayController::ShowDisplaySettings() {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->ShowDisplaySettings(); system_tray_client_->ShowDisplaySettings();
} }
void SystemTrayController::ShowPowerSettings() { void SystemTrayController::ShowPowerSettings() {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->ShowPowerSettings(); system_tray_client_->ShowPowerSettings();
} }
void SystemTrayController::ShowChromeSlow() { void SystemTrayController::ShowChromeSlow() {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->ShowChromeSlow(); system_tray_client_->ShowChromeSlow();
} }
void SystemTrayController::ShowIMESettings() { void SystemTrayController::ShowIMESettings() {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->ShowIMESettings(); system_tray_client_->ShowIMESettings();
} }
void SystemTrayController::ShowHelp() { void SystemTrayController::ShowHelp() {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->ShowHelp(); system_tray_client_->ShowHelp();
} }
void SystemTrayController::ShowAccessibilityHelp() { void SystemTrayController::ShowAccessibilityHelp() {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->ShowAccessibilityHelp(); system_tray_client_->ShowAccessibilityHelp();
} }
void SystemTrayController::ShowAccessibilitySettings() { void SystemTrayController::ShowAccessibilitySettings() {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->ShowAccessibilitySettings(); system_tray_client_->ShowAccessibilitySettings();
} }
void SystemTrayController::ShowPaletteHelp() { void SystemTrayController::ShowPaletteHelp() {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->ShowPaletteHelp(); system_tray_client_->ShowPaletteHelp();
} }
void SystemTrayController::ShowPaletteSettings() { void SystemTrayController::ShowPaletteSettings() {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->ShowPaletteSettings(); system_tray_client_->ShowPaletteSettings();
} }
void SystemTrayController::ShowPublicAccountInfo() { void SystemTrayController::ShowPublicAccountInfo() {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->ShowPublicAccountInfo(); system_tray_client_->ShowPublicAccountInfo();
} }
void SystemTrayController::ShowNetworkConfigure(const std::string& network_id) { void SystemTrayController::ShowNetworkConfigure(const std::string& network_id) {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->ShowNetworkConfigure(network_id); system_tray_client_->ShowNetworkConfigure(network_id);
} }
void SystemTrayController::ShowNetworkCreate(const std::string& type) { void SystemTrayController::ShowNetworkCreate(const std::string& type) {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->ShowNetworkCreate(type); system_tray_client_->ShowNetworkCreate(type);
} }
void SystemTrayController::ShowThirdPartyVpnCreate( void SystemTrayController::ShowThirdPartyVpnCreate(
const std::string& extension_id) { const std::string& extension_id) {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->ShowThirdPartyVpnCreate(extension_id); system_tray_client_->ShowThirdPartyVpnCreate(extension_id);
} }
void SystemTrayController::ShowNetworkSettings(const std::string& network_id) { void SystemTrayController::ShowNetworkSettings(const std::string& network_id) {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->ShowNetworkSettings(network_id); system_tray_client_->ShowNetworkSettings(network_id);
} }
void SystemTrayController::ShowProxySettings() { void SystemTrayController::ShowProxySettings() {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->ShowProxySettings(); system_tray_client_->ShowProxySettings();
} }
void SystemTrayController::SignOut() { void SystemTrayController::SignOut() {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->SignOut(); system_tray_client_->SignOut();
} }
void SystemTrayController::RequestRestartForUpdate() { void SystemTrayController::RequestRestartForUpdate() {
if (ConnectToSystemTrayClient()) if (system_tray_client_)
system_tray_client_->RequestRestartForUpdate(); system_tray_client_->RequestRestartForUpdate();
} }
...@@ -124,33 +119,8 @@ void SystemTrayController::BindRequest(mojom::SystemTrayRequest request) { ...@@ -124,33 +119,8 @@ void SystemTrayController::BindRequest(mojom::SystemTrayRequest request) {
bindings_.AddBinding(this, std::move(request)); bindings_.AddBinding(this, std::move(request));
} }
bool SystemTrayController::ConnectToSystemTrayClient() { void SystemTrayController::SetClient(mojom::SystemTrayClientPtr client) {
// Unit tests may not have a connector. system_tray_client_ = std::move(client);
if (!connector_)
return false;
#if defined(OS_CHROMEOS)
// If already connected, nothing to do.
if (system_tray_client_.is_bound())
return true;
// Connect (or reconnect) to the interface.
connector_->ConnectToInterface(content::mojom::kBrowserServiceName,
&system_tray_client_);
// Handle chrome crashes by forcing a reconnect on the next request.
system_tray_client_.set_connection_error_handler(base::Bind(
&SystemTrayController::OnClientConnectionError, base::Unretained(this)));
return true;
#else
// The SystemTrayClient interface in the browser is only implemented for
// Chrome OS, so don't try to connect on other platforms.
return false;
#endif // defined(OS_CHROMEOS)
}
void SystemTrayController::OnClientConnectionError() {
system_tray_client_.reset();
} }
void SystemTrayController::SetUse24HourClock(bool use_24_hour) { void SystemTrayController::SetUse24HourClock(bool use_24_hour) {
......
...@@ -12,29 +12,21 @@ ...@@ -12,29 +12,21 @@
#include "base/macros.h" #include "base/macros.h"
#include "mojo/public/cpp/bindings/binding_set.h" #include "mojo/public/cpp/bindings/binding_set.h"
namespace service_manager {
class Connector;
}
namespace ash { namespace ash {
// Both implements mojom::SystemTray and wraps the mojom::SystemTrayClient // Both implements mojom::SystemTray and wraps the mojom::SystemTrayClient
// interface. The wrapper makes the initial connection and handles reconnecting // interface. Implements both because it caches state pushed down from the
// on error. Implements both because it caches state pushed down from the
// browser process via SystemTray so it can be synchronously queried inside ash. // browser process via SystemTray so it can be synchronously queried inside ash.
// //
// Conceptually similar to historical ash-to-chrome interfaces like // Conceptually similar to historical ash-to-chrome interfaces like
// SystemTrayDelegate. Lives on the main thread. // SystemTrayDelegate. Lives on the main thread.
// //
// Only connects to the actual mojom::SystemTrayClient interface when running on
// Chrome OS. In tests and on Windows all operations are no-ops.
//
// TODO: Consider renaming this to SystemTrayClient or renaming the current // TODO: Consider renaming this to SystemTrayClient or renaming the current
// SystemTray to SystemTrayView and making this class SystemTray. // SystemTray to SystemTrayView and making this class SystemTray.
class ASH_EXPORT SystemTrayController class ASH_EXPORT SystemTrayController
: NON_EXPORTED_BASE(public mojom::SystemTray) { : NON_EXPORTED_BASE(public mojom::SystemTray) {
public: public:
explicit SystemTrayController(service_manager::Connector* connector); SystemTrayController();
~SystemTrayController() override; ~SystemTrayController() override;
base::HourClockType hour_clock_type() const { return hour_clock_type_; } base::HourClockType hour_clock_type() const { return hour_clock_type_; }
...@@ -65,19 +57,10 @@ class ASH_EXPORT SystemTrayController ...@@ -65,19 +57,10 @@ class ASH_EXPORT SystemTrayController
void BindRequest(mojom::SystemTrayRequest request); void BindRequest(mojom::SystemTrayRequest request);
private: private:
// Connects or reconnects to the mojom::SystemTrayClient interface when
// running on Chrome OS. Otherwise does nothing. Returns true if connected.
bool ConnectToSystemTrayClient();
// Handles errors on the |system_tray_client_| interface connection.
void OnClientConnectionError();
// mojom::SystemTray: // mojom::SystemTray:
void SetClient(mojom::SystemTrayClientPtr client) override;
void SetUse24HourClock(bool use_24_hour) override; void SetUse24HourClock(bool use_24_hour) override;
// May be null in unit tests.
service_manager::Connector* connector_;
// Client interface in chrome browser. Only bound on Chrome OS. // Client interface in chrome browser. Only bound on Chrome OS.
mojom::SystemTrayClientPtr system_tray_client_; mojom::SystemTrayClientPtr system_tray_client_;
......
...@@ -255,8 +255,7 @@ WmShell::WmShell(std::unique_ptr<ShellDelegate> shell_delegate) ...@@ -255,8 +255,7 @@ WmShell::WmShell(std::unique_ptr<ShellDelegate> shell_delegate)
delegate_->GetShellConnector())), delegate_->GetShellConnector())),
shelf_controller_(base::MakeUnique<ShelfController>()), shelf_controller_(base::MakeUnique<ShelfController>()),
shutdown_controller_(base::MakeUnique<ShutdownController>()), shutdown_controller_(base::MakeUnique<ShutdownController>()),
system_tray_controller_(base::MakeUnique<SystemTrayController>( system_tray_controller_(base::MakeUnique<SystemTrayController>()),
delegate_->GetShellConnector())),
system_tray_notifier_(base::MakeUnique<SystemTrayNotifier>()), system_tray_notifier_(base::MakeUnique<SystemTrayNotifier>()),
wallpaper_delegate_(delegate_->CreateWallpaperDelegate()), wallpaper_delegate_(delegate_->CreateWallpaperDelegate()),
window_cycle_controller_(base::MakeUnique<WindowCycleController>()), window_cycle_controller_(base::MakeUnique<WindowCycleController>()),
......
...@@ -6,6 +6,9 @@ module ash.mojom; ...@@ -6,6 +6,9 @@ module ash.mojom;
// Allows clients (e.g. Chrome browser) to control the ash system tray menu. // Allows clients (e.g. Chrome browser) to control the ash system tray menu.
interface SystemTray { interface SystemTray {
// Sets the client interface.
SetClient(SystemTrayClient client);
// Sets the clock to use 24 hour time formatting if |use_24_hour| is true. // Sets the clock to use 24 hour time formatting if |use_24_hour| is true.
// Otherwise sets 12 hour time formatting. // Otherwise sets 12 hour time formatting.
SetUse24HourClock(bool use_24_hour); SetUse24HourClock(bool use_24_hour);
......
...@@ -24,7 +24,6 @@ ...@@ -24,7 +24,6 @@
"ash::mojom::ShelfController", "ash::mojom::ShelfController",
"ash::mojom::ShutdownController", "ash::mojom::ShutdownController",
"ash::mojom::SystemTray", "ash::mojom::SystemTray",
"ash::mojom::SystemTrayClient",
"ash::mojom::WallpaperController", "ash::mojom::WallpaperController",
"ash::mojom::WallpaperManager", "ash::mojom::WallpaperManager",
"ash::mojom::VolumeController", "ash::mojom::VolumeController",
......
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
#include "chrome/browser/ui/ash/ash_util.h" #include "chrome/browser/ui/ash/ash_util.h"
#include "chrome/browser/ui/ash/chrome_new_window_client.h" #include "chrome/browser/ui/ash/chrome_new_window_client.h"
#include "chrome/browser/ui/ash/keyboard_ui_service.h" #include "chrome/browser/ui/ash/keyboard_ui_service.h"
#include "chrome/browser/ui/ash/system_tray_client.h"
#include "chrome/browser/ui/ash/volume_controller_chromeos.h" #include "chrome/browser/ui/ash/volume_controller_chromeos.h"
#include "chrome/browser/ui/browser_commands.h" #include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h" #include "chrome/browser/ui/browser_finder.h"
...@@ -125,11 +124,6 @@ class FactoryImpl { ...@@ -125,11 +124,6 @@ class FactoryImpl {
std::move(request)); std::move(request));
} }
void BindRequest(ash::mojom::SystemTrayClientRequest request) {
system_tray_client_bindings_.AddBinding(SystemTrayClient::Get(),
std::move(request));
}
void BindRequest(ash::mojom::WallpaperManagerRequest request) { void BindRequest(ash::mojom::WallpaperManagerRequest request) {
WallpaperManager::Get()->BindRequest(std::move(request)); WallpaperManager::Get()->BindRequest(std::move(request));
} }
...@@ -154,7 +148,6 @@ class FactoryImpl { ...@@ -154,7 +148,6 @@ class FactoryImpl {
std::unique_ptr<ChromeLaunchable> launchable_; std::unique_ptr<ChromeLaunchable> launchable_;
std::unique_ptr<ChromeNewWindowClient> new_window_client_; std::unique_ptr<ChromeNewWindowClient> new_window_client_;
mojo::BindingSet<ash::mojom::NewWindowClient> new_window_client_bindings_; mojo::BindingSet<ash::mojom::NewWindowClient> new_window_client_bindings_;
mojo::BindingSet<ash::mojom::SystemTrayClient> system_tray_client_bindings_;
std::unique_ptr<VolumeController> volume_controller_; std::unique_ptr<VolumeController> volume_controller_;
std::unique_ptr<AppListPresenterService> app_list_presenter_service_; std::unique_ptr<AppListPresenterService> app_list_presenter_service_;
mojo::BindingSet<app_list::mojom::AppListPresenter> mojo::BindingSet<app_list::mojom::AppListPresenter>
...@@ -186,8 +179,6 @@ bool ChromeInterfaceFactory::OnConnect( ...@@ -186,8 +179,6 @@ bool ChromeInterfaceFactory::OnConnect(
main_thread_task_runner_); main_thread_task_runner_);
FactoryImpl::AddFactory<ash::mojom::NewWindowClient>( FactoryImpl::AddFactory<ash::mojom::NewWindowClient>(
registry, main_thread_task_runner_); registry, main_thread_task_runner_);
FactoryImpl::AddFactory<ash::mojom::SystemTrayClient>(
registry, main_thread_task_runner_);
FactoryImpl::AddFactory<ash::mojom::VolumeController>( FactoryImpl::AddFactory<ash::mojom::VolumeController>(
registry, main_thread_task_runner_); registry, main_thread_task_runner_);
FactoryImpl::AddFactory<ash::mojom::WallpaperManager>( FactoryImpl::AddFactory<ash::mojom::WallpaperManager>(
......
...@@ -9,8 +9,6 @@ ...@@ -9,8 +9,6 @@
#include "ash/common/wm_shell.h" #include "ash/common/wm_shell.h"
#include "ash/public/cpp/shell_window_ids.h" #include "ash/public/cpp/shell_window_ids.h"
#include "ash/shell.h" #include "ash/shell.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/logging.h" #include "base/logging.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h" #include "chrome/browser/browser_process_platform_part.h"
...@@ -63,7 +61,20 @@ void ShowSettingsSubPageForActiveUser(const std::string& sub_page) { ...@@ -63,7 +61,20 @@ void ShowSettingsSubPageForActiveUser(const std::string& sub_page) {
} // namespace } // namespace
SystemTrayClient::SystemTrayClient() { SystemTrayClient::SystemTrayClient() : binding_(this) {
service_manager::Connector* connector =
content::ServiceManagerConnection::GetForProcess()->GetConnector();
// Connect to the SystemTray interface in ash. Under mash the SystemTray
// interface is in the ash process. In classic ash we provide it to ourself.
if (chrome::IsRunningInMash()) {
connector->ConnectToInterface("ash", &system_tray_);
} else {
connector->ConnectToInterface(content::mojom::kBrowserServiceName,
&system_tray_);
}
// Register this object as the client interface implementation.
system_tray_->SetClient(binding_.CreateInterfacePtrAndBind());
// If this observes clock setting changes before ash comes up the IPCs will // If this observes clock setting changes before ash comes up the IPCs will
// be queued on |system_tray_|. // be queued on |system_tray_|.
g_browser_process->platform_part()->GetSystemClock()->AddObserver(this); g_browser_process->platform_part()->GetSystemClock()->AddObserver(this);
...@@ -314,30 +325,5 @@ void SystemTrayClient::RequestRestartForUpdate() { ...@@ -314,30 +325,5 @@ void SystemTrayClient::RequestRestartForUpdate() {
void SystemTrayClient::OnSystemClockChanged( void SystemTrayClient::OnSystemClockChanged(
chromeos::system::SystemClock* clock) { chromeos::system::SystemClock* clock) {
ConnectToSystemTray();
system_tray_->SetUse24HourClock(clock->ShouldUse24HourClock()); system_tray_->SetUse24HourClock(clock->ShouldUse24HourClock());
} }
void SystemTrayClient::ConnectToSystemTray() {
if (system_tray_.is_bound())
return;
service_manager::Connector* connector =
content::ServiceManagerConnection::GetForProcess()->GetConnector();
// Under mash the SystemTray interface is in the ash process. In classic ash
// we provide it to ourself.
if (chrome::IsRunningInMash()) {
connector->ConnectToInterface("ash", &system_tray_);
} else {
connector->ConnectToInterface(content::mojom::kBrowserServiceName,
&system_tray_);
}
// Tolerate ash crashing and coming back up.
system_tray_.set_connection_error_handler(base::Bind(
&SystemTrayClient::OnClientConnectionError, base::Unretained(this)));
}
void SystemTrayClient::OnClientConnectionError() {
system_tray_.reset();
}
...@@ -69,15 +69,12 @@ class SystemTrayClient : public ash::mojom::SystemTrayClient, ...@@ -69,15 +69,12 @@ class SystemTrayClient : public ash::mojom::SystemTrayClient,
// chromeos::system::SystemClockObserver: // chromeos::system::SystemClockObserver:
void OnSystemClockChanged(chromeos::system::SystemClock* clock) override; void OnSystemClockChanged(chromeos::system::SystemClock* clock) override;
// Connects or reconnects the |system_tray_| interface.
void ConnectToSystemTray();
// Handles errors on the |system_tray_| interface connection.
void OnClientConnectionError();
// System tray mojo service in ash. // System tray mojo service in ash.
ash::mojom::SystemTrayPtr system_tray_; ash::mojom::SystemTrayPtr system_tray_;
// Binds this object to the client interface.
mojo::Binding<ash::mojom::SystemTrayClient> binding_;
DISALLOW_COPY_AND_ASSIGN(SystemTrayClient); DISALLOW_COPY_AND_ASSIGN(SystemTrayClient);
}; };
......
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