OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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_POLICY_CLIENT_H_ | |
6 #define CHROME_BROWSER_POLICY_CLOUD_POLICY_CLIENT_H_ | |
7 | |
8 #include <map> | |
9 #include <set> | |
10 #include <string> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/observer_list.h" | |
15 #include "base/time.h" | |
16 #include "chrome/browser/policy/cloud_policy_constants.h" | |
17 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
18 | |
19 namespace policy { | |
20 | |
21 class DeviceManagementRequestJob; | |
22 class DeviceManagementService; | |
23 | |
24 // Implements the core logic required to talk to the device management service. | |
25 // Also keeps track of the current state of the association with the service, | |
26 // such as whether there is a valid registration (DMToken is present in that | |
27 // case) and whether and what errors occurred in the latest request. | |
28 // | |
29 // Note that CloudPolicyClient doesn't do any validation of policy responses | |
30 // such as signature and time stamp checks. These happen once the policy gets | |
31 // installed in the cloud policy cache. | |
32 class CloudPolicyClient { | |
33 public: | |
34 // Maps a PolicyNamespaceKey to its corresponding PolicyFetchResponse. | |
35 typedef std::map<PolicyNamespaceKey, | |
36 enterprise_management::PolicyFetchResponse*> ResponseMap; | |
37 | |
38 // Observer interface for state and policy changes. | |
39 class Observer { | |
40 public: | |
41 virtual ~Observer(); | |
42 | |
43 // Called when a policy fetch completes successfully. If a policy fetch | |
44 // triggers an error, OnClientError() will fire. | |
45 virtual void OnPolicyFetched(CloudPolicyClient* client) = 0; | |
46 | |
47 // Called upon registration state changes. This callback is invoked for | |
48 // successful completion of registration and unregistration requests. | |
49 virtual void OnRegistrationStateChanged(CloudPolicyClient* client) = 0; | |
50 | |
51 // Indicates there's been an error in a previously-issued request. | |
52 virtual void OnClientError(CloudPolicyClient* client) = 0; | |
53 }; | |
54 | |
55 // Delegate interface for supplying status information to upload to the server | |
56 // as part of the policy fetch request. | |
57 class StatusProvider { | |
58 public: | |
59 virtual ~StatusProvider(); | |
60 | |
61 // Retrieves status information to send with the next policy fetch. | |
62 // Implementations must return true if status information was filled in. | |
63 virtual bool GetDeviceStatus( | |
64 enterprise_management::DeviceStatusReportRequest* status) = 0; | |
65 virtual bool GetSessionStatus( | |
66 enterprise_management::SessionStatusReportRequest* status) = 0; | |
67 | |
68 // Called after the status information has successfully been submitted to | |
69 // the server. | |
70 virtual void OnSubmittedSuccessfully() = 0; | |
71 }; | |
72 | |
73 // |provider| and |service| are weak pointers and it's the caller's | |
74 // responsibility to keep them valid for the lifetime of CloudPolicyClient. | |
75 CloudPolicyClient(const std::string& machine_id, | |
76 const std::string& machine_model, | |
77 UserAffiliation user_affiliation, | |
78 StatusProvider* provider, | |
79 DeviceManagementService* service); | |
80 virtual ~CloudPolicyClient(); | |
81 | |
82 // Sets the DMToken, thereby establishing a registration with the server. A | |
83 // policy fetch is not automatically issued but can be requested by calling | |
84 // FetchPolicy(). | |
85 virtual void SetupRegistration(const std::string& dm_token, | |
86 const std::string& client_id); | |
87 | |
88 // Attempts to register with the device management service. Results in a | |
89 // registration change or error notification. | |
90 virtual void Register( | |
91 enterprise_management::DeviceRegisterRequest::Type registration_type, | |
92 const std::string& auth_token, | |
93 const std::string& client_id, | |
94 bool is_auto_enrollment); | |
95 | |
96 // Requests a policy fetch. The client being registered is a prerequisite to | |
97 // this operation and this call will CHECK if the client is not in registered | |
98 // state. FetchPolicy() triggers a policy fetch from the cloud. A policy | |
99 // change notification is reported to the observers and the new policy blob | |
100 // can be retrieved once the policy fetch operation completes. In case of | |
101 // multiple requests to fetch policy, new requests will cancel any pending | |
102 // requests and the latest request will eventually trigger notifications. | |
103 virtual void FetchPolicy(); | |
104 | |
105 // Sends an unregistration request to the server. | |
106 virtual void Unregister(); | |
107 | |
108 // Adds an observer to be called back upon policy and state changes. | |
109 void AddObserver(Observer* observer); | |
110 | |
111 // Removes the specified observer. | |
112 void RemoveObserver(Observer* observer); | |
113 | |
114 void set_submit_machine_id(bool submit_machine_id) { | |
115 submit_machine_id_ = submit_machine_id; | |
116 } | |
117 | |
118 void set_last_policy_timestamp(const base::Time& timestamp) { | |
119 last_policy_timestamp_ = timestamp; | |
120 } | |
121 | |
122 void set_public_key_version(int public_key_version) { | |
123 public_key_version_ = public_key_version; | |
124 public_key_version_valid_ = true; | |
125 } | |
126 | |
127 void clear_public_key_version() { | |
128 public_key_version_valid_ = false; | |
129 } | |
130 | |
131 // FetchPolicy() calls will request this policy namespace. | |
132 void AddNamespaceToFetch(const PolicyNamespaceKey& policy_ns_key); | |
133 | |
134 // FetchPolicy() calls won't request the given policy namespace anymore. | |
135 void RemoveNamespaceToFetch(const PolicyNamespaceKey& policy_ns_key); | |
136 | |
137 // Whether the client is registered with the device management service. | |
138 bool is_registered() const { return !dm_token_.empty(); } | |
139 | |
140 const std::string& dm_token() const { return dm_token_; } | |
141 | |
142 // The device mode as received in the registration request. | |
143 DeviceMode device_mode() const { return device_mode_; } | |
144 | |
145 // The policy responses as obtained by the last request to the cloud. These | |
146 // policies haven't gone through verification, so their contents cannot be | |
147 // trusted. Use CloudPolicyStore::policy() and CloudPolicyStore::policy_map() | |
148 // instead for making policy decisions. | |
149 const ResponseMap& responses() const { | |
150 return responses_; | |
151 } | |
152 | |
153 // Returns the policy response for |policy_ns_key|, if found in |responses()|; | |
154 // otherwise returns NULL. | |
155 const enterprise_management::PolicyFetchResponse* GetPolicyFor( | |
156 const PolicyNamespaceKey& policy_ns_key) const; | |
157 | |
158 DeviceManagementStatus status() const { | |
159 return status_; | |
160 } | |
161 | |
162 protected: | |
163 // A set of PolicyNamespaceKeys to fetch. | |
164 typedef std::set<PolicyNamespaceKey> NamespaceSet; | |
165 | |
166 // Callback for retries of registration requests. | |
167 void OnRetryRegister(DeviceManagementRequestJob* job); | |
168 | |
169 // Callback for registration requests. | |
170 void OnRegisterCompleted( | |
171 DeviceManagementStatus status, | |
172 const enterprise_management::DeviceManagementResponse& response); | |
173 | |
174 // Callback for policy fetch requests. | |
175 void OnPolicyFetchCompleted( | |
176 DeviceManagementStatus status, | |
177 const enterprise_management::DeviceManagementResponse& response); | |
178 | |
179 // Callback for unregistration requests. | |
180 void OnUnregisterCompleted( | |
181 DeviceManagementStatus status, | |
182 const enterprise_management::DeviceManagementResponse& response); | |
183 | |
184 // Observer notification helpers. | |
185 void NotifyPolicyFetched(); | |
186 void NotifyRegistrationStateChanged(); | |
187 void NotifyClientError(); | |
188 | |
189 // Data necessary for constructing policy requests. | |
190 const std::string machine_id_; | |
191 const std::string machine_model_; | |
192 const UserAffiliation user_affiliation_; | |
193 NamespaceSet namespaces_to_fetch_; | |
194 | |
195 std::string dm_token_; | |
196 DeviceMode device_mode_; | |
197 std::string client_id_; | |
198 bool submit_machine_id_; | |
199 base::Time last_policy_timestamp_; | |
200 int public_key_version_; | |
201 bool public_key_version_valid_; | |
202 | |
203 // Used for issuing requests to the cloud. | |
204 DeviceManagementService* service_; | |
205 scoped_ptr<DeviceManagementRequestJob> request_job_; | |
206 | |
207 // Status upload data is produced by |status_provider_|. | |
208 StatusProvider* status_provider_; | |
209 | |
210 // The policy responses returned by the last policy fetch operation. | |
211 ResponseMap responses_; | |
212 DeviceManagementStatus status_; | |
213 | |
214 ObserverList<Observer, true> observers_; | |
215 | |
216 private: | |
217 DISALLOW_COPY_AND_ASSIGN(CloudPolicyClient); | |
218 }; | |
219 | |
220 } // namespace policy | |
221 | |
222 #endif // CHROME_BROWSER_POLICY_CLOUD_POLICY_CLIENT_H_ | |
OLD | NEW |