| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/chromeos/policy/cloud_external_data_manager_base.h" | 5 #include "chrome/browser/chromeos/policy/cloud_external_data_manager_base.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/callback.h" |
| 13 #include "base/location.h" | 14 #include "base/location.h" |
| 14 #include "base/logging.h" | 15 #include "base/logging.h" |
| 15 #include "base/message_loop/message_loop_proxy.h" | 16 #include "base/message_loop/message_loop_proxy.h" |
| 16 #include "base/sequenced_task_runner.h" | 17 #include "base/sequenced_task_runner.h" |
| 17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/values.h" | 19 #include "base/values.h" |
| 19 #include "chrome/browser/chromeos/policy/cloud_external_data_store.h" | 20 #include "chrome/browser/chromeos/policy/cloud_external_data_store.h" |
| 20 #include "chrome/browser/policy/cloud/cloud_policy_store.h" | 21 #include "chrome/browser/policy/cloud/cloud_policy_store.h" |
| 21 #include "chrome/browser/policy/cloud/external_policy_data_fetcher.h" | 22 #include "chrome/browser/policy/cloud/external_policy_data_fetcher.h" |
| 22 #include "chrome/browser/policy/cloud/external_policy_data_updater.h" | 23 #include "chrome/browser/policy/cloud/external_policy_data_updater.h" |
| 23 #include "chrome/browser/policy/external_data_fetcher.h" | 24 #include "chrome/browser/policy/external_data_fetcher.h" |
| 24 #include "chrome/browser/policy/policy_map.h" | 25 #include "chrome/browser/policy/policy_map.h" |
| 25 #include "net/url_request/url_request_context_getter.h" | 26 #include "net/url_request/url_request_context_getter.h" |
| 26 #include "policy/policy_constants.h" | |
| 27 | 27 |
| 28 namespace policy { | 28 namespace policy { |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 // Fetch data for at most two external data references at the same time. | 32 // Fetch data for at most two external data references at the same time. |
| 33 const int kMaxParallelFetches = 2; | 33 const int kMaxParallelFetches = 2; |
| 34 | 34 |
| 35 // Allows policies to reference |max_external_data_size_for_testing| bytes of | 35 // Allows policies to reference |max_external_data_size_for_testing| bytes of |
| 36 // external data even if no |max_size| was specified in policy_templates.json. | 36 // external data even if no |max_size| was specified in policy_templates.json. |
| 37 int max_external_data_size_for_testing = 0; | 37 int max_external_data_size_for_testing = 0; |
| 38 | 38 |
| 39 } // namespace | 39 } // namespace |
| 40 | 40 |
| 41 // Backend for the CloudExternalDataManagerBase that handles all data download, | 41 // Backend for the CloudExternalDataManagerBase that handles all data download, |
| 42 // verification, caching and retrieval. | 42 // verification, caching and retrieval. |
| 43 class CloudExternalDataManagerBase::Backend { | 43 class CloudExternalDataManagerBase::Backend { |
| 44 public: | 44 public: |
| 45 // The |policy_definitions| are used to determine the maximum size that the | 45 // |get_policy_details| is used to determine the maximum size that the |
| 46 // data referenced by each policy can have. This class can be instantiated on | 46 // data referenced by each policy can have. This class can be instantiated on |
| 47 // any thread but from then on, may be accessed via the |task_runner_| only. | 47 // any thread but from then on, may be accessed via the |task_runner_| only. |
| 48 // All FetchCallbacks will be invoked via |callback_task_runner|. | 48 // All FetchCallbacks will be invoked via |callback_task_runner|. |
| 49 Backend(const PolicyDefinitionList* policy_definitions, | 49 Backend(const GetChromePolicyDetailsCallback& get_policy_details, |
| 50 scoped_refptr<base::SequencedTaskRunner> task_runner, | 50 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 51 scoped_refptr<base::SequencedTaskRunner> callback_task_runner); | 51 scoped_refptr<base::SequencedTaskRunner> callback_task_runner); |
| 52 | 52 |
| 53 // Allows downloaded external data to be cached in |external_data_store|. | 53 // Allows downloaded external data to be cached in |external_data_store|. |
| 54 // Ownership of the store is taken. The store can be destroyed by calling | 54 // Ownership of the store is taken. The store can be destroyed by calling |
| 55 // SetExternalDataStore(scoped_ptr<CloudExternalDataStore>()). | 55 // SetExternalDataStore(scoped_ptr<CloudExternalDataStore>()). |
| 56 void SetExternalDataStore( | 56 void SetExternalDataStore( |
| 57 scoped_ptr<CloudExternalDataStore> external_data_store); | 57 scoped_ptr<CloudExternalDataStore> external_data_store); |
| 58 | 58 |
| 59 // Allows downloading of external data via the |external_policy_data_fetcher|. | 59 // Allows downloading of external data via the |external_policy_data_fetcher|. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 void FetchAll(); | 94 void FetchAll(); |
| 95 | 95 |
| 96 private: | 96 private: |
| 97 // List of callbacks to invoke when the attempt to retrieve external data | 97 // List of callbacks to invoke when the attempt to retrieve external data |
| 98 // referenced by a policy completes successfully or fails permanently. | 98 // referenced by a policy completes successfully or fails permanently. |
| 99 typedef std::vector<ExternalDataFetcher::FetchCallback> FetchCallbackList; | 99 typedef std::vector<ExternalDataFetcher::FetchCallback> FetchCallbackList; |
| 100 | 100 |
| 101 // Map from policy names to the lists of callbacks defined above. | 101 // Map from policy names to the lists of callbacks defined above. |
| 102 typedef std::map<std::string, FetchCallbackList> FetchCallbackMap; | 102 typedef std::map<std::string, FetchCallbackList> FetchCallbackMap; |
| 103 | 103 |
| 104 // Looks up the maximum size that the data referenced by |policy| can have in | 104 // Looks up the maximum size that the data referenced by |policy| can have. |
| 105 // |policy_definitions_|. | |
| 106 size_t GetMaxExternalDataSize(const std::string& policy) const; | 105 size_t GetMaxExternalDataSize(const std::string& policy) const; |
| 107 | 106 |
| 108 // Invokes |callback| via the |callback_task_runner_|, passing |data| as a | 107 // Invokes |callback| via the |callback_task_runner_|, passing |data| as a |
| 109 // parameter. | 108 // parameter. |
| 110 void RunCallback(const ExternalDataFetcher::FetchCallback& callback, | 109 void RunCallback(const ExternalDataFetcher::FetchCallback& callback, |
| 111 scoped_ptr<std::string> data) const; | 110 scoped_ptr<std::string> data) const; |
| 112 | 111 |
| 113 // Tells the |updater_| to download the external data referenced by |policy|. | 112 // Tells the |updater_| to download the external data referenced by |policy|. |
| 114 // If Connect() was not called yet and no |updater_| exists, does nothing. | 113 // If Connect() was not called yet and no |updater_| exists, does nothing. |
| 115 void StartDownload(const std::string& policy); | 114 void StartDownload(const std::string& policy); |
| 116 | 115 |
| 117 // Used to determine the maximum size that the data referenced by each policy | 116 // Used to determine the maximum size that the data referenced by each policy |
| 118 // can have. | 117 // can have. |
| 119 const PolicyDefinitionList* policy_definitions_; | 118 GetChromePolicyDetailsCallback get_policy_details_; |
| 120 | 119 |
| 121 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 120 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 122 scoped_refptr<base::SequencedTaskRunner> callback_task_runner_; | 121 scoped_refptr<base::SequencedTaskRunner> callback_task_runner_; |
| 123 | 122 |
| 124 // Contains the policies for which a download of the referenced external data | 123 // Contains the policies for which a download of the referenced external data |
| 125 // has been requested. Each policy is mapped to a list of callbacks to invoke | 124 // has been requested. Each policy is mapped to a list of callbacks to invoke |
| 126 // when the download completes successfully or fails permanently. If no | 125 // when the download completes successfully or fails permanently. If no |
| 127 // callback needs to be invoked (because the download was requested via | 126 // callback needs to be invoked (because the download was requested via |
| 128 // FetchAll()), a map entry will still exist but the list of callbacks it maps | 127 // FetchAll()), a map entry will still exist but the list of callbacks it maps |
| 129 // to will be empty. | 128 // to will be empty. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 140 // Used to cache external data referenced by policies. | 139 // Used to cache external data referenced by policies. |
| 141 scoped_ptr<CloudExternalDataStore> external_data_store_; | 140 scoped_ptr<CloudExternalDataStore> external_data_store_; |
| 142 | 141 |
| 143 // Used to download external data referenced by policies. | 142 // Used to download external data referenced by policies. |
| 144 scoped_ptr<ExternalPolicyDataUpdater> updater_; | 143 scoped_ptr<ExternalPolicyDataUpdater> updater_; |
| 145 | 144 |
| 146 DISALLOW_COPY_AND_ASSIGN(Backend); | 145 DISALLOW_COPY_AND_ASSIGN(Backend); |
| 147 }; | 146 }; |
| 148 | 147 |
| 149 CloudExternalDataManagerBase::Backend::Backend( | 148 CloudExternalDataManagerBase::Backend::Backend( |
| 150 const PolicyDefinitionList* policy_definitions, | 149 const GetChromePolicyDetailsCallback& get_policy_details, |
| 151 scoped_refptr<base::SequencedTaskRunner> task_runner, | 150 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 152 scoped_refptr<base::SequencedTaskRunner> callback_task_runner) | 151 scoped_refptr<base::SequencedTaskRunner> callback_task_runner) |
| 153 : policy_definitions_(policy_definitions), | 152 : get_policy_details_(get_policy_details), |
| 154 task_runner_(task_runner), | 153 task_runner_(task_runner), |
| 155 callback_task_runner_(callback_task_runner), | 154 callback_task_runner_(callback_task_runner), |
| 156 metadata_set_(false) { | 155 metadata_set_(false) { |
| 157 } | 156 } |
| 158 | 157 |
| 159 void CloudExternalDataManagerBase::Backend::SetExternalDataStore( | 158 void CloudExternalDataManagerBase::Backend::SetExternalDataStore( |
| 160 scoped_ptr<CloudExternalDataStore> external_data_store) { | 159 scoped_ptr<CloudExternalDataStore> external_data_store) { |
| 161 external_data_store_.reset(external_data_store.release()); | 160 external_data_store_.reset(external_data_store.release()); |
| 162 if (metadata_set_ && external_data_store_) | 161 if (metadata_set_ && external_data_store_) |
| 163 external_data_store_->Prune(metadata_); | 162 external_data_store_->Prune(metadata_); |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 StartDownload(policy); | 293 StartDownload(policy); |
| 295 } | 294 } |
| 296 } | 295 } |
| 297 | 296 |
| 298 size_t CloudExternalDataManagerBase::Backend::GetMaxExternalDataSize( | 297 size_t CloudExternalDataManagerBase::Backend::GetMaxExternalDataSize( |
| 299 const std::string& policy) const { | 298 const std::string& policy) const { |
| 300 if (max_external_data_size_for_testing) | 299 if (max_external_data_size_for_testing) |
| 301 return max_external_data_size_for_testing; | 300 return max_external_data_size_for_testing; |
| 302 | 301 |
| 303 // Look up the maximum size that the data referenced by |policy| can have in | 302 // Look up the maximum size that the data referenced by |policy| can have in |
| 304 // policy_definitions_, which is constructed from the information in | 303 // get_policy_details, which is constructed from the information in |
| 305 // policy_templates.json, allowing the maximum data size to be specified as | 304 // policy_templates.json, allowing the maximum data size to be specified as |
| 306 // part of the policy definition. | 305 // part of the policy definition. |
| 307 for (const PolicyDefinitionList::Entry* entry = policy_definitions_->begin; | 306 const PolicyDetails* details = get_policy_details_.Run(policy); |
| 308 entry != policy_definitions_->end; ++entry) { | 307 if (details) |
| 309 if (entry->name == policy) | 308 return details->max_external_data_size; |
| 310 return entry->max_external_data_size; | |
| 311 } | |
| 312 NOTREACHED(); | 309 NOTREACHED(); |
| 313 return 0; | 310 return 0; |
| 314 } | 311 } |
| 315 | 312 |
| 316 void CloudExternalDataManagerBase::Backend::RunCallback( | 313 void CloudExternalDataManagerBase::Backend::RunCallback( |
| 317 const ExternalDataFetcher::FetchCallback& callback, | 314 const ExternalDataFetcher::FetchCallback& callback, |
| 318 scoped_ptr<std::string> data) const { | 315 scoped_ptr<std::string> data) const { |
| 319 callback_task_runner_->PostTask(FROM_HERE, | 316 callback_task_runner_->PostTask(FROM_HERE, |
| 320 base::Bind(callback, base::Passed(&data))); | 317 base::Bind(callback, base::Passed(&data))); |
| 321 } | 318 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 332 ExternalPolicyDataUpdater::Request(metadata.url, | 329 ExternalPolicyDataUpdater::Request(metadata.url, |
| 333 metadata.hash, | 330 metadata.hash, |
| 334 GetMaxExternalDataSize(policy)), | 331 GetMaxExternalDataSize(policy)), |
| 335 base::Bind(&CloudExternalDataManagerBase::Backend::OnDownloadSuccess, | 332 base::Bind(&CloudExternalDataManagerBase::Backend::OnDownloadSuccess, |
| 336 base::Unretained(this), | 333 base::Unretained(this), |
| 337 policy, | 334 policy, |
| 338 metadata.hash)); | 335 metadata.hash)); |
| 339 } | 336 } |
| 340 | 337 |
| 341 CloudExternalDataManagerBase::CloudExternalDataManagerBase( | 338 CloudExternalDataManagerBase::CloudExternalDataManagerBase( |
| 342 const PolicyDefinitionList* policy_definitions, | 339 const GetChromePolicyDetailsCallback& get_policy_details, |
| 343 scoped_refptr<base::SequencedTaskRunner> backend_task_runner, | 340 scoped_refptr<base::SequencedTaskRunner> backend_task_runner, |
| 344 scoped_refptr<base::SequencedTaskRunner> io_task_runner) | 341 scoped_refptr<base::SequencedTaskRunner> io_task_runner) |
| 345 : backend_task_runner_(backend_task_runner), | 342 : backend_task_runner_(backend_task_runner), |
| 346 io_task_runner_(io_task_runner), | 343 io_task_runner_(io_task_runner), |
| 347 backend_(new Backend(policy_definitions, | 344 backend_(new Backend(get_policy_details, |
| 348 backend_task_runner_, | 345 backend_task_runner_, |
| 349 base::MessageLoopProxy::current())) { | 346 base::MessageLoopProxy::current())) { |
| 350 } | 347 } |
| 351 | 348 |
| 352 CloudExternalDataManagerBase::~CloudExternalDataManagerBase() { | 349 CloudExternalDataManagerBase::~CloudExternalDataManagerBase() { |
| 353 DCHECK(CalledOnValidThread()); | 350 DCHECK(CalledOnValidThread()); |
| 354 io_task_runner_->DeleteSoon(FROM_HERE, | 351 io_task_runner_->DeleteSoon(FROM_HERE, |
| 355 external_policy_data_fetcher_backend_.release()); | 352 external_policy_data_fetcher_backend_.release()); |
| 356 backend_task_runner_->DeleteSoon(FROM_HERE, backend_.release()); | 353 backend_task_runner_->DeleteSoon(FROM_HERE, backend_.release()); |
| 357 } | 354 } |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 max_external_data_size_for_testing = max_size; | 440 max_external_data_size_for_testing = max_size; |
| 444 } | 441 } |
| 445 | 442 |
| 446 void CloudExternalDataManagerBase::FetchAll() { | 443 void CloudExternalDataManagerBase::FetchAll() { |
| 447 DCHECK(CalledOnValidThread()); | 444 DCHECK(CalledOnValidThread()); |
| 448 backend_task_runner_->PostTask(FROM_HERE, base::Bind( | 445 backend_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 449 &Backend::FetchAll, base::Unretained(backend_.get()))); | 446 &Backend::FetchAll, base::Unretained(backend_.get()))); |
| 450 } | 447 } |
| 451 | 448 |
| 452 } // namespace policy | 449 } // namespace policy |
| OLD | NEW |