1 // Copyright (c) 2013 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 #ifndef CHROME_BROWSER_POLICY_CLOUD_COMPONENT_CLOUD_POLICY_SERVICE_H_
6 #define CHROME_BROWSER_POLICY_CLOUD_COMPONENT_CLOUD_POLICY_SERVICE_H_
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/threading/non_thread_safe.h"
18 #include "chrome/browser/policy/cloud/cloud_policy_client.h"
19 #include "chrome/browser/policy/cloud/cloud_policy_store.h"
20 #include "chrome/browser/policy/policy_bundle.h"
21 #include "components/policy/core/common/policy_namespace.h"
24 class SequencedTaskRunner;
28 class URLRequestContextGetter;
33 class ExternalPolicyDataFetcherBackend;
34 class PolicyDomainDescriptor;
37 // Manages cloud policy for components.
39 // This class takes care of fetching, validating, storing and updating policy
40 // for components. The components to manage have to be explicitly registered.
41 class ComponentCloudPolicyService : public CloudPolicyClient::Observer,
42 public CloudPolicyStore::Observer,
43 public base::NonThreadSafe {
45 // Key for the ResourceCache where the list of known components is cached.
46 static const char kComponentNamespaceCache[];
52 // Invoked whenever the service has appended new namespaces to fetch to
53 // the CloudPolicyClient, signaling that a policy fetch should be done soon.
54 virtual void OnComponentCloudPolicyRefreshNeeded() = 0;
56 // Invoked whenever the policy served by policy() changes. This is also
57 // invoked for the first time once the backend is initialized, and
58 // is_initialized() becomes true.
59 virtual void OnComponentCloudPolicyUpdated() = 0;
62 // |store| is used to get the current DMToken and the username.
63 // |cache| is used to load and store local copies of the downloaded policies.
64 // Download scheduling, validation and caching of policies are done via the
65 // |backend_task_runner|, which must support file I/O. Network I/O is done via
66 // the |io_task_runner|.
67 ComponentCloudPolicyService(
69 CloudPolicyStore* store,
70 scoped_ptr<ResourceCache> cache,
71 scoped_refptr<base::SequencedTaskRunner> backend_task_runner,
72 scoped_refptr<base::SequencedTaskRunner> io_task_runner);
73 virtual ~ComponentCloudPolicyService();
75 // Returns true if |domain| is supported by the service.
76 static bool SupportsDomain(PolicyDomain domain);
78 // Returns true if the backend is initialized, and the initial policies and
79 // components are being served.
80 bool is_initialized() const { return is_initialized_; }
82 // Returns the current policies for components.
83 const PolicyBundle& policy() const { return policy_; }
85 // Connects to the cloud policy service using |client|. |client| must outlive
86 // this object. Only cached policies will be served until a |client| is
88 // |request_context| is used with the URLFetchers triggered by the updater.
89 void Connect(CloudPolicyClient* client,
90 scoped_refptr<net::URLRequestContextGetter> request_context);
92 // Disconnects from the cloud policy service and stops trying to download
93 // remote policy data.
96 // |descriptor| lists the complete set of components to track for its domain.
97 // This purges unused components from the cache, and starts updating the
98 // components listed in the descriptor. It's only valid to call this for
99 // domains that are supported, i.e. SupportsDomain(domain) is true.
100 void RegisterPolicyDomain(
101 scoped_refptr<const PolicyDomainDescriptor> descriptor);
103 // CloudPolicyClient::Observer implementation:
104 virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE;
105 virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE;
106 virtual void OnClientError(CloudPolicyClient* client) OVERRIDE;
108 // CloudPolicyStore::Observer implementation:
109 virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE;
110 virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE;
114 typedef std::set<std::string> StringSet;
115 typedef std::map<PolicyDomain, StringSet> ComponentMap;
117 void InitializeBackend();
118 void OnBackendInitialized(scoped_ptr<ComponentMap> components,
119 scoped_ptr<PolicyBundle> initial_policy);
120 void InitializeClient();
121 void OnPolicyUpdated(scoped_ptr<PolicyBundle> policy);
123 void SetCredentialsAndReloadClient();
124 bool UpdateClientNamespaces(PolicyDomain domain,
125 const StringSet& old_set,
126 const StringSet& new_set);
127 void AddNamespacesToFetch(PolicyDomain domain, const StringSet& set);
128 void RemoveNamespacesToFetch(PolicyDomain domain, const StringSet& set);
132 scoped_refptr<base::SequencedTaskRunner> backend_task_runner_;
133 scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
135 // The |external_policy_data_fetcher_backend_| handles network I/O for the
136 // |backend_| because URLRequestContextGetter and URLFetchers cannot be
137 // referenced from background threads. It is instantiated on the thread |this|
138 // runs on but after that, must only be accessed and eventually destroyed via
139 // the |io_task_runner_|.
140 scoped_ptr<ExternalPolicyDataFetcherBackend>
141 external_policy_data_fetcher_backend_;
143 // The |backend_| handles all download scheduling, validation and caching of
144 // policies. It is instantiated on the thread |this| runs on but after that,
145 // must only be accessed and eventually destroyed via the
146 // |backend_task_runner_|.
147 scoped_ptr<Backend> backend_;
149 CloudPolicyClient* client_;
150 CloudPolicyStore* store_;
152 // The currently registered components for each policy domain. If a policy
153 // domain doesn't have an entry in this map then it hasn't registered its
154 // component yet. A domain in this map with an empty set of components means
155 // that the domain is registered, but has no components.
156 ComponentMap registered_components_;
158 // Contains all the current policies for components.
159 PolicyBundle policy_;
161 bool is_initialized_;
162 base::WeakPtrFactory<ComponentCloudPolicyService> weak_ptr_factory_;
164 DISALLOW_COPY_AND_ASSIGN(ComponentCloudPolicyService);
167 } // namespace policy
169 #endif // CHROME_BROWSER_POLICY_CLOUD_COMPONENT_CLOUD_POLICY_SERVICE_H_