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 #include "chrome/browser/policy/cloud_policy_client.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "chrome/browser/policy/device_management_service.h" | |
| 10 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
| 11 #include "chrome/common/guid.h" | |
| 12 | |
| 13 namespace em = enterprise_management; | |
| 14 | |
| 15 namespace policy { | |
| 16 | |
| 17 CloudPolicyClient::PublicKeyVersion::PublicKeyVersion() | |
| 18 : valid_(false), | |
| 19 version_(-1) {} | |
| 20 | |
| 21 CloudPolicyClient::PublicKeyVersion::PublicKeyVersion(int version) | |
| 22 : valid_(true), | |
| 23 version_(version) {} | |
| 24 | |
| 25 CloudPolicyClient::PublicKeyVersion::PublicKeyVersion( | |
| 26 const PublicKeyVersion& other) | |
| 27 : valid_(other.valid_), | |
| 28 version_(other.version_) {} | |
| 29 | |
| 30 const CloudPolicyClient::PublicKeyVersion& | |
| 31 CloudPolicyClient::PublicKeyVersion::operator=( | |
| 32 const PublicKeyVersion& other) { | |
| 33 valid_ = other.valid_; | |
| 34 version_ = other.version_; | |
| 35 return *this; | |
| 36 } | |
| 37 | |
| 38 CloudPolicyClient::Observer::~Observer() {} | |
| 39 | |
| 40 CloudPolicyClient::StatusProvider::~StatusProvider() {} | |
| 41 | |
| 42 CloudPolicyClient::CloudPolicyClient(const std::string& machine_id, | |
| 43 const std::string& machine_model, | |
| 44 UserAffiliation user_affiliation, | |
| 45 PolicyScope scope, | |
| 46 StatusProvider* status_provider, | |
| 47 DeviceManagementService* service) | |
| 48 : machine_id_(machine_id), | |
| 49 machine_model_(machine_model), | |
| 50 user_affiliation_(user_affiliation), | |
| 51 scope_(scope), | |
| 52 submit_machine_id_(false), | |
| 53 service_(service), | |
| 54 request_job_(NULL), | |
| 55 status_provider_(status_provider), | |
| 56 status_(DM_STATUS_SUCCESS) {} | |
| 57 | |
| 58 CloudPolicyClient::~CloudPolicyClient() {} | |
| 59 | |
| 60 void CloudPolicyClient::SetupRegistration(const std::string& dm_token, | |
| 61 const std::string& client_id) { | |
| 62 DCHECK(!dm_token.empty()); | |
| 63 DCHECK(!client_id.empty()); | |
| 64 | |
| 65 dm_token_ = dm_token; | |
| 66 client_id_ = client_id; | |
| 67 request_job_.reset(); | |
| 68 policy_.reset(); | |
| 69 | |
| 70 NotifyRegistrationStateChanged(); | |
| 71 } | |
| 72 | |
| 73 void CloudPolicyClient::Register(const std::string& auth_token) { | |
| 74 client_id_ = guid::GenerateGUID(); | |
|
Joao da Silva
2012/04/16 23:45:00
CHECK(dm_token.empty())?
Mattias Nissler (ping if slow)
2012/05/22 14:05:26
Probably a good idea. We probably want to support
| |
| 75 | |
| 76 request_job_.reset( | |
| 77 service_->CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION)); | |
| 78 request_job_->SetOAuthToken(auth_token); | |
| 79 request_job_->SetClientID(client_id_); | |
| 80 | |
| 81 em::DeviceRegisterRequest* request = | |
| 82 request_job_->GetRequest()->mutable_register_request(); | |
| 83 SetRegistrationType(request); | |
| 84 if (!machine_id_.empty()) | |
| 85 request->set_machine_id(machine_id_); | |
| 86 if (!machine_model_.empty()) | |
| 87 request->set_machine_model(machine_model_); | |
| 88 | |
| 89 request_job_->Start(base::Bind(&CloudPolicyClient::OnRegisterCompleted, | |
| 90 base::Unretained(this))); | |
| 91 } | |
| 92 | |
| 93 void CloudPolicyClient::FetchPolicy() { | |
| 94 CHECK(!dm_token_.empty()); | |
| 95 | |
| 96 request_job_.reset( | |
| 97 service_->CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)); | |
| 98 request_job_->SetDMToken(dm_token_); | |
| 99 request_job_->SetClientID(client_id_); | |
| 100 request_job_->SetUserAffiliation(user_affiliation_); | |
| 101 | |
| 102 em::DeviceManagementRequest* request = request_job_->GetRequest(); | |
| 103 | |
| 104 // Build policy fetch request. | |
| 105 em::PolicyFetchRequest* policy_request = | |
| 106 request->mutable_policy_request()->add_request(); | |
| 107 policy_request->set_signature_type(em::PolicyFetchRequest::SHA1_RSA); | |
| 108 SetPolicyType(policy_request); | |
| 109 if (!last_fetch_timestamp_.is_null()) { | |
| 110 base::TimeDelta timestamp = last_fetch_timestamp_ - base::Time::UnixEpoch(); | |
| 111 policy_request->set_timestamp(timestamp.InMilliseconds()); | |
| 112 } | |
| 113 if (submit_machine_id_ && !machine_id_.empty()) | |
| 114 policy_request->set_machine_id(machine_id_); | |
| 115 if (public_key_version_.valid()) | |
| 116 policy_request->set_public_key_version(public_key_version_.version()); | |
| 117 | |
| 118 // Add status data. | |
| 119 if (status_provider_) { | |
| 120 if (!status_provider_->GetDeviceStatus( | |
| 121 request->mutable_device_status_report_request())) { | |
| 122 request->clear_device_status_report_request(); | |
| 123 } | |
| 124 if (!status_provider_->GetSessionStatus( | |
| 125 request->mutable_session_status_report_request())) { | |
| 126 request->clear_session_status_report_request(); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 // Fire the job. | |
| 131 request_job_->Start(base::Bind(&CloudPolicyClient::OnPolicyFetchCompleted, | |
| 132 base::Unretained(this))); | |
| 133 } | |
| 134 | |
| 135 void CloudPolicyClient::Unregister() { | |
| 136 request_job_.reset( | |
| 137 service_->CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION)); | |
| 138 request_job_->SetDMToken(dm_token_); | |
| 139 request_job_->SetClientID(client_id_); | |
| 140 request_job_->GetRequest()->mutable_unregister_request(); | |
| 141 request_job_->Start(base::Bind(&CloudPolicyClient::OnUnregisterCompleted, | |
| 142 base::Unretained(this))); | |
| 143 } | |
| 144 | |
| 145 void CloudPolicyClient::AddObserver(Observer* observer) { | |
| 146 observers_.AddObserver(observer); | |
| 147 } | |
| 148 | |
| 149 void CloudPolicyClient::RemoveObserver(Observer* observer) { | |
| 150 observers_.RemoveObserver(observer); | |
| 151 } | |
| 152 | |
| 153 void CloudPolicyClient::SetRegistrationType( | |
| 154 em::DeviceRegisterRequest* request) const { | |
| 155 switch (scope_) { | |
| 156 case POLICY_SCOPE_USER: | |
| 157 request->set_type(em::DeviceRegisterRequest::USER); | |
| 158 return; | |
| 159 case POLICY_SCOPE_DEVICE: | |
| 160 request->set_type(em::DeviceRegisterRequest::DEVICE); | |
| 161 return; | |
| 162 } | |
| 163 NOTREACHED() << "Invalid policy scope " << scope_; | |
| 164 } | |
| 165 | |
| 166 void CloudPolicyClient::SetPolicyType(em::PolicyFetchRequest* request) const { | |
| 167 switch (scope_) { | |
| 168 case POLICY_SCOPE_USER: | |
| 169 request->set_policy_type(dm_protocol::kChromeUserPolicyType); | |
| 170 return; | |
| 171 case POLICY_SCOPE_DEVICE: | |
| 172 request->set_policy_type(dm_protocol::kChromeDevicePolicyType); | |
| 173 return; | |
| 174 } | |
| 175 NOTREACHED() << "Invalid policy scope " << scope_; | |
| 176 } | |
| 177 | |
| 178 void CloudPolicyClient::OnRegisterCompleted( | |
| 179 DeviceManagementStatus status, | |
| 180 const em::DeviceManagementResponse& response) { | |
| 181 if (status == DM_STATUS_SUCCESS && | |
| 182 (!response.has_register_response() || | |
| 183 !response.register_response().has_device_management_token())) { | |
| 184 LOG(WARNING) << "Empty registration response."; | |
| 185 status = DM_STATUS_RESPONSE_DECODING_ERROR; | |
| 186 } | |
| 187 | |
| 188 status_ = status; | |
| 189 if (status == DM_STATUS_SUCCESS) { | |
| 190 dm_token_ = response.register_response().device_management_token(); | |
| 191 NotifyRegistrationStateChanged(); | |
| 192 } else { | |
| 193 NotifyClientError(); | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 void CloudPolicyClient::OnPolicyFetchCompleted( | |
| 198 DeviceManagementStatus status, | |
| 199 const em::DeviceManagementResponse& response) { | |
| 200 if (status == DM_STATUS_SUCCESS) { | |
| 201 if (!response.has_policy_response() || | |
| 202 response.policy_response().response_size() == 0) { | |
| 203 LOG(WARNING) << "Empty policy response."; | |
| 204 status = DM_STATUS_RESPONSE_DECODING_ERROR; | |
| 205 } else if (response.policy_response().response_size() > 1) { | |
| 206 LOG(WARNING) << "More than one response entries, ignoring all but first."; | |
| 207 } | |
| 208 } | |
| 209 | |
| 210 status_ = status; | |
| 211 if (status == DM_STATUS_SUCCESS) { | |
| 212 policy_.reset(new enterprise_management::PolicyFetchResponse( | |
| 213 response.policy_response().response(0))); | |
| 214 if (status_provider_) | |
| 215 status_provider_->OnSubmittedSuccessfully(); | |
| 216 NotifyPolicyFetched(); | |
| 217 } else { | |
| 218 NotifyClientError(); | |
| 219 } | |
| 220 } | |
| 221 | |
| 222 void CloudPolicyClient::OnUnregisterCompleted( | |
| 223 DeviceManagementStatus status, | |
| 224 const em::DeviceManagementResponse& response) { | |
| 225 if (status == DM_STATUS_SUCCESS && !response.has_unregister_response()) { | |
| 226 // Assume unregistration has succeeded either way. | |
| 227 LOG(WARNING) << "Empty unregistration response."; | |
| 228 } | |
| 229 | |
| 230 status_ = status; | |
| 231 if (status == DM_STATUS_SUCCESS) { | |
| 232 dm_token_.clear(); | |
| 233 NotifyRegistrationStateChanged(); | |
| 234 } else { | |
| 235 NotifyClientError(); | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 void CloudPolicyClient::NotifyPolicyFetched() { | |
| 240 FOR_EACH_OBSERVER(Observer, observers_, OnPolicyFetched(this)); | |
| 241 } | |
| 242 | |
| 243 void CloudPolicyClient::NotifyRegistrationStateChanged() { | |
| 244 FOR_EACH_OBSERVER(Observer, observers_, OnRegistrationStateChanged(this)); | |
| 245 } | |
| 246 | |
| 247 void CloudPolicyClient::NotifyClientError() { | |
| 248 FOR_EACH_OBSERVER(Observer, observers_, OnClientError(this)); | |
| 249 } | |
| 250 | |
| 251 } // namespace policy | |
| OLD | NEW |