Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2245)

Unified Diff: chrome/browser/policy/device_token_fetcher.cc

Issue 9064006: Convert DeviceTokenFetcher and CloudPolicyController response callbacks. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address nits Created 8 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/policy/device_token_fetcher.cc
diff --git a/chrome/browser/policy/device_token_fetcher.cc b/chrome/browser/policy/device_token_fetcher.cc
index 540c4df0c4990b6f10ab310ede62771db2d624b4..95942b68a048b961bdad8004968dfa9895c3ec06 100644
--- a/chrome/browser/policy/device_token_fetcher.cc
+++ b/chrome/browser/policy/device_token_fetcher.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -10,12 +10,12 @@
#include "base/metrics/histogram.h"
#include "base/time.h"
#include "chrome/browser/policy/cloud_policy_cache_base.h"
+#include "chrome/browser/policy/cloud_policy_constants.h"
#include "chrome/browser/policy/cloud_policy_data_store.h"
#include "chrome/browser/policy/delayed_work_scheduler.h"
#include "chrome/browser/policy/device_management_service.h"
#include "chrome/browser/policy/enterprise_metrics.h"
#include "chrome/browser/policy/policy_notifier.h"
-#include "chrome/browser/policy/proto/device_management_constants.h"
#include "chrome/browser/policy/proto/device_management_local.pb.h"
namespace {
@@ -63,27 +63,6 @@ void DeviceTokenFetcher::FetchToken() {
FetchTokenInternal();
}
-void DeviceTokenFetcher::FetchTokenInternal() {
- DCHECK(state_ != STATE_TOKEN_AVAILABLE);
- if (!data_store_->has_auth_token() || data_store_->device_id().empty()) {
- // Maybe this device is unmanaged, just exit. The CloudPolicyController
- // will call FetchToken() again if something changes.
- return;
- }
- // Construct a new backend, which will discard any previous requests.
- backend_.reset(service_->CreateBackend());
- em::DeviceRegisterRequest request;
- request.set_type(data_store_->policy_register_type());
- if (!data_store_->machine_id().empty())
- request.set_machine_id(data_store_->machine_id());
- if (!data_store_->machine_model().empty())
- request.set_machine_model(data_store_->machine_model());
- backend_->ProcessRegisterRequest(data_store_->gaia_token(),
- data_store_->oauth_token(),
- data_store_->device_id(),
- request, this);
-}
-
void DeviceTokenFetcher::SetUnmanagedState() {
// The call to |cache_->SetUnmanaged()| has to happen first because it sets
// the timestamp that |SetState()| needs to determine the correct refresh
@@ -100,45 +79,95 @@ void DeviceTokenFetcher::Reset() {
SetState(STATE_INACTIVE);
}
-void DeviceTokenFetcher::HandleRegisterResponse(
- const em::DeviceRegisterResponse& response) {
- if (response.has_device_management_token()) {
- UMA_HISTOGRAM_ENUMERATION(kMetricToken, kMetricTokenFetchOK,
- kMetricTokenSize);
- data_store_->SetDeviceToken(response.device_management_token(), false);
- SetState(STATE_TOKEN_AVAILABLE);
- } else {
- NOTREACHED();
- UMA_HISTOGRAM_ENUMERATION(kMetricToken, kMetricTokenFetchBadResponse,
- kMetricTokenSize);
- SetState(STATE_ERROR);
+void DeviceTokenFetcher::Initialize(DeviceManagementService* service,
+ CloudPolicyCacheBase* cache,
+ CloudPolicyDataStore* data_store,
+ PolicyNotifier* notifier,
+ DelayedWorkScheduler* scheduler) {
+ service_ = service;
+ cache_ = cache;
+ notifier_ = notifier;
+ data_store_ = data_store;
+ effective_token_fetch_error_delay_ms_ = kTokenFetchErrorDelayMilliseconds;
+ state_ = STATE_INACTIVE;
+ scheduler_.reset(scheduler);
+
+ if (cache_->is_unmanaged())
+ SetState(STATE_UNMANAGED);
+}
+
+void DeviceTokenFetcher::FetchTokenInternal() {
+ DCHECK(state_ != STATE_TOKEN_AVAILABLE);
+ if (!data_store_->has_auth_token() || data_store_->device_id().empty()) {
+ // Maybe this device is unmanaged, just exit. The CloudPolicyController
+ // will call FetchToken() again if something changes.
+ return;
}
+ // Reinitialize |request_job_|, discarding any previous requests.
+ request_job_.reset(
+ service_->CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION));
+ request_job_->SetGaiaToken(data_store_->gaia_token());
+ request_job_->SetOAuthToken(data_store_->oauth_token());
+ request_job_->SetClientID(data_store_->device_id());
+ em::DeviceRegisterRequest* request =
+ request_job_->GetRequest()->mutable_register_request();
+ request->set_type(data_store_->policy_register_type());
+ if (!data_store_->machine_id().empty())
+ request->set_machine_id(data_store_->machine_id());
+ if (!data_store_->machine_model().empty())
+ request->set_machine_model(data_store_->machine_model());
+ request_job_->Start(base::Bind(&DeviceTokenFetcher::OnTokenFetchCompleted,
+ base::Unretained(this)));
}
-void DeviceTokenFetcher::OnError(DeviceManagementBackend::ErrorCode code) {
- switch (code) {
- case DeviceManagementBackend::kErrorServiceManagementNotSupported:
+void DeviceTokenFetcher::OnTokenFetchCompleted(
+ DeviceManagementStatus status,
+ const em::DeviceManagementResponse& response) {
+ if (status == DM_STATUS_SUCCESS && !response.has_register_response()) {
+ // Handled below.
+ status = DM_STATUS_RESPONSE_DECODING_ERROR;
+ }
+
+ switch (status) {
+ case DM_STATUS_SUCCESS: {
+ const em::DeviceRegisterResponse& register_response =
+ response.register_response();
+ if (register_response.has_device_management_token()) {
+ UMA_HISTOGRAM_ENUMERATION(kMetricToken, kMetricTokenFetchOK,
+ kMetricTokenSize);
+ data_store_->SetDeviceToken(register_response.device_management_token(),
+ false);
+ SetState(STATE_TOKEN_AVAILABLE);
+ } else {
+ NOTREACHED();
+ UMA_HISTOGRAM_ENUMERATION(kMetricToken, kMetricTokenFetchBadResponse,
+ kMetricTokenSize);
+ SetState(STATE_ERROR);
+ }
+ return;
+ }
+ case DM_STATUS_SERVICE_MANAGEMENT_NOT_SUPPORTED:
SetUnmanagedState();
return;
- case DeviceManagementBackend::kErrorRequestFailed:
- case DeviceManagementBackend::kErrorTemporaryUnavailable:
- case DeviceManagementBackend::kErrorServiceDeviceNotFound:
- case DeviceManagementBackend::kErrorServiceDeviceIdConflict:
+ case DM_STATUS_REQUEST_FAILED:
+ case DM_STATUS_TEMPORARY_UNAVAILABLE:
+ case DM_STATUS_SERVICE_DEVICE_NOT_FOUND:
+ case DM_STATUS_SERVICE_DEVICE_ID_CONFLICT:
SetState(STATE_TEMPORARY_ERROR);
return;
- case DeviceManagementBackend::kErrorServiceManagementTokenInvalid:
+ case DM_STATUS_SERVICE_MANAGEMENT_TOKEN_INVALID:
// Most probably the GAIA auth cookie has expired. We can not do anything
// until the user logs-in again.
SetState(STATE_BAD_AUTH);
return;
- case DeviceManagementBackend::kErrorServiceInvalidSerialNumber:
+ case DM_STATUS_SERVICE_INVALID_SERIAL_NUMBER:
SetSerialNumberInvalidState();
return;
- case DeviceManagementBackend::kErrorRequestInvalid:
- case DeviceManagementBackend::kErrorHttpStatus:
- case DeviceManagementBackend::kErrorResponseDecoding:
- case DeviceManagementBackend::kErrorServiceActivationPending:
- case DeviceManagementBackend::kErrorServicePolicyNotFound:
+ case DM_STATUS_REQUEST_INVALID:
+ case DM_STATUS_HTTP_STATUS_ERROR:
+ case DM_STATUS_RESPONSE_DECODING_ERROR:
+ case DM_STATUS_SERVICE_ACTIVATION_PENDING:
+ case DM_STATUS_SERVICE_POLICY_NOT_FOUND:
SetState(STATE_ERROR);
return;
}
@@ -146,29 +175,12 @@ void DeviceTokenFetcher::OnError(DeviceManagementBackend::ErrorCode code) {
SetState(STATE_ERROR);
}
-void DeviceTokenFetcher::Initialize(DeviceManagementService* service,
- CloudPolicyCacheBase* cache,
- CloudPolicyDataStore* data_store,
- PolicyNotifier* notifier,
- DelayedWorkScheduler* scheduler) {
- service_ = service;
- cache_ = cache;
- notifier_ = notifier;
- data_store_ = data_store;
- effective_token_fetch_error_delay_ms_ = kTokenFetchErrorDelayMilliseconds;
- state_ = STATE_INACTIVE;
- scheduler_.reset(scheduler);
-
- if (cache_->is_unmanaged())
- SetState(STATE_UNMANAGED);
-}
-
void DeviceTokenFetcher::SetState(FetcherState state) {
state_ = state;
if (state_ != STATE_TEMPORARY_ERROR)
effective_token_fetch_error_delay_ms_ = kTokenFetchErrorDelayMilliseconds;
- backend_.reset(); // Stop any pending requests.
+ request_job_.reset(); // Stop any pending requests.
base::Time delayed_work_at;
switch (state_) {
« no previous file with comments | « chrome/browser/policy/device_token_fetcher.h ('k') | chrome/browser/policy/device_token_fetcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698