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