Chromium Code Reviews| 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 #pragma once | |
| 8 | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/observer_list.h" | |
| 14 #include "base/time.h" | |
| 15 #include "chrome/browser/policy/cloud_policy_constants.h" | |
| 16 #include "chrome/browser/policy/policy_constants.h" | |
| 17 | |
| 18 namespace enterprise_management { | |
| 19 class DeviceManagementResponse; | |
| 20 class DeviceRegisterRequest; | |
| 21 class DeviceStatusReportRequest; | |
| 22 class PolicyFetchRequest; | |
| 23 class PolicyFetchResponse; | |
| 24 class SessionStatusReportRequest; | |
| 25 } | |
| 26 | |
| 27 namespace policy { | |
| 28 | |
| 29 class DeviceManagementRequestJob; | |
| 30 class DeviceManagementService; | |
| 31 class MockCloudPolicyClient; | |
|
Joao da Silva
2012/05/22 20:59:30
Nit: not needed
Mattias Nissler (ping if slow)
2012/05/24 10:12:01
Done.
| |
| 32 | |
| 33 // Implements the core logic required to talk to the device management service. | |
| 34 // Also keeps track of the current state of the association with the service, | |
| 35 // such as whether there is a valid registration (DMToken is present in that | |
| 36 // case) and whether and what errors occurred in the latest request. | |
| 37 // | |
| 38 // Note that CloudPolicyClient doesn't do any validation of policy responses | |
| 39 // such as signature and time stamp checks. These happen once the policy gets | |
| 40 // installed in the cloud policy cache. | |
| 41 class CloudPolicyClient { | |
| 42 public: | |
| 43 // Container for public key version and validity flag. | |
| 44 class PublicKeyVersion { | |
| 45 public: | |
| 46 // Constructs and invalid public key version. | |
|
Joao da Silva
2012/05/22 20:59:30
Nit: s/and/an
Mattias Nissler (ping if slow)
2012/05/24 10:12:01
Obsolete. I removed this class entirely, it turned
| |
| 47 PublicKeyVersion(); | |
| 48 // Create a valid public key version object. | |
| 49 explicit PublicKeyVersion(int version); | |
| 50 | |
| 51 // Copy constructor and assignment operator so this can be passed by value. | |
| 52 explicit PublicKeyVersion(const PublicKeyVersion& other); | |
| 53 const PublicKeyVersion& operator=(const PublicKeyVersion& other); | |
| 54 | |
| 55 bool valid() const { return valid_; } | |
| 56 int version() const { return version_; } | |
| 57 | |
| 58 private: | |
| 59 bool valid_; | |
| 60 int version_; | |
| 61 }; | |
| 62 | |
| 63 // Observer interface for state and policy changes. | |
| 64 class Observer { | |
| 65 public: | |
| 66 virtual ~Observer(); | |
| 67 | |
| 68 // Called when a policy fetch completes successfully. If a policy fetch | |
| 69 // triggers an error, OnClientError() will fire. | |
| 70 virtual void OnPolicyFetched(CloudPolicyClient* client) = 0; | |
| 71 | |
| 72 // Called upon registration state changes. This callback is invoked for | |
| 73 // successful completion of registration and unregistration requests. | |
| 74 virtual void OnRegistrationStateChanged(CloudPolicyClient* client) = 0; | |
| 75 | |
| 76 // Indicates there's been an error in a previously-issued request. | |
| 77 virtual void OnClientError(CloudPolicyClient* client) = 0; | |
| 78 }; | |
| 79 | |
| 80 // Delegate interface for supplying status information to upload to the server | |
| 81 // as part of the policy fetch request. | |
| 82 class StatusProvider { | |
| 83 public: | |
| 84 virtual ~StatusProvider(); | |
| 85 | |
| 86 // Retrieves status information to send with the next policy fetch. | |
| 87 // Implementations must return true if status information was filled in. | |
| 88 virtual bool GetDeviceStatus( | |
| 89 enterprise_management::DeviceStatusReportRequest* status) = 0; | |
| 90 virtual bool GetSessionStatus( | |
| 91 enterprise_management::SessionStatusReportRequest* status) = 0; | |
| 92 | |
| 93 // Called after the status information has successfully been submitted to | |
| 94 // the server. | |
| 95 virtual void OnSubmittedSuccessfully() = 0; | |
| 96 }; | |
| 97 | |
| 98 // |provider| and |service| are weak pointers and it's the caller's | |
| 99 // responsibility to keep them valid for the lifetime of CloudPolicyClient. | |
| 100 CloudPolicyClient(const std::string& machine_id, | |
| 101 const std::string& machine_model, | |
| 102 UserAffiliation user_affiliation, | |
| 103 PolicyScope scope, | |
| 104 StatusProvider* provider, | |
| 105 DeviceManagementService* service); | |
| 106 virtual ~CloudPolicyClient(); | |
| 107 | |
| 108 // Sets the DMToken, thereby establishing a registration with the server. A | |
| 109 // policy fetch is not automatically issued but can be requested by calling | |
| 110 // FetchPolicy(). | |
| 111 virtual void SetupRegistration(const std::string& dm_token, | |
| 112 const std::string& client_id); | |
| 113 | |
| 114 // Attempts to register with the device management service. Results in a | |
| 115 // registration change or error notification. | |
| 116 virtual void Register(const std::string& auth_token); | |
| 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 // Sends an unregistration request to the server. | |
| 128 virtual void Unregister(); | |
| 129 | |
| 130 // Adds an observer to be called back upon policy and state changes. | |
| 131 void AddObserver(Observer* observer); | |
| 132 | |
| 133 // Removes the specified observer. | |
| 134 void RemoveObserver(Observer* observer); | |
| 135 | |
| 136 void set_submit_machine_id(bool submit_machine_id) { | |
| 137 submit_machine_id_ = submit_machine_id; | |
| 138 } | |
| 139 | |
| 140 void set_last_policy_timestamp(const base::Time& timestamp) { | |
| 141 last_policy_timestamp_ = timestamp; | |
| 142 } | |
| 143 | |
| 144 void set_public_key_version(const PublicKeyVersion& public_key_version) { | |
| 145 public_key_version_ = public_key_version; | |
| 146 } | |
| 147 | |
| 148 // Whether the client is registered with the device management service. | |
| 149 bool is_registered() const { return !dm_token_.empty(); } | |
| 150 | |
| 151 // The policy response as obtained by the last request to the cloud. This | |
| 152 // policy blob hasn't gone through verification, so it's contents cannot be | |
| 153 // trusted. Use CloudPolicyStore::policy() and CloudPolicyStore::policy_map() | |
| 154 // instead for making policy decisions. | |
| 155 const enterprise_management::PolicyFetchResponse* policy() const { | |
| 156 return policy_.get(); | |
| 157 } | |
| 158 | |
| 159 DeviceManagementStatus status() const { | |
| 160 return status_; | |
| 161 } | |
| 162 | |
| 163 protected: | |
| 164 // Sets the registration type suitable for the policy scope used. | |
| 165 void SetRegistrationType( | |
| 166 enterprise_management::DeviceRegisterRequest* request) const; | |
| 167 | |
| 168 // Sets the appropriate policy type in the fetch request. | |
| 169 void SetPolicyType(enterprise_management::PolicyFetchRequest* request) const; | |
| 170 | |
| 171 // Callback for registration requests. | |
| 172 void OnRegisterCompleted( | |
| 173 DeviceManagementStatus status, | |
| 174 const enterprise_management::DeviceManagementResponse& response); | |
| 175 | |
| 176 // Callback for policy fetch requests. | |
| 177 void OnPolicyFetchCompleted( | |
| 178 DeviceManagementStatus status, | |
| 179 const enterprise_management::DeviceManagementResponse& response); | |
| 180 | |
| 181 // Callback for unregistration requests. | |
| 182 void OnUnregisterCompleted( | |
| 183 DeviceManagementStatus status, | |
| 184 const enterprise_management::DeviceManagementResponse& response); | |
| 185 | |
| 186 // Observer notification helpers. | |
| 187 void NotifyPolicyFetched(); | |
| 188 void NotifyRegistrationStateChanged(); | |
| 189 void NotifyClientError(); | |
| 190 | |
| 191 // Data necessary for constructing policy requests. | |
| 192 const std::string machine_id_; | |
| 193 const std::string machine_model_; | |
| 194 const UserAffiliation user_affiliation_; | |
| 195 const PolicyScope scope_; | |
| 196 | |
| 197 std::string dm_token_; | |
| 198 std::string client_id_; | |
| 199 bool submit_machine_id_; | |
| 200 base::Time last_policy_timestamp_; | |
| 201 PublicKeyVersion public_key_version_; | |
| 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 blob returned by the last policy fetch operation. | |
| 211 scoped_ptr<enterprise_management::PolicyFetchResponse> policy_; | |
| 212 DeviceManagementStatus status_; | |
| 213 | |
| 214 ObserverList<Observer, true> observers_; | |
| 215 | |
|
Joao da Silva
2012/05/22 20:59:30
private:
Mattias Nissler (ping if slow)
2012/05/24 10:12:01
Done.
| |
| 216 DISALLOW_COPY_AND_ASSIGN(CloudPolicyClient); | |
| 217 }; | |
| 218 | |
| 219 } // namespace policy | |
| 220 | |
| 221 #endif // CHROME_BROWSER_POLICY_CLOUD_POLICY_CLIENT_H_ | |
| OLD | NEW |