| 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_CLOUD_POLICY_CLIENT_H_ | |
| 6 #define CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_CLIENT_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <set> | |
| 10 #include <string> | |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/callback.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/observer_list.h" | |
| 16 #include "base/time/time.h" | |
| 17 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | |
| 18 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" | |
| 19 | |
| 20 namespace net { | |
| 21 class URLRequestContextGetter; | |
| 22 } | |
| 23 | |
| 24 namespace policy { | |
| 25 | |
| 26 class DeviceManagementRequestJob; | |
| 27 class DeviceManagementService; | |
| 28 | |
| 29 // Implements the core logic required to talk to the device management service. | |
| 30 // Also keeps track of the current state of the association with the service, | |
| 31 // such as whether there is a valid registration (DMToken is present in that | |
| 32 // case) and whether and what errors occurred in the latest request. | |
| 33 // | |
| 34 // Note that CloudPolicyClient doesn't do any validation of policy responses | |
| 35 // such as signature and time stamp checks. These happen once the policy gets | |
| 36 // installed in the cloud policy cache. | |
| 37 class CloudPolicyClient { | |
| 38 public: | |
| 39 // Maps a PolicyNamespaceKey to its corresponding PolicyFetchResponse. | |
| 40 typedef std::map<PolicyNamespaceKey, | |
| 41 enterprise_management::PolicyFetchResponse*> ResponseMap; | |
| 42 | |
| 43 // A callback which receives boolean status of an operation. If the operation | |
| 44 // succeeded, |status| is true. | |
| 45 typedef base::Callback<void(bool status)> StatusCallback; | |
| 46 | |
| 47 // Observer interface for state and policy changes. | |
| 48 class Observer { | |
| 49 public: | |
| 50 virtual ~Observer(); | |
| 51 | |
| 52 // Called when a policy fetch completes successfully. If a policy fetch | |
| 53 // triggers an error, OnClientError() will fire. | |
| 54 virtual void OnPolicyFetched(CloudPolicyClient* client) = 0; | |
| 55 | |
| 56 // Called upon registration state changes. This callback is invoked for | |
| 57 // successful completion of registration and unregistration requests. | |
| 58 virtual void OnRegistrationStateChanged(CloudPolicyClient* client) = 0; | |
| 59 | |
| 60 // Called when a request for device robot OAuth2 authorization tokens | |
| 61 // returns successfully. Only occurs during enrollment. Optional | |
| 62 // (default implementation is a noop). | |
| 63 virtual void OnRobotAuthCodesFetched(CloudPolicyClient* client); | |
| 64 | |
| 65 // Indicates there's been an error in a previously-issued request. | |
| 66 virtual void OnClientError(CloudPolicyClient* client) = 0; | |
| 67 }; | |
| 68 | |
| 69 // Delegate interface for supplying status information to upload to the server | |
| 70 // as part of the policy fetch request. | |
| 71 class StatusProvider { | |
| 72 public: | |
| 73 virtual ~StatusProvider(); | |
| 74 | |
| 75 // Retrieves status information to send with the next policy fetch. | |
| 76 // Implementations must return true if status information was filled in. | |
| 77 virtual bool GetDeviceStatus( | |
| 78 enterprise_management::DeviceStatusReportRequest* status) = 0; | |
| 79 virtual bool GetSessionStatus( | |
| 80 enterprise_management::SessionStatusReportRequest* status) = 0; | |
| 81 | |
| 82 // Called after the status information has successfully been submitted to | |
| 83 // the server. | |
| 84 virtual void OnSubmittedSuccessfully() = 0; | |
| 85 }; | |
| 86 | |
| 87 // |provider| and |service| are weak pointers and it's the caller's | |
| 88 // responsibility to keep them valid for the lifetime of CloudPolicyClient. | |
| 89 CloudPolicyClient( | |
| 90 const std::string& machine_id, | |
| 91 const std::string& machine_model, | |
| 92 UserAffiliation user_affiliation, | |
| 93 StatusProvider* provider, | |
| 94 DeviceManagementService* service, | |
| 95 scoped_refptr<net::URLRequestContextGetter> request_context); | |
| 96 virtual ~CloudPolicyClient(); | |
| 97 | |
| 98 // Sets the DMToken, thereby establishing a registration with the server. A | |
| 99 // policy fetch is not automatically issued but can be requested by calling | |
| 100 // FetchPolicy(). | |
| 101 virtual void SetupRegistration(const std::string& dm_token, | |
| 102 const std::string& client_id); | |
| 103 | |
| 104 // Attempts to register with the device management service. Results in a | |
| 105 // registration change or error notification. | |
| 106 virtual void Register( | |
| 107 enterprise_management::DeviceRegisterRequest::Type registration_type, | |
| 108 const std::string& auth_token, | |
| 109 const std::string& client_id, | |
| 110 bool is_auto_enrollment, | |
| 111 const std::string& requisition); | |
| 112 | |
| 113 // Sets information about a policy invalidation. Subsequent fetch operations | |
| 114 // will use the given info, and callers can use fetched_invalidation_version | |
| 115 // to determine which version of policy was fetched. | |
| 116 void SetInvalidationInfo(int64 version, const std::string& payload); | |
| 117 | |
| 118 // Requests a policy fetch. The client being registered is a prerequisite to | |
| 119 // this operation and this call will CHECK if the client is not in registered | |
| 120 // state. FetchPolicy() triggers a policy fetch from the cloud. A policy | |
| 121 // change notification is reported to the observers and the new policy blob | |
| 122 // can be retrieved once the policy fetch operation completes. In case of | |
| 123 // multiple requests to fetch policy, new requests will cancel any pending | |
| 124 // requests and the latest request will eventually trigger notifications. | |
| 125 virtual void FetchPolicy(); | |
| 126 | |
| 127 // Requests OAuth2 auth codes for the device robot account. The client being | |
| 128 // registered is a prerequisite to this operation and this call will CHECK if | |
| 129 // the client is not in registered state. | |
| 130 virtual void FetchRobotAuthCodes(const std::string& auth_token); | |
| 131 | |
| 132 // Sends an unregistration request to the server. | |
| 133 virtual void Unregister(); | |
| 134 | |
| 135 // Upload a device certificate to the server. Like FetchPolicy, this method | |
| 136 // requires that the client is in a registered state. |certificate_data| must | |
| 137 // hold the X.509 certificate data to be sent to the server. The |callback| | |
| 138 // will be called when the operation completes. | |
| 139 virtual void UploadCertificate(const std::string& certificate_data, | |
| 140 const StatusCallback& callback); | |
| 141 | |
| 142 // Adds an observer to be called back upon policy and state changes. | |
| 143 void AddObserver(Observer* observer); | |
| 144 | |
| 145 // Removes the specified observer. | |
| 146 void RemoveObserver(Observer* observer); | |
| 147 | |
| 148 void set_submit_machine_id(bool submit_machine_id) { | |
| 149 submit_machine_id_ = submit_machine_id; | |
| 150 } | |
| 151 | |
| 152 void set_last_policy_timestamp(const base::Time& timestamp) { | |
| 153 last_policy_timestamp_ = timestamp; | |
| 154 } | |
| 155 | |
| 156 void set_public_key_version(int public_key_version) { | |
| 157 public_key_version_ = public_key_version; | |
| 158 public_key_version_valid_ = true; | |
| 159 } | |
| 160 | |
| 161 void clear_public_key_version() { | |
| 162 public_key_version_valid_ = false; | |
| 163 } | |
| 164 | |
| 165 // FetchPolicy() calls will request this policy namespace. | |
| 166 void AddNamespaceToFetch(const PolicyNamespaceKey& policy_ns_key); | |
| 167 | |
| 168 // FetchPolicy() calls won't request the given policy namespace anymore. | |
| 169 void RemoveNamespaceToFetch(const PolicyNamespaceKey& policy_ns_key); | |
| 170 | |
| 171 // Whether the client is registered with the device management service. | |
| 172 bool is_registered() const { return !dm_token_.empty(); } | |
| 173 | |
| 174 const std::string& dm_token() const { return dm_token_; } | |
| 175 const std::string& client_id() const { return client_id_; } | |
| 176 | |
| 177 // The device mode as received in the registration request. | |
| 178 DeviceMode device_mode() const { return device_mode_; } | |
| 179 | |
| 180 // The policy responses as obtained by the last request to the cloud. These | |
| 181 // policies haven't gone through verification, so their contents cannot be | |
| 182 // trusted. Use CloudPolicyStore::policy() and CloudPolicyStore::policy_map() | |
| 183 // instead for making policy decisions. | |
| 184 const ResponseMap& responses() const { | |
| 185 return responses_; | |
| 186 } | |
| 187 | |
| 188 // Returns the policy response for |policy_ns_key|, if found in |responses()|; | |
| 189 // otherwise returns NULL. | |
| 190 const enterprise_management::PolicyFetchResponse* GetPolicyFor( | |
| 191 const PolicyNamespaceKey& policy_ns_key) const; | |
| 192 | |
| 193 DeviceManagementStatus status() const { | |
| 194 return status_; | |
| 195 } | |
| 196 | |
| 197 const std::string& robot_api_auth_code() const { | |
| 198 return robot_api_auth_code_; | |
| 199 } | |
| 200 | |
| 201 // Returns the invalidation version that was used for the last FetchPolicy. | |
| 202 // Observers can call this method from their OnPolicyFetched method to | |
| 203 // determine which at which invalidation version the policy was fetched. | |
| 204 int64 fetched_invalidation_version() const { | |
| 205 return fetched_invalidation_version_; | |
| 206 } | |
| 207 | |
| 208 scoped_refptr<net::URLRequestContextGetter> GetRequestContext(); | |
| 209 | |
| 210 protected: | |
| 211 // A set of PolicyNamespaceKeys to fetch. | |
| 212 typedef std::set<PolicyNamespaceKey> NamespaceSet; | |
| 213 | |
| 214 // Callback for retries of registration requests. | |
| 215 void OnRetryRegister(DeviceManagementRequestJob* job); | |
| 216 | |
| 217 // Callback for registration requests. | |
| 218 void OnRegisterCompleted( | |
| 219 DeviceManagementStatus status, | |
| 220 int net_error, | |
| 221 const enterprise_management::DeviceManagementResponse& response); | |
| 222 | |
| 223 // Callback for policy fetch requests. | |
| 224 void OnPolicyFetchCompleted( | |
| 225 DeviceManagementStatus status, | |
| 226 int net_error, | |
| 227 const enterprise_management::DeviceManagementResponse& response); | |
| 228 | |
| 229 // Callback for robot account api authorization requests. | |
| 230 void OnFetchRobotAuthCodesCompleted( | |
| 231 DeviceManagementStatus status, | |
| 232 int net_error, | |
| 233 const enterprise_management::DeviceManagementResponse& response); | |
| 234 | |
| 235 // Callback for unregistration requests. | |
| 236 void OnUnregisterCompleted( | |
| 237 DeviceManagementStatus status, | |
| 238 int net_error, | |
| 239 const enterprise_management::DeviceManagementResponse& response); | |
| 240 | |
| 241 // Callback for certificate upload requests. | |
| 242 void OnCertificateUploadCompleted( | |
| 243 const StatusCallback& callback, | |
| 244 DeviceManagementStatus status, | |
| 245 int net_error, | |
| 246 const enterprise_management::DeviceManagementResponse& response); | |
| 247 | |
| 248 // Observer notification helpers. | |
| 249 void NotifyPolicyFetched(); | |
| 250 void NotifyRegistrationStateChanged(); | |
| 251 void NotifyRobotAuthCodesFetched(); | |
| 252 void NotifyClientError(); | |
| 253 | |
| 254 // Data necessary for constructing policy requests. | |
| 255 const std::string machine_id_; | |
| 256 const std::string machine_model_; | |
| 257 const UserAffiliation user_affiliation_; | |
| 258 NamespaceSet namespaces_to_fetch_; | |
| 259 | |
| 260 std::string dm_token_; | |
| 261 DeviceMode device_mode_; | |
| 262 std::string client_id_; | |
| 263 bool submit_machine_id_; | |
| 264 base::Time last_policy_timestamp_; | |
| 265 int public_key_version_; | |
| 266 bool public_key_version_valid_; | |
| 267 std::string robot_api_auth_code_; | |
| 268 | |
| 269 // Information for the latest policy invalidation received. | |
| 270 int64 invalidation_version_; | |
| 271 std::string invalidation_payload_; | |
| 272 | |
| 273 // The invalidation version used for the most recent fetch operation. | |
| 274 int64 fetched_invalidation_version_; | |
| 275 | |
| 276 // Used for issuing requests to the cloud. | |
| 277 DeviceManagementService* service_; | |
| 278 scoped_ptr<DeviceManagementRequestJob> request_job_; | |
| 279 | |
| 280 // Status upload data is produced by |status_provider_|. | |
| 281 StatusProvider* status_provider_; | |
| 282 | |
| 283 // The policy responses returned by the last policy fetch operation. | |
| 284 ResponseMap responses_; | |
| 285 DeviceManagementStatus status_; | |
| 286 | |
| 287 ObserverList<Observer, true> observers_; | |
| 288 scoped_refptr<net::URLRequestContextGetter> request_context_; | |
| 289 | |
| 290 private: | |
| 291 DISALLOW_COPY_AND_ASSIGN(CloudPolicyClient); | |
| 292 }; | |
| 293 | |
| 294 } // namespace policy | |
| 295 | |
| 296 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_CLIENT_H_ | |
| OLD | NEW |