Chromium Code Reviews| 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/device_management_service.h" | 5 #include "components/policy/core/common/cloud/device_management_service.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 void HandleResponse(const net::URLRequestStatus& status, | 169 void HandleResponse(const net::URLRequestStatus& status, |
| 170 int response_code, | 170 int response_code, |
| 171 const std::string& data); | 171 const std::string& data); |
| 172 | 172 |
| 173 // Gets the URL to contact. | 173 // Gets the URL to contact. |
| 174 GURL GetURL(const std::string& server_url); | 174 GURL GetURL(const std::string& server_url); |
| 175 | 175 |
| 176 // Configures the fetcher, setting up payload and headers. | 176 // Configures the fetcher, setting up payload and headers. |
| 177 void ConfigureRequest(net::URLFetcher* fetcher); | 177 void ConfigureRequest(net::URLFetcher* fetcher); |
| 178 | 178 |
| 179 // Returns true if this job should be retried. |fetcher| has just completed, | 179 enum RetryMethod { NO_RETRY, RETRY_IMMEDIATELY, RETRY_WITH_DELAY }; |
|
Andrew T Wilson (Slow)
2016/08/23 14:53:08
In general, enums are not crammed on one line like
Marton Hunyady
2016/08/23 16:00:38
Done.
| |
| 180 // and can be inspected to determine if the request failed and should be | 180 |
| 181 // retried. | 181 // Returns if and how this job should be retried. |fetcher| has just |
| 182 bool ShouldRetry(const net::URLFetcher* fetcher); | 182 // completed, and can be inspected to determine if the request failed and |
| 183 // should be retried. | |
| 184 RetryMethod ShouldRetry(const net::URLFetcher* fetcher); | |
| 183 | 185 |
| 184 // Invoked right before retrying this job. | 186 // Invoked right before retrying this job. |
| 185 void PrepareRetry(); | 187 void PrepareRetry(); |
| 186 | 188 |
| 187 // Number of times that this job has been retried due to connection errors. | 189 // Number of times that this job has been retried due to connection errors. |
| 188 int retries_count() { return retries_count_; } | 190 int retries_count() { return retries_count_; } |
| 189 | 191 |
| 190 // Get weak pointer | 192 // Get weak pointer |
| 191 base::WeakPtr<DeviceManagementRequestJobImpl> GetWeakPtr() { | 193 base::WeakPtr<DeviceManagementRequestJobImpl> GetWeakPtr() { |
| 192 return weak_ptr_factory_.GetWeakPtr(); | 194 return weak_ptr_factory_.GetWeakPtr(); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 CHECK(request_.SerializeToString(&payload)); | 344 CHECK(request_.SerializeToString(&payload)); |
| 343 fetcher->SetUploadData(kPostContentType, payload); | 345 fetcher->SetUploadData(kPostContentType, payload); |
| 344 std::string extra_headers; | 346 std::string extra_headers; |
| 345 if (!gaia_token_.empty()) | 347 if (!gaia_token_.empty()) |
| 346 extra_headers += kServiceTokenAuthHeader + gaia_token_ + "\n"; | 348 extra_headers += kServiceTokenAuthHeader + gaia_token_ + "\n"; |
| 347 if (!dm_token_.empty()) | 349 if (!dm_token_.empty()) |
| 348 extra_headers += kDMTokenAuthHeader + dm_token_ + "\n"; | 350 extra_headers += kDMTokenAuthHeader + dm_token_ + "\n"; |
| 349 fetcher->SetExtraRequestHeaders(extra_headers); | 351 fetcher->SetExtraRequestHeaders(extra_headers); |
| 350 } | 352 } |
| 351 | 353 |
| 352 bool DeviceManagementRequestJobImpl::ShouldRetry( | 354 DeviceManagementRequestJobImpl::RetryMethod |
| 353 const net::URLFetcher* fetcher) { | 355 DeviceManagementRequestJobImpl::ShouldRetry(const net::URLFetcher* fetcher) { |
| 354 if (FailedWithProxy(fetcher) && !bypass_proxy_) { | 356 if (FailedWithProxy(fetcher) && !bypass_proxy_) { |
| 355 // Retry the job if it failed due to a broken proxy, by bypassing the | 357 // Retry the job immediately if it failed due to a broken proxy, by |
| 356 // proxy on the next try. | 358 // bypassing the proxy on the next try. |
| 357 bypass_proxy_ = true; | 359 bypass_proxy_ = true; |
| 358 return true; | 360 return RETRY_IMMEDIATELY; |
| 359 } | 361 } |
| 360 | 362 |
| 361 // Early device policy fetches on ChromeOS and Auto-Enrollment checks are | 363 // Early device policy fetches on ChromeOS and Auto-Enrollment checks are |
| 362 // often interrupted during ChromeOS startup when network is not yet ready. | 364 // often interrupted during ChromeOS startup when network is not yet ready. |
| 363 // Allowing the fetcher to retry once after that is enough to recover; allow | 365 // Allowing the fetcher to retry once after that is enough to recover; allow |
| 364 // it to retry up to 3 times just in case. | 366 // it to retry up to 3 times just in case. |
| 365 if (IsConnectionError(fetcher->GetStatus()) && retries_count_ < kMaxRetries) { | 367 if (IsConnectionError(fetcher->GetStatus()) && retries_count_ < kMaxRetries && |
| 368 type_ != DeviceManagementRequestJob::TYPE_POLICY_FETCH) { | |
|
Andrew T Wilson (Slow)
2016/08/23 14:53:08
Per our conversation, let's retry TYPE_POLICY_FETC
Marton Hunyady
2016/08/23 16:00:38
Done.
| |
| 366 ++retries_count_; | 369 ++retries_count_; |
| 367 return true; | 370 return RETRY_WITH_DELAY; |
| 368 } | 371 } |
| 369 | 372 |
| 370 // The request didn't fail, or the limit of retry attempts has been reached; | 373 // The request didn't fail, or the limit of retry attempts has been reached; |
| 371 // forward the result to the job owner. | 374 // forward the result to the job owner. |
| 372 return false; | 375 return NO_RETRY; |
| 373 } | 376 } |
| 374 | 377 |
| 375 void DeviceManagementRequestJobImpl::PrepareRetry() { | 378 void DeviceManagementRequestJobImpl::PrepareRetry() { |
| 376 if (!retry_callback_.is_null()) | 379 if (!retry_callback_.is_null()) |
| 377 retry_callback_.Run(this); | 380 retry_callback_.Run(this); |
| 378 } | 381 } |
| 379 | 382 |
| 380 void DeviceManagementRequestJobImpl::ReportError(DeviceManagementStatus code) { | 383 void DeviceManagementRequestJobImpl::ReportError(DeviceManagementStatus code) { |
| 381 em::DeviceManagementResponse dummy_response; | 384 em::DeviceManagementResponse dummy_response; |
| 382 callback_.Run(code, net::OK, dummy_response); | 385 callback_.Run(code, net::OK, dummy_response); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 400 AddParameter(dm_protocol::kParamDeviceID, client_id); | 403 AddParameter(dm_protocol::kParamDeviceID, client_id); |
| 401 } | 404 } |
| 402 | 405 |
| 403 em::DeviceManagementRequest* DeviceManagementRequestJob::GetRequest() { | 406 em::DeviceManagementRequest* DeviceManagementRequestJob::GetRequest() { |
| 404 return &request_; | 407 return &request_; |
| 405 } | 408 } |
| 406 | 409 |
| 407 DeviceManagementRequestJob::DeviceManagementRequestJob( | 410 DeviceManagementRequestJob::DeviceManagementRequestJob( |
| 408 JobType type, | 411 JobType type, |
| 409 const std::string& agent_parameter, | 412 const std::string& agent_parameter, |
| 410 const std::string& platform_parameter) { | 413 const std::string& platform_parameter) |
| 414 : type_(type) { | |
| 411 AddParameter(dm_protocol::kParamRequest, JobTypeToRequestType(type)); | 415 AddParameter(dm_protocol::kParamRequest, JobTypeToRequestType(type)); |
| 412 AddParameter(dm_protocol::kParamDeviceType, dm_protocol::kValueDeviceType); | 416 AddParameter(dm_protocol::kParamDeviceType, dm_protocol::kValueDeviceType); |
| 413 AddParameter(dm_protocol::kParamAppType, dm_protocol::kValueAppType); | 417 AddParameter(dm_protocol::kParamAppType, dm_protocol::kValueAppType); |
| 414 AddParameter(dm_protocol::kParamAgent, agent_parameter); | 418 AddParameter(dm_protocol::kParamAgent, agent_parameter); |
| 415 AddParameter(dm_protocol::kParamPlatform, platform_parameter); | 419 AddParameter(dm_protocol::kParamPlatform, platform_parameter); |
| 416 } | 420 } |
| 417 | 421 |
| 418 void DeviceManagementRequestJob::SetRetryCallback( | 422 void DeviceManagementRequestJob::SetRetryCallback( |
| 419 const RetryCallback& retry_callback) { | 423 const RetryCallback& retry_callback) { |
| 420 retry_callback_ = retry_callback; | 424 retry_callback_ = retry_callback; |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 535 const net::URLFetcher* source) { | 539 const net::URLFetcher* source) { |
| 536 JobFetcherMap::iterator entry(pending_jobs_.find(source)); | 540 JobFetcherMap::iterator entry(pending_jobs_.find(source)); |
| 537 if (entry == pending_jobs_.end()) { | 541 if (entry == pending_jobs_.end()) { |
| 538 NOTREACHED() << "Callback from foreign URL fetcher"; | 542 NOTREACHED() << "Callback from foreign URL fetcher"; |
| 539 return; | 543 return; |
| 540 } | 544 } |
| 541 | 545 |
| 542 DeviceManagementRequestJobImpl* job = entry->second; | 546 DeviceManagementRequestJobImpl* job = entry->second; |
| 543 pending_jobs_.erase(entry); | 547 pending_jobs_.erase(entry); |
| 544 | 548 |
| 545 if (job->ShouldRetry(source)) { | 549 DeviceManagementRequestJobImpl::RetryMethod retry_method = |
| 550 job->ShouldRetry(source); | |
| 551 if (retry_method != DeviceManagementRequestJobImpl::RetryMethod::NO_RETRY) { | |
| 546 job->PrepareRetry(); | 552 job->PrepareRetry(); |
| 547 int delay = g_retry_delay_ms << (job->retries_count() - 1); | 553 int delay = |
| 554 retry_method == | |
|
Andrew T Wilson (Slow)
2016/08/23 14:53:08
This is hard to read. Maybe break this out into a
Marton Hunyady
2016/08/23 16:00:38
Done.
| |
| 555 DeviceManagementRequestJobImpl::RetryMethod::RETRY_WITH_DELAY | |
| 556 ? g_retry_delay_ms << (job->retries_count() - 1) | |
| 557 : 0; | |
| 548 LOG(WARNING) << "Dmserver request failed, retrying in " << delay / 1000 | 558 LOG(WARNING) << "Dmserver request failed, retrying in " << delay / 1000 |
| 549 << "s."; | 559 << "s."; |
| 550 task_runner_->PostDelayedTask( | 560 task_runner_->PostDelayedTask( |
| 551 FROM_HERE, | 561 FROM_HERE, |
| 552 base::Bind(&DeviceManagementService::StartJobAfterDelay, | 562 base::Bind(&DeviceManagementService::StartJobAfterDelay, |
| 553 weak_ptr_factory_.GetWeakPtr(), job->GetWeakPtr()), | 563 weak_ptr_factory_.GetWeakPtr(), job->GetWeakPtr()), |
| 554 base::TimeDelta::FromMilliseconds(delay)); | 564 base::TimeDelta::FromMilliseconds(delay)); |
| 555 } else { | 565 } else { |
| 556 std::string data; | 566 std::string data; |
| 557 source->GetResponseAsString(&data); | 567 source->GetResponseAsString(&data); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 578 } | 588 } |
| 579 } | 589 } |
| 580 | 590 |
| 581 const JobQueue::iterator elem = | 591 const JobQueue::iterator elem = |
| 582 std::find(queued_jobs_.begin(), queued_jobs_.end(), job); | 592 std::find(queued_jobs_.begin(), queued_jobs_.end(), job); |
| 583 if (elem != queued_jobs_.end()) | 593 if (elem != queued_jobs_.end()) |
| 584 queued_jobs_.erase(elem); | 594 queued_jobs_.erase(elem); |
| 585 } | 595 } |
| 586 | 596 |
| 587 } // namespace policy | 597 } // namespace policy |
| OLD | NEW |