OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/chromeos/login/signed_settings.h" | 5 #include "chrome/browser/chromeos/login/signed_settings.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 SignedSettings::ReturnCode SignedSettings::MapKeyOpCode( | 51 SignedSettings::ReturnCode SignedSettings::MapKeyOpCode( |
52 OwnerManager::KeyOpCode return_code) { | 52 OwnerManager::KeyOpCode return_code) { |
53 return (return_code == OwnerManager::KEY_UNAVAILABLE ? | 53 return (return_code == OwnerManager::KEY_UNAVAILABLE ? |
54 KEY_UNAVAILABLE : BAD_SIGNATURE); | 54 KEY_UNAVAILABLE : BAD_SIGNATURE); |
55 } | 55 } |
56 | 56 |
57 class StorePolicyOp : public SignedSettings { | 57 class StorePolicyOp : public SignedSettings { |
58 public: | 58 public: |
59 StorePolicyOp(em::PolicyFetchResponse* policy, | 59 StorePolicyOp(em::PolicyFetchResponse* policy, |
60 SignedSettings::Delegate<bool>* d); | 60 SignedSettings::Delegate<bool>* d); |
61 virtual ~StorePolicyOp(); | |
62 void Execute(); | |
63 void Fail(SignedSettings::ReturnCode code); | |
64 void Succeed(bool value); | 61 void Succeed(bool value); |
65 // Implementation of OwnerManager::Delegate | 62 // Implementation of OwnerManager::Delegate |
66 void OnKeyOpComplete(const OwnerManager::KeyOpCode return_code, | 63 virtual void Execute() OVERRIDE; |
67 const std::vector<uint8>& payload); | 64 virtual void Fail(SignedSettings::ReturnCode code) OVERRIDE; |
| 65 virtual void OnKeyOpComplete(const OwnerManager::KeyOpCode return_code, |
| 66 const std::vector<uint8>& payload) OVERRIDE; |
| 67 |
| 68 protected: |
| 69 virtual ~StorePolicyOp(); |
68 | 70 |
69 private: | 71 private: |
| 72 void RequestStorePolicy(); |
| 73 |
70 void OnBoolComplete(bool success); | 74 void OnBoolComplete(bool success); |
71 // Always call d_->OnSettingOpCompleted() via this call. | 75 // Always call d_->OnSettingOpCompleted() via this call. |
72 // It guarantees that the callback will not be triggered until _after_ | 76 // It guarantees that the callback will not be triggered until _after_ |
73 // Execute() returns, which is implicitly assumed by SignedSettingsHelper | 77 // Execute() returns, which is implicitly assumed by SignedSettingsHelper |
74 // in some cases. | 78 // in some cases. |
75 void PerformCallback(SignedSettings::ReturnCode code, bool value); | 79 void PerformCallback(SignedSettings::ReturnCode code, bool value); |
76 | 80 |
77 em::PolicyFetchResponse* policy_; | 81 em::PolicyFetchResponse* policy_; |
78 SignedSettings::Delegate<bool>* d_; | 82 SignedSettings::Delegate<bool>* d_; |
79 | |
80 void RequestStorePolicy(); | |
81 }; | 83 }; |
82 | 84 |
83 class RetrievePolicyOp : public SignedSettings { | 85 class RetrievePolicyOp : public SignedSettings { |
84 public: | 86 public: |
85 explicit RetrievePolicyOp( | 87 explicit RetrievePolicyOp( |
86 SignedSettings::Delegate<const em::PolicyFetchResponse&>* d); | 88 SignedSettings::Delegate<const em::PolicyFetchResponse&>* d); |
87 virtual ~RetrievePolicyOp(); | |
88 void Execute(); | |
89 void Fail(SignedSettings::ReturnCode code); | |
90 void Succeed(const em::PolicyFetchResponse& value); | 89 void Succeed(const em::PolicyFetchResponse& value); |
91 // Implementation of OwnerManager::Delegate | 90 // Implementation of OwnerManager::Delegate |
92 void OnKeyOpComplete(const OwnerManager::KeyOpCode return_code, | 91 virtual void Execute() OVERRIDE; |
93 const std::vector<uint8>& payload); | 92 virtual void Fail(SignedSettings::ReturnCode code) OVERRIDE; |
| 93 virtual void OnKeyOpComplete(const OwnerManager::KeyOpCode return_code, |
| 94 const std::vector<uint8>& payload) OVERRIDE; |
| 95 |
| 96 protected: |
| 97 virtual ~RetrievePolicyOp(); |
94 | 98 |
95 private: | 99 private: |
96 void OnStringComplete(const std::string& serialized_proto); | 100 void OnStringComplete(const std::string& serialized_proto); |
97 // Always call d_->OnSettingOpCompleted() via this call. | 101 // Always call d_->OnSettingOpCompleted() via this call. |
98 // It guarantees that the callback will not be triggered until _after_ | 102 // It guarantees that the callback will not be triggered until _after_ |
99 // Execute() returns, which is implicitly assumed by SignedSettingsHelper | 103 // Execute() returns, which is implicitly assumed by SignedSettingsHelper |
100 // in some cases. | 104 // in some cases. |
101 void PerformCallback(SignedSettings::ReturnCode code, | 105 void PerformCallback(SignedSettings::ReturnCode code, |
102 const em::PolicyFetchResponse& value); | 106 const em::PolicyFetchResponse& value); |
103 | 107 |
(...skipping 19 matching lines...) Expand all Loading... |
123 return new RetrievePolicyOp(d); | 127 return new RetrievePolicyOp(d); |
124 } | 128 } |
125 | 129 |
126 | 130 |
127 StorePolicyOp::StorePolicyOp(em::PolicyFetchResponse* policy, | 131 StorePolicyOp::StorePolicyOp(em::PolicyFetchResponse* policy, |
128 SignedSettings::Delegate<bool>* d) | 132 SignedSettings::Delegate<bool>* d) |
129 : policy_(policy), | 133 : policy_(policy), |
130 d_(d) { | 134 d_(d) { |
131 } | 135 } |
132 | 136 |
133 StorePolicyOp::~StorePolicyOp() {} | 137 void StorePolicyOp::Succeed(bool ignored) { |
134 | 138 SignedSettings::ReturnCode code = SUCCESS; |
135 void StorePolicyOp::OnBoolComplete(bool success) { | 139 bool to_ret = true; |
136 if (success) | 140 em::PolicyData poldata; |
137 Succeed(true); | 141 if (SignedSettings::PolicyIsSane(*policy_, &poldata)) { |
138 else | 142 } else { |
139 Fail(NOT_FOUND); | 143 code = NOT_FOUND; |
| 144 to_ret = false; |
| 145 } |
| 146 BrowserThread::PostTask( |
| 147 BrowserThread::UI, FROM_HERE, |
| 148 base::Bind(&StorePolicyOp::PerformCallback, this, code, to_ret)); |
140 } | 149 } |
141 | 150 |
142 void StorePolicyOp::Execute() { | 151 void StorePolicyOp::Execute() { |
143 // get protobuf contents to sign | 152 // get protobuf contents to sign |
144 if (!policy_->has_policy_data()) | 153 if (!policy_->has_policy_data()) |
145 Fail(OPERATION_FAILED); | 154 Fail(OPERATION_FAILED); |
146 else if (!policy_->has_policy_data_signature()) | 155 else if (!policy_->has_policy_data_signature()) |
147 service_->StartSigningAttempt(policy_->policy_data(), this); | 156 service_->StartSigningAttempt(policy_->policy_data(), this); |
148 else | 157 else |
149 RequestStorePolicy(); | 158 RequestStorePolicy(); |
150 } | 159 } |
151 | 160 |
152 void StorePolicyOp::Fail(SignedSettings::ReturnCode code) { | 161 void StorePolicyOp::Fail(SignedSettings::ReturnCode code) { |
153 BrowserThread::PostTask( | 162 BrowserThread::PostTask( |
154 BrowserThread::UI, FROM_HERE, | 163 BrowserThread::UI, FROM_HERE, |
155 base::Bind(&StorePolicyOp::PerformCallback, this, code, false)); | 164 base::Bind(&StorePolicyOp::PerformCallback, this, code, false)); |
156 } | 165 } |
157 | 166 |
158 void StorePolicyOp::Succeed(bool ignored) { | |
159 SignedSettings::ReturnCode code = SUCCESS; | |
160 bool to_ret = true; | |
161 em::PolicyData poldata; | |
162 if (SignedSettings::PolicyIsSane(*policy_, &poldata)) { | |
163 } else { | |
164 code = NOT_FOUND; | |
165 to_ret = false; | |
166 } | |
167 BrowserThread::PostTask( | |
168 BrowserThread::UI, FROM_HERE, | |
169 base::Bind(&StorePolicyOp::PerformCallback, this, code, to_ret)); | |
170 } | |
171 | |
172 void StorePolicyOp::OnKeyOpComplete(const OwnerManager::KeyOpCode return_code, | 167 void StorePolicyOp::OnKeyOpComplete(const OwnerManager::KeyOpCode return_code, |
173 const std::vector<uint8>& payload) { | 168 const std::vector<uint8>& payload) { |
174 // Ensure we're on the UI thread, due to the need to send DBus traffic. | 169 // Ensure we're on the UI thread, due to the need to send DBus traffic. |
175 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 170 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
176 BrowserThread::PostTask( | 171 BrowserThread::PostTask( |
177 BrowserThread::UI, FROM_HERE, | 172 BrowserThread::UI, FROM_HERE, |
178 base::Bind(&StorePolicyOp::OnKeyOpComplete, this, return_code, | 173 base::Bind(&StorePolicyOp::OnKeyOpComplete, this, return_code, |
179 payload)); | 174 payload)); |
180 return; | 175 return; |
181 } | 176 } |
182 VLOG(2) << "StorePolicyOp::OnKeyOpComplete return_code = " << return_code; | 177 VLOG(2) << "StorePolicyOp::OnKeyOpComplete return_code = " << return_code; |
183 // Now, sure we're on the UI thread. | 178 // Now, sure we're on the UI thread. |
184 if (return_code == OwnerManager::SUCCESS) { | 179 if (return_code == OwnerManager::SUCCESS) { |
185 policy_->set_policy_data_signature(std::string(payload.begin(), | 180 policy_->set_policy_data_signature(std::string(payload.begin(), |
186 payload.end())); | 181 payload.end())); |
187 RequestStorePolicy(); | 182 RequestStorePolicy(); |
188 return; | 183 return; |
189 } | 184 } |
190 Fail(SignedSettings::MapKeyOpCode(return_code)); | 185 Fail(SignedSettings::MapKeyOpCode(return_code)); |
191 } | 186 } |
192 | 187 |
| 188 StorePolicyOp::~StorePolicyOp() {} |
| 189 |
193 void StorePolicyOp::RequestStorePolicy() { | 190 void StorePolicyOp::RequestStorePolicy() { |
194 std::string serialized; | 191 std::string serialized; |
195 if (policy_->SerializeToString(&serialized)) { | 192 if (policy_->SerializeToString(&serialized)) { |
196 DBusThreadManager::Get()->GetSessionManagerClient()->StoreDevicePolicy( | 193 DBusThreadManager::Get()->GetSessionManagerClient()->StoreDevicePolicy( |
197 serialized, | 194 serialized, |
198 base::Bind(&StorePolicyOp::OnBoolComplete, this)); | 195 base::Bind(&StorePolicyOp::OnBoolComplete, this)); |
199 } else { | 196 } else { |
200 Fail(OPERATION_FAILED); | 197 Fail(OPERATION_FAILED); |
201 } | 198 } |
202 } | 199 } |
203 | 200 |
| 201 void StorePolicyOp::OnBoolComplete(bool success) { |
| 202 if (success) |
| 203 Succeed(true); |
| 204 else |
| 205 Fail(NOT_FOUND); |
| 206 } |
| 207 |
204 void StorePolicyOp::PerformCallback(SignedSettings::ReturnCode code, | 208 void StorePolicyOp::PerformCallback(SignedSettings::ReturnCode code, |
205 bool value) { | 209 bool value) { |
206 d_->OnSettingsOpCompleted(code, value); | 210 d_->OnSettingsOpCompleted(code, value); |
207 } | 211 } |
208 | 212 |
209 RetrievePolicyOp::RetrievePolicyOp( | 213 RetrievePolicyOp::RetrievePolicyOp( |
210 SignedSettings::Delegate<const em::PolicyFetchResponse&>* d) | 214 SignedSettings::Delegate<const em::PolicyFetchResponse&>* d) |
211 : d_(d) { | 215 : d_(d) { |
212 } | 216 } |
213 | 217 |
214 RetrievePolicyOp::~RetrievePolicyOp() {} | 218 void RetrievePolicyOp::Succeed(const em::PolicyFetchResponse& value) { |
| 219 em::PolicyData poldata; |
| 220 if (SignedSettings::PolicyIsSane(value, &poldata)) { |
| 221 BrowserThread::PostTask( |
| 222 BrowserThread::UI, FROM_HERE, |
| 223 base::Bind(&RetrievePolicyOp::PerformCallback, this, SUCCESS, value)); |
| 224 } else { |
| 225 Fail(NOT_FOUND); |
| 226 } |
| 227 } |
215 | 228 |
216 void RetrievePolicyOp::Execute() { | 229 void RetrievePolicyOp::Execute() { |
217 DBusThreadManager::Get()->GetSessionManagerClient()->RetrieveDevicePolicy( | 230 DBusThreadManager::Get()->GetSessionManagerClient()->RetrieveDevicePolicy( |
218 base::Bind(&RetrievePolicyOp::OnStringComplete, this)); | 231 base::Bind(&RetrievePolicyOp::OnStringComplete, this)); |
219 } | 232 } |
220 | 233 |
221 void RetrievePolicyOp::Fail(SignedSettings::ReturnCode code) { | 234 void RetrievePolicyOp::Fail(SignedSettings::ReturnCode code) { |
222 VLOG(2) << "RetrievePolicyOp::Execute() failed with " << code; | 235 VLOG(2) << "RetrievePolicyOp::Execute() failed with " << code; |
223 BrowserThread::PostTask( | 236 BrowserThread::PostTask( |
224 BrowserThread::UI, FROM_HERE, | 237 BrowserThread::UI, FROM_HERE, |
225 base::Bind(&RetrievePolicyOp::PerformCallback, this, code, | 238 base::Bind(&RetrievePolicyOp::PerformCallback, this, code, |
226 em::PolicyFetchResponse())); | 239 em::PolicyFetchResponse())); |
227 } | 240 } |
228 | 241 |
229 void RetrievePolicyOp::Succeed(const em::PolicyFetchResponse& value) { | |
230 em::PolicyData poldata; | |
231 if (SignedSettings::PolicyIsSane(value, &poldata)) { | |
232 BrowserThread::PostTask( | |
233 BrowserThread::UI, FROM_HERE, | |
234 base::Bind(&RetrievePolicyOp::PerformCallback, this, SUCCESS, value)); | |
235 } else { | |
236 Fail(NOT_FOUND); | |
237 } | |
238 } | |
239 | |
240 void RetrievePolicyOp::OnKeyOpComplete( | 242 void RetrievePolicyOp::OnKeyOpComplete( |
241 const OwnerManager::KeyOpCode return_code, | 243 const OwnerManager::KeyOpCode return_code, |
242 const std::vector<uint8>& payload) { | 244 const std::vector<uint8>& payload) { |
243 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 245 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
244 BrowserThread::PostTask( | 246 BrowserThread::PostTask( |
245 BrowserThread::UI, FROM_HERE, | 247 BrowserThread::UI, FROM_HERE, |
246 base::Bind(&RetrievePolicyOp::OnKeyOpComplete, this, return_code, | 248 base::Bind(&RetrievePolicyOp::OnKeyOpComplete, this, return_code, |
247 payload)); | 249 payload)); |
248 return; | 250 return; |
249 } | 251 } |
250 // Now, sure we're on the UI thread. | 252 // Now, sure we're on the UI thread. |
251 if (return_code == OwnerManager::SUCCESS) | 253 if (return_code == OwnerManager::SUCCESS) |
252 Succeed(policy_); | 254 Succeed(policy_); |
253 else | 255 else |
254 Fail(SignedSettings::MapKeyOpCode(return_code)); | 256 Fail(SignedSettings::MapKeyOpCode(return_code)); |
255 } | 257 } |
256 | 258 |
| 259 RetrievePolicyOp::~RetrievePolicyOp() {} |
| 260 |
257 void RetrievePolicyOp::OnStringComplete(const std::string& serialized_proto) { | 261 void RetrievePolicyOp::OnStringComplete(const std::string& serialized_proto) { |
258 ProcessPolicy(serialized_proto); | 262 ProcessPolicy(serialized_proto); |
259 } | 263 } |
260 | 264 |
261 void RetrievePolicyOp::ProcessPolicy(const std::string& serialized_proto) { | 265 void RetrievePolicyOp::ProcessPolicy(const std::string& serialized_proto) { |
262 if (serialized_proto.empty() || !policy_.ParseFromString(serialized_proto) || | 266 if (serialized_proto.empty() || !policy_.ParseFromString(serialized_proto) || |
263 (!policy_.has_policy_data() && !policy_.has_policy_data_signature())) { | 267 (!policy_.has_policy_data() && !policy_.has_policy_data_signature())) { |
264 Fail(NOT_FOUND); | 268 Fail(NOT_FOUND); |
265 return; | 269 return; |
266 } | 270 } |
(...skipping 10 matching lines...) Expand all Loading... |
277 sig.assign(sig_ptr, sig_ptr + policy_.policy_data_signature().length()); | 281 sig.assign(sig_ptr, sig_ptr + policy_.policy_data_signature().length()); |
278 service_->StartVerifyAttempt(policy_.policy_data(), sig, this); | 282 service_->StartVerifyAttempt(policy_.policy_data(), sig, this); |
279 } | 283 } |
280 | 284 |
281 void RetrievePolicyOp::PerformCallback(SignedSettings::ReturnCode code, | 285 void RetrievePolicyOp::PerformCallback(SignedSettings::ReturnCode code, |
282 const em::PolicyFetchResponse& value) { | 286 const em::PolicyFetchResponse& value) { |
283 d_->OnSettingsOpCompleted(code, value); | 287 d_->OnSettingsOpCompleted(code, value); |
284 } | 288 } |
285 | 289 |
286 } // namespace chromeos | 290 } // namespace chromeos |
OLD | NEW |