From 5953178290205c37dbe478fcba0048d2141dcaf3 Mon Sep 17 00:00:00 2001 From: ben Date: Thu, 17 Mar 2016 17:54:45 -0700 Subject: Rather than keeping it around forever, we should kill it. Now that the user shell is acting as the login broker for apps run for that user, it's not necessary to keep login too. This simplifies the login app a bit. BUG= Review URL: https://codereview.chromium.org/1806203002 Cr-Commit-Position: refs/heads/master@{#381856} --- mash/init/init.cc | 7 +- mash/init/init.h | 1 + mash/init/manifest.json | 2 +- mash/login/BUILD.gn | 2 - mash/login/login.cc | 209 +++++++++++++++++++++++++++++++++++++++--------- mash/login/login.h | 41 +--------- mash/login/main.cc | 2 +- mash/login/ui.cc | 136 ------------------------------- mash/login/ui.h | 69 ---------------- 9 files changed, 181 insertions(+), 288 deletions(-) delete mode 100644 mash/login/ui.cc delete mode 100644 mash/login/ui.h (limited to 'mash') diff --git a/mash/init/init.cc b/mash/init/init.cc index aab8448..118482a 100644 --- a/mash/init/init.cc +++ b/mash/init/init.cc @@ -28,6 +28,11 @@ void Init::Initialize(mojo::Connector* connector, StartLogin(); } +bool Init::AcceptConnection(mojo::Connection* connection) { + connection->AddInterface(this); + return true; +} + void Init::StartService(const mojo::String& name, const mojo::String& user_id) { if (user_services_.find(user_id) == user_services_.end()) { @@ -66,8 +71,6 @@ void Init::StartResourceProvider() { void Init::StartLogin() { login_connection_ = connector_->Connect("mojo:login"); login_connection_->AddInterface(this); - login_connection_->SetConnectionLostClosure( - base::Bind(&Init::StartLogin, base::Unretained(this))); mash::login::mojom::LoginPtr login; login_connection_->GetInterface(&login); login->ShowLoginUI(); diff --git a/mash/init/init.h b/mash/init/init.h index 452b089..7f27a6d 100644 --- a/mash/init/init.h +++ b/mash/init/init.h @@ -33,6 +33,7 @@ class Init : public mojo::ShellClient, // mojo::ShellClient: void Initialize(mojo::Connector* connector, const mojo::Identity& identity, uint32_t id) override; + bool AcceptConnection(mojo::Connection* connection) override; // mojo::InterfaceFactory: void Create(mojo::Connection* connection, diff --git a/mash/init/manifest.json b/mash/init/manifest.json index c407094..70c79e2 100644 --- a/mash/init/manifest.json +++ b/mash/init/manifest.json @@ -5,7 +5,7 @@ "capabilities": { "required": { "*": { "interfaces": [ "*" ] }, - "mojo:shell": { "classes": ["user_id"] } + "mojo:shell": { "classes": ["user_id", "all_users"] } } } } diff --git a/mash/login/BUILD.gn b/mash/login/BUILD.gn index bf0bbb9..a9d7f98 100644 --- a/mash/login/BUILD.gn +++ b/mash/login/BUILD.gn @@ -13,8 +13,6 @@ mojo_native_application("login") { "login.cc", "login.h", "main.cc", - "ui.cc", - "ui.h", ] deps = [ diff --git a/mash/login/login.cc b/mash/login/login.cc index 1ae4e77..e5c566b 100644 --- a/mash/login/login.cc +++ b/mash/login/login.cc @@ -4,72 +4,203 @@ #include "mash/login/login.h" +#include + +#include "base/guid.h" #include "base/macros.h" -#include "mash/login/ui.h" -#include "mojo/public/cpp/bindings/strong_binding.h" +#include "base/message_loop/message_loop.h" +#include "base/strings/utf_string_conversions.h" +#include "components/mus/public/cpp/property_type_converters.h" +#include "components/mus/public/interfaces/user_access_manager.mojom.h" +#include "mash/init/public/interfaces/init.mojom.h" +#include "mash/login/public/interfaces/login.mojom.h" +#include "mash/wm/public/interfaces/container.mojom.h" +#include "mojo/public/cpp/bindings/binding_set.h" +#include "mojo/services/tracing/public/cpp/tracing_impl.h" #include "mojo/shell/public/cpp/connector.h" +#include "mojo/shell/public/cpp/shell_client.h" +#include "ui/views/background.h" +#include "ui/views/controls/button/label_button.h" #include "ui/views/mus/aura_init.h" +#include "ui/views/mus/native_widget_mus.h" #include "ui/views/mus/window_manager_connection.h" +#include "ui/views/widget/widget_delegate.h" namespace mash { namespace login { namespace { -class Login : public mojom::Login { +class Login; + +class UI : public views::WidgetDelegateView, + public views::ButtonListener { + public: + static void Show(mojo::Connector* connector, Login* login) { + UI* ui = new UI(login, connector); + ui->StartWindowManager(); + + views::WindowManagerConnection::Create(connector); + + views::Widget* widget = new views::Widget; + views::Widget::InitParams params( + views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); + params.delegate = ui; + + std::map> properties; + properties[mash::wm::mojom::kWindowContainer_Property] = + mojo::TypeConverter, int32_t>::Convert( + static_cast(mash::wm::mojom::Container::LOGIN_WINDOWS)); + mus::Window* window = + views::WindowManagerConnection::Get()->NewWindow(properties); + params.native_widget = new views::NativeWidgetMus( + widget, connector, window, mus::mojom::SurfaceType::DEFAULT); + widget->Init(params); + widget->Show(); + } + + private: + UI(Login* login, mojo::Connector* connector) + : login_(login), + connector_(connector), + user_id_1_("00000000-0000-4000-8000-000000000000"), + user_id_2_("00000000-0000-4000-8000-000000000001"), + login_button_1_( + new views::LabelButton(this, base::ASCIIToUTF16("Timothy"))), + login_button_2_( + new views::LabelButton(this, base::ASCIIToUTF16("Jimothy"))) { + set_background(views::Background::CreateSolidBackground(SK_ColorRED)); + login_button_1_->SetStyle(views::Button::STYLE_BUTTON); + login_button_2_->SetStyle(views::Button::STYLE_BUTTON); + AddChildView(login_button_1_); + AddChildView(login_button_2_); + } + ~UI() override { + // Prevent the window manager from restarting during graceful shutdown. + window_manager_connection_->SetConnectionLostClosure(base::Closure()); + base::MessageLoop::current()->QuitWhenIdle(); + } + + // Overridden from views::WidgetDelegate: + views::View* GetContentsView() override { return this; } + base::string16 GetWindowTitle() const override { + // TODO(beng): use resources. + return base::ASCIIToUTF16("Login"); + } + void DeleteDelegate() override { delete this; } + + // Overridden from views::View: + void Layout() override { + gfx::Rect button_box = GetLocalBounds(); + button_box.Inset(10, 10); + + gfx::Size ps1 = login_button_1_->GetPreferredSize(); + gfx::Size ps2 = login_button_2_->GetPreferredSize(); + + DCHECK(ps1.height() == ps2.height()); + + // The 10 is inter-button spacing. + button_box.set_x((button_box.width() - ps1.width() - ps2.width() - 10) / 2); + button_box.set_y((button_box.height() - ps1.height()) / 2); + + login_button_1_->SetBounds(button_box.x(), button_box.y(), ps1.width(), + ps1.height()); + login_button_2_->SetBounds(login_button_1_->bounds().right() + 10, + button_box.y(), ps2.width(), ps2.height()); + } + + // Overridden from views::ButtonListener: + void ButtonPressed(views::Button* sender, const ui::Event& event) override; + + void StartWindowManager() { + window_manager_connection_ = connector_->Connect("mojo:desktop_wm"); + window_manager_connection_->SetConnectionLostClosure( + base::Bind(&UI::StartWindowManager, base::Unretained(this))); + } + + Login* login_; + mojo::Connector* connector_; + const std::string user_id_1_; + const std::string user_id_2_; + views::LabelButton* login_button_1_; + views::LabelButton* login_button_2_; + scoped_ptr window_manager_connection_; + + DISALLOW_COPY_AND_ASSIGN(UI); +}; + +class Login : public mojo::ShellClient, + public mojo::InterfaceFactory, + public mojom::Login { public: - Login(mojo::Connector* connector, - LoginController* controller, - const std::string& user_id, - mojom::LoginRequest request) - : connector_(connector), - controller_(controller), - user_id_(user_id), - binding_(this, std::move(request)) {} + Login() {} ~Login() override {} + void LoginAs(const std::string& user_id) { + user_access_manager_->SetActiveUser(user_id); + mash::init::mojom::InitPtr init; + connector_->ConnectToInterface("mojo:mash_init", &init); + init->StartService("mojo:mash_shell", user_id); + } + private: + // mojo::ShellClient: + void Initialize(mojo::Connector* connector, const mojo::Identity& identity, + uint32_t id) override { + connector_ = connector; + tracing_.Initialize(connector, identity.name()); + + aura_init_.reset(new views::AuraInit(connector, "views_mus_resources.pak")); + + connector_->ConnectToInterface("mojo:mus", &user_access_manager_); + user_access_manager_->SetActiveUser(identity.user_id()); + } + bool AcceptConnection(mojo::Connection* connection) override { + connection->AddInterface(this); + return true; + } + + // mojo::InterfaceFactory: + void Create(mojo::Connection* connection, + mojom::LoginRequest request) override { + bindings_.AddBinding(this, std::move(request)); + } + // mojom::Login: void ShowLoginUI() override { - UI::Show(connector_, controller_); + UI::Show(connector_, this); } void SwitchUser() override { - UI::Show(connector_, controller_); + UI::Show(connector_, this); } + void StartWindowManager(); + mojo::Connector* connector_; - LoginController* controller_; - const std::string user_id_; - mojo::StrongBinding binding_; + mojo::TracingImpl tracing_; + scoped_ptr aura_init_; + mojo::BindingSet bindings_; + mus::mojom::UserAccessManagerPtr user_access_manager_; + scoped_ptr window_manager_connection_; DISALLOW_COPY_AND_ASSIGN(Login); }; -} // namespace - -LoginController::LoginController() {} -LoginController::~LoginController() {} - -void LoginController::Initialize(mojo::Connector* connector, - const mojo::Identity& identity, - uint32_t id) { - connector_ = connector; - login_user_id_ = identity.user_id(); - tracing_.Initialize(connector, identity.name()); - - aura_init_.reset(new views::AuraInit(connector, "views_mus_resources.pak")); +void UI::ButtonPressed(views::Button* sender, const ui::Event& event) { + // Login... + if (sender == login_button_1_) { + login_->LoginAs(user_id_1_); + } else if (sender == login_button_2_) { + login_->LoginAs(user_id_2_); + } else { + NOTREACHED(); + } + GetWidget()->Close(); } -bool LoginController::AcceptConnection(mojo::Connection* connection) { - if (connection->GetRemoteIdentity().name() == "mojo:mash_init") - connection->GetInterface(&init_); - connection->AddInterface(this); - return true; -} +} // namespace -void LoginController::Create(mojo::Connection* connection, - mojom::LoginRequest request) { - new Login(connector_, this, connection->GetRemoteIdentity().user_id(), - std::move(request)); +mojo::ShellClient* CreateLogin() { + return new Login; } } // namespace login diff --git a/mash/login/login.h b/mash/login/login.h index b50837d..8f2c9f3 100644 --- a/mash/login/login.h +++ b/mash/login/login.h @@ -5,49 +5,14 @@ #ifndef MASH_LOGIN_LOGIN_H_ #define MASH_LOGIN_LOGIN_H_ -#include - -#include "base/macros.h" -#include "base/memory/scoped_ptr.h" -#include "mash/init/public/interfaces/init.mojom.h" -#include "mash/login/public/interfaces/login.mojom.h" -#include "mojo/services/tracing/public/cpp/tracing_impl.h" -#include "mojo/shell/public/cpp/shell_client.h" - -namespace views { -class AuraInit; +namespace mojo { +class ShellClient; } namespace mash { namespace login { -class LoginController : public mojo::ShellClient, - public mojo::InterfaceFactory { - public: - LoginController(); - ~LoginController() override; - - init::mojom::Init* init() { return init_.get(); } - const std::string& login_user_id() const { return login_user_id_; } - - private: - // mojo::ShellClient: - void Initialize(mojo::Connector* connector, const mojo::Identity& identity, - uint32_t id) override; - bool AcceptConnection(mojo::Connection* connection) override; - - // mojo::InterfaceFactory: - void Create(mojo::Connection* connection, - mojom::LoginRequest request) override; - - mojo::Connector* connector_; - std::string login_user_id_; - mojo::TracingImpl tracing_; - scoped_ptr aura_init_; - init::mojom::InitPtr init_; - - DISALLOW_COPY_AND_ASSIGN(LoginController); -}; +mojo::ShellClient* CreateLogin(); } // namespace login } // namespace mash diff --git a/mash/login/main.cc b/mash/login/main.cc index 1901dd0..753f7db 100644 --- a/mash/login/main.cc +++ b/mash/login/main.cc @@ -7,6 +7,6 @@ #include "mojo/shell/public/cpp/application_runner.h" MojoResult MojoMain(MojoHandle shell_handle) { - mojo::ApplicationRunner runner(new mash::login::LoginController); + mojo::ApplicationRunner runner(mash::login::CreateLogin()); return runner.Run(shell_handle); } diff --git a/mash/login/ui.cc b/mash/login/ui.cc deleted file mode 100644 index b2f1770..0000000 --- a/mash/login/ui.cc +++ /dev/null @@ -1,136 +0,0 @@ -// Copyright 2016 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 "mash/login/ui.h" - -#include "base/guid.h" -#include "base/strings/utf_string_conversions.h" -#include "components/mus/public/cpp/property_type_converters.h" -#include "mash/login/login.h" -#include "mash/wm/public/interfaces/container.mojom.h" -#include "mojo/shell/public/cpp/connector.h" -#include "ui/views/background.h" -#include "ui/views/mus/native_widget_mus.h" -#include "ui/views/mus/window_manager_connection.h" - -namespace mash { -namespace login { - -// static -bool UI::is_showing_ = false; - -// static -void UI::Show(mojo::Connector* connector, LoginController* login_controller) { - // It's possible multiple clients may have a connection to the Login service, - // so make sure that only one login UI can be shown at a time. - if (is_showing_) - return; - - UI* ui = new UI(login_controller, connector); - - // TODO(beng): If this is only done once, it should be done in - // LoginController::Initialize(). However, for as yet unknown reasons it needs - // to be done the first time after UI(). Figure this out. Also, I'm not - // certain the window manager is being killed when this UI is closed. - if (!views::WindowManagerConnection::Exists()) - views::WindowManagerConnection::Create(connector); - - views::Widget* widget = new views::Widget; - views::Widget::InitParams params( - views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); - params.delegate = ui; - - std::map> properties; - properties[mash::wm::mojom::kWindowContainer_Property] = - mojo::TypeConverter, int32_t>::Convert( - static_cast(mash::wm::mojom::Container::LOGIN_WINDOWS)); - mus::Window* window = - views::WindowManagerConnection::Get()->NewWindow(properties); - params.native_widget = new views::NativeWidgetMus( - widget, connector, window, mus::mojom::SurfaceType::DEFAULT); - widget->Init(params); - widget->Show(); - - is_showing_ = true; -} - -UI::UI(LoginController* login_controller, mojo::Connector* connector) - : login_controller_(login_controller), - connector_(connector), - user_id_1_("00000000-0000-4000-8000-000000000000"), - user_id_2_("00000000-0000-4000-8000-000000000001"), - login_button_1_( - new views::LabelButton(this, base::ASCIIToUTF16("Timothy"))), - login_button_2_( - new views::LabelButton(this, base::ASCIIToUTF16("Jimothy"))) { - connector_->ConnectToInterface("mojo:mus", &user_access_manager_); - user_access_manager_->SetActiveUser(login_controller->login_user_id()); - StartWindowManager(); - - set_background(views::Background::CreateSolidBackground(SK_ColorRED)); - login_button_1_->SetStyle(views::Button::STYLE_BUTTON); - login_button_2_->SetStyle(views::Button::STYLE_BUTTON); - AddChildView(login_button_1_); - AddChildView(login_button_2_); -} - -UI::~UI() { - // Prevent the window manager from restarting during graceful shutdown. - window_manager_connection_->SetConnectionLostClosure(base::Closure()); - is_showing_ = false; - // TODO(beng): we should be terminating this app at this point. -} - -views::View* UI::GetContentsView() { return this; } - -base::string16 UI::GetWindowTitle() const { - // TODO(beng): use resources. - return base::ASCIIToUTF16("Login"); -} - -void UI::DeleteDelegate() { - delete this; -} - -void UI::Layout() { - gfx::Rect button_box = GetLocalBounds(); - button_box.Inset(10, 10); - - gfx::Size ps1 = login_button_1_->GetPreferredSize(); - gfx::Size ps2 = login_button_2_->GetPreferredSize(); - - DCHECK(ps1.height() == ps2.height()); - - // The 10 is inter-button spacing. - button_box.set_x((button_box.width() - ps1.width() - ps2.width() - 10) / 2); - button_box.set_y((button_box.height() - ps1.height()) / 2); - - login_button_1_->SetBounds(button_box.x(), button_box.y(), ps1.width(), - ps1.height()); - login_button_2_->SetBounds(login_button_1_->bounds().right() + 10, - button_box.y(), ps2.width(), ps2.height()); -} - -void UI::ButtonPressed(views::Button* sender, const ui::Event& event) { - // Login... - if (sender == login_button_1_) { - user_access_manager_->SetActiveUser(user_id_1_); - login_controller_->init()->StartService("mojo:mash_shell", user_id_1_); - } else if (sender == login_button_2_) { - user_access_manager_->SetActiveUser(user_id_2_); - login_controller_->init()->StartService("mojo:mash_shell", user_id_2_); - } else { - NOTREACHED(); - } - GetWidget()->Close(); -} - -void UI::StartWindowManager() { - window_manager_connection_ = connector_->Connect("mojo:desktop_wm"); - window_manager_connection_->SetConnectionLostClosure( - base::Bind(&UI::StartWindowManager, base::Unretained(this))); -} - -} // namespace login -} // namespace mash diff --git a/mash/login/ui.h b/mash/login/ui.h deleted file mode 100644 index 4c141c4..0000000 --- a/mash/login/ui.h +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright 2016 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. - -#ifndef MASH_LOGIN_UI_H_ -#define MASH_LOGIN_UI_H_ - -#include - -#include "base/macros.h" -#include "base/memory/scoped_ptr.h" -#include "components/mus/public/interfaces/user_access_manager.mojom.h" -#include "ui/views/controls/button/label_button.h" -#include "ui/views/widget/widget_delegate.h" - -namespace mojo { -class Connection; -class Connector; -} - -namespace views { -class AuraInit; -} - -namespace mash { -namespace login { - -class LoginController; - -class UI : public views::WidgetDelegateView, - public views::ButtonListener { - public: - static void Show(mojo::Connector* connector, - LoginController* login_controller); - - private: - UI(LoginController* login_controller, mojo::Connector* connector); - ~UI() override; - - // Overridden from views::WidgetDelegate: - views::View* GetContentsView() override; - base::string16 GetWindowTitle() const override; - void DeleteDelegate() override; - - // Overridden from views::View: - void Layout() override; - - // Overridden from views::ButtonListener: - void ButtonPressed(views::Button* sender, const ui::Event& event) override; - - void StartWindowManager(); - - static bool is_showing_; - LoginController* login_controller_; - mojo::Connector* connector_; - const std::string user_id_1_; - const std::string user_id_2_; - views::LabelButton* login_button_1_; - views::LabelButton* login_button_2_; - mus::mojom::UserAccessManagerPtr user_access_manager_; - scoped_ptr window_manager_connection_; - - DISALLOW_COPY_AND_ASSIGN(UI); -}; - -} // namespace login -} // namespace mash - -#endif // MASH_LOGIN_UI_H_ -- cgit v1.1