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
3 // found in the LICENSE file.
5 #include "components/policy/core/common/cloud/cloud_policy_manager.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/logging.h"
12 #include "base/prefs/pref_service.h"
13 #include "components/policy/core/common/cloud/cloud_policy_service.h"
14 #include "components/policy/core/common/policy_bundle.h"
15 #include "components/policy/core/common/policy_map.h"
16 #include "components/policy/core/common/policy_switches.h"
17 #include "net/url_request/url_request_context_getter.h"
19 #if !defined(OS_ANDROID) && !defined(OS_IOS)
20 #include "components/policy/core/common/cloud/resource_cache.h"
25 CloudPolicyManager::CloudPolicyManager(
26 const std::string
& policy_type
,
27 const std::string
& settings_entity_id
,
28 CloudPolicyStore
* cloud_policy_store
,
29 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
,
30 const scoped_refptr
<base::SequencedTaskRunner
>& file_task_runner
,
31 const scoped_refptr
<base::SequencedTaskRunner
>& io_task_runner
)
32 : core_(policy_type
, settings_entity_id
, cloud_policy_store
, task_runner
),
33 waiting_for_policy_refresh_(false),
34 file_task_runner_(file_task_runner
),
35 io_task_runner_(io_task_runner
) {
36 store()->AddObserver(this);
38 // If the underlying store is already initialized, publish the loaded
39 // policy. Otherwise, request a load now.
40 if (store()->is_initialized())
41 CheckAndPublishPolicy();
46 CloudPolicyManager::~CloudPolicyManager() {}
48 void CloudPolicyManager::Shutdown() {
49 component_policy_service_
.reset();
51 store()->RemoveObserver(this);
52 ConfigurationPolicyProvider::Shutdown();
55 bool CloudPolicyManager::IsInitializationComplete(PolicyDomain domain
) const {
56 if (domain
== POLICY_DOMAIN_CHROME
)
57 return store()->is_initialized();
58 if (ComponentCloudPolicyService::SupportsDomain(domain
) &&
59 component_policy_service_
) {
60 return component_policy_service_
->is_initialized();
65 void CloudPolicyManager::RefreshPolicies() {
67 waiting_for_policy_refresh_
= true;
68 service()->RefreshPolicy(
69 base::Bind(&CloudPolicyManager::OnRefreshComplete
,
70 base::Unretained(this)));
72 OnRefreshComplete(false);
76 void CloudPolicyManager::OnStoreLoaded(CloudPolicyStore
* cloud_policy_store
) {
77 DCHECK_EQ(store(), cloud_policy_store
);
78 CheckAndPublishPolicy();
81 void CloudPolicyManager::OnStoreError(CloudPolicyStore
* cloud_policy_store
) {
82 DCHECK_EQ(store(), cloud_policy_store
);
83 // Publish policy (even though it hasn't changed) in order to signal load
84 // complete on the ConfigurationPolicyProvider interface. Technically, this
85 // is only required on the first load, but doesn't hurt in any case.
86 CheckAndPublishPolicy();
89 void CloudPolicyManager::OnComponentCloudPolicyUpdated() {
90 CheckAndPublishPolicy();
93 void CloudPolicyManager::CheckAndPublishPolicy() {
94 if (IsInitializationComplete(POLICY_DOMAIN_CHROME
) &&
95 !waiting_for_policy_refresh_
) {
96 scoped_ptr
<PolicyBundle
> bundle(new PolicyBundle
);
98 &bundle
->Get(PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string())));
99 if (component_policy_service_
)
100 bundle
->MergeFrom(component_policy_service_
->policy());
101 UpdatePolicy(bundle
.Pass());
105 void CloudPolicyManager::GetChromePolicy(PolicyMap
* policy_map
) {
106 policy_map
->CopyFrom(store()->policy_map());
109 void CloudPolicyManager::CreateComponentCloudPolicyService(
110 const base::FilePath
& policy_cache_path
,
111 const scoped_refptr
<net::URLRequestContextGetter
>& request_context
,
112 CloudPolicyClient
* client
) {
113 #if !defined(OS_ANDROID) && !defined(OS_IOS)
114 // Init() must have been called.
115 CHECK(schema_registry());
116 // Called at most once.
117 CHECK(!component_policy_service_
);
118 // The core can't be connected yet.
119 // See the comments on ComponentCloudPolicyService for the details.
120 CHECK(!core()->client());
122 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
123 switches::kDisableComponentCloudPolicy
) ||
124 policy_cache_path
.empty()) {
128 // TODO(joaodasilva): Move the |file_task_runner_| to the blocking pool.
129 // Currently it's not possible because the ComponentCloudPolicyStore is
130 // NonThreadSafe and doesn't support getting calls from different threads.
131 scoped_ptr
<ResourceCache
> resource_cache(
132 new ResourceCache(policy_cache_path
, file_task_runner_
));
133 component_policy_service_
.reset(new ComponentCloudPolicyService(
138 resource_cache
.Pass(),
142 #endif // !defined(OS_ANDROID) && !defined(OS_IOS)
145 void CloudPolicyManager::ClearAndDestroyComponentCloudPolicyService() {
146 #if !defined(OS_ANDROID) && !defined(OS_IOS)
147 if (component_policy_service_
) {
148 component_policy_service_
->ClearCache();
149 component_policy_service_
.reset();
151 #endif // !defined(OS_ANDROID) && !defined(OS_IOS)
154 void CloudPolicyManager::OnRefreshComplete(bool success
) {
155 waiting_for_policy_refresh_
= false;
156 CheckAndPublishPolicy();
159 } // namespace policy