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