| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_BROWSER_POLICY_CLOUD_COMPONENT_CLOUD_POLICY_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_POLICY_CLOUD_COMPONENT_CLOUD_POLICY_SERVICE_H_ |
| 6 #define CHROME_BROWSER_POLICY_CLOUD_COMPONENT_CLOUD_POLICY_SERVICE_H_ | 6 #define CHROME_BROWSER_POLICY_CLOUD_COMPONENT_CLOUD_POLICY_SERVICE_H_ |
| 7 | 7 |
| 8 #include <set> | |
| 9 | |
| 10 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 11 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 12 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 15 #include "base/threading/non_thread_safe.h" | 13 #include "base/threading/non_thread_safe.h" |
| 14 #include "base/timer/timer.h" |
| 16 #include "chrome/browser/policy/cloud/cloud_policy_client.h" | 15 #include "chrome/browser/policy/cloud/cloud_policy_client.h" |
| 17 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | |
| 18 #include "chrome/browser/policy/cloud/cloud_policy_store.h" | 16 #include "chrome/browser/policy/cloud/cloud_policy_store.h" |
| 19 #include "chrome/browser/policy/policy_bundle.h" | 17 #include "chrome/browser/policy/policy_bundle.h" |
| 18 #include "chrome/browser/policy/schema_registry.h" |
| 20 #include "components/policy/core/common/policy_namespace.h" | 19 #include "components/policy/core/common/policy_namespace.h" |
| 21 | 20 |
| 22 namespace base { | 21 namespace base { |
| 23 class SequencedTaskRunner; | 22 class SequencedTaskRunner; |
| 24 } | 23 } |
| 25 | 24 |
| 26 namespace net { | 25 namespace net { |
| 27 class URLRequestContextGetter; | 26 class URLRequestContextGetter; |
| 28 } | 27 } |
| 29 | 28 |
| 30 namespace policy { | 29 namespace policy { |
| 31 | 30 |
| 32 class ExternalPolicyDataFetcherBackend; | 31 class ExternalPolicyDataFetcherBackend; |
| 33 class ResourceCache; | 32 class ResourceCache; |
| 34 class SchemaMap; | 33 class SchemaMap; |
| 35 | 34 |
| 36 // Manages cloud policy for components. | 35 // Manages cloud policy for components. |
| 37 // | 36 // |
| 38 // This class takes care of fetching, validating, storing and updating policy | 37 // This class takes care of fetching, validating, storing and updating policy |
| 39 // for components. The components to manage have to be explicitly registered. | 38 // for components. The components to manage come from a SchemaRegistry. |
| 40 class ComponentCloudPolicyService : public CloudPolicyClient::Observer, | 39 class ComponentCloudPolicyService : public CloudPolicyClient::Observer, |
| 41 public CloudPolicyStore::Observer, | 40 public CloudPolicyStore::Observer, |
| 41 public SchemaRegistry::Observer, |
| 42 public base::NonThreadSafe { | 42 public base::NonThreadSafe { |
| 43 public: | 43 public: |
| 44 // Key for the ResourceCache where the list of known components is cached. | |
| 45 static const char kComponentNamespaceCache[]; | |
| 46 | |
| 47 class Delegate { | 44 class Delegate { |
| 48 public: | 45 public: |
| 49 virtual ~Delegate(); | 46 virtual ~Delegate(); |
| 50 | 47 |
| 51 // Invoked whenever the service has appended new namespaces to fetch to | 48 // Invoked whenever the service has appended new namespaces to fetch to |
| 52 // the CloudPolicyClient, signaling that a policy fetch should be done soon. | 49 // the CloudPolicyClient, signaling that a policy fetch should be done soon. |
| 53 virtual void OnComponentCloudPolicyRefreshNeeded() = 0; | 50 virtual void OnComponentCloudPolicyRefreshNeeded() = 0; |
| 54 | 51 |
| 55 // Invoked whenever the policy served by policy() changes. This is also | 52 // Invoked whenever the policy served by policy() changes. This is also |
| 56 // invoked for the first time once the backend is initialized, and | 53 // invoked for the first time once the backend is initialized, and |
| 57 // is_initialized() becomes true. | 54 // is_initialized() becomes true. |
| 58 virtual void OnComponentCloudPolicyUpdated() = 0; | 55 virtual void OnComponentCloudPolicyUpdated() = 0; |
| 59 }; | 56 }; |
| 60 | 57 |
| 58 // All of these components must outlive this instance. |
| 59 // |
| 60 // The |delegate| is notified of updates to the downloaded policies, and is |
| 61 // notified whenever a refresh is needed. |
| 62 // |schema_registry| contains the list of components to fetch policy for. |
| 61 // |store| is used to get the current DMToken and the username. | 63 // |store| is used to get the current DMToken and the username. |
| 62 // |cache| is used to load and store local copies of the downloaded policies. | 64 // |cache| is used to load and store local copies of the downloaded policies. |
| 63 // Download scheduling, validation and caching of policies are done via the | 65 // Download scheduling, validation and caching of policies are done via the |
| 64 // |backend_task_runner|, which must support file I/O. Network I/O is done via | 66 // |backend_task_runner|, which must support file I/O. Network I/O is done via |
| 65 // the |io_task_runner|. | 67 // the |io_task_runner|. |
| 68 // |client| is updated with the list of components to fetch. |
| 69 // |request_context| is used by the background URLFetchers. |
| 66 ComponentCloudPolicyService( | 70 ComponentCloudPolicyService( |
| 67 Delegate* delegate, | 71 Delegate* delegate, |
| 72 SchemaRegistry* schema_registry, |
| 68 CloudPolicyStore* store, | 73 CloudPolicyStore* store, |
| 69 scoped_ptr<ResourceCache> cache, | 74 scoped_ptr<ResourceCache> cache, |
| 75 CloudPolicyClient* client, |
| 76 scoped_refptr<net::URLRequestContextGetter> request_context, |
| 70 scoped_refptr<base::SequencedTaskRunner> backend_task_runner, | 77 scoped_refptr<base::SequencedTaskRunner> backend_task_runner, |
| 71 scoped_refptr<base::SequencedTaskRunner> io_task_runner); | 78 scoped_refptr<base::SequencedTaskRunner> io_task_runner); |
| 72 virtual ~ComponentCloudPolicyService(); | 79 virtual ~ComponentCloudPolicyService(); |
| 73 | 80 |
| 74 // Returns true if |domain| is supported by the service. | 81 // Returns true if |domain| is supported by the service. |
| 75 static bool SupportsDomain(PolicyDomain domain); | 82 static bool SupportsDomain(PolicyDomain domain); |
| 76 | 83 |
| 77 // Returns true if the backend is initialized, and the initial policies and | 84 // Returns true if the backend is initialized, and the initial policies and |
| 78 // components are being served. | 85 // components are being served. |
| 79 bool is_initialized() const { return is_initialized_; } | 86 bool is_initialized() const { return is_initialized_; } |
| 80 | 87 |
| 81 // Returns the current policies for components. | 88 // Returns the current policies for components. |
| 82 const PolicyBundle& policy() const { return policy_; } | 89 const PolicyBundle& policy() const { return policy_; } |
| 83 | 90 |
| 84 // Connects to the cloud policy service using |client|. |client| must outlive | |
| 85 // this object. Only cached policies will be served until a |client| is | |
| 86 // connected. | |
| 87 // |request_context| is used with the URLFetchers triggered by the updater. | |
| 88 void Connect(CloudPolicyClient* client, | |
| 89 scoped_refptr<net::URLRequestContextGetter> request_context); | |
| 90 | |
| 91 // Disconnects from the cloud policy service and stops trying to download | |
| 92 // remote policy data. | |
| 93 void Disconnect(); | |
| 94 | |
| 95 // |schema_map| contains the schemas for all the components that this | |
| 96 // service should load policy for. | |
| 97 // This purges unused components from the cache, and starts updating the | |
| 98 // components listed in the map. | |
| 99 // Unsupported domains in the map are ignored. | |
| 100 void OnSchemasUpdated(const scoped_refptr<SchemaMap>& schema_map); | |
| 101 | |
| 102 // CloudPolicyClient::Observer implementation: | 91 // CloudPolicyClient::Observer implementation: |
| 103 virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE; | 92 virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE; |
| 104 virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE; | 93 virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE; |
| 105 virtual void OnClientError(CloudPolicyClient* client) OVERRIDE; | 94 virtual void OnClientError(CloudPolicyClient* client) OVERRIDE; |
| 106 | 95 |
| 107 // CloudPolicyStore::Observer implementation: | 96 // CloudPolicyStore::Observer implementation: |
| 108 virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE; | 97 virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE; |
| 109 virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE; | 98 virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE; |
| 110 | 99 |
| 100 // SchemaRegistry::Observer implementation: |
| 101 virtual void OnSchemaRegistryReady() OVERRIDE; |
| 102 virtual void OnSchemaRegistryUpdated(bool has_new_schemas) OVERRIDE; |
| 103 |
| 111 private: | 104 private: |
| 112 class Backend; | 105 class Backend; |
| 113 typedef std::set<PolicyNamespaceKey> PolicyNamespaceKeys; | |
| 114 | 106 |
| 115 void InitializeBackend(); | 107 void InitializeIfReady(); |
| 116 void OnBackendInitialized(scoped_ptr<PolicyNamespaceKeys> initial_keys, | 108 void OnBackendInitialized(scoped_ptr<PolicyBundle> initial_policy); |
| 117 scoped_ptr<PolicyBundle> initial_policy); | 109 void SetCurrentSchema(const scoped_refptr<SchemaMap>& new_schema_map, |
| 118 void InitializeClient(); | 110 bool send_to_backend); |
| 119 void OnPolicyUpdated(scoped_ptr<PolicyBundle> policy); | 111 void OnPolicyUpdated(scoped_ptr<PolicyBundle> policy); |
| 120 | 112 |
| 121 void SetCredentialsAndReloadClient(); | |
| 122 bool UpdateClientNamespaces(const PolicyNamespaceKeys& old_keys, | |
| 123 const PolicyNamespaceKeys& new_keys); | |
| 124 void AddNamespacesToFetch(const PolicyNamespaceKeys& keys); | |
| 125 void RemoveNamespacesToFetch(const PolicyNamespaceKeys& keys); | |
| 126 | |
| 127 Delegate* delegate_; | 113 Delegate* delegate_; |
| 128 | 114 SchemaRegistry* schema_registry_; |
| 115 CloudPolicyStore* store_; |
| 116 CloudPolicyClient* client_; |
| 117 scoped_refptr<net::URLRequestContextGetter> request_context_; |
| 129 scoped_refptr<base::SequencedTaskRunner> backend_task_runner_; | 118 scoped_refptr<base::SequencedTaskRunner> backend_task_runner_; |
| 130 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; | 119 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; |
| 131 | 120 |
| 132 // The |external_policy_data_fetcher_backend_| handles network I/O for the | 121 // The |external_policy_data_fetcher_backend_| handles network I/O for the |
| 133 // |backend_| because URLRequestContextGetter and URLFetchers cannot be | 122 // |backend_| because URLRequestContextGetter and URLFetchers cannot be |
| 134 // referenced from background threads. It is instantiated on the thread |this| | 123 // referenced from background threads. It is instantiated on the thread |this| |
| 135 // runs on but after that, must only be accessed and eventually destroyed via | 124 // runs on but after that, must only be accessed and eventually destroyed via |
| 136 // the |io_task_runner_|. | 125 // the |io_task_runner_|. |
| 137 scoped_ptr<ExternalPolicyDataFetcherBackend> | 126 scoped_ptr<ExternalPolicyDataFetcherBackend> |
| 138 external_policy_data_fetcher_backend_; | 127 external_policy_data_fetcher_backend_; |
| 139 | 128 |
| 140 // The |backend_| handles all download scheduling, validation and caching of | 129 // The |backend_| handles all download scheduling, validation and caching of |
| 141 // policies. It is instantiated on the thread |this| runs on but after that, | 130 // policies. It is instantiated on the thread |this| runs on but after that, |
| 142 // must only be accessed and eventually destroyed via the | 131 // must only be accessed and eventually destroyed via the |
| 143 // |backend_task_runner_|. | 132 // |backend_task_runner_|. |
| 144 scoped_ptr<Backend> backend_; | 133 scoped_ptr<Backend> backend_; |
| 145 | 134 |
| 146 CloudPolicyClient* client_; | 135 // The currently registered components for each policy domain. Used to |
| 147 CloudPolicyStore* store_; | 136 // determine which components changed when a new SchemaMap becomes |
| 148 | 137 // available. |
| 149 // The currently registered components for each policy domain. | 138 scoped_refptr<SchemaMap> current_schema_map_; |
| 150 PolicyNamespaceKeys keys_; | |
| 151 | 139 |
| 152 // Contains all the current policies for components. | 140 // Contains all the current policies for components. |
| 153 PolicyBundle policy_; | 141 PolicyBundle policy_; |
| 154 | 142 |
| 143 // Used to delay changes triggered by updates to the SchemaRegistry. See |
| 144 // the implementation of OnSchemaRegistryUpdated() for details. |
| 145 base::Timer schema_update_timer_; |
| 146 |
| 155 bool is_initialized_; | 147 bool is_initialized_; |
| 156 bool has_initial_keys_; | 148 bool has_credentials_; |
| 157 base::WeakPtrFactory<ComponentCloudPolicyService> weak_ptr_factory_; | 149 base::WeakPtrFactory<ComponentCloudPolicyService> weak_ptr_factory_; |
| 158 | 150 |
| 159 DISALLOW_COPY_AND_ASSIGN(ComponentCloudPolicyService); | 151 DISALLOW_COPY_AND_ASSIGN(ComponentCloudPolicyService); |
| 160 }; | 152 }; |
| 161 | 153 |
| 162 } // namespace policy | 154 } // namespace policy |
| 163 | 155 |
| 164 #endif // CHROME_BROWSER_POLICY_CLOUD_COMPONENT_CLOUD_POLICY_SERVICE_H_ | 156 #endif // CHROME_BROWSER_POLICY_CLOUD_COMPONENT_CLOUD_POLICY_SERVICE_H_ |
| OLD | NEW |