Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / policy / core / common / 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 COMPONENTS_POLICY_CORE_COMMON_CLOUD_COMPONENT_CLOUD_POLICY_SERVICE_H_
6 #define COMPONENTS_POLICY_CORE_COMMON_CLOUD_COMPONENT_CLOUD_POLICY_SERVICE_H_
7
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "components/policy/core/common/cloud/cloud_policy_client.h"
15 #include "components/policy/core/common/cloud/cloud_policy_core.h"
16 #include "components/policy/core/common/cloud/cloud_policy_store.h"
17 #include "components/policy/core/common/policy_bundle.h"
18 #include "components/policy/core/common/policy_namespace.h"
19 #include "components/policy/core/common/schema_registry.h"
20 #include "components/policy/policy_export.h"
21
22 namespace base {
23 class SequencedTaskRunner;
24 }
25
26 namespace net {
27 class URLRequestContextGetter;
28 }
29
30 namespace policy {
31
32 class ExternalPolicyDataFetcherBackend;
33 class ResourceCache;
34 class SchemaMap;
35
36 // Manages cloud policy for components.
37 //
38 // This class takes care of fetching, validating, storing and updating policy
39 // for components. The components to manage come from a SchemaRegistry.
40 class POLICY_EXPORT ComponentCloudPolicyService
41     : public CloudPolicyClient::Observer,
42       public CloudPolicyCore::Observer,
43       public CloudPolicyStore::Observer,
44       public SchemaRegistry::Observer,
45       public base::NonThreadSafe {
46  public:
47   class POLICY_EXPORT Delegate {
48    public:
49     virtual ~Delegate();
50
51     // Invoked whenever the policy served by policy() changes. This is also
52     // invoked for the first time once the backend is initialized, and
53     // is_initialized() becomes true.
54     virtual void OnComponentCloudPolicyUpdated() = 0;
55   };
56
57   // The |delegate| is notified of updates to the downloaded policies and must
58   // outlive this object.
59   //
60   // |schema_registry| is used to get the list of components to fetch cloud
61   // policy for. It must outlive this object.
62   //
63   // |core| is used to obtain the CloudPolicyStore and CloudPolicyClient used
64   // by this service. The store will be the source of the registration status
65   // and registration credentials; the client will be used to fetch cloud
66   // policy. It must outlive this object.
67   //
68   // |cache| is used to load and store local copies of the downloaded policies.
69   //
70   // Download scheduling, validation and caching of policies are done via the
71   // |backend_task_runner|, which must support file I/O. Network I/O is done via
72   // the |io_task_runner|.
73   //
74   // |request_context| is used by the background URLFetchers.
75   ComponentCloudPolicyService(
76       Delegate* delegate,
77       SchemaRegistry* schema_registry,
78       CloudPolicyCore* core,
79 #if !defined(OS_ANDROID) && !defined(OS_IOS)
80       scoped_ptr<ResourceCache> cache,
81 #endif
82       scoped_refptr<net::URLRequestContextGetter> request_context,
83       scoped_refptr<base::SequencedTaskRunner> backend_task_runner,
84       scoped_refptr<base::SequencedTaskRunner> io_task_runner);
85   ~ComponentCloudPolicyService() override;
86
87   // Returns true if |domain| is supported by the service.
88   static bool SupportsDomain(PolicyDomain domain);
89
90   // Returns true if the backend is initialized, and the initial policies and
91   // components are being served.
92   bool is_initialized() const { return loaded_initial_policy_; }
93
94   // Returns the current policies for components.
95   const PolicyBundle& policy() const { return policy_; }
96
97   // Deletes all the cached component policy.
98   void ClearCache();
99
100   // SchemaRegistry::Observer implementation:
101   void OnSchemaRegistryReady() override;
102   void OnSchemaRegistryUpdated(bool has_new_schemas) override;
103
104   // CloudPolicyCore::Observer implementation:
105   void OnCoreConnected(CloudPolicyCore* core) override;
106   void OnCoreDisconnecting(CloudPolicyCore* core) override;
107   void OnRefreshSchedulerStarted(CloudPolicyCore* core) override;
108
109   // CloudPolicyStore::Observer implementation:
110   void OnStoreLoaded(CloudPolicyStore* store) override;
111   void OnStoreError(CloudPolicyStore* store) override;
112
113   // CloudPolicyClient::Observer implementation:
114   void OnPolicyFetched(CloudPolicyClient* client) override;
115   void OnRegistrationStateChanged(CloudPolicyClient* client) override;
116   void OnClientError(CloudPolicyClient* client) override;
117
118  private:
119 #if !defined(OS_ANDROID) && !defined(OS_IOS)
120   class Backend;
121
122   void InitializeIfReady();
123   void OnBackendInitialized(scoped_ptr<PolicyBundle> initial_policy);
124   void ReloadSchema();
125   void OnPolicyUpdated(scoped_ptr<PolicyBundle> policy);
126
127   Delegate* delegate_;
128   SchemaRegistry* schema_registry_;
129   CloudPolicyCore* core_;
130   scoped_refptr<net::URLRequestContextGetter> request_context_;
131   scoped_refptr<base::SequencedTaskRunner> backend_task_runner_;
132   scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
133
134   // The |external_policy_data_fetcher_backend_| handles network I/O for the
135   // |backend_| because URLRequestContextGetter and URLFetchers cannot be
136   // referenced from background threads. It is instantiated on the thread |this|
137   // runs on but after that, must only be accessed and eventually destroyed via
138   // the |io_task_runner_|.
139   scoped_ptr<ExternalPolicyDataFetcherBackend>
140       external_policy_data_fetcher_backend_;
141
142   // The |backend_| handles all download scheduling, validation and caching of
143   // policies. It is instantiated on the thread |this| runs on but after that,
144   // must only be accessed and eventually destroyed via the
145   // |backend_task_runner_|.
146   scoped_ptr<Backend> backend_;
147
148   // The currently registered components for each policy domain. Used to
149   // determine which components changed when a new SchemaMap becomes
150   // available.
151   scoped_refptr<SchemaMap> current_schema_map_;
152 #endif  // !defined(OS_ANDROID) && !defined(OS_IOS)
153
154   // Contains all the policies loaded from the store, before having been
155   // filtered by the |current_schema_map_|.
156   scoped_ptr<PolicyBundle> unfiltered_policy_;
157
158   // Contains all the current policies for components, filtered by the
159   // |current_schema_map_|.
160   PolicyBundle policy_;
161
162   // Whether the backend has started initializing asynchronously. Used to
163   // prevent double initialization, since both OnSchemaRegistryUpdated() and
164   // OnStoreLoaded() can happen while the backend is initializing.
165   bool started_loading_initial_policy_;
166
167   // Whether the backend has been initialized with the initial credentials and
168   // schemas, and this provider is serving the initial policies loaded from the
169   // cache.
170   bool loaded_initial_policy_;
171
172   // True if the backend currently has valid cloud policy credentials. This
173   // can go back to false if the user signs out, and back again to true if the
174   // user signs in again.
175   bool is_registered_for_cloud_policy_;
176
177   base::WeakPtrFactory<ComponentCloudPolicyService> weak_ptr_factory_;
178
179   DISALLOW_COPY_AND_ASSIGN(ComponentCloudPolicyService);
180 };
181
182 }  // namespace policy
183
184 #endif  // COMPONENTS_POLICY_CORE_COMMON_CLOUD_COMPONENT_CLOUD_POLICY_SERVICE_H_