| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/dbus/session_manager_client.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback.h" | |
| 9 #include "base/chromeos/chromeos_version.h" | |
| 10 #include "base/string_util.h" | |
| 11 #include "dbus/bus.h" | |
| 12 #include "dbus/message.h" | |
| 13 #include "dbus/object_path.h" | |
| 14 #include "dbus/object_proxy.h" | |
| 15 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 16 | |
| 17 namespace chromeos { | |
| 18 | |
| 19 // The SessionManagerClient implementation used in production. | |
| 20 class SessionManagerClientImpl : public SessionManagerClient { | |
| 21 public: | |
| 22 explicit SessionManagerClientImpl(dbus::Bus* bus) | |
| 23 : session_manager_proxy_(NULL), | |
| 24 weak_ptr_factory_(this) { | |
| 25 session_manager_proxy_ = bus->GetObjectProxy( | |
| 26 login_manager::kSessionManagerServiceName, | |
| 27 dbus::ObjectPath(login_manager::kSessionManagerServicePath)); | |
| 28 | |
| 29 // Monitor the D-Bus signal for owner key changes. | |
| 30 session_manager_proxy_->ConnectToSignal( | |
| 31 chromium::kChromiumInterface, | |
| 32 chromium::kOwnerKeySetSignal, | |
| 33 base::Bind(&SessionManagerClientImpl::OwnerKeySetReceived, | |
| 34 weak_ptr_factory_.GetWeakPtr()), | |
| 35 base::Bind(&SessionManagerClientImpl::SignalConnected, | |
| 36 weak_ptr_factory_.GetWeakPtr())); | |
| 37 | |
| 38 // Monitor the D-Bus signal for property changes. | |
| 39 session_manager_proxy_->ConnectToSignal( | |
| 40 chromium::kChromiumInterface, | |
| 41 chromium::kPropertyChangeCompleteSignal, | |
| 42 base::Bind(&SessionManagerClientImpl::PropertyChangeCompleteReceived, | |
| 43 weak_ptr_factory_.GetWeakPtr()), | |
| 44 base::Bind(&SessionManagerClientImpl::SignalConnected, | |
| 45 weak_ptr_factory_.GetWeakPtr())); | |
| 46 } | |
| 47 | |
| 48 virtual ~SessionManagerClientImpl() { | |
| 49 } | |
| 50 | |
| 51 // SessionManagerClient override. | |
| 52 virtual void AddObserver(Observer* observer) OVERRIDE { | |
| 53 observers_.AddObserver(observer); | |
| 54 } | |
| 55 | |
| 56 // SessionManagerClient override. | |
| 57 virtual void RemoveObserver(Observer* observer) OVERRIDE { | |
| 58 observers_.RemoveObserver(observer); | |
| 59 } | |
| 60 | |
| 61 // SessionManagerClient override. | |
| 62 virtual void EmitLoginPromptReady() OVERRIDE { | |
| 63 dbus::MethodCall method_call( | |
| 64 login_manager::kSessionManagerInterface, | |
| 65 login_manager::kSessionManagerEmitLoginPromptReady); | |
| 66 session_manager_proxy_->CallMethod( | |
| 67 &method_call, | |
| 68 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 69 base::Bind(&SessionManagerClientImpl::OnEmitLoginPromptReady, | |
| 70 weak_ptr_factory_.GetWeakPtr())); | |
| 71 } | |
| 72 | |
| 73 // SessionManagerClient override. | |
| 74 virtual void EmitLoginPromptVisible() OVERRIDE { | |
| 75 dbus::MethodCall method_call( | |
| 76 login_manager::kSessionManagerInterface, | |
| 77 login_manager::kSessionManagerEmitLoginPromptVisible); | |
| 78 session_manager_proxy_->CallMethod( | |
| 79 &method_call, | |
| 80 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 81 base::Bind(&SessionManagerClientImpl::OnEmitLoginPromptVisible, | |
| 82 weak_ptr_factory_.GetWeakPtr())); | |
| 83 } | |
| 84 | |
| 85 // SessionManagerClient override. | |
| 86 virtual void RestartJob(int pid, const std::string& command_line) OVERRIDE { | |
| 87 dbus::MethodCall method_call(login_manager::kSessionManagerInterface, | |
| 88 login_manager::kSessionManagerRestartJob); | |
| 89 dbus::MessageWriter writer(&method_call); | |
| 90 writer.AppendInt32(pid); | |
| 91 writer.AppendString(command_line); | |
| 92 session_manager_proxy_->CallMethod( | |
| 93 &method_call, | |
| 94 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 95 base::Bind(&SessionManagerClientImpl::OnRestartJob, | |
| 96 weak_ptr_factory_.GetWeakPtr())); | |
| 97 } | |
| 98 | |
| 99 // SessionManagerClient override. | |
| 100 virtual void RestartEntd() OVERRIDE { | |
| 101 dbus::MethodCall method_call(login_manager::kSessionManagerInterface, | |
| 102 login_manager::kSessionManagerRestartEntd); | |
| 103 session_manager_proxy_->CallMethod( | |
| 104 &method_call, | |
| 105 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 106 base::Bind(&SessionManagerClientImpl::OnRestartEntd, | |
| 107 weak_ptr_factory_.GetWeakPtr())); | |
| 108 } | |
| 109 | |
| 110 // SessionManagerClient override. | |
| 111 virtual void StartSession(const std::string& user_email) OVERRIDE { | |
| 112 dbus::MethodCall method_call(login_manager::kSessionManagerInterface, | |
| 113 login_manager::kSessionManagerStartSession); | |
| 114 dbus::MessageWriter writer(&method_call); | |
| 115 writer.AppendString(user_email); | |
| 116 writer.AppendString(""); // Unique ID is deprecated | |
| 117 session_manager_proxy_->CallMethod( | |
| 118 &method_call, | |
| 119 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 120 base::Bind(&SessionManagerClientImpl::OnStartSession, | |
| 121 weak_ptr_factory_.GetWeakPtr())); | |
| 122 } | |
| 123 | |
| 124 // SessionManagerClient override. | |
| 125 virtual void StopSession() OVERRIDE { | |
| 126 dbus::MethodCall method_call(login_manager::kSessionManagerInterface, | |
| 127 login_manager::kSessionManagerStopSession); | |
| 128 dbus::MessageWriter writer(&method_call); | |
| 129 writer.AppendString(""); // Unique ID is deprecated | |
| 130 session_manager_proxy_->CallMethod( | |
| 131 &method_call, | |
| 132 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 133 base::Bind(&SessionManagerClientImpl::OnStopSession, | |
| 134 weak_ptr_factory_.GetWeakPtr())); | |
| 135 } | |
| 136 | |
| 137 // SessionManagerClient override. | |
| 138 virtual void RetrievePolicy(RetrievePolicyCallback callback) OVERRIDE { | |
| 139 dbus::MethodCall method_call(login_manager::kSessionManagerInterface, | |
| 140 login_manager::kSessionManagerRetrievePolicy); | |
| 141 session_manager_proxy_->CallMethod( | |
| 142 &method_call, | |
| 143 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 144 base::Bind(&SessionManagerClientImpl::OnRetrievePolicy, | |
| 145 weak_ptr_factory_.GetWeakPtr(), | |
| 146 callback)); | |
| 147 } | |
| 148 | |
| 149 // SessionManagerClient override. | |
| 150 virtual void StorePolicy(const std::string& policy_blob, | |
| 151 StorePolicyCallback callback) OVERRIDE { | |
| 152 dbus::MethodCall method_call(login_manager::kSessionManagerInterface, | |
| 153 login_manager::kSessionManagerStorePolicy); | |
| 154 dbus::MessageWriter writer(&method_call); | |
| 155 // static_cast does not work due to signedness. | |
| 156 writer.AppendArrayOfBytes( | |
| 157 reinterpret_cast<const uint8*>(policy_blob.data()), policy_blob.size()); | |
| 158 session_manager_proxy_->CallMethod( | |
| 159 &method_call, | |
| 160 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 161 base::Bind(&SessionManagerClientImpl::OnStorePolicy, | |
| 162 weak_ptr_factory_.GetWeakPtr(), | |
| 163 callback)); | |
| 164 } | |
| 165 | |
| 166 private: | |
| 167 // Called when kSessionManagerEmitLoginPromptReady method is complete. | |
| 168 void OnEmitLoginPromptReady(dbus::Response* response) { | |
| 169 LOG_IF(ERROR, !response) | |
| 170 << "Failed to call " | |
| 171 << login_manager::kSessionManagerEmitLoginPromptReady; | |
| 172 } | |
| 173 | |
| 174 // Called when kSessionManagerEmitLoginPromptVisible method is complete. | |
| 175 void OnEmitLoginPromptVisible(dbus::Response* response) { | |
| 176 LOG_IF(ERROR, !response) | |
| 177 << "Failed to call " | |
| 178 << login_manager::kSessionManagerEmitLoginPromptVisible; | |
| 179 } | |
| 180 | |
| 181 // Called when kSessionManagerRestartJob method is complete. | |
| 182 void OnRestartJob(dbus::Response* response) { | |
| 183 LOG_IF(ERROR, !response) | |
| 184 << "Failed to call " | |
| 185 << login_manager::kSessionManagerRestartJob; | |
| 186 } | |
| 187 | |
| 188 // Called when kSessionManagerRestartEntd method is complete. | |
| 189 void OnRestartEntd(dbus::Response* response) { | |
| 190 LOG_IF(ERROR, !response) | |
| 191 << "Failed to call " | |
| 192 << login_manager::kSessionManagerRestartEntd; | |
| 193 } | |
| 194 | |
| 195 // Called when kSessionManagerStartSession method is complete. | |
| 196 void OnStartSession(dbus::Response* response) { | |
| 197 LOG_IF(ERROR, !response) | |
| 198 << "Failed to call " | |
| 199 << login_manager::kSessionManagerStartSession; | |
| 200 } | |
| 201 | |
| 202 // Called when kSessionManagerStopSession method is complete. | |
| 203 void OnStopSession(dbus::Response* response) { | |
| 204 LOG_IF(ERROR, !response) | |
| 205 << "Failed to call " | |
| 206 << login_manager::kSessionManagerStopSession; | |
| 207 } | |
| 208 | |
| 209 // Called when kSessionManagerRetrievePolicy method is complete. | |
| 210 void OnRetrievePolicy(RetrievePolicyCallback callback, | |
| 211 dbus::Response* response) { | |
| 212 if (!response) { | |
| 213 LOG(ERROR) << "Failed to call " | |
| 214 << login_manager::kSessionManagerRetrievePolicy; | |
| 215 callback.Run(""); | |
| 216 return; | |
| 217 } | |
| 218 dbus::MessageReader reader(response); | |
| 219 uint8* values = NULL; | |
| 220 size_t length = 0; | |
| 221 if (!reader.PopArrayOfBytes(&values, &length)) { | |
| 222 LOG(ERROR) << "Invalid response: " << response->ToString(); | |
| 223 callback.Run(""); | |
| 224 return; | |
| 225 } | |
| 226 // static_cast does not work due to signedness. | |
| 227 std::string serialized_proto(reinterpret_cast<char*>(values), length); | |
| 228 callback.Run(serialized_proto); | |
| 229 } | |
| 230 | |
| 231 // Called when kSessionManagerStorePolicy method is complete. | |
| 232 void OnStorePolicy(StorePolicyCallback callback, dbus::Response* response) { | |
| 233 if (!response) { | |
| 234 LOG(ERROR) << "Failed to call " | |
| 235 << login_manager::kSessionManagerStorePolicy; | |
| 236 return; | |
| 237 } | |
| 238 dbus::MessageReader reader(response); | |
| 239 bool success = false; | |
| 240 if (!reader.PopBool(&success)) { | |
| 241 LOG(ERROR) << "Invalid response: " << response->ToString(); | |
| 242 return; | |
| 243 } | |
| 244 callback.Run(success); | |
| 245 } | |
| 246 | |
| 247 // Called when the owner key set signal is received. | |
| 248 void OwnerKeySetReceived(dbus::Signal* signal) { | |
| 249 dbus::MessageReader reader(signal); | |
| 250 std::string result_string; | |
| 251 if (!reader.PopString(&result_string)) { | |
| 252 LOG(ERROR) << "Invalid signal: " << signal->ToString(); | |
| 253 return; | |
| 254 } | |
| 255 const bool success = StartsWithASCII(result_string, "success", false); | |
| 256 FOR_EACH_OBSERVER(Observer, observers_, OwnerKeySet(success)); | |
| 257 } | |
| 258 | |
| 259 // Called when the property change complete signal is received. | |
| 260 void PropertyChangeCompleteReceived(dbus::Signal* signal) { | |
| 261 dbus::MessageReader reader(signal); | |
| 262 std::string result_string; | |
| 263 if (!reader.PopString(&result_string)) { | |
| 264 LOG(ERROR) << "Invalid signal: " << signal->ToString(); | |
| 265 return; | |
| 266 } | |
| 267 const bool success = StartsWithASCII(result_string, "success", false); | |
| 268 FOR_EACH_OBSERVER(Observer, observers_, PropertyChangeComplete(success)); | |
| 269 } | |
| 270 | |
| 271 // Called when the object is connected to the signal. | |
| 272 void SignalConnected(const std::string& interface_name, | |
| 273 const std::string& signal_name, | |
| 274 bool success) { | |
| 275 LOG_IF(ERROR, !success) << "Failed to connect to " << signal_name; | |
| 276 } | |
| 277 | |
| 278 dbus::ObjectProxy* session_manager_proxy_; | |
| 279 ObserverList<Observer> observers_; | |
| 280 base::WeakPtrFactory<SessionManagerClientImpl> weak_ptr_factory_; | |
| 281 | |
| 282 DISALLOW_COPY_AND_ASSIGN(SessionManagerClientImpl); | |
| 283 }; | |
| 284 | |
| 285 // The SessionManagerClient implementation used on Linux desktop, | |
| 286 // which does nothing. | |
| 287 class SessionManagerClientStubImpl : public SessionManagerClient { | |
| 288 // SessionManagerClient overrides. | |
| 289 virtual void AddObserver(Observer* observer) OVERRIDE {} | |
| 290 virtual void RemoveObserver(Observer* observer) OVERRIDE {} | |
| 291 virtual void EmitLoginPromptReady() OVERRIDE {} | |
| 292 virtual void EmitLoginPromptVisible() OVERRIDE {} | |
| 293 virtual void RestartJob(int pid, const std::string& command_line) OVERRIDE {} | |
| 294 virtual void RestartEntd() OVERRIDE {} | |
| 295 virtual void StartSession(const std::string& user_email) OVERRIDE {} | |
| 296 virtual void StopSession() OVERRIDE {} | |
| 297 virtual void RetrievePolicy(RetrievePolicyCallback callback) OVERRIDE { | |
| 298 callback.Run(""); | |
| 299 } | |
| 300 virtual void StorePolicy(const std::string& policy_blob, | |
| 301 StorePolicyCallback callback) OVERRIDE { | |
| 302 callback.Run(true); | |
| 303 } | |
| 304 | |
| 305 }; | |
| 306 | |
| 307 SessionManagerClient::SessionManagerClient() { | |
| 308 } | |
| 309 | |
| 310 SessionManagerClient::~SessionManagerClient() { | |
| 311 } | |
| 312 | |
| 313 SessionManagerClient* SessionManagerClient::Create(dbus::Bus* bus) { | |
| 314 if (base::chromeos::IsRunningOnChromeOS()) { | |
| 315 return new SessionManagerClientImpl(bus); | |
| 316 } else { | |
| 317 return new SessionManagerClientStubImpl(); | |
| 318 } | |
| 319 } | |
| 320 | |
| 321 } // namespace chromeos | |
| OLD | NEW |