| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/session/session.h" | 5 #include "mash/session/session.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "content/public/common/service_names.mojom.h" | 10 #include "content/public/common/service_names.mojom.h" |
| 11 #include "mash/common/config.h" | 11 #include "mash/common/config.h" |
| 12 #include "mash/login/public/interfaces/constants.mojom.h" | |
| 13 #include "mash/login/public/interfaces/login.mojom.h" | |
| 14 #include "mash/quick_launch/public/interfaces/constants.mojom.h" | 12 #include "mash/quick_launch/public/interfaces/constants.mojom.h" |
| 15 #include "mash/screenlock/public/interfaces/constants.mojom.h" | |
| 16 #include "services/service_manager/public/cpp/connection.h" | 13 #include "services/service_manager/public/cpp/connection.h" |
| 17 #include "services/service_manager/public/cpp/connector.h" | 14 #include "services/service_manager/public/cpp/connector.h" |
| 18 #include "services/service_manager/public/cpp/interface_registry.h" | 15 #include "services/service_manager/public/cpp/interface_registry.h" |
| 19 #include "services/service_manager/public/cpp/service_context.h" | 16 #include "services/service_manager/public/cpp/service_context.h" |
| 20 | 17 |
| 21 namespace { | 18 namespace { |
| 22 | 19 |
| 23 void LogAndCallServiceRestartCallback(const std::string& url, | 20 void LogAndCallServiceRestartCallback(const std::string& url, |
| 24 const base::Closure& callback) { | 21 const base::Closure& callback) { |
| 25 LOG(ERROR) << "Restarting service: " << url; | 22 LOG(ERROR) << "Restarting service: " << url; |
| 26 callback.Run(); | 23 callback.Run(); |
| 27 } | 24 } |
| 28 | 25 |
| 29 } // namespace | 26 } // namespace |
| 30 | 27 |
| 31 namespace mash { | 28 namespace mash { |
| 32 namespace session { | 29 namespace session { |
| 33 | 30 |
| 34 Session::Session() : screen_locked_(false) {} | 31 Session::Session() {} |
| 35 Session::~Session() {} | 32 Session::~Session() {} |
| 36 | 33 |
| 37 void Session::OnStart() { | 34 void Session::OnStart() { |
| 38 StartWindowManager(); | 35 StartWindowManager(); |
| 39 StartQuickLaunch(); | 36 StartQuickLaunch(); |
| 40 | 37 |
| 41 // Launch a chrome window for dev convience; don't do this in the long term. | 38 // Launch a chrome window for dev convience; don't do this in the long term. |
| 42 context()->connector()->Connect(content::mojom::kBrowserServiceName); | 39 context()->connector()->Connect(content::mojom::kBrowserServiceName); |
| 43 } | 40 } |
| 44 | 41 |
| 45 bool Session::OnConnect(const service_manager::ServiceInfo& remote_info, | 42 bool Session::OnConnect(const service_manager::ServiceInfo& remote_info, |
| 46 service_manager::InterfaceRegistry* registry) { | 43 service_manager::InterfaceRegistry* registry) { |
| 47 registry->AddInterface<mojom::Session>(this); | |
| 48 return true; | 44 return true; |
| 49 } | 45 } |
| 50 | 46 |
| 51 void Session::Logout() { | |
| 52 // TODO(beng): Notify connected listeners that login is happening, potentially | |
| 53 // give them the option to stop it. | |
| 54 mash::login::mojom::LoginPtr login; | |
| 55 context()->connector()->ConnectToInterface(login::mojom::kServiceName, | |
| 56 &login); | |
| 57 login->ShowLoginUI(); | |
| 58 // This kills the user environment. | |
| 59 base::MessageLoop::current()->QuitWhenIdle(); | |
| 60 } | |
| 61 | |
| 62 void Session::SwitchUser() { | |
| 63 mash::login::mojom::LoginPtr login; | |
| 64 context()->connector()->ConnectToInterface(login::mojom::kServiceName, | |
| 65 &login); | |
| 66 login->SwitchUser(); | |
| 67 } | |
| 68 | |
| 69 void Session::AddScreenlockStateListener( | |
| 70 mojom::ScreenlockStateListenerPtr listener) { | |
| 71 listener->ScreenlockStateChanged(screen_locked_); | |
| 72 screenlock_listeners_.AddPtr(std::move(listener)); | |
| 73 } | |
| 74 | |
| 75 void Session::LockScreen() { | |
| 76 if (screen_locked_) | |
| 77 return; | |
| 78 screen_locked_ = true; | |
| 79 screenlock_listeners_.ForAllPtrs( | |
| 80 [](mojom::ScreenlockStateListener* listener) { | |
| 81 listener->ScreenlockStateChanged(true); | |
| 82 }); | |
| 83 StartScreenlock(); | |
| 84 } | |
| 85 void Session::UnlockScreen() { | |
| 86 if (!screen_locked_) | |
| 87 return; | |
| 88 screen_locked_ = false; | |
| 89 screenlock_listeners_.ForAllPtrs( | |
| 90 [](mojom::ScreenlockStateListener* listener) { | |
| 91 listener->ScreenlockStateChanged(false); | |
| 92 }); | |
| 93 StopScreenlock(); | |
| 94 } | |
| 95 | |
| 96 void Session::Create(const service_manager::Identity& remote_identity, | |
| 97 mojom::SessionRequest request) { | |
| 98 bindings_.AddBinding(this, std::move(request)); | |
| 99 } | |
| 100 | |
| 101 void Session::StartWindowManager() { | 47 void Session::StartWindowManager() { |
| 102 StartRestartableService( | 48 StartRestartableService( |
| 103 common::GetWindowManagerServiceName(), | 49 common::GetWindowManagerServiceName(), |
| 104 base::Bind(&Session::StartWindowManager, | 50 base::Bind(&Session::StartWindowManager, |
| 105 base::Unretained(this))); | 51 base::Unretained(this))); |
| 106 } | 52 } |
| 107 | 53 |
| 108 void Session::StartQuickLaunch() { | 54 void Session::StartQuickLaunch() { |
| 109 StartRestartableService( | 55 StartRestartableService( |
| 110 quick_launch::mojom::kServiceName, | 56 quick_launch::mojom::kServiceName, |
| 111 base::Bind(&Session::StartQuickLaunch, | 57 base::Bind(&Session::StartQuickLaunch, |
| 112 base::Unretained(this))); | 58 base::Unretained(this))); |
| 113 } | 59 } |
| 114 | 60 |
| 115 void Session::StartScreenlock() { | |
| 116 StartRestartableService( | |
| 117 screenlock::mojom::kServiceName, | |
| 118 base::Bind(&Session::StartScreenlock, | |
| 119 base::Unretained(this))); | |
| 120 } | |
| 121 | |
| 122 void Session::StopScreenlock() { | |
| 123 auto connection = connections_.find("screenlock"); | |
| 124 DCHECK(connections_.end() != connection); | |
| 125 connections_.erase(connection); | |
| 126 } | |
| 127 | |
| 128 void Session::StartRestartableService( | 61 void Session::StartRestartableService( |
| 129 const std::string& url, | 62 const std::string& url, |
| 130 const base::Closure& restart_callback) { | 63 const base::Closure& restart_callback) { |
| 131 // TODO(beng): This would be the place to insert logic that counted restarts | 64 // TODO(beng): This would be the place to insert logic that counted restarts |
| 132 // to avoid infinite crash-restart loops. | 65 // to avoid infinite crash-restart loops. |
| 133 std::unique_ptr<service_manager::Connection> connection = | 66 std::unique_ptr<service_manager::Connection> connection = |
| 134 context()->connector()->Connect(url); | 67 context()->connector()->Connect(url); |
| 135 // Note: |connection| may be null if we've lost our connection to the service | 68 // Note: |connection| may be null if we've lost our connection to the service |
| 136 // manager. | 69 // manager. |
| 137 if (connection) { | 70 if (connection) { |
| 138 connection->SetConnectionLostClosure( | 71 connection->SetConnectionLostClosure( |
| 139 base::Bind(&LogAndCallServiceRestartCallback, url, restart_callback)); | 72 base::Bind(&LogAndCallServiceRestartCallback, url, restart_callback)); |
| 140 connections_[url] = std::move(connection); | 73 connections_[url] = std::move(connection); |
| 141 } | 74 } |
| 142 } | 75 } |
| 143 | 76 |
| 144 } // namespace session | 77 } // namespace session |
| 145 } // namespace main | 78 } // namespace main |
| OLD | NEW |