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