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