OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/policy/core/common/cloud/cloud_policy_client.h" | 5 #include "components/policy/core/common/cloud/cloud_policy_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/guid.h" | 8 #include "base/guid.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 : machine_id_(machine_id), | 58 : machine_id_(machine_id), |
59 machine_model_(machine_model), | 59 machine_model_(machine_model), |
60 verification_key_hash_(verification_key_hash), | 60 verification_key_hash_(verification_key_hash), |
61 user_affiliation_(user_affiliation), | 61 user_affiliation_(user_affiliation), |
62 device_mode_(DEVICE_MODE_NOT_SET), | 62 device_mode_(DEVICE_MODE_NOT_SET), |
63 submit_machine_id_(false), | 63 submit_machine_id_(false), |
64 public_key_version_(-1), | 64 public_key_version_(-1), |
65 public_key_version_valid_(false), | 65 public_key_version_valid_(false), |
66 invalidation_version_(0), | 66 invalidation_version_(0), |
67 fetched_invalidation_version_(0), | 67 fetched_invalidation_version_(0), |
68 service_(service), // Can be NULL for unit tests. | 68 service_(service), // Can be null for unit tests. |
69 status_provider_(status_provider), // Can be NULL for unit tests. | 69 status_provider_(status_provider), // Can be null for unit tests. |
70 status_(DM_STATUS_SUCCESS), | 70 status_(DM_STATUS_SUCCESS), |
71 request_context_(request_context) { | 71 request_context_(request_context) { |
72 } | 72 } |
73 | 73 |
74 CloudPolicyClient::~CloudPolicyClient() { | 74 CloudPolicyClient::~CloudPolicyClient() { |
75 STLDeleteValues(&responses_); | 75 STLDeleteValues(&responses_); |
76 } | 76 } |
77 | 77 |
78 void CloudPolicyClient::SetupRegistration(const std::string& dm_token, | 78 void CloudPolicyClient::SetupRegistration(const std::string& dm_token, |
79 const std::string& client_id) { | 79 const std::string& client_id) { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 | 139 |
140 void CloudPolicyClient::SetInvalidationInfo( | 140 void CloudPolicyClient::SetInvalidationInfo( |
141 int64 version, | 141 int64 version, |
142 const std::string& payload) { | 142 const std::string& payload) { |
143 invalidation_version_ = version; | 143 invalidation_version_ = version; |
144 invalidation_payload_ = payload; | 144 invalidation_payload_ = payload; |
145 } | 145 } |
146 | 146 |
147 void CloudPolicyClient::FetchPolicy() { | 147 void CloudPolicyClient::FetchPolicy() { |
148 CHECK(is_registered()); | 148 CHECK(is_registered()); |
149 CHECK(!namespaces_to_fetch_.empty()); | 149 CHECK(!types_to_fetch_.empty()); |
150 | 150 |
151 request_job_.reset( | 151 request_job_.reset( |
152 service_->CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH, | 152 service_->CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH, |
153 GetRequestContext())); | 153 GetRequestContext())); |
154 request_job_->SetDMToken(dm_token_); | 154 request_job_->SetDMToken(dm_token_); |
155 request_job_->SetClientID(client_id_); | 155 request_job_->SetClientID(client_id_); |
156 request_job_->SetUserAffiliation(user_affiliation_); | 156 request_job_->SetUserAffiliation(user_affiliation_); |
157 | 157 |
158 em::DeviceManagementRequest* request = request_job_->GetRequest(); | 158 em::DeviceManagementRequest* request = request_job_->GetRequest(); |
159 | 159 |
160 // Build policy fetch requests. | 160 // Build policy fetch requests. |
161 em::DevicePolicyRequest* policy_request = request->mutable_policy_request(); | 161 em::DevicePolicyRequest* policy_request = request->mutable_policy_request(); |
162 for (NamespaceSet::iterator it = namespaces_to_fetch_.begin(); | 162 for (const auto& type_to_fetch : types_to_fetch_) { |
163 it != namespaces_to_fetch_.end(); ++it) { | |
164 em::PolicyFetchRequest* fetch_request = policy_request->add_request(); | 163 em::PolicyFetchRequest* fetch_request = policy_request->add_request(); |
165 fetch_request->set_policy_type(it->first); | 164 fetch_request->set_policy_type(type_to_fetch.first); |
166 if (!it->second.empty()) | 165 if (!type_to_fetch.second.empty()) |
167 fetch_request->set_settings_entity_id(it->second); | 166 fetch_request->set_settings_entity_id(type_to_fetch.second); |
168 | 167 |
169 // Request signed policy blobs to help prevent tampering on the client. | 168 // Request signed policy blobs to help prevent tampering on the client. |
170 fetch_request->set_signature_type(em::PolicyFetchRequest::SHA1_RSA); | 169 fetch_request->set_signature_type(em::PolicyFetchRequest::SHA1_RSA); |
171 if (public_key_version_valid_) | 170 if (public_key_version_valid_) |
172 fetch_request->set_public_key_version(public_key_version_); | 171 fetch_request->set_public_key_version(public_key_version_); |
173 | 172 |
174 if (!verification_key_hash_.empty()) | 173 if (!verification_key_hash_.empty()) |
175 fetch_request->set_verification_key_hash(verification_key_hash_); | 174 fetch_request->set_verification_key_hash(verification_key_hash_); |
176 | 175 |
177 // These fields are included only in requests for chrome policy. | 176 // These fields are included only in requests for chrome policy. |
178 if (IsChromePolicy(it->first)) { | 177 if (IsChromePolicy(type_to_fetch.first)) { |
Mattias Nissler (ping if slow)
2014/11/27 08:34:10
Side note: We may eventually change this to not ha
Joao da Silva
2014/11/27 12:19:31
Ack
| |
179 if (submit_machine_id_ && !machine_id_.empty()) | 178 if (submit_machine_id_ && !machine_id_.empty()) |
180 fetch_request->set_machine_id(machine_id_); | 179 fetch_request->set_machine_id(machine_id_); |
181 if (!last_policy_timestamp_.is_null()) { | 180 if (!last_policy_timestamp_.is_null()) { |
182 base::TimeDelta timestamp( | 181 base::TimeDelta timestamp( |
183 last_policy_timestamp_ - base::Time::UnixEpoch()); | 182 last_policy_timestamp_ - base::Time::UnixEpoch()); |
184 fetch_request->set_timestamp(timestamp.InMilliseconds()); | 183 fetch_request->set_timestamp(timestamp.InMilliseconds()); |
185 } | 184 } |
186 if (!invalidation_payload_.empty()) { | 185 if (!invalidation_payload_.empty()) { |
187 fetch_request->set_invalidation_version(invalidation_version_); | 186 fetch_request->set_invalidation_version(invalidation_version_); |
188 fetch_request->set_invalidation_payload(invalidation_payload_); | 187 fetch_request->set_invalidation_payload(invalidation_payload_); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 } | 280 } |
282 | 281 |
283 void CloudPolicyClient::AddObserver(Observer* observer) { | 282 void CloudPolicyClient::AddObserver(Observer* observer) { |
284 observers_.AddObserver(observer); | 283 observers_.AddObserver(observer); |
285 } | 284 } |
286 | 285 |
287 void CloudPolicyClient::RemoveObserver(Observer* observer) { | 286 void CloudPolicyClient::RemoveObserver(Observer* observer) { |
288 observers_.RemoveObserver(observer); | 287 observers_.RemoveObserver(observer); |
289 } | 288 } |
290 | 289 |
291 void CloudPolicyClient::AddNamespaceToFetch(const PolicyNamespaceKey& key) { | 290 void CloudPolicyClient::AddPolicyTypeToFetch( |
292 namespaces_to_fetch_.insert(key); | 291 const std::string& policy_type, |
292 const std::string& settings_entity_id) { | |
293 types_to_fetch_.insert(std::make_pair(policy_type, settings_entity_id)); | |
293 } | 294 } |
294 | 295 |
295 void CloudPolicyClient::RemoveNamespaceToFetch(const PolicyNamespaceKey& key) { | 296 void CloudPolicyClient::RemovePolicyTypeToFetch( |
296 namespaces_to_fetch_.erase(key); | 297 const std::string& policy_type, |
298 const std::string& settings_entity_id) { | |
299 types_to_fetch_.erase(std::make_pair(policy_type, settings_entity_id)); | |
297 } | 300 } |
298 | 301 |
299 void CloudPolicyClient::SetStateKeysToUpload( | 302 void CloudPolicyClient::SetStateKeysToUpload( |
300 const std::vector<std::string>& keys) { | 303 const std::vector<std::string>& keys) { |
301 state_keys_to_upload_ = keys; | 304 state_keys_to_upload_ = keys; |
302 } | 305 } |
303 | 306 |
304 const em::PolicyFetchResponse* CloudPolicyClient::GetPolicyFor( | 307 const em::PolicyFetchResponse* CloudPolicyClient::GetPolicyFor( |
305 const PolicyNamespaceKey& key) const { | 308 const std::string& policy_type, |
306 ResponseMap::const_iterator it = responses_.find(key); | 309 const std::string& settings_entity_id) const { |
307 return it == responses_.end() ? NULL : it->second; | 310 ResponseMap::const_iterator it = |
311 responses_.find(std::make_pair(policy_type, settings_entity_id)); | |
312 return it == responses_.end() ? nullptr : it->second; | |
308 } | 313 } |
309 | 314 |
310 scoped_refptr<net::URLRequestContextGetter> | 315 scoped_refptr<net::URLRequestContextGetter> |
311 CloudPolicyClient::GetRequestContext() { | 316 CloudPolicyClient::GetRequestContext() { |
312 return request_context_; | 317 return request_context_; |
313 } | 318 } |
314 | 319 |
315 void CloudPolicyClient::OnRetryRegister(DeviceManagementRequestJob* job) { | 320 void CloudPolicyClient::OnRetryRegister(DeviceManagementRequestJob* job) { |
316 DCHECK_EQ(request_job_.get(), job); | 321 DCHECK_EQ(request_job_.get(), job); |
317 // If the initial request managed to get to the server but the response didn't | 322 // If the initial request managed to get to the server but the response didn't |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
398 if (!policy_data.ParseFromString(response.policy_data()) || | 403 if (!policy_data.ParseFromString(response.policy_data()) || |
399 !policy_data.IsInitialized() || | 404 !policy_data.IsInitialized() || |
400 !policy_data.has_policy_type()) { | 405 !policy_data.has_policy_type()) { |
401 LOG(WARNING) << "Invalid PolicyData received, ignoring"; | 406 LOG(WARNING) << "Invalid PolicyData received, ignoring"; |
402 continue; | 407 continue; |
403 } | 408 } |
404 const std::string& type = policy_data.policy_type(); | 409 const std::string& type = policy_data.policy_type(); |
405 std::string entity_id; | 410 std::string entity_id; |
406 if (policy_data.has_settings_entity_id()) | 411 if (policy_data.has_settings_entity_id()) |
407 entity_id = policy_data.settings_entity_id(); | 412 entity_id = policy_data.settings_entity_id(); |
408 PolicyNamespaceKey key(type, entity_id); | 413 std::pair<std::string, std::string> key(type, entity_id); |
409 if (ContainsKey(responses_, key)) { | 414 if (ContainsKey(responses_, key)) { |
410 LOG(WARNING) << "Duplicate PolicyFetchResponse for type: " | 415 LOG(WARNING) << "Duplicate PolicyFetchResponse for type: " |
411 << type << ", entity: " << entity_id << ", ignoring"; | 416 << type << ", entity: " << entity_id << ", ignoring"; |
412 continue; | 417 continue; |
413 } | 418 } |
414 responses_[key] = new em::PolicyFetchResponse(response); | 419 responses_[key] = new em::PolicyFetchResponse(response); |
415 } | 420 } |
416 if (status_provider_) | 421 if (status_provider_) |
417 status_provider_->OnSubmittedSuccessfully(); | 422 status_provider_->OnSubmittedSuccessfully(); |
418 state_keys_to_upload_.clear(); | 423 state_keys_to_upload_.clear(); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
470 | 475 |
471 void CloudPolicyClient::NotifyRobotAuthCodesFetched() { | 476 void CloudPolicyClient::NotifyRobotAuthCodesFetched() { |
472 FOR_EACH_OBSERVER(Observer, observers_, OnRobotAuthCodesFetched(this)); | 477 FOR_EACH_OBSERVER(Observer, observers_, OnRobotAuthCodesFetched(this)); |
473 } | 478 } |
474 | 479 |
475 void CloudPolicyClient::NotifyClientError() { | 480 void CloudPolicyClient::NotifyClientError() { |
476 FOR_EACH_OBSERVER(Observer, observers_, OnClientError(this)); | 481 FOR_EACH_OBSERVER(Observer, observers_, OnClientError(this)); |
477 } | 482 } |
478 | 483 |
479 } // namespace policy | 484 } // namespace policy |
OLD | NEW |