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