Chromium Code Reviews| 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 "base/guid.h" | |
| 8 #include "base/macros.h" | 7 #include "base/macros.h" |
| 9 #include "base/message_loop/message_loop.h" | 8 #include "mash/login/ui.h" |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "components/mus/public/cpp/property_type_converters.h" | |
| 12 #include "mash/wm/public/interfaces/container.mojom.h" | |
| 13 #include "mojo/public/cpp/bindings/binding.h" | 9 #include "mojo/public/cpp/bindings/binding.h" |
| 14 #include "mojo/shell/public/cpp/connector.h" | 10 #include "mojo/shell/public/cpp/connector.h" |
| 15 #include "ui/views/background.h" | |
| 16 #include "ui/views/controls/button/label_button.h" | |
| 17 #include "ui/views/mus/aura_init.h" | 11 #include "ui/views/mus/aura_init.h" |
| 18 #include "ui/views/mus/native_widget_mus.h" | |
| 19 #include "ui/views/mus/window_manager_connection.h" | |
| 20 #include "ui/views/widget/widget_delegate.h" | |
| 21 | 12 |
| 22 namespace mash { | 13 namespace mash { |
| 23 namespace login { | 14 namespace login { |
| 24 namespace { | 15 namespace { |
| 25 | 16 |
| 26 class LoginView : public views::WidgetDelegateView, | 17 class Login : public mojom::Login { |
| 27 public views::ButtonListener { | |
| 28 public: | 18 public: |
| 29 explicit LoginView(Login* login) | 19 Login(mojo::Connector* connector, |
| 30 : login_(login), | 20 LoginController* controller, |
| 31 user_id_1_(base::GenerateGUID()), | 21 const std::string& user_id, |
| 32 user_id_2_(base::GenerateGUID()), | 22 mojom::LoginRequest request) |
| 33 login_button_1_( | 23 : connector_(connector), |
| 34 new views::LabelButton(this, base::ASCIIToUTF16("Timothy"))), | 24 controller_(controller), |
| 35 login_button_2_( | 25 user_id_(user_id), |
| 36 new views::LabelButton(this, base::ASCIIToUTF16("Jimothy"))) { | 26 binding_(this, std::move(request)) {} |
| 37 set_background(views::Background::CreateSolidBackground(SK_ColorRED)); | 27 ~Login() override {} |
| 38 login_button_1_->SetStyle(views::Button::STYLE_BUTTON); | |
| 39 login_button_2_->SetStyle(views::Button::STYLE_BUTTON); | |
| 40 AddChildView(login_button_1_); | |
| 41 AddChildView(login_button_2_); | |
| 42 } | |
| 43 ~LoginView() override {} | |
| 44 | 28 |
| 45 private: | 29 private: |
| 46 // Overridden from views::WidgetDelegate: | 30 // mojom::Login: |
| 47 views::View* GetContentsView() override { return this; } | 31 void ShowLoginUI() override { |
| 48 base::string16 GetWindowTitle() const override { | 32 UI::Show(connector_, controller_); |
| 49 // TODO(beng): use resources. | 33 } |
| 50 return base::ASCIIToUTF16("Login"); | 34 void Logout() override { |
| 35 controller_->init()->StopServicesForUser(user_id_); | |
| 36 UI::Show(connector_, controller_); | |
| 37 } | |
| 38 void SwitchUser() override { | |
| 39 UI::Show(connector_, controller_); | |
| 51 } | 40 } |
| 52 | 41 |
| 53 // Overridden from views::View: | 42 mojo::Connector* connector_; |
| 54 void Layout() override { | 43 LoginController* controller_; |
| 55 gfx::Rect button_box = GetLocalBounds(); | 44 const std::string user_id_; |
| 56 button_box.Inset(10, 10); | 45 mojo::Binding<mojom::Login> binding_; |
| 57 | 46 |
| 58 gfx::Size ps1 = login_button_1_->GetPreferredSize(); | 47 DISALLOW_COPY_AND_ASSIGN(Login); |
| 59 gfx::Size ps2 = login_button_2_->GetPreferredSize(); | |
| 60 | |
| 61 DCHECK(ps1.height() == ps2.height()); | |
| 62 | |
| 63 // The 10 is inter-button spacing. | |
| 64 button_box.set_x((button_box.width() - ps1.width() - ps2.width() - 10) / 2); | |
| 65 button_box.set_y((button_box.height() - ps1.height()) / 2); | |
| 66 | |
| 67 login_button_1_->SetBounds(button_box.x(), button_box.y(), ps1.width(), | |
| 68 ps1.height()); | |
| 69 login_button_2_->SetBounds(login_button_1_->bounds().right() + 10, | |
| 70 button_box.y(), ps2.width(), ps2.height()); | |
| 71 } | |
| 72 | |
| 73 // Overridden from views::ButtonListener: | |
| 74 void ButtonPressed(views::Button* sender, const ui::Event& event) override { | |
| 75 // Login... | |
| 76 mojo::Connector::ConnectParams params("mojo:mash_shell"); | |
| 77 if (sender == login_button_1_) { | |
| 78 login_->login()->LoginAs(user_id_1_); | |
| 79 } else if (sender == login_button_2_) { | |
| 80 login_->login()->LoginAs(user_id_2_); | |
| 81 } else { | |
| 82 NOTREACHED(); | |
| 83 } | |
| 84 base::MessageLoop::current()->QuitWhenIdle(); | |
| 85 } | |
| 86 | |
| 87 Login* login_; | |
| 88 const std::string user_id_1_; | |
| 89 const std::string user_id_2_; | |
| 90 views::LabelButton* login_button_1_; | |
| 91 views::LabelButton* login_button_2_; | |
| 92 | |
| 93 DISALLOW_COPY_AND_ASSIGN(LoginView); | |
| 94 }; | 48 }; |
| 95 | 49 |
| 96 } // namespace | 50 } // namespace |
| 97 | 51 |
| 98 Login::Login() {} | 52 LoginController::LoginController() {} |
| 99 Login::~Login() {} | 53 LoginController::~LoginController() {} |
| 100 | 54 |
| 101 void Login::Initialize(mojo::Connector* connector, | 55 void LoginController::Initialize(mojo::Connector* connector, |
| 102 const mojo::Identity& identity, | 56 const mojo::Identity& identity, |
| 103 uint32_t id) { | 57 uint32_t id) { |
| 58 connector_ = connector; | |
| 59 login_user_id_ = identity.user_id(); | |
| 104 tracing_.Initialize(connector, identity.name()); | 60 tracing_.Initialize(connector, identity.name()); |
| 105 | 61 |
| 106 aura_init_.reset(new views::AuraInit(connector, "views_mus_resources.pak")); | 62 aura_init_.reset(new views::AuraInit(connector, "views_mus_resources.pak")); |
| 107 views::WindowManagerConnection::Create(connector); | |
| 108 | |
| 109 views::Widget* widget = new views::Widget; | |
| 110 views::Widget::InitParams params( | |
| 111 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | |
| 112 params.delegate = new LoginView(this); | |
| 113 | |
| 114 std::map<std::string, std::vector<uint8_t>> properties; | |
| 115 properties[mash::wm::mojom::kWindowContainer_Property] = | |
| 116 mojo::TypeConverter<const std::vector<uint8_t>, int32_t>::Convert( | |
| 117 static_cast<int32_t>(mash::wm::mojom::Container::LOGIN_WINDOWS)); | |
| 118 mus::Window* window = | |
| 119 views::WindowManagerConnection::Get()->NewWindow(properties); | |
| 120 params.native_widget = new views::NativeWidgetMus( | |
| 121 widget, connector, window, mus::mojom::SurfaceType::DEFAULT); | |
| 122 widget->Init(params); | |
| 123 widget->Show(); | |
| 124 } | 63 } |
| 125 | 64 |
| 126 bool Login::AcceptConnection(mojo::Connection* connection) { | 65 bool LoginController::AcceptConnection(mojo::Connection* connection) { |
| 127 if (connection->GetRemoteIdentity().name() == "mojo:mash_init") { | 66 if (connection->GetRemoteIdentity().name() == "mojo:mash_init") |
| 128 connection->GetInterface(&login_); | 67 connection->GetInterface(&init_); |
| 129 return true; | 68 connection->AddInterface<mojom::Login>(this); |
| 130 } | 69 return true; |
| 131 return false; | 70 } |
| 71 | |
| 72 void LoginController::Create(mojo::Connection* connection, | |
| 73 mojom::LoginRequest request) { | |
| 74 new Login(connector_, this, connection->GetRemoteIdentity().user_id(), | |
|
sky
2016/03/15 20:33:51
Doesn't this leak?
| |
| 75 std::move(request)); | |
| 132 } | 76 } |
| 133 | 77 |
| 134 } // namespace login | 78 } // namespace login |
| 135 } // namespace main | 79 } // namespace main |
| OLD | NEW |