| 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/user_cloud_policy_manager_chromeos.h" | 5 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/metrics/histogram.h" |
| 11 #include "base/metrics/sparse_histogram.h" |
| 10 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.h" | 13 #include "chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.h" |
| 12 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chrom
eos.h" | 14 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chrom
eos.h" |
| 13 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 15 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 14 #include "chrome/browser/policy/cloud/cloud_policy_refresh_scheduler.h" | 16 #include "chrome/browser/policy/cloud/cloud_policy_refresh_scheduler.h" |
| 15 #include "chrome/browser/policy/cloud/resource_cache.h" | 17 #include "chrome/browser/policy/cloud/resource_cache.h" |
| 16 #include "chrome/browser/policy/policy_bundle.h" | 18 #include "chrome/browser/policy/policy_bundle.h" |
| 17 #include "chrome/browser/policy/policy_domain_descriptor.h" | 19 #include "chrome/browser/policy/policy_domain_descriptor.h" |
| 18 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 19 #include "net/url_request/url_request_context_getter.h" | 21 #include "net/url_request/url_request_context_getter.h" |
| 20 | 22 |
| 21 namespace em = enterprise_management; | 23 namespace em = enterprise_management; |
| 22 | 24 |
| 23 namespace policy { | 25 namespace policy { |
| 24 | 26 |
| 27 namespace { |
| 28 |
| 29 // UMA histogram names. |
| 30 const char kUMADelayInitialization[] = |
| 31 "Enterprise.UserPolicyChromeOS.DelayInitialization"; |
| 32 const char kUMAInitialFetchClientError[] = |
| 33 "Enterprise.UserPolicyChromeOS.InitialFetch.ClientError"; |
| 34 const char kUMAInitialFetchDelayClientRegister[] = |
| 35 "Enterprise.UserPolicyChromeOS.InitialFetch.DelayClientRegister"; |
| 36 const char kUMAInitialFetchDelayOAuth2Token[] = |
| 37 "Enterprise.UserPolicyChromeOS.InitialFetch.DelayOAuth2Token"; |
| 38 const char kUMAInitialFetchDelayPolicyFetch[] = |
| 39 "Enterprise.UserPolicyChromeOS.InitialFetch.DelayPolicyFetch"; |
| 40 const char kUMAInitialFetchDelayTotal[] = |
| 41 "Enterprise.UserPolicyChromeOS.InitialFetch.DelayTotal"; |
| 42 const char kUMAInitialFetchOAuth2Error[] = |
| 43 "Enterprise.UserPolicyChromeOS.InitialFetch.OAuth2Error"; |
| 44 const char kUMAInitialFetchOAuth2NetworkError[] = |
| 45 "Enterprise.UserPolicyChromeOS.InitialFetch.OAuth2NetworkError"; |
| 46 |
| 47 } // namespace |
| 48 |
| 25 UserCloudPolicyManagerChromeOS::UserCloudPolicyManagerChromeOS( | 49 UserCloudPolicyManagerChromeOS::UserCloudPolicyManagerChromeOS( |
| 26 scoped_ptr<CloudPolicyStore> store, | 50 scoped_ptr<CloudPolicyStore> store, |
| 27 scoped_ptr<ResourceCache> resource_cache, | 51 scoped_ptr<ResourceCache> resource_cache, |
| 28 bool wait_for_policy_fetch) | 52 bool wait_for_policy_fetch) |
| 29 : CloudPolicyManager( | 53 : CloudPolicyManager( |
| 30 PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType, std::string()), | 54 PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType, std::string()), |
| 31 store.get()), | 55 store.get()), |
| 32 store_(store.Pass()), | 56 store_(store.Pass()), |
| 33 wait_for_policy_fetch_(wait_for_policy_fetch) { | 57 wait_for_policy_fetch_(wait_for_policy_fetch) { |
| 58 time_init_started_ = base::Time::Now(); |
| 34 if (resource_cache) { | 59 if (resource_cache) { |
| 35 component_policy_service_.reset(new ComponentCloudPolicyService( | 60 component_policy_service_.reset(new ComponentCloudPolicyService( |
| 36 this, store_.get(), resource_cache.Pass())); | 61 this, store_.get(), resource_cache.Pass())); |
| 37 } | 62 } |
| 38 } | 63 } |
| 39 | 64 |
| 40 UserCloudPolicyManagerChromeOS::~UserCloudPolicyManagerChromeOS() {} | 65 UserCloudPolicyManagerChromeOS::~UserCloudPolicyManagerChromeOS() {} |
| 41 | 66 |
| 42 void UserCloudPolicyManagerChromeOS::Connect( | 67 void UserCloudPolicyManagerChromeOS::Connect( |
| 43 PrefService* local_state, | 68 PrefService* local_state, |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 scoped_ptr<PolicyBundle> bundle = CloudPolicyManager::CreatePolicyBundle(); | 138 scoped_ptr<PolicyBundle> bundle = CloudPolicyManager::CreatePolicyBundle(); |
| 114 if (component_policy_service_) | 139 if (component_policy_service_) |
| 115 bundle->MergeFrom(component_policy_service_->policy()); | 140 bundle->MergeFrom(component_policy_service_->policy()); |
| 116 return bundle.Pass(); | 141 return bundle.Pass(); |
| 117 } | 142 } |
| 118 | 143 |
| 119 void UserCloudPolicyManagerChromeOS::OnInitializationCompleted( | 144 void UserCloudPolicyManagerChromeOS::OnInitializationCompleted( |
| 120 CloudPolicyService* cloud_policy_service) { | 145 CloudPolicyService* cloud_policy_service) { |
| 121 DCHECK_EQ(service(), cloud_policy_service); | 146 DCHECK_EQ(service(), cloud_policy_service); |
| 122 cloud_policy_service->RemoveObserver(this); | 147 cloud_policy_service->RemoveObserver(this); |
| 148 |
| 149 time_init_completed_ = base::Time::Now(); |
| 150 UMA_HISTOGRAM_TIMES(kUMADelayInitialization, |
| 151 time_init_completed_ - time_init_started_); |
| 152 |
| 123 // If the CloudPolicyClient isn't registered at this stage then it needs an | 153 // If the CloudPolicyClient isn't registered at this stage then it needs an |
| 124 // OAuth token for the initial registration. | 154 // OAuth token for the initial registration. |
| 125 // | 155 // |
| 126 // If |wait_for_policy_fetch_| is true then Profile initialization is blocking | 156 // If |wait_for_policy_fetch_| is true then Profile initialization is blocking |
| 127 // on the initial policy fetch, so the token must be fetched immediately. | 157 // on the initial policy fetch, so the token must be fetched immediately. |
| 128 // In that case, the signin Profile is used to authenticate a Gaia request to | 158 // In that case, the signin Profile is used to authenticate a Gaia request to |
| 129 // fetch a refresh token, and then the policy token is fetched. | 159 // fetch a refresh token, and then the policy token is fetched. |
| 130 // | 160 // |
| 131 // If |wait_for_policy_fetch_| is false then the UserCloudPolicyTokenForwarder | 161 // If |wait_for_policy_fetch_| is false then the UserCloudPolicyTokenForwarder |
| 132 // service will eventually call OnAccessTokenAvailable() once an access token | 162 // service will eventually call OnAccessTokenAvailable() once an access token |
| (...skipping 21 matching lines...) Expand all Loading... |
| 154 | 184 |
| 155 void UserCloudPolicyManagerChromeOS::OnPolicyFetched( | 185 void UserCloudPolicyManagerChromeOS::OnPolicyFetched( |
| 156 CloudPolicyClient* client) { | 186 CloudPolicyClient* client) { |
| 157 // No action required. If we're blocked on a policy fetch, we'll learn about | 187 // No action required. If we're blocked on a policy fetch, we'll learn about |
| 158 // completion of it through OnInitialPolicyFetchComplete(). | 188 // completion of it through OnInitialPolicyFetchComplete(). |
| 159 } | 189 } |
| 160 | 190 |
| 161 void UserCloudPolicyManagerChromeOS::OnRegistrationStateChanged( | 191 void UserCloudPolicyManagerChromeOS::OnRegistrationStateChanged( |
| 162 CloudPolicyClient* cloud_policy_client) { | 192 CloudPolicyClient* cloud_policy_client) { |
| 163 DCHECK_EQ(client(), cloud_policy_client); | 193 DCHECK_EQ(client(), cloud_policy_client); |
| 194 |
| 164 if (wait_for_policy_fetch_) { | 195 if (wait_for_policy_fetch_) { |
| 196 time_client_registered_ = base::Time::Now(); |
| 197 if (!time_token_available_.is_null()) { |
| 198 UMA_HISTOGRAM_TIMES(kUMAInitialFetchDelayClientRegister, |
| 199 time_client_registered_ - time_token_available_); |
| 200 } |
| 201 |
| 165 // If we're blocked on the policy fetch, now is a good time to issue it. | 202 // If we're blocked on the policy fetch, now is a good time to issue it. |
| 166 if (client()->is_registered()) { | 203 if (client()->is_registered()) { |
| 167 service()->RefreshPolicy( | 204 service()->RefreshPolicy( |
| 168 base::Bind( | 205 base::Bind( |
| 169 &UserCloudPolicyManagerChromeOS::OnInitialPolicyFetchComplete, | 206 &UserCloudPolicyManagerChromeOS::OnInitialPolicyFetchComplete, |
| 170 base::Unretained(this))); | 207 base::Unretained(this))); |
| 171 } else { | 208 } else { |
| 172 // If the client has switched to not registered, we bail out as this | 209 // If the client has switched to not registered, we bail out as this |
| 173 // indicates the cloud policy setup flow has been aborted. | 210 // indicates the cloud policy setup flow has been aborted. |
| 174 CancelWaitForPolicyFetch(); | 211 CancelWaitForPolicyFetch(); |
| 175 } | 212 } |
| 176 } | 213 } |
| 177 } | 214 } |
| 178 | 215 |
| 179 void UserCloudPolicyManagerChromeOS::OnClientError( | 216 void UserCloudPolicyManagerChromeOS::OnClientError( |
| 180 CloudPolicyClient* cloud_policy_client) { | 217 CloudPolicyClient* cloud_policy_client) { |
| 181 DCHECK_EQ(client(), cloud_policy_client); | 218 DCHECK_EQ(client(), cloud_policy_client); |
| 182 CancelWaitForPolicyFetch(); | 219 CancelWaitForPolicyFetch(); |
| 220 |
| 221 if (wait_for_policy_fetch_) { |
| 222 UMA_HISTOGRAM_SPARSE_SLOWLY(kUMAInitialFetchClientError, |
| 223 cloud_policy_client->status()); |
| 224 } |
| 183 } | 225 } |
| 184 | 226 |
| 185 void UserCloudPolicyManagerChromeOS::OnComponentCloudPolicyRefreshNeeded() { | 227 void UserCloudPolicyManagerChromeOS::OnComponentCloudPolicyRefreshNeeded() { |
| 186 core()->RefreshSoon(); | 228 core()->RefreshSoon(); |
| 187 } | 229 } |
| 188 | 230 |
| 189 void UserCloudPolicyManagerChromeOS::OnComponentCloudPolicyUpdated() { | 231 void UserCloudPolicyManagerChromeOS::OnComponentCloudPolicyUpdated() { |
| 190 CheckAndPublishPolicy(); | 232 CheckAndPublishPolicy(); |
| 191 StartRefreshSchedulerIfReady(); | 233 StartRefreshSchedulerIfReady(); |
| 192 } | 234 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 208 g_browser_process->system_request_context(), | 250 g_browser_process->system_request_context(), |
| 209 base::Bind(&UserCloudPolicyManagerChromeOS::OnOAuth2PolicyTokenFetched, | 251 base::Bind(&UserCloudPolicyManagerChromeOS::OnOAuth2PolicyTokenFetched, |
| 210 base::Unretained(this)))); | 252 base::Unretained(this)))); |
| 211 token_fetcher_->Start(); | 253 token_fetcher_->Start(); |
| 212 } | 254 } |
| 213 | 255 |
| 214 void UserCloudPolicyManagerChromeOS::OnOAuth2PolicyTokenFetched( | 256 void UserCloudPolicyManagerChromeOS::OnOAuth2PolicyTokenFetched( |
| 215 const std::string& policy_token, | 257 const std::string& policy_token, |
| 216 const GoogleServiceAuthError& error) { | 258 const GoogleServiceAuthError& error) { |
| 217 DCHECK(!client()->is_registered()); | 259 DCHECK(!client()->is_registered()); |
| 260 |
| 261 time_token_available_ = base::Time::Now(); |
| 262 if (wait_for_policy_fetch_) { |
| 263 UMA_HISTOGRAM_TIMES(kUMAInitialFetchDelayOAuth2Token, |
| 264 time_token_available_ - time_init_completed_); |
| 265 } |
| 266 |
| 218 if (error.state() == GoogleServiceAuthError::NONE) { | 267 if (error.state() == GoogleServiceAuthError::NONE) { |
| 219 // Start client registration. Either OnRegistrationStateChanged() or | 268 // Start client registration. Either OnRegistrationStateChanged() or |
| 220 // OnClientError() will be called back. | 269 // OnClientError() will be called back. |
| 221 client()->Register(em::DeviceRegisterRequest::USER, | 270 client()->Register(em::DeviceRegisterRequest::USER, |
| 222 policy_token, std::string(), false, std::string()); | 271 policy_token, std::string(), false, std::string()); |
| 223 } else { | 272 } else { |
| 224 // Failed to get a token, stop waiting and use an empty policy. | 273 // Failed to get a token, stop waiting and use an empty policy. |
| 225 CancelWaitForPolicyFetch(); | 274 CancelWaitForPolicyFetch(); |
| 275 |
| 276 UMA_HISTOGRAM_ENUMERATION(kUMAInitialFetchOAuth2Error, |
| 277 error.state(), |
| 278 GoogleServiceAuthError::NUM_STATES); |
| 279 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED) { |
| 280 UMA_HISTOGRAM_SPARSE_SLOWLY(kUMAInitialFetchOAuth2NetworkError, |
| 281 error.network_error()); |
| 282 } |
| 226 } | 283 } |
| 227 | 284 |
| 228 token_fetcher_.reset(); | 285 token_fetcher_.reset(); |
| 229 } | 286 } |
| 230 | 287 |
| 231 void UserCloudPolicyManagerChromeOS::OnInitialPolicyFetchComplete( | 288 void UserCloudPolicyManagerChromeOS::OnInitialPolicyFetchComplete( |
| 232 bool success) { | 289 bool success) { |
| 290 |
| 291 const base::Time now = base::Time::Now(); |
| 292 UMA_HISTOGRAM_TIMES(kUMAInitialFetchDelayPolicyFetch, |
| 293 now - time_client_registered_); |
| 294 UMA_HISTOGRAM_TIMES(kUMAInitialFetchDelayTotal, now - time_init_started_); |
| 233 CancelWaitForPolicyFetch(); | 295 CancelWaitForPolicyFetch(); |
| 234 } | 296 } |
| 235 | 297 |
| 236 void UserCloudPolicyManagerChromeOS::CancelWaitForPolicyFetch() { | 298 void UserCloudPolicyManagerChromeOS::CancelWaitForPolicyFetch() { |
| 237 wait_for_policy_fetch_ = false; | 299 wait_for_policy_fetch_ = false; |
| 238 CheckAndPublishPolicy(); | 300 CheckAndPublishPolicy(); |
| 239 // Now that |wait_for_policy_fetch_| is guaranteed to be false, the scheduler | 301 // Now that |wait_for_policy_fetch_| is guaranteed to be false, the scheduler |
| 240 // can be started. | 302 // can be started. |
| 241 StartRefreshSchedulerIfReady(); | 303 StartRefreshSchedulerIfReady(); |
| 242 } | 304 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 257 // start the scheduler. The |component_policy_service_| will call back into | 319 // start the scheduler. The |component_policy_service_| will call back into |
| 258 // OnComponentCloudPolicyUpdated() once it's ready. | 320 // OnComponentCloudPolicyUpdated() once it's ready. |
| 259 return; | 321 return; |
| 260 } | 322 } |
| 261 | 323 |
| 262 StartRefreshScheduler(); | 324 StartRefreshScheduler(); |
| 263 core()->TrackRefreshDelayPref(local_state_, prefs::kUserPolicyRefreshRate); | 325 core()->TrackRefreshDelayPref(local_state_, prefs::kUserPolicyRefreshRate); |
| 264 } | 326 } |
| 265 | 327 |
| 266 } // namespace policy | 328 } // namespace policy |
| OLD | NEW |