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) { | |
stevenjb
2011/10/17 18:32:56
OVERRIDE
satorux1
2011/10/17 21:24:44
Done.
| |
52 observers_.AddObserver(observer); | |
53 } | |
54 | |
55 // SessionManagerClient override. | |
56 virtual void RemoveObserver(Observer* observer) { | |
stevenjb
2011/10/17 18:32:56
OVERRIDE
satorux1
2011/10/17 21:24:44
Done.
| |
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 session_manager_proxy_->CallMethod( | |
154 &method_call, | |
155 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
156 base::Bind(&SessionManagerClientImpl::OnStorePolicy, | |
157 weak_ptr_factory_.GetWeakPtr(), | |
158 callback)); | |
159 } | |
160 | |
161 private: | |
162 // Called when kSessionManagerEmitLoginPromptReady method is complete. | |
163 void OnEmitLoginPromptReady(dbus::Response* response) { | |
164 LOG_IF(ERROR, !response) | |
165 << "Failed to call " | |
166 << login_manager::kSessionManagerEmitLoginPromptReady; | |
167 } | |
168 | |
169 // Called when kSessionManagerEmitLoginPromptVisible method is complete. | |
170 void OnEmitLoginPromptVisible(dbus::Response* response) { | |
171 LOG_IF(ERROR, !response) | |
172 << "Failed to call " | |
173 << login_manager::kSessionManagerEmitLoginPromptVisible; | |
174 } | |
175 | |
176 // Called when kSessionManagerRestartJob method is complete. | |
177 void OnRestartJob(dbus::Response* response) { | |
178 LOG_IF(ERROR, !response) | |
179 << "Failed to call " | |
180 << login_manager::kSessionManagerRestartJob; | |
181 } | |
182 | |
183 // Called when kSessionManagerRestartEntd method is complete. | |
184 void OnRestartEntd(dbus::Response* response) { | |
185 LOG_IF(ERROR, !response) | |
186 << "Failed to call " | |
187 << login_manager::kSessionManagerRestartEntd; | |
188 } | |
189 | |
190 // Called when kSessionManagerStartSession method is complete. | |
191 void OnStartSession(dbus::Response* response) { | |
192 LOG_IF(ERROR, !response) | |
193 << "Failed to call " | |
194 << login_manager::kSessionManagerStartSession; | |
195 } | |
196 | |
197 // Called when kSessionManagerStopSession method is complete. | |
198 void OnStopSession(dbus::Response* response) { | |
199 LOG_IF(ERROR, !response) | |
200 << "Failed to call " | |
201 << login_manager::kSessionManagerStopSession; | |
202 } | |
203 | |
204 // Called when kSessionManagerRetrievePolicy method is complete. | |
205 void OnRetrievePolicy(RetrievePolicyCallback callback, | |
206 dbus::Response* response) { | |
207 if (!response) { | |
208 LOG(ERROR) << "Failed to call " | |
209 << login_manager::kSessionManagerRetrievePolicy; | |
210 return; | |
211 } | |
212 dbus::MessageReader reader(response); | |
213 uint8* values = NULL; | |
214 size_t length = 0; | |
215 if (!reader.PopArrayOfBytes(&values, &length)) { | |
216 LOG(ERROR) << "Invalid response: " << response->ToString(); | |
217 return; | |
218 } | |
219 // static_cast does not work due to signedness. | |
220 std::string serialized_proto(reinterpret_cast<char*>(values), length); | |
221 callback.Run(serialized_proto); | |
222 } | |
223 | |
224 // Called when kSessionManagerStorePolicy method is complete. | |
225 void OnStorePolicy(StorePolicyCallback callback, dbus::Response* response) { | |
226 if (!response) { | |
227 LOG(ERROR) << "Failed to call " | |
228 << login_manager::kSessionManagerStorePolicy; | |
229 return; | |
230 } | |
231 dbus::MessageReader reader(response); | |
232 bool success = false; | |
233 if (!reader.PopBool(&success)) { | |
234 LOG(ERROR) << "Invalid response: " << response->ToString(); | |
235 return; | |
236 } | |
237 callback.Run(success); | |
238 } | |
239 | |
240 // Called when the owner key set signal is received. | |
241 void OwnerKeySetReceived(dbus::Signal* signal) { | |
242 dbus::MessageReader reader(signal); | |
243 std::string result_string; | |
244 if (!reader.PopString(&result_string)) { | |
245 LOG(ERROR) << "Invalid signal: " << signal->ToString(); | |
246 return; | |
247 } | |
248 const bool success = StartsWithASCII(result_string, "success", false); | |
249 FOR_EACH_OBSERVER(Observer, observers_, OwnerKeySet(success)); | |
250 } | |
251 | |
252 // Called when the property change complete signal is received. | |
253 void PropertyChangeCompleteReceived(dbus::Signal* signal) { | |
254 dbus::MessageReader reader(signal); | |
255 std::string result_string; | |
256 if (!reader.PopString(&result_string)) { | |
257 LOG(ERROR) << "Invalid signal: " << signal->ToString(); | |
258 return; | |
259 } | |
260 const bool success = StartsWithASCII(result_string, "success", false); | |
261 FOR_EACH_OBSERVER(Observer, observers_, PropertyChangeComplete(success)); | |
262 } | |
263 | |
264 // Called when the object is connected to the signal. | |
265 void SignalConnected(const std::string& interface_name, | |
266 const std::string& signal_name, | |
267 bool success) { | |
268 LOG_IF(ERROR, !success) << "Failed to connect to " << signal_name; | |
269 } | |
270 | |
271 dbus::ObjectProxy* session_manager_proxy_; | |
272 ObserverList<Observer> observers_; | |
273 base::WeakPtrFactory<SessionManagerClientImpl> weak_ptr_factory_; | |
274 | |
275 DISALLOW_COPY_AND_ASSIGN(SessionManagerClientImpl); | |
276 }; | |
277 | |
278 // The SessionManagerClient implementation used on Linux desktop, | |
279 // which does nothing. | |
280 class SessionManagerClientStubImpl : public SessionManagerClient { | |
281 // SessionManagerClient overrides. | |
282 virtual void AddObserver(Observer* observer) OVERRIDE {} | |
283 virtual void RemoveObserver(Observer* observer) OVERRIDE {} | |
284 virtual void EmitLoginPromptReady() OVERRIDE {} | |
285 virtual void EmitLoginPromptVisible() OVERRIDE {} | |
286 virtual void RestartJob(int pid, const std::string& command_line) OVERRIDE {} | |
287 virtual void RestartEntd() OVERRIDE {} | |
288 virtual void StartSession(const std::string& user_email) OVERRIDE {} | |
289 virtual void StopSession() OVERRIDE {} | |
290 virtual void RetrievePolicy(RetrievePolicyCallback callback) OVERRIDE { | |
291 callback.Run(""); | |
292 } | |
293 virtual void StorePolicy(const std::string& policy_blob, | |
294 StorePolicyCallback callback) OVERRIDE { | |
295 callback.Run(true); | |
296 } | |
297 | |
298 }; | |
299 | |
300 SessionManagerClient::SessionManagerClient() { | |
301 } | |
302 | |
303 SessionManagerClient::~SessionManagerClient() { | |
304 } | |
305 | |
306 SessionManagerClient* SessionManagerClient::Create(dbus::Bus* bus) { | |
307 if (system::runtime_environment::IsRunningOnChromeOS()) { | |
308 return new SessionManagerClientImpl(bus); | |
309 } else { | |
310 return new SessionManagerClientStubImpl(); | |
311 } | |
312 } | |
313 | |
314 } // namespace chromeos | |
OLD | NEW |