OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mash/login/login.h" | 5 #include "mash/login/login.h" |
6 | 6 |
| 7 #include <map> |
| 8 |
| 9 #include "base/guid.h" |
7 #include "base/macros.h" | 10 #include "base/macros.h" |
8 #include "mash/login/ui.h" | 11 #include "base/message_loop/message_loop.h" |
9 #include "mojo/public/cpp/bindings/strong_binding.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "components/mus/public/cpp/property_type_converters.h" |
| 14 #include "components/mus/public/interfaces/user_access_manager.mojom.h" |
| 15 #include "mash/init/public/interfaces/init.mojom.h" |
| 16 #include "mash/login/public/interfaces/login.mojom.h" |
| 17 #include "mash/wm/public/interfaces/container.mojom.h" |
| 18 #include "mojo/public/cpp/bindings/binding_set.h" |
| 19 #include "mojo/services/tracing/public/cpp/tracing_impl.h" |
10 #include "mojo/shell/public/cpp/connector.h" | 20 #include "mojo/shell/public/cpp/connector.h" |
| 21 #include "mojo/shell/public/cpp/shell_client.h" |
| 22 #include "ui/views/background.h" |
| 23 #include "ui/views/controls/button/label_button.h" |
11 #include "ui/views/mus/aura_init.h" | 24 #include "ui/views/mus/aura_init.h" |
| 25 #include "ui/views/mus/native_widget_mus.h" |
12 #include "ui/views/mus/window_manager_connection.h" | 26 #include "ui/views/mus/window_manager_connection.h" |
| 27 #include "ui/views/widget/widget_delegate.h" |
13 | 28 |
14 namespace mash { | 29 namespace mash { |
15 namespace login { | 30 namespace login { |
16 namespace { | 31 namespace { |
17 | 32 |
18 class Login : public mojom::Login { | 33 class Login; |
| 34 |
| 35 class UI : public views::WidgetDelegateView, |
| 36 public views::ButtonListener { |
19 public: | 37 public: |
20 Login(mojo::Connector* connector, | 38 static void Show(mojo::Connector* connector, Login* login) { |
21 LoginController* controller, | 39 UI* ui = new UI(login, connector); |
22 const std::string& user_id, | 40 ui->StartWindowManager(); |
23 mojom::LoginRequest request) | 41 |
24 : connector_(connector), | 42 views::WindowManagerConnection::Create(connector); |
25 controller_(controller), | 43 |
26 user_id_(user_id), | 44 views::Widget* widget = new views::Widget; |
27 binding_(this, std::move(request)) {} | 45 views::Widget::InitParams params( |
| 46 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 47 params.delegate = ui; |
| 48 |
| 49 std::map<std::string, std::vector<uint8_t>> properties; |
| 50 properties[mash::wm::mojom::kWindowContainer_Property] = |
| 51 mojo::TypeConverter<const std::vector<uint8_t>, int32_t>::Convert( |
| 52 static_cast<int32_t>(mash::wm::mojom::Container::LOGIN_WINDOWS)); |
| 53 mus::Window* window = |
| 54 views::WindowManagerConnection::Get()->NewWindow(properties); |
| 55 params.native_widget = new views::NativeWidgetMus( |
| 56 widget, connector, window, mus::mojom::SurfaceType::DEFAULT); |
| 57 widget->Init(params); |
| 58 widget->Show(); |
| 59 } |
| 60 |
| 61 private: |
| 62 UI(Login* login, mojo::Connector* connector) |
| 63 : login_(login), |
| 64 connector_(connector), |
| 65 user_id_1_("00000000-0000-4000-8000-000000000000"), |
| 66 user_id_2_("00000000-0000-4000-8000-000000000001"), |
| 67 login_button_1_( |
| 68 new views::LabelButton(this, base::ASCIIToUTF16("Timothy"))), |
| 69 login_button_2_( |
| 70 new views::LabelButton(this, base::ASCIIToUTF16("Jimothy"))) { |
| 71 set_background(views::Background::CreateSolidBackground(SK_ColorRED)); |
| 72 login_button_1_->SetStyle(views::Button::STYLE_BUTTON); |
| 73 login_button_2_->SetStyle(views::Button::STYLE_BUTTON); |
| 74 AddChildView(login_button_1_); |
| 75 AddChildView(login_button_2_); |
| 76 } |
| 77 ~UI() override { |
| 78 // Prevent the window manager from restarting during graceful shutdown. |
| 79 window_manager_connection_->SetConnectionLostClosure(base::Closure()); |
| 80 base::MessageLoop::current()->QuitWhenIdle(); |
| 81 } |
| 82 |
| 83 // Overridden from views::WidgetDelegate: |
| 84 views::View* GetContentsView() override { return this; } |
| 85 base::string16 GetWindowTitle() const override { |
| 86 // TODO(beng): use resources. |
| 87 return base::ASCIIToUTF16("Login"); |
| 88 } |
| 89 void DeleteDelegate() override { delete this; } |
| 90 |
| 91 // Overridden from views::View: |
| 92 void Layout() override { |
| 93 gfx::Rect button_box = GetLocalBounds(); |
| 94 button_box.Inset(10, 10); |
| 95 |
| 96 gfx::Size ps1 = login_button_1_->GetPreferredSize(); |
| 97 gfx::Size ps2 = login_button_2_->GetPreferredSize(); |
| 98 |
| 99 DCHECK(ps1.height() == ps2.height()); |
| 100 |
| 101 // The 10 is inter-button spacing. |
| 102 button_box.set_x((button_box.width() - ps1.width() - ps2.width() - 10) / 2); |
| 103 button_box.set_y((button_box.height() - ps1.height()) / 2); |
| 104 |
| 105 login_button_1_->SetBounds(button_box.x(), button_box.y(), ps1.width(), |
| 106 ps1.height()); |
| 107 login_button_2_->SetBounds(login_button_1_->bounds().right() + 10, |
| 108 button_box.y(), ps2.width(), ps2.height()); |
| 109 } |
| 110 |
| 111 // Overridden from views::ButtonListener: |
| 112 void ButtonPressed(views::Button* sender, const ui::Event& event) override; |
| 113 |
| 114 void StartWindowManager() { |
| 115 window_manager_connection_ = connector_->Connect("mojo:desktop_wm"); |
| 116 window_manager_connection_->SetConnectionLostClosure( |
| 117 base::Bind(&UI::StartWindowManager, base::Unretained(this))); |
| 118 } |
| 119 |
| 120 Login* login_; |
| 121 mojo::Connector* connector_; |
| 122 const std::string user_id_1_; |
| 123 const std::string user_id_2_; |
| 124 views::LabelButton* login_button_1_; |
| 125 views::LabelButton* login_button_2_; |
| 126 scoped_ptr<mojo::Connection> window_manager_connection_; |
| 127 |
| 128 DISALLOW_COPY_AND_ASSIGN(UI); |
| 129 }; |
| 130 |
| 131 class Login : public mojo::ShellClient, |
| 132 public mojo::InterfaceFactory<mojom::Login>, |
| 133 public mojom::Login { |
| 134 public: |
| 135 Login() {} |
28 ~Login() override {} | 136 ~Login() override {} |
29 | 137 |
| 138 void LoginAs(const std::string& user_id) { |
| 139 user_access_manager_->SetActiveUser(user_id); |
| 140 mash::init::mojom::InitPtr init; |
| 141 connector_->ConnectToInterface("mojo:mash_init", &init); |
| 142 init->StartService("mojo:mash_shell", user_id); |
| 143 } |
| 144 |
30 private: | 145 private: |
| 146 // mojo::ShellClient: |
| 147 void Initialize(mojo::Connector* connector, const mojo::Identity& identity, |
| 148 uint32_t id) override { |
| 149 connector_ = connector; |
| 150 tracing_.Initialize(connector, identity.name()); |
| 151 |
| 152 aura_init_.reset(new views::AuraInit(connector, "views_mus_resources.pak")); |
| 153 |
| 154 connector_->ConnectToInterface("mojo:mus", &user_access_manager_); |
| 155 user_access_manager_->SetActiveUser(identity.user_id()); |
| 156 } |
| 157 bool AcceptConnection(mojo::Connection* connection) override { |
| 158 connection->AddInterface<mojom::Login>(this); |
| 159 return true; |
| 160 } |
| 161 |
| 162 // mojo::InterfaceFactory<mojom::Login>: |
| 163 void Create(mojo::Connection* connection, |
| 164 mojom::LoginRequest request) override { |
| 165 bindings_.AddBinding(this, std::move(request)); |
| 166 } |
| 167 |
31 // mojom::Login: | 168 // mojom::Login: |
32 void ShowLoginUI() override { | 169 void ShowLoginUI() override { |
33 UI::Show(connector_, controller_); | 170 UI::Show(connector_, this); |
34 } | 171 } |
35 void SwitchUser() override { | 172 void SwitchUser() override { |
36 UI::Show(connector_, controller_); | 173 UI::Show(connector_, this); |
37 } | 174 } |
| 175 |
| 176 void StartWindowManager(); |
38 | 177 |
39 mojo::Connector* connector_; | 178 mojo::Connector* connector_; |
40 LoginController* controller_; | 179 mojo::TracingImpl tracing_; |
41 const std::string user_id_; | 180 scoped_ptr<views::AuraInit> aura_init_; |
42 mojo::StrongBinding<mojom::Login> binding_; | 181 mojo::BindingSet<mojom::Login> bindings_; |
| 182 mus::mojom::UserAccessManagerPtr user_access_manager_; |
| 183 scoped_ptr<mojo::Connection> window_manager_connection_; |
43 | 184 |
44 DISALLOW_COPY_AND_ASSIGN(Login); | 185 DISALLOW_COPY_AND_ASSIGN(Login); |
45 }; | 186 }; |
46 | 187 |
| 188 void UI::ButtonPressed(views::Button* sender, const ui::Event& event) { |
| 189 // Login... |
| 190 if (sender == login_button_1_) { |
| 191 login_->LoginAs(user_id_1_); |
| 192 } else if (sender == login_button_2_) { |
| 193 login_->LoginAs(user_id_2_); |
| 194 } else { |
| 195 NOTREACHED(); |
| 196 } |
| 197 GetWidget()->Close(); |
| 198 } |
| 199 |
47 } // namespace | 200 } // namespace |
48 | 201 |
49 LoginController::LoginController() {} | 202 mojo::ShellClient* CreateLogin() { |
50 LoginController::~LoginController() {} | 203 return new Login; |
51 | |
52 void LoginController::Initialize(mojo::Connector* connector, | |
53 const mojo::Identity& identity, | |
54 uint32_t id) { | |
55 connector_ = connector; | |
56 login_user_id_ = identity.user_id(); | |
57 tracing_.Initialize(connector, identity.name()); | |
58 | |
59 aura_init_.reset(new views::AuraInit(connector, "views_mus_resources.pak")); | |
60 } | 204 } |
61 | 205 |
62 bool LoginController::AcceptConnection(mojo::Connection* connection) { | |
63 if (connection->GetRemoteIdentity().name() == "mojo:mash_init") | |
64 connection->GetInterface(&init_); | |
65 connection->AddInterface<mojom::Login>(this); | |
66 return true; | |
67 } | |
68 | |
69 void LoginController::Create(mojo::Connection* connection, | |
70 mojom::LoginRequest request) { | |
71 new Login(connector_, this, connection->GetRemoteIdentity().user_id(), | |
72 std::move(request)); | |
73 } | |
74 | |
75 } // namespace login | 206 } // namespace login |
76 } // namespace main | 207 } // namespace main |
OLD | NEW |