- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / policy / cloud / component_cloud_policy_service.h
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.
4
5 #ifndef CHROME_BROWSER_POLICY_CLOUD_COMPONENT_CLOUD_POLICY_SERVICE_H_
6 #define CHROME_BROWSER_POLICY_CLOUD_COMPONENT_CLOUD_POLICY_SERVICE_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11
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"
22
23 namespace base {
24 class SequencedTaskRunner;
25 }
26
27 namespace net {
28 class URLRequestContextGetter;
29 }
30
31 namespace policy {
32
33 class ExternalPolicyDataFetcherBackend;
34 class PolicyDomainDescriptor;
35 class ResourceCache;
36
37 // Manages cloud policy for components.
38 //
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 {
44  public:
45   // Key for the ResourceCache where the list of known components is cached.
46   static const char kComponentNamespaceCache[];
47
48   class Delegate {
49    public:
50     virtual ~Delegate();
51
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;
55
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;
60   };
61
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(
68       Delegate* delegate,
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();
74
75   // Returns true if |domain| is supported by the service.
76   static bool SupportsDomain(PolicyDomain domain);
77
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_; }
81
82   // Returns the current policies for components.
83   const PolicyBundle& policy() const { return policy_; }
84
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
87   // connected.
88   // |request_context| is used with the URLFetchers triggered by the updater.
89   void Connect(CloudPolicyClient* client,
90                scoped_refptr<net::URLRequestContextGetter> request_context);
91
92   // Disconnects from the cloud policy service and stops trying to download
93   // remote policy data.
94   void Disconnect();
95
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);
102
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;
107
108   // CloudPolicyStore::Observer implementation:
109   virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE;
110   virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE;
111
112  private:
113   class Backend;
114   typedef std::set<std::string> StringSet;
115   typedef std::map<PolicyDomain, StringSet> ComponentMap;
116
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);
122
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);
129
130   Delegate* delegate_;
131
132   scoped_refptr<base::SequencedTaskRunner> backend_task_runner_;
133   scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
134
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_;
142
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_;
148
149   CloudPolicyClient* client_;
150   CloudPolicyStore* store_;
151
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_;
157
158   // Contains all the current policies for components.
159   PolicyBundle policy_;
160
161   bool is_initialized_;
162   base::WeakPtrFactory<ComponentCloudPolicyService> weak_ptr_factory_;
163
164   DISALLOW_COPY_AND_ASSIGN(ComponentCloudPolicyService);
165 };
166
167 }  // namespace policy
168
169 #endif  // CHROME_BROWSER_POLICY_CLOUD_COMPONENT_CLOUD_POLICY_SERVICE_H_