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 "components/policy/core/common/cloud/cloud_policy_client.h" | 5 #include "components/policy/core/common/cloud/cloud_policy_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/guid.h" | 8 #include "base/guid.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 type == GetChromeUserPolicyType(); | 38 type == GetChromeUserPolicyType(); |
39 } | 39 } |
40 | 40 |
41 } // namespace | 41 } // namespace |
42 | 42 |
43 CloudPolicyClient::Observer::~Observer() {} | 43 CloudPolicyClient::Observer::~Observer() {} |
44 | 44 |
45 void CloudPolicyClient::Observer::OnRobotAuthCodesFetched( | 45 void CloudPolicyClient::Observer::OnRobotAuthCodesFetched( |
46 CloudPolicyClient* client) {} | 46 CloudPolicyClient* client) {} |
47 | 47 |
48 CloudPolicyClient::StatusProvider::~StatusProvider() {} | |
49 | |
50 CloudPolicyClient::CloudPolicyClient( | 48 CloudPolicyClient::CloudPolicyClient( |
51 const std::string& machine_id, | 49 const std::string& machine_id, |
52 const std::string& machine_model, | 50 const std::string& machine_model, |
53 const std::string& verification_key_hash, | 51 const std::string& verification_key_hash, |
54 UserAffiliation user_affiliation, | 52 UserAffiliation user_affiliation, |
55 DeviceManagementService* service, | 53 DeviceManagementService* service, |
56 scoped_refptr<net::URLRequestContextGetter> request_context) | 54 scoped_refptr<net::URLRequestContextGetter> request_context) |
57 : machine_id_(machine_id), | 55 : machine_id_(machine_id), |
58 machine_model_(machine_model), | 56 machine_model_(machine_model), |
59 verification_key_hash_(verification_key_hash), | 57 verification_key_hash_(verification_key_hash), |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 last_policy_timestamp_ - base::Time::UnixEpoch()); | 177 last_policy_timestamp_ - base::Time::UnixEpoch()); |
180 fetch_request->set_timestamp(timestamp.InMilliseconds()); | 178 fetch_request->set_timestamp(timestamp.InMilliseconds()); |
181 } | 179 } |
182 if (!invalidation_payload_.empty()) { | 180 if (!invalidation_payload_.empty()) { |
183 fetch_request->set_invalidation_version(invalidation_version_); | 181 fetch_request->set_invalidation_version(invalidation_version_); |
184 fetch_request->set_invalidation_payload(invalidation_payload_); | 182 fetch_request->set_invalidation_payload(invalidation_payload_); |
185 } | 183 } |
186 } | 184 } |
187 } | 185 } |
188 | 186 |
189 // Add status data. | |
190 if (status_provider_) { | |
191 if (!status_provider_->GetDeviceStatus( | |
192 request->mutable_device_status_report_request())) { | |
193 request->clear_device_status_report_request(); | |
194 } | |
195 if (!status_provider_->GetSessionStatus( | |
196 request->mutable_session_status_report_request())) { | |
197 request->clear_session_status_report_request(); | |
198 } | |
199 } | |
200 | |
201 // Add device state keys. | 187 // Add device state keys. |
202 if (!state_keys_to_upload_.empty()) { | 188 if (!state_keys_to_upload_.empty()) { |
203 em::DeviceStateKeyUpdateRequest* key_update_request = | 189 em::DeviceStateKeyUpdateRequest* key_update_request = |
204 request->mutable_device_state_key_update_request(); | 190 request->mutable_device_state_key_update_request(); |
205 for (std::vector<std::string>::const_iterator key( | 191 for (std::vector<std::string>::const_iterator key( |
206 state_keys_to_upload_.begin()); | 192 state_keys_to_upload_.begin()); |
207 key != state_keys_to_upload_.end(); | 193 key != state_keys_to_upload_.end(); |
208 ++key) { | 194 ++key) { |
209 key_update_request->add_server_backed_state_key(*key); | 195 key_update_request->add_server_backed_state_key(*key); |
210 } | 196 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 request->mutable_cert_upload_request()->set_device_certificate( | 255 request->mutable_cert_upload_request()->set_device_certificate( |
270 certificate_data); | 256 certificate_data); |
271 | 257 |
272 DeviceManagementRequestJob::Callback job_callback = base::Bind( | 258 DeviceManagementRequestJob::Callback job_callback = base::Bind( |
273 &CloudPolicyClient::OnCertificateUploadCompleted, | 259 &CloudPolicyClient::OnCertificateUploadCompleted, |
274 base::Unretained(this), | 260 base::Unretained(this), |
275 callback); | 261 callback); |
276 request_job_->Start(job_callback); | 262 request_job_->Start(job_callback); |
277 } | 263 } |
278 | 264 |
| 265 void CloudPolicyClient::UploadDeviceStatus( |
| 266 const em::DeviceStatusReportRequest* device_status, |
| 267 const em::SessionStatusReportRequest* session_status, |
| 268 const CloudPolicyClient::StatusCallback& callback) { |
| 269 CHECK(is_registered()); |
| 270 // Should pass in at least one type of status. |
| 271 DCHECK(device_status || session_status); |
| 272 request_job_.reset( |
| 273 service_->CreateJob(DeviceManagementRequestJob::TYPE_UPLOAD_STATUS, |
| 274 GetRequestContext())); |
| 275 request_job_->SetDMToken(dm_token_); |
| 276 request_job_->SetClientID(client_id_); |
| 277 |
| 278 em::DeviceManagementRequest* request = request_job_->GetRequest(); |
| 279 if (device_status) |
| 280 *request->mutable_device_status_report_request() = *device_status; |
| 281 if (session_status) |
| 282 *request->mutable_session_status_report_request() = *session_status; |
| 283 |
| 284 DeviceManagementRequestJob::Callback job_callback = base::Bind( |
| 285 &CloudPolicyClient::OnStatusUploadCompleted, |
| 286 base::Unretained(this), |
| 287 callback); |
| 288 |
| 289 // TODO(atwilson): Change CloudPolicyClient to support multiple requests in |
| 290 // parallel, so status upload requests don't get cancelled by things like |
| 291 // policy fetches (http://crbug.com/452563). |
| 292 request_job_->Start(job_callback); |
| 293 } |
| 294 |
279 void CloudPolicyClient::AddObserver(Observer* observer) { | 295 void CloudPolicyClient::AddObserver(Observer* observer) { |
280 observers_.AddObserver(observer); | 296 observers_.AddObserver(observer); |
281 } | 297 } |
282 | 298 |
283 void CloudPolicyClient::RemoveObserver(Observer* observer) { | 299 void CloudPolicyClient::RemoveObserver(Observer* observer) { |
284 observers_.RemoveObserver(observer); | 300 observers_.RemoveObserver(observer); |
285 } | 301 } |
286 | 302 |
287 void CloudPolicyClient::SetStatusProvider(scoped_ptr<StatusProvider> provider) { | |
288 status_provider_ = provider.Pass(); | |
289 } | |
290 | |
291 bool CloudPolicyClient::HasStatusProviderForTest() { | |
292 return status_provider_; | |
293 } | |
294 | |
295 void CloudPolicyClient::AddPolicyTypeToFetch( | 303 void CloudPolicyClient::AddPolicyTypeToFetch( |
296 const std::string& policy_type, | 304 const std::string& policy_type, |
297 const std::string& settings_entity_id) { | 305 const std::string& settings_entity_id) { |
298 types_to_fetch_.insert(std::make_pair(policy_type, settings_entity_id)); | 306 types_to_fetch_.insert(std::make_pair(policy_type, settings_entity_id)); |
299 } | 307 } |
300 | 308 |
301 void CloudPolicyClient::RemovePolicyTypeToFetch( | 309 void CloudPolicyClient::RemovePolicyTypeToFetch( |
302 const std::string& policy_type, | 310 const std::string& policy_type, |
303 const std::string& settings_entity_id) { | 311 const std::string& settings_entity_id) { |
304 types_to_fetch_.erase(std::make_pair(policy_type, settings_entity_id)); | 312 types_to_fetch_.erase(std::make_pair(policy_type, settings_entity_id)); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 if (policy_data.has_settings_entity_id()) | 424 if (policy_data.has_settings_entity_id()) |
417 entity_id = policy_data.settings_entity_id(); | 425 entity_id = policy_data.settings_entity_id(); |
418 std::pair<std::string, std::string> key(type, entity_id); | 426 std::pair<std::string, std::string> key(type, entity_id); |
419 if (ContainsKey(responses_, key)) { | 427 if (ContainsKey(responses_, key)) { |
420 LOG(WARNING) << "Duplicate PolicyFetchResponse for type: " | 428 LOG(WARNING) << "Duplicate PolicyFetchResponse for type: " |
421 << type << ", entity: " << entity_id << ", ignoring"; | 429 << type << ", entity: " << entity_id << ", ignoring"; |
422 continue; | 430 continue; |
423 } | 431 } |
424 responses_[key] = new em::PolicyFetchResponse(response); | 432 responses_[key] = new em::PolicyFetchResponse(response); |
425 } | 433 } |
426 if (status_provider_) | |
427 status_provider_->OnSubmittedSuccessfully(); | |
428 state_keys_to_upload_.clear(); | 434 state_keys_to_upload_.clear(); |
429 NotifyPolicyFetched(); | 435 NotifyPolicyFetched(); |
430 } else { | 436 } else { |
431 NotifyClientError(); | 437 NotifyClientError(); |
432 } | 438 } |
433 } | 439 } |
434 | 440 |
435 void CloudPolicyClient::OnUnregisterCompleted( | 441 void CloudPolicyClient::OnUnregisterCompleted( |
436 DeviceManagementStatus status, | 442 DeviceManagementStatus status, |
437 int net_error, | 443 int net_error, |
(...skipping 25 matching lines...) Expand all Loading... |
463 | 469 |
464 status_ = status; | 470 status_ = status; |
465 if (status != DM_STATUS_SUCCESS) { | 471 if (status != DM_STATUS_SUCCESS) { |
466 NotifyClientError(); | 472 NotifyClientError(); |
467 callback.Run(false); | 473 callback.Run(false); |
468 return; | 474 return; |
469 } | 475 } |
470 callback.Run(true); | 476 callback.Run(true); |
471 } | 477 } |
472 | 478 |
| 479 void CloudPolicyClient::OnStatusUploadCompleted( |
| 480 const CloudPolicyClient::StatusCallback& callback, |
| 481 DeviceManagementStatus status, |
| 482 int net_error, |
| 483 const enterprise_management::DeviceManagementResponse& response) { |
| 484 status_ = status; |
| 485 if (status != DM_STATUS_SUCCESS) |
| 486 NotifyClientError(); |
| 487 |
| 488 callback.Run(status == DM_STATUS_SUCCESS); |
| 489 } |
| 490 |
473 void CloudPolicyClient::NotifyPolicyFetched() { | 491 void CloudPolicyClient::NotifyPolicyFetched() { |
474 FOR_EACH_OBSERVER(Observer, observers_, OnPolicyFetched(this)); | 492 FOR_EACH_OBSERVER(Observer, observers_, OnPolicyFetched(this)); |
475 } | 493 } |
476 | 494 |
477 void CloudPolicyClient::NotifyRegistrationStateChanged() { | 495 void CloudPolicyClient::NotifyRegistrationStateChanged() { |
478 FOR_EACH_OBSERVER(Observer, observers_, OnRegistrationStateChanged(this)); | 496 FOR_EACH_OBSERVER(Observer, observers_, OnRegistrationStateChanged(this)); |
479 } | 497 } |
480 | 498 |
481 void CloudPolicyClient::NotifyRobotAuthCodesFetched() { | 499 void CloudPolicyClient::NotifyRobotAuthCodesFetched() { |
482 FOR_EACH_OBSERVER(Observer, observers_, OnRobotAuthCodesFetched(this)); | 500 FOR_EACH_OBSERVER(Observer, observers_, OnRobotAuthCodesFetched(this)); |
483 } | 501 } |
484 | 502 |
485 void CloudPolicyClient::NotifyClientError() { | 503 void CloudPolicyClient::NotifyClientError() { |
486 FOR_EACH_OBSERVER(Observer, observers_, OnClientError(this)); | 504 FOR_EACH_OBSERVER(Observer, observers_, OnClientError(this)); |
487 } | 505 } |
488 | 506 |
489 } // namespace policy | 507 } // namespace policy |
OLD | NEW |