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 "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/command_line.h" | |
| 10 #include "base/logging.h" | 9 #include "base/logging.h" |
| 11 #include "base/message_loop/message_loop_proxy.h" | |
| 12 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 13 #include "base/metrics/sparse_histogram.h" | 11 #include "base/metrics/sparse_histogram.h" |
| 14 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
| 15 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.h" | 14 #include "chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.h" |
| 17 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chrom eos.h" | 15 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chrom eos.h" |
| 18 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 16 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 19 #include "chrome/browser/policy/cloud/cloud_external_data_manager.h" | 17 #include "chrome/browser/policy/cloud/cloud_external_data_manager.h" |
| 20 #include "chrome/browser/policy/cloud/cloud_policy_refresh_scheduler.h" | 18 #include "chrome/browser/policy/cloud/cloud_policy_refresh_scheduler.h" |
| 21 #include "chrome/browser/policy/cloud/resource_cache.h" | 19 #include "chrome/browser/policy/cloud/resource_cache.h" |
|
bartfab (slow)
2013/11/25 15:05:50
Nit: No longeer used.
Joao da Silva
2013/11/25 16:44:11
Done.
| |
| 22 #include "components/policy/core/common/policy_bundle.h" | 20 #include "components/policy/core/common/policy_bundle.h" |
|
bartfab (slow)
2013/11/25 15:05:50
Nit: No longer used.
Joao da Silva
2013/11/25 16:44:11
Done.
| |
| 23 #include "components/policy/core/common/policy_pref_names.h" | 21 #include "components/policy/core/common/policy_pref_names.h" |
| 24 #include "components/policy/core/common/policy_switches.h" | |
| 25 #include "content/public/browser/browser_thread.h" | |
| 26 #include "net/url_request/url_request_context_getter.h" | 22 #include "net/url_request/url_request_context_getter.h" |
| 27 | 23 |
| 28 namespace em = enterprise_management; | 24 namespace em = enterprise_management; |
| 29 | 25 |
| 30 namespace policy { | 26 namespace policy { |
| 31 | 27 |
| 32 namespace { | 28 namespace { |
| 33 | 29 |
| 34 // UMA histogram names. | 30 // UMA histogram names. |
| 35 const char kUMADelayInitialization[] = | 31 const char kUMADelayInitialization[] = |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 47 const char kUMAInitialFetchOAuth2Error[] = | 43 const char kUMAInitialFetchOAuth2Error[] = |
| 48 "Enterprise.UserPolicyChromeOS.InitialFetch.OAuth2Error"; | 44 "Enterprise.UserPolicyChromeOS.InitialFetch.OAuth2Error"; |
| 49 const char kUMAInitialFetchOAuth2NetworkError[] = | 45 const char kUMAInitialFetchOAuth2NetworkError[] = |
| 50 "Enterprise.UserPolicyChromeOS.InitialFetch.OAuth2NetworkError"; | 46 "Enterprise.UserPolicyChromeOS.InitialFetch.OAuth2NetworkError"; |
| 51 | 47 |
| 52 } // namespace | 48 } // namespace |
| 53 | 49 |
| 54 UserCloudPolicyManagerChromeOS::UserCloudPolicyManagerChromeOS( | 50 UserCloudPolicyManagerChromeOS::UserCloudPolicyManagerChromeOS( |
| 55 scoped_ptr<CloudPolicyStore> store, | 51 scoped_ptr<CloudPolicyStore> store, |
| 56 scoped_ptr<CloudExternalDataManager> external_data_manager, | 52 scoped_ptr<CloudExternalDataManager> external_data_manager, |
| 53 const base::FilePath& component_policy_cache_path, | |
| 54 bool wait_for_policy_fetch, | |
| 55 base::TimeDelta initial_policy_fetch_timeout, | |
| 57 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | 56 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| 58 const base::FilePath& resource_cache_dir, | 57 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner, |
| 59 bool wait_for_policy_fetch, | 58 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) |
| 60 base::TimeDelta initial_policy_fetch_timeout) | |
| 61 : CloudPolicyManager( | 59 : CloudPolicyManager( |
| 62 PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType, std::string()), | 60 PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType, std::string()), |
| 63 store.get(), | 61 store.get(), |
| 64 task_runner), | 62 task_runner, |
| 63 file_task_runner, | |
| 64 io_task_runner), | |
| 65 store_(store.Pass()), | 65 store_(store.Pass()), |
| 66 external_data_manager_(external_data_manager.Pass()), | 66 external_data_manager_(external_data_manager.Pass()), |
| 67 resource_cache_dir_(resource_cache_dir), | 67 component_policy_cache_path_(component_policy_cache_path), |
| 68 wait_for_policy_fetch_(wait_for_policy_fetch), | 68 wait_for_policy_fetch_(wait_for_policy_fetch), |
| 69 policy_fetch_timeout_(false, false) { | 69 policy_fetch_timeout_(false, false) { |
| 70 time_init_started_ = base::Time::Now(); | 70 time_init_started_ = base::Time::Now(); |
| 71 if (wait_for_policy_fetch_) { | 71 if (wait_for_policy_fetch_) { |
| 72 policy_fetch_timeout_.Start( | 72 policy_fetch_timeout_.Start( |
| 73 FROM_HERE, | 73 FROM_HERE, |
| 74 initial_policy_fetch_timeout, | 74 initial_policy_fetch_timeout, |
| 75 base::Bind(&UserCloudPolicyManagerChromeOS::CancelWaitForPolicyFetch, | 75 base::Bind(&UserCloudPolicyManagerChromeOS::CancelWaitForPolicyFetch, |
| 76 base::Unretained(this))); | 76 base::Unretained(this))); |
| 77 } | 77 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 88 DCHECK(local_state); | 88 DCHECK(local_state); |
| 89 local_state_ = local_state; | 89 local_state_ = local_state; |
| 90 scoped_ptr<CloudPolicyClient> cloud_policy_client( | 90 scoped_ptr<CloudPolicyClient> cloud_policy_client( |
| 91 new CloudPolicyClient(std::string(), std::string(), user_affiliation, | 91 new CloudPolicyClient(std::string(), std::string(), user_affiliation, |
| 92 NULL, device_management_service)); | 92 NULL, device_management_service)); |
| 93 core()->Connect(cloud_policy_client.Pass()); | 93 core()->Connect(cloud_policy_client.Pass()); |
| 94 client()->AddObserver(this); | 94 client()->AddObserver(this); |
| 95 | 95 |
| 96 external_data_manager_->Connect(request_context); | 96 external_data_manager_->Connect(request_context); |
| 97 | 97 |
| 98 CreateComponentCloudPolicyService(request_context); | 98 CreateComponentCloudPolicyService(component_policy_cache_path_, |
| 99 request_context); | |
| 99 | 100 |
| 100 // Determine the next step after the CloudPolicyService initializes. | 101 // Determine the next step after the CloudPolicyService initializes. |
| 101 if (service()->IsInitializationComplete()) { | 102 if (service()->IsInitializationComplete()) { |
| 102 OnInitializationCompleted(service()); | 103 OnInitializationCompleted(service()); |
| 103 } else { | 104 } else { |
| 104 service()->AddObserver(this); | 105 service()->AddObserver(this); |
| 105 } | 106 } |
| 106 } | 107 } |
| 107 | 108 |
| 108 void UserCloudPolicyManagerChromeOS::OnAccessTokenAvailable( | 109 void UserCloudPolicyManagerChromeOS::OnAccessTokenAvailable( |
| 109 const std::string& access_token) { | 110 const std::string& access_token) { |
| 110 access_token_ = access_token; | 111 access_token_ = access_token; |
| 111 if (service() && service()->IsInitializationComplete() && | 112 if (service() && service()->IsInitializationComplete() && |
| 112 client() && !client()->is_registered()) { | 113 client() && !client()->is_registered()) { |
| 113 OnOAuth2PolicyTokenFetched( | 114 OnOAuth2PolicyTokenFetched( |
| 114 access_token, GoogleServiceAuthError(GoogleServiceAuthError::NONE)); | 115 access_token, GoogleServiceAuthError(GoogleServiceAuthError::NONE)); |
| 115 } | 116 } |
| 116 } | 117 } |
| 117 | 118 |
| 118 bool UserCloudPolicyManagerChromeOS::IsClientRegistered() const { | 119 bool UserCloudPolicyManagerChromeOS::IsClientRegistered() const { |
| 119 return client() && client()->is_registered(); | 120 return client() && client()->is_registered(); |
| 120 } | 121 } |
| 121 | 122 |
| 122 void UserCloudPolicyManagerChromeOS::Shutdown() { | 123 void UserCloudPolicyManagerChromeOS::Shutdown() { |
| 123 if (client()) | 124 if (client()) |
| 124 client()->RemoveObserver(this); | 125 client()->RemoveObserver(this); |
| 125 if (service()) | 126 if (service()) |
| 126 service()->RemoveObserver(this); | 127 service()->RemoveObserver(this); |
| 127 token_fetcher_.reset(); | 128 token_fetcher_.reset(); |
| 128 component_policy_service_.reset(); | |
| 129 external_data_manager_->Disconnect(); | 129 external_data_manager_->Disconnect(); |
| 130 CloudPolicyManager::Shutdown(); | 130 CloudPolicyManager::Shutdown(); |
| 131 } | 131 } |
| 132 | 132 |
| 133 bool UserCloudPolicyManagerChromeOS::IsInitializationComplete( | 133 bool UserCloudPolicyManagerChromeOS::IsInitializationComplete( |
| 134 PolicyDomain domain) const { | 134 PolicyDomain domain) const { |
| 135 if (!CloudPolicyManager::IsInitializationComplete(domain)) | 135 if (!CloudPolicyManager::IsInitializationComplete(domain)) |
| 136 return false; | 136 return false; |
| 137 if (domain == POLICY_DOMAIN_CHROME) | 137 if (domain == POLICY_DOMAIN_CHROME) |
| 138 return !wait_for_policy_fetch_; | 138 return !wait_for_policy_fetch_; |
| 139 if (ComponentCloudPolicyService::SupportsDomain(domain) && | |
| 140 component_policy_service_) { | |
| 141 return component_policy_service_->is_initialized(); | |
| 142 } | |
| 143 return true; | 139 return true; |
| 144 } | 140 } |
| 145 | 141 |
| 146 scoped_ptr<PolicyBundle> UserCloudPolicyManagerChromeOS::CreatePolicyBundle() { | |
| 147 scoped_ptr<PolicyBundle> bundle = CloudPolicyManager::CreatePolicyBundle(); | |
| 148 if (component_policy_service_) | |
| 149 bundle->MergeFrom(component_policy_service_->policy()); | |
| 150 return bundle.Pass(); | |
| 151 } | |
| 152 | |
| 153 void UserCloudPolicyManagerChromeOS::OnInitializationCompleted( | 142 void UserCloudPolicyManagerChromeOS::OnInitializationCompleted( |
| 154 CloudPolicyService* cloud_policy_service) { | 143 CloudPolicyService* cloud_policy_service) { |
| 155 DCHECK_EQ(service(), cloud_policy_service); | 144 DCHECK_EQ(service(), cloud_policy_service); |
| 156 cloud_policy_service->RemoveObserver(this); | 145 cloud_policy_service->RemoveObserver(this); |
| 157 | 146 |
| 158 time_init_completed_ = base::Time::Now(); | 147 time_init_completed_ = base::Time::Now(); |
| 159 UMA_HISTOGRAM_TIMES(kUMADelayInitialization, | 148 UMA_HISTOGRAM_TIMES(kUMADelayInitialization, |
| 160 time_init_completed_ - time_init_started_); | 149 time_init_completed_ - time_init_started_); |
| 161 | 150 |
| 162 // If the CloudPolicyClient isn't registered at this stage then it needs an | 151 // If the CloudPolicyClient isn't registered at this stage then it needs an |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 226 CloudPolicyClient* cloud_policy_client) { | 215 CloudPolicyClient* cloud_policy_client) { |
| 227 DCHECK_EQ(client(), cloud_policy_client); | 216 DCHECK_EQ(client(), cloud_policy_client); |
| 228 if (wait_for_policy_fetch_) { | 217 if (wait_for_policy_fetch_) { |
| 229 UMA_HISTOGRAM_SPARSE_SLOWLY(kUMAInitialFetchClientError, | 218 UMA_HISTOGRAM_SPARSE_SLOWLY(kUMAInitialFetchClientError, |
| 230 cloud_policy_client->status()); | 219 cloud_policy_client->status()); |
| 231 } | 220 } |
| 232 CancelWaitForPolicyFetch(); | 221 CancelWaitForPolicyFetch(); |
| 233 } | 222 } |
| 234 | 223 |
| 235 void UserCloudPolicyManagerChromeOS::OnComponentCloudPolicyUpdated() { | 224 void UserCloudPolicyManagerChromeOS::OnComponentCloudPolicyUpdated() { |
| 236 CheckAndPublishPolicy(); | 225 CloudPolicyManager::OnComponentCloudPolicyUpdated(); |
| 237 StartRefreshSchedulerIfReady(); | 226 StartRefreshSchedulerIfReady(); |
| 238 } | 227 } |
| 239 | 228 |
| 240 void UserCloudPolicyManagerChromeOS::CreateComponentCloudPolicyService( | |
| 241 const scoped_refptr<net::URLRequestContextGetter>& request_context) { | |
| 242 // Init() must have been called. | |
| 243 DCHECK(schema_registry()); | |
| 244 // Called at most once. | |
| 245 DCHECK(!component_policy_service_); | |
| 246 | |
| 247 if (!CommandLine::ForCurrentProcess()->HasSwitch( | |
| 248 switches::kEnableComponentCloudPolicy)) { | |
| 249 return; | |
| 250 } | |
| 251 | |
| 252 // TODO(joaodasilva): Move the |backend_task_runner| to the blocking pool. | |
| 253 // Currently it's not possible because the ComponentCloudPolicyStore is | |
| 254 // NonThreadSafe and doesn't support getting calls from different threads. | |
| 255 scoped_refptr<base::SequencedTaskRunner> backend_task_runner = | |
| 256 content::BrowserThread::GetMessageLoopProxyForThread( | |
| 257 content::BrowserThread::FILE); | |
| 258 scoped_ptr<ResourceCache> resource_cache( | |
| 259 new ResourceCache(resource_cache_dir_, backend_task_runner)); | |
| 260 component_policy_service_.reset(new ComponentCloudPolicyService( | |
| 261 this, | |
| 262 schema_registry(), | |
| 263 core(), | |
| 264 resource_cache.Pass(), | |
| 265 request_context, | |
| 266 backend_task_runner, | |
| 267 content::BrowserThread::GetMessageLoopProxyForThread( | |
| 268 content::BrowserThread::IO))); | |
| 269 } | |
| 270 | |
| 271 void UserCloudPolicyManagerChromeOS::FetchPolicyOAuthTokenUsingSigninProfile() { | 229 void UserCloudPolicyManagerChromeOS::FetchPolicyOAuthTokenUsingSigninProfile() { |
| 272 scoped_refptr<net::URLRequestContextGetter> signin_context; | 230 scoped_refptr<net::URLRequestContextGetter> signin_context; |
| 273 Profile* signin_profile = chromeos::ProfileHelper::GetSigninProfile(); | 231 Profile* signin_profile = chromeos::ProfileHelper::GetSigninProfile(); |
| 274 if (signin_profile) | 232 if (signin_profile) |
| 275 signin_context = signin_profile->GetRequestContext(); | 233 signin_context = signin_profile->GetRequestContext(); |
| 276 if (!signin_context.get()) { | 234 if (!signin_context.get()) { |
| 277 LOG(ERROR) << "No signin Profile for policy oauth token fetch!"; | 235 LOG(ERROR) << "No signin Profile for policy oauth token fetch!"; |
| 278 OnOAuth2PolicyTokenFetched( | 236 OnOAuth2PolicyTokenFetched( |
| 279 std::string(), GoogleServiceAuthError(GoogleServiceAuthError::NONE)); | 237 std::string(), GoogleServiceAuthError(GoogleServiceAuthError::NONE)); |
| 280 return; | 238 return; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 void UserCloudPolicyManagerChromeOS::StartRefreshSchedulerIfReady() { | 301 void UserCloudPolicyManagerChromeOS::StartRefreshSchedulerIfReady() { |
| 344 if (core()->refresh_scheduler()) | 302 if (core()->refresh_scheduler()) |
| 345 return; // Already started. | 303 return; // Already started. |
| 346 | 304 |
| 347 if (wait_for_policy_fetch_) | 305 if (wait_for_policy_fetch_) |
| 348 return; // Still waiting for the initial, blocking fetch. | 306 return; // Still waiting for the initial, blocking fetch. |
| 349 | 307 |
| 350 if (!service() || !local_state_) | 308 if (!service() || !local_state_) |
| 351 return; // Not connected. | 309 return; // Not connected. |
| 352 | 310 |
| 353 if (component_policy_service_ && | 311 if (component_policy_service() && |
| 354 !component_policy_service_->is_initialized()) { | 312 !component_policy_service()->is_initialized()) { |
| 355 // If the client doesn't have the list of components to fetch yet then don't | 313 // If the client doesn't have the list of components to fetch yet then don't |
| 356 // start the scheduler. The |component_policy_service_| will call back into | 314 // start the scheduler. The |component_policy_service_| will call back into |
| 357 // OnComponentCloudPolicyUpdated() once it's ready. | 315 // OnComponentCloudPolicyUpdated() once it's ready. |
| 358 return; | 316 return; |
| 359 } | 317 } |
| 360 | 318 |
| 361 core()->StartRefreshScheduler(); | 319 core()->StartRefreshScheduler(); |
| 362 core()->TrackRefreshDelayPref(local_state_, | 320 core()->TrackRefreshDelayPref(local_state_, |
| 363 policy_prefs::kUserPolicyRefreshRate); | 321 policy_prefs::kUserPolicyRefreshRate); |
| 364 } | 322 } |
| 365 | 323 |
| 366 } // namespace policy | 324 } // namespace policy |
| OLD | NEW |