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/string_util.h" | |
10 #include "dbus/bus.h" | |
11 #include "dbus/message.h" | |
12 #include "dbus/object_path.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 dbus::ObjectPath(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 RetrieveDevicePolicy(RetrievePolicyCallback callback) OVERRIDE { | |
138 CallRetrievePolicy(login_manager::kSessionManagerRetrievePolicy, | |
139 callback); | |
140 } | |
141 | |
142 // SessionManagerClient override. | |
143 virtual void RetrieveUserPolicy(RetrievePolicyCallback callback) OVERRIDE { | |
144 CallRetrievePolicy(login_manager::kSessionManagerRetrieveUserPolicy, | |
145 callback); | |
146 } | |
147 | |
148 // SessionManagerClient override. | |
149 virtual void StoreDevicePolicy(const std::string& policy_blob, | |
150 StorePolicyCallback callback) OVERRIDE { | |
151 CallStorePolicy(login_manager::kSessionManagerStorePolicy, | |
152 policy_blob, callback); | |
153 } | |
154 | |
155 // SessionManagerClient override. | |
156 virtual void StoreUserPolicy(const std::string& policy_blob, | |
157 StorePolicyCallback callback) OVERRIDE { | |
158 CallStorePolicy(login_manager::kSessionManagerStoreUserPolicy, | |
159 policy_blob, callback); | |
160 } | |
161 | |
162 private: | |
163 // Helper for Retrieve{User,Device}Policy. | |
164 virtual void CallRetrievePolicy(const std::string& method_name, | |
165 RetrievePolicyCallback callback) { | |
166 dbus::MethodCall method_call(login_manager::kSessionManagerInterface, | |
167 method_name); | |
168 session_manager_proxy_->CallMethod( | |
169 &method_call, | |
170 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
171 base::Bind(&SessionManagerClientImpl::OnRetrievePolicy, | |
172 weak_ptr_factory_.GetWeakPtr(), | |
173 method_name, | |
174 callback)); | |
175 } | |
176 | |
177 // Helper for Store{User,Device}Policy. | |
178 virtual void CallStorePolicy(const std::string& method_name, | |
179 const std::string& policy_blob, | |
180 StorePolicyCallback callback) { | |
181 dbus::MethodCall method_call(login_manager::kSessionManagerInterface, | |
182 method_name); | |
183 dbus::MessageWriter writer(&method_call); | |
184 // static_cast does not work due to signedness. | |
185 writer.AppendArrayOfBytes( | |
186 reinterpret_cast<const uint8*>(policy_blob.data()), policy_blob.size()); | |
187 session_manager_proxy_->CallMethod( | |
188 &method_call, | |
189 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
190 base::Bind(&SessionManagerClientImpl::OnStorePolicy, | |
191 weak_ptr_factory_.GetWeakPtr(), | |
192 method_name, | |
193 callback)); | |
194 } | |
195 | |
196 // Called when kSessionManagerEmitLoginPromptReady method is complete. | |
197 void OnEmitLoginPromptReady(dbus::Response* response) { | |
198 LOG_IF(ERROR, !response) | |
199 << "Failed to call " | |
200 << login_manager::kSessionManagerEmitLoginPromptReady; | |
201 } | |
202 | |
203 // Called when kSessionManagerEmitLoginPromptVisible method is complete. | |
204 void OnEmitLoginPromptVisible(dbus::Response* response) { | |
205 LOG_IF(ERROR, !response) | |
206 << "Failed to call " | |
207 << login_manager::kSessionManagerEmitLoginPromptVisible; | |
208 } | |
209 | |
210 // Called when kSessionManagerRestartJob method is complete. | |
211 void OnRestartJob(dbus::Response* response) { | |
212 LOG_IF(ERROR, !response) | |
213 << "Failed to call " | |
214 << login_manager::kSessionManagerRestartJob; | |
215 } | |
216 | |
217 // Called when kSessionManagerRestartEntd method is complete. | |
218 void OnRestartEntd(dbus::Response* response) { | |
219 LOG_IF(ERROR, !response) | |
220 << "Failed to call " | |
221 << login_manager::kSessionManagerRestartEntd; | |
222 } | |
223 | |
224 // Called when kSessionManagerStartSession method is complete. | |
225 void OnStartSession(dbus::Response* response) { | |
226 LOG_IF(ERROR, !response) | |
227 << "Failed to call " | |
228 << login_manager::kSessionManagerStartSession; | |
229 } | |
230 | |
231 // Called when kSessionManagerStopSession method is complete. | |
232 void OnStopSession(dbus::Response* response) { | |
233 LOG_IF(ERROR, !response) | |
234 << "Failed to call " | |
235 << login_manager::kSessionManagerStopSession; | |
236 } | |
237 | |
238 // Called when kSessionManagerRetrievePolicy or | |
239 // kSessionManagerRetrieveUserPolicy method is complete. | |
240 void OnRetrievePolicy(const std::string& method_name, | |
241 RetrievePolicyCallback callback, | |
242 dbus::Response* response) { | |
243 if (!response) { | |
244 LOG(ERROR) << "Failed to call " << method_name; | |
245 callback.Run(""); | |
246 return; | |
247 } | |
248 dbus::MessageReader reader(response); | |
249 uint8* values = NULL; | |
250 size_t length = 0; | |
251 if (!reader.PopArrayOfBytes(&values, &length)) { | |
252 LOG(ERROR) << "Invalid response: " << response->ToString(); | |
253 callback.Run(""); | |
254 return; | |
255 } | |
256 // static_cast does not work due to signedness. | |
257 std::string serialized_proto(reinterpret_cast<char*>(values), length); | |
258 callback.Run(serialized_proto); | |
259 } | |
260 | |
261 // Called when kSessionManagerStorePolicy or kSessionManagerStoreUserPolicy | |
262 // method is complete. | |
263 void OnStorePolicy(const std::string& method_name, | |
264 StorePolicyCallback callback, | |
265 dbus::Response* response) { | |
266 bool success = false; | |
267 if (!response) { | |
268 LOG(ERROR) << "Failed to call " << method_name; | |
269 } else { | |
270 dbus::MessageReader reader(response); | |
271 if (!reader.PopBool(&success)) | |
272 LOG(ERROR) << "Invalid response: " << response->ToString(); | |
273 } | |
274 callback.Run(success); | |
275 } | |
276 | |
277 // Called when the owner key set signal is received. | |
278 void OwnerKeySetReceived(dbus::Signal* signal) { | |
279 dbus::MessageReader reader(signal); | |
280 std::string result_string; | |
281 if (!reader.PopString(&result_string)) { | |
282 LOG(ERROR) << "Invalid signal: " << signal->ToString(); | |
283 return; | |
284 } | |
285 const bool success = StartsWithASCII(result_string, "success", false); | |
286 FOR_EACH_OBSERVER(Observer, observers_, OwnerKeySet(success)); | |
287 } | |
288 | |
289 // Called when the property change complete signal is received. | |
290 void PropertyChangeCompleteReceived(dbus::Signal* signal) { | |
291 dbus::MessageReader reader(signal); | |
292 std::string result_string; | |
293 if (!reader.PopString(&result_string)) { | |
294 LOG(ERROR) << "Invalid signal: " << signal->ToString(); | |
295 return; | |
296 } | |
297 const bool success = StartsWithASCII(result_string, "success", false); | |
298 FOR_EACH_OBSERVER(Observer, observers_, PropertyChangeComplete(success)); | |
299 } | |
300 | |
301 // Called when the object is connected to the signal. | |
302 void SignalConnected(const std::string& interface_name, | |
303 const std::string& signal_name, | |
304 bool success) { | |
305 LOG_IF(ERROR, !success) << "Failed to connect to " << signal_name; | |
306 } | |
307 | |
308 dbus::ObjectProxy* session_manager_proxy_; | |
309 ObserverList<Observer> observers_; | |
310 base::WeakPtrFactory<SessionManagerClientImpl> weak_ptr_factory_; | |
311 | |
312 DISALLOW_COPY_AND_ASSIGN(SessionManagerClientImpl); | |
313 }; | |
314 | |
315 // The SessionManagerClient implementation used on Linux desktop, | |
316 // which does nothing. | |
317 class SessionManagerClientStubImpl : public SessionManagerClient { | |
318 // SessionManagerClient overrides. | |
319 virtual void AddObserver(Observer* observer) OVERRIDE {} | |
320 virtual void RemoveObserver(Observer* observer) OVERRIDE {} | |
321 virtual void EmitLoginPromptReady() OVERRIDE {} | |
322 virtual void EmitLoginPromptVisible() OVERRIDE {} | |
323 virtual void RestartJob(int pid, const std::string& command_line) OVERRIDE {} | |
324 virtual void RestartEntd() OVERRIDE {} | |
325 virtual void StartSession(const std::string& user_email) OVERRIDE {} | |
326 virtual void StopSession() OVERRIDE {} | |
327 virtual void RetrieveDevicePolicy(RetrievePolicyCallback callback) OVERRIDE { | |
328 callback.Run(""); | |
329 } | |
330 virtual void RetrieveUserPolicy(RetrievePolicyCallback callback) OVERRIDE { | |
331 callback.Run(""); | |
332 } | |
333 virtual void StoreDevicePolicy(const std::string& policy_blob, | |
334 StorePolicyCallback callback) OVERRIDE { | |
335 callback.Run(true); | |
336 } | |
337 virtual void StoreUserPolicy(const std::string& policy_blob, | |
338 StorePolicyCallback callback) OVERRIDE { | |
339 callback.Run(true); | |
340 } | |
341 }; | |
342 | |
343 SessionManagerClient::SessionManagerClient() { | |
344 } | |
345 | |
346 SessionManagerClient::~SessionManagerClient() { | |
347 } | |
348 | |
349 SessionManagerClient* SessionManagerClient::Create( | |
350 DBusClientImplementationType type, | |
351 dbus::Bus* bus) { | |
352 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | |
353 return new SessionManagerClientImpl(bus); | |
354 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
355 return new SessionManagerClientStubImpl(); | |
356 } | |
357 | |
358 } // namespace chromeos | |
OLD | NEW |