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/policy/enrollment_handler_chromeos.h" | 5 #include "chrome/browser/chromeos/policy/enrollment_handler_chromeos.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "chrome/browser/browser_process.h" | |
10 #include "chrome/browser/chromeos/policy/device_cloud_policy_store_chromeos.h" | 11 #include "chrome/browser/chromeos/policy/device_cloud_policy_store_chromeos.h" |
11 #include "chrome/browser/chromeos/policy/enterprise_install_attributes.h" | 12 #include "chrome/browser/chromeos/policy/enterprise_install_attributes.h" |
12 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" | 13 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" |
13 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | 14 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" |
14 #include "chrome/browser/policy/cloud/proto/device_management_backend.pb.h" | 15 #include "chrome/browser/policy/cloud/proto/device_management_backend.pb.h" |
16 #include "google_apis/gaia/gaia_urls.h" | |
15 | 17 |
16 namespace em = enterprise_management; | 18 namespace em = enterprise_management; |
17 | 19 |
18 namespace policy { | 20 namespace policy { |
19 | 21 |
20 namespace { | 22 namespace { |
21 | 23 |
22 // Retry for InstallAttrs initialization every 500ms. | 24 // Retry for InstallAttrs initialization every 500ms. |
23 const int kLockRetryIntervalMs = 500; | 25 const int kLockRetryIntervalMs = 500; |
24 // Maximum time to retry InstallAttrs initialization before we give up. | 26 // Maximum time to retry InstallAttrs initialization before we give up. |
25 const int kLockRetryTimeoutMs = 10 * 60 * 1000; // 10 minutes. | 27 const int kLockRetryTimeoutMs = 10 * 60 * 1000; // 10 minutes. |
28 // Number of times to retry fetching the device-level API refresh token. | |
29 const int kRobotRefreshTokenFetchRetryCount = 2; | |
26 | 30 |
27 } // namespace | 31 } // namespace |
28 | 32 |
29 EnrollmentHandlerChromeOS::EnrollmentHandlerChromeOS( | 33 EnrollmentHandlerChromeOS::EnrollmentHandlerChromeOS( |
30 DeviceCloudPolicyStoreChromeOS* store, | 34 DeviceCloudPolicyStoreChromeOS* store, |
31 EnterpriseInstallAttributes* install_attributes, | 35 EnterpriseInstallAttributes* install_attributes, |
32 scoped_ptr<CloudPolicyClient> client, | 36 scoped_ptr<CloudPolicyClient> client, |
33 const std::string& auth_token, | 37 const std::string& auth_token, |
34 const std::string& client_id, | 38 const std::string& client_id, |
35 bool is_auto_enrollment, | 39 bool is_auto_enrollment, |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 validator.release()->StartValidation( | 107 validator.release()->StartValidation( |
104 base::Bind(&EnrollmentHandlerChromeOS::PolicyValidated, | 108 base::Bind(&EnrollmentHandlerChromeOS::PolicyValidated, |
105 weak_factory_.GetWeakPtr())); | 109 weak_factory_.GetWeakPtr())); |
106 } | 110 } |
107 | 111 |
108 void EnrollmentHandlerChromeOS::OnRegistrationStateChanged( | 112 void EnrollmentHandlerChromeOS::OnRegistrationStateChanged( |
109 CloudPolicyClient* client) { | 113 CloudPolicyClient* client) { |
110 DCHECK_EQ(client_.get(), client); | 114 DCHECK_EQ(client_.get(), client); |
111 | 115 |
112 if (enrollment_step_ == STEP_REGISTRATION && client_->is_registered()) { | 116 if (enrollment_step_ == STEP_REGISTRATION && client_->is_registered()) { |
113 enrollment_step_ = STEP_POLICY_FETCH, | 117 enrollment_step_ = STEP_ROBOT_AUTH_FETCH, |
114 device_mode_ = client_->device_mode(); | 118 device_mode_ = client_->device_mode(); |
115 if (device_mode_ == DEVICE_MODE_NOT_SET) | 119 if (device_mode_ == DEVICE_MODE_NOT_SET) |
116 device_mode_ = DEVICE_MODE_ENTERPRISE; | 120 device_mode_ = DEVICE_MODE_ENTERPRISE; |
117 if (!allowed_device_modes_.test(device_mode_)) { | 121 if (!allowed_device_modes_.test(device_mode_)) { |
118 LOG(ERROR) << "Bad device mode " << device_mode_; | 122 LOG(ERROR) << "Bad device mode " << device_mode_; |
119 ReportResult(EnrollmentStatus::ForStatus( | 123 ReportResult(EnrollmentStatus::ForStatus( |
120 EnrollmentStatus::STATUS_REGISTRATION_BAD_MODE)); | 124 EnrollmentStatus::STATUS_REGISTRATION_BAD_MODE)); |
121 return; | 125 return; |
122 } | 126 } |
123 client_->FetchPolicy(); | 127 client_->FetchRobotAuthTokens(auth_token_); |
124 } else { | 128 } else { |
125 LOG(FATAL) << "Registration state changed to " << client_->is_registered() | 129 LOG(FATAL) << "Registration state changed to " << client_->is_registered() |
126 << " in step " << enrollment_step_; | 130 << " in step " << enrollment_step_; |
127 } | 131 } |
128 } | 132 } |
129 | 133 |
134 void EnrollmentHandlerChromeOS::OnRobotAuthCodesFetched( | |
135 CloudPolicyClient* client) { | |
136 DCHECK_EQ(client_.get(), client); | |
137 CHECK_EQ(STEP_ROBOT_AUTH_FETCH, enrollment_step_); | |
138 | |
139 enrollment_step_ = STEP_ROBOT_AUTH_REFRESH; | |
140 | |
141 gaia::OAuthClientInfo client_info; | |
142 client_info.client_id = GaiaUrls::GetInstance()->oauth2_chrome_client_id(); | |
143 client_info.client_secret = | |
144 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(); | |
145 | |
146 // Use the system request context to avoid sending user cookies. | |
Mattias Nissler (ping if slow)
2013/04/02 14:16:46
Is that actually accurate? The system request cont
David Roche
2013/04/04 01:39:53
I think it is, from my reading of the code. The i
Mattias Nissler (ping if slow)
2013/04/04 13:18:34
You're right, I didn't missed the "user" part in t
| |
147 gaia_oauth_client_.reset(new gaia::GaiaOAuthClient( | |
148 gaia::kGaiaOAuth2Url, | |
149 g_browser_process->system_request_context())); | |
150 gaia_oauth_client_->GetTokensFromAuthCode(client_info, | |
151 client->robot_api_auth_code(), | |
152 kRobotRefreshTokenFetchRetryCount, | |
153 this); | |
154 } | |
155 | |
156 // GaiaOAuthClient::Delegate callback for OAuth2 refresh token fetched. | |
157 void EnrollmentHandlerChromeOS::OnGetTokensResponse( | |
158 const std::string& refresh_token, | |
159 const std::string& access_token, | |
160 int expires_in_seconds) { | |
161 CHECK_EQ(STEP_ROBOT_AUTH_REFRESH, enrollment_step_); | |
162 | |
163 enrollment_step_ = STEP_POLICY_FETCH, | |
164 | |
165 // TODO: persist token in DeviceOAuth2TokenService when CL 12647008 lands. | |
166 | |
167 client_->FetchPolicy(); | |
168 } | |
169 | |
170 // GaiaOAuthClient::Delegate | |
171 void EnrollmentHandlerChromeOS::OnRefreshTokenResponse( | |
172 const std::string& access_token, | |
173 int expires_in_seconds) { | |
174 // We never use the code that should trigger this callback. | |
175 LOG(FATAL) << "Unexpected callback invoked"; | |
176 } | |
177 | |
178 // GaiaOAuthClient::Delegate OAuth2 error when fetching refresh token request. | |
179 void EnrollmentHandlerChromeOS::OnOAuthError() { | |
180 ReportResult(EnrollmentStatus::ForStatus( | |
181 EnrollmentStatus::STATUS_ROBOT_REFRESH_FETCH_FAILED)); | |
182 } | |
183 | |
184 // GaiaOAuthClient::Delegate network error when fetching refresh token. | |
185 void EnrollmentHandlerChromeOS::OnNetworkError(int response_code) { | |
186 LOG(ERROR) << "Network error while fetching API refresh token: " | |
187 << response_code; | |
188 ReportResult(EnrollmentStatus::ForStatus( | |
189 EnrollmentStatus::STATUS_ROBOT_REFRESH_FETCH_FAILED)); | |
190 } | |
191 | |
130 void EnrollmentHandlerChromeOS::OnClientError(CloudPolicyClient* client) { | 192 void EnrollmentHandlerChromeOS::OnClientError(CloudPolicyClient* client) { |
131 DCHECK_EQ(client_.get(), client); | 193 DCHECK_EQ(client_.get(), client); |
132 | 194 |
133 if (enrollment_step_ < STEP_POLICY_FETCH) | 195 if (enrollment_step_ == STEP_ROBOT_AUTH_FETCH) |
196 ReportResult(EnrollmentStatus::ForRobotAuthError(client_->status())); | |
197 else if (enrollment_step_ < STEP_POLICY_FETCH) | |
134 ReportResult(EnrollmentStatus::ForRegistrationError(client_->status())); | 198 ReportResult(EnrollmentStatus::ForRegistrationError(client_->status())); |
135 else | 199 else |
136 ReportResult(EnrollmentStatus::ForFetchError(client_->status())); | 200 ReportResult(EnrollmentStatus::ForFetchError(client_->status())); |
137 } | 201 } |
138 | 202 |
139 void EnrollmentHandlerChromeOS::OnStoreLoaded(CloudPolicyStore* store) { | 203 void EnrollmentHandlerChromeOS::OnStoreLoaded(CloudPolicyStore* store) { |
140 DCHECK_EQ(store_, store); | 204 DCHECK_EQ(store_, store); |
141 | 205 |
142 if (enrollment_step_ == STEP_LOADING_STORE) { | 206 if (enrollment_step_ == STEP_LOADING_STORE) { |
207 // If the |store_| wasn't initialized when StartEnrollment() was | |
208 // called, then AttemptRegistration() bails silently. This gets | |
209 // registration rolling again after the store finishes loading. | |
143 AttemptRegistration(); | 210 AttemptRegistration(); |
144 } else if (enrollment_step_ == STEP_STORE_POLICY) { | 211 } else if (enrollment_step_ == STEP_STORE_POLICY) { |
145 ReportResult(EnrollmentStatus::ForStatus(EnrollmentStatus::STATUS_SUCCESS)); | 212 ReportResult(EnrollmentStatus::ForStatus(EnrollmentStatus::STATUS_SUCCESS)); |
146 } | 213 } |
147 } | 214 } |
148 | 215 |
149 void EnrollmentHandlerChromeOS::OnStoreError(CloudPolicyStore* store) { | 216 void EnrollmentHandlerChromeOS::OnStoreError(CloudPolicyStore* store) { |
150 DCHECK_EQ(store_, store); | 217 DCHECK_EQ(store_, store); |
151 ReportResult(EnrollmentStatus::ForStoreError(store_->status(), | 218 ReportResult(EnrollmentStatus::ForStoreError(store_->status(), |
152 store_->validation_status())); | 219 store_->validation_status())); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
242 << " " << status.client_status() | 309 << " " << status.client_status() |
243 << " " << status.validation_status() | 310 << " " << status.validation_status() |
244 << " " << status.store_status(); | 311 << " " << status.store_status(); |
245 } | 312 } |
246 | 313 |
247 if (!callback.is_null()) | 314 if (!callback.is_null()) |
248 callback.Run(status); | 315 callback.Run(status); |
249 } | 316 } |
250 | 317 |
251 } // namespace policy | 318 } // namespace policy |
OLD | NEW |