OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/policy/cloud/cloud_external_data_manager_base.h" |
| 6 |
| 7 #include <map> |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" |
| 13 #include "base/location.h" |
| 14 #include "base/logging.h" |
| 15 #include "base/sequenced_task_runner.h" |
| 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/values.h" |
| 18 #include "chrome/browser/policy/cloud/cloud_external_data_store.h" |
| 19 #include "chrome/browser/policy/cloud/cloud_policy_store.h" |
| 20 #include "chrome/browser/policy/cloud/external_policy_data_updater.h" |
| 21 #include "chrome/browser/policy/external_data_fetcher.h" |
| 22 #include "chrome/browser/policy/policy_map.h" |
| 23 #include "content/public/browser/browser_thread.h" |
| 24 #include "net/url_request/url_request_context_getter.h" |
| 25 #include "policy/policy_constants.h" |
| 26 |
| 27 namespace policy { |
| 28 |
| 29 namespace { |
| 30 |
| 31 // Fetch data for at most two external data references at the same time. |
| 32 const int kMaxParallelFetches = 2; |
| 33 |
| 34 void RunCallbackOnUIThread(const ExternalDataFetcher::FetchCallback& callback, |
| 35 scoped_ptr<std::string> data) { |
| 36 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 37 callback.Run(data.Pass()); |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
| 42 // Backend for the CloudExternalDataManagerBase that handles all data download, |
| 43 // verification, caching and retrieval. |
| 44 class CloudExternalDataManagerBase::Backend { |
| 45 public: |
| 46 // The |policy_definitions| are used to determine the maximum size that the |
| 47 // data referenced by each policy can have. This class is instantiated on the |
| 48 // UI thread but from then on, is accessed via the |task_runner_| only. |
| 49 Backend(const PolicyDefinitionList* policy_definitions, |
| 50 scoped_refptr<base::SequencedTaskRunner> task_runner); |
| 51 |
| 52 // Allows downloaded external data to be cached in |external_data_store|. |
| 53 // Ownership of the store is taken. The store can be destroyed by calling |
| 54 // SetExternalDataStore(scoped_ptr<CloudExternalDataStore>()). |
| 55 void SetExternalDataStore( |
| 56 scoped_ptr<CloudExternalDataStore> external_data_store); |
| 57 |
| 58 // Allows downloading of external data by constructing URLFetchers from |
| 59 // |request_context|. |
| 60 void Connect(scoped_refptr<net::URLRequestContextGetter> request_context); |
| 61 |
| 62 // Prevents further external data downloads and aborts any downloads currently |
| 63 // in progress |
| 64 void Disconnect(); |
| 65 |
| 66 // Called when the external data references that this backend is responsible |
| 67 // for change. |metadata| maps from policy names to the metadata specifying |
| 68 // the external data that each of the policies references. |
| 69 void OnMetadataUpdated(scoped_ptr<Metadata> metadata); |
| 70 |
| 71 // Called by the |updater_| when the external |data| referenced by |policy| |
| 72 // has been successfully downloaded and verified to match |hash|. |
| 73 bool OnDownloadSuccess(const std::string& policy, |
| 74 const std::string& hash, |
| 75 const std::string& data); |
| 76 |
| 77 // Retrieves the external data referenced by |policy| and invokes |callback| |
| 78 // with the result. If |policy| does not reference any external data, the |
| 79 // |callback| is invoked with a NULL pointer. Otherwise, the |callback| is |
| 80 // invoked with the referenced data once it has been successfully retrieved. |
| 81 // If retrieval is temporarily impossible (e.g. the data is not cached yet and |
| 82 // there is no network connectivity), the |callback| will be invoked when the |
| 83 // temporary hindrance is resolved. If retrieval is permanently impossible |
| 84 // (e.g. |policy| references data that does not exist on the server), the |
| 85 // |callback| will never be invoked. |
| 86 void Fetch(const std::string& policy, |
| 87 const ExternalDataFetcher::FetchCallback& callback); |
| 88 |
| 89 // Try to download and cache all external data referenced by |metadata_|. |
| 90 void FetchAll(); |
| 91 |
| 92 private: |
| 93 // List of callbacks to invoke when the attempt to retrieve external data |
| 94 // referenced by a policy completes successfully or fails permanently. |
| 95 typedef std::vector<ExternalDataFetcher::FetchCallback> FetchCallbackList; |
| 96 |
| 97 // Map from policy names to the lists of callbacks defined above. |
| 98 typedef std::map<std::string, FetchCallbackList> FetchCallbackMap; |
| 99 |
| 100 // Looks up the maximum size that the data referenced by |policy| can have in |
| 101 // |policy_definitions_|. |
| 102 size_t GetMaxExternalDataSize(const std::string& policy) const; |
| 103 |
| 104 // Invokes |callback| on the UI thread, passing |data| as a parameter. |
| 105 void RunCallback(const ExternalDataFetcher::FetchCallback& callback, |
| 106 scoped_ptr<std::string> data) const; |
| 107 |
| 108 // Tells the |updater_| to download the external data referenced by |policy|. |
| 109 // If Connect() was not called yet and no |updater_| exists, does nothing. |
| 110 void StartDownload(const std::string& policy); |
| 111 |
| 112 // Used to determine the maximum size that the data referenced by each policy |
| 113 // can have. |
| 114 const PolicyDefinitionList* policy_definitions_; |
| 115 |
| 116 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 117 |
| 118 // Contains the policies for which a download of the referenced external data |
| 119 // has been requested. Each policy is mapped to a list of callbacks to invoke |
| 120 // when the download completes successfully or fails permanently. If no |
| 121 // callback needs to be invoked (because the download was requested via |
| 122 // FetchAll()), a map entry will still exist but the list of callbacks it maps |
| 123 // to will be empty. |
| 124 FetchCallbackMap pending_downloads_; |
| 125 |
| 126 // Indicates that OnMetadataUpdated() has been called at least once and the |
| 127 // contents of |metadata_| is initialized. |
| 128 bool metadata_set_; |
| 129 |
| 130 // Maps from policy names to the metadata specifying the external data that |
| 131 // each of the policies references. |
| 132 Metadata metadata_; |
| 133 |
| 134 // Used to cache external data referenced by policies. |
| 135 scoped_ptr<CloudExternalDataStore> external_data_store_; |
| 136 |
| 137 // Used to download external data referenced by policies. |
| 138 scoped_ptr<ExternalPolicyDataUpdater> updater_; |
| 139 |
| 140 DISALLOW_COPY_AND_ASSIGN(Backend); |
| 141 }; |
| 142 |
| 143 CloudExternalDataManagerBase::Backend::Backend( |
| 144 const PolicyDefinitionList* policy_definitions, |
| 145 scoped_refptr<base::SequencedTaskRunner> task_runner) |
| 146 : policy_definitions_(policy_definitions), |
| 147 task_runner_(task_runner), |
| 148 metadata_set_(false) { |
| 149 } |
| 150 |
| 151 void CloudExternalDataManagerBase::Backend::SetExternalDataStore( |
| 152 scoped_ptr<CloudExternalDataStore> external_data_store) { |
| 153 external_data_store_.reset(external_data_store.release()); |
| 154 if (metadata_set_ && external_data_store_) |
| 155 external_data_store_->Prune(metadata_); |
| 156 } |
| 157 |
| 158 void CloudExternalDataManagerBase::Backend::Connect( |
| 159 scoped_refptr<net::URLRequestContextGetter> request_context) { |
| 160 DCHECK(!updater_); |
| 161 updater_.reset(new ExternalPolicyDataUpdater(task_runner_, |
| 162 request_context, |
| 163 kMaxParallelFetches)); |
| 164 for (FetchCallbackMap::const_iterator it = pending_downloads_.begin(); |
| 165 it != pending_downloads_.end(); ++it) { |
| 166 StartDownload(it->first); |
| 167 } |
| 168 } |
| 169 |
| 170 void CloudExternalDataManagerBase::Backend::Disconnect() { |
| 171 updater_.reset(); |
| 172 } |
| 173 |
| 174 void CloudExternalDataManagerBase::Backend::OnMetadataUpdated( |
| 175 scoped_ptr<Metadata> metadata) { |
| 176 metadata_set_ = true; |
| 177 Metadata old_metadata; |
| 178 metadata_.swap(old_metadata); |
| 179 if (metadata) |
| 180 metadata_.swap(*metadata); |
| 181 |
| 182 if (external_data_store_) |
| 183 external_data_store_->Prune(metadata_); |
| 184 |
| 185 for (FetchCallbackMap::iterator it = pending_downloads_.begin(); |
| 186 it != pending_downloads_.end(); ) { |
| 187 const std::string policy = it->first; |
| 188 Metadata::const_iterator metadata = metadata_.find(policy); |
| 189 if (metadata == metadata_.end()) { |
| 190 // |policy| no longer references external data. |
| 191 if (updater_) { |
| 192 // Cancel the external data download. |
| 193 updater_->CancelExternalDataFetch(policy); |
| 194 } |
| 195 for (FetchCallbackList::const_iterator callback = it->second.begin(); |
| 196 callback != it->second.end(); ++callback) { |
| 197 // Invoke all callbacks for |policy|, indicating permanent failure. |
| 198 RunCallback(*callback, scoped_ptr<std::string>()); |
| 199 } |
| 200 pending_downloads_.erase(it++); |
| 201 continue; |
| 202 } |
| 203 |
| 204 if (updater_ && metadata->second != old_metadata[policy]) { |
| 205 // |policy| still references external data but the reference has changed. |
| 206 // Cancel the external data download and start a new one. |
| 207 updater_->CancelExternalDataFetch(policy); |
| 208 StartDownload(policy); |
| 209 } |
| 210 ++it; |
| 211 } |
| 212 } |
| 213 |
| 214 bool CloudExternalDataManagerBase::Backend::OnDownloadSuccess( |
| 215 const std::string& policy, |
| 216 const std::string& hash, |
| 217 const std::string& data) { |
| 218 DCHECK(metadata_.find(policy) != metadata_.end()); |
| 219 DCHECK_EQ(hash, metadata_[policy].hash); |
| 220 if (external_data_store_) |
| 221 external_data_store_->Store(policy, hash, data); |
| 222 |
| 223 const FetchCallbackList& pending_callbacks = pending_downloads_[policy]; |
| 224 for (FetchCallbackList::const_iterator it = pending_callbacks.begin(); |
| 225 it != pending_callbacks.end(); ++it) { |
| 226 RunCallback(*it, make_scoped_ptr(new std::string(data))); |
| 227 } |
| 228 pending_downloads_.erase(policy); |
| 229 return true; |
| 230 } |
| 231 |
| 232 void CloudExternalDataManagerBase::Backend::Fetch( |
| 233 const std::string& policy, |
| 234 const ExternalDataFetcher::FetchCallback& callback) { |
| 235 Metadata::const_iterator metadata = metadata_.find(policy); |
| 236 if (metadata == metadata_.end()) { |
| 237 // If |policy| does not reference any external data, indicate permanent |
| 238 // failure. |
| 239 RunCallback(callback, scoped_ptr<std::string>()); |
| 240 return; |
| 241 } |
| 242 |
| 243 if (pending_downloads_.find(policy) != pending_downloads_.end()) { |
| 244 // If a download of the external data referenced by |policy| has already |
| 245 // been requested, add |callback| to the list of callbacks for |policy| and |
| 246 // return. |
| 247 pending_downloads_[policy].push_back(callback); |
| 248 return; |
| 249 } |
| 250 |
| 251 scoped_ptr<std::string> data(new std::string); |
| 252 if (external_data_store_ && external_data_store_->Load( |
| 253 policy, metadata->second.hash, GetMaxExternalDataSize(policy), |
| 254 data.get())) { |
| 255 // If the external data referenced by |policy| exists in the cache and |
| 256 // matches the expected hash, pass it to the callback. |
| 257 RunCallback(callback, data.Pass()); |
| 258 return; |
| 259 } |
| 260 |
| 261 // Request a download of the the external data referenced by |policy| and |
| 262 // initialize the list of callbacks by adding |callback|. |
| 263 pending_downloads_[policy].push_back(callback); |
| 264 StartDownload(policy); |
| 265 } |
| 266 |
| 267 void CloudExternalDataManagerBase::Backend::FetchAll() { |
| 268 // Loop through all external data references. |
| 269 for (Metadata::const_iterator it = metadata_.begin(); it != metadata_.end(); |
| 270 ++it) { |
| 271 const std::string& policy = it->first; |
| 272 scoped_ptr<std::string> data(new std::string); |
| 273 if (pending_downloads_.find(policy) != pending_downloads_.end() || |
| 274 (external_data_store_ && external_data_store_->Load( |
| 275 policy, it->second.hash, GetMaxExternalDataSize(policy), |
| 276 data.get()))) { |
| 277 // If a download of the external data referenced by |policy| has already |
| 278 // been requested or the data exists in the cache and matches the expected |
| 279 // hash, there is nothing to be done. |
| 280 continue; |
| 281 } |
| 282 // Request a download of the the external data referenced by |policy| and |
| 283 // initialize the list of callbacks to an empty list. |
| 284 pending_downloads_[policy]; |
| 285 StartDownload(policy); |
| 286 } |
| 287 } |
| 288 |
| 289 size_t CloudExternalDataManagerBase::Backend::GetMaxExternalDataSize( |
| 290 const std::string& policy) const { |
| 291 // Look up the maximum size that the data referenced by |policy| can have in |
| 292 // policy_definitions_, which is constructed from the information in |
| 293 // policy_templates.json, allowing the maximum data size to be specified as |
| 294 // part of the policy definition. |
| 295 for (const PolicyDefinitionList::Entry* entry = policy_definitions_->begin; |
| 296 entry != policy_definitions_->end; ++entry) { |
| 297 if (entry->name == policy) |
| 298 return entry->max_external_data_size; |
| 299 } |
| 300 NOTREACHED(); |
| 301 return 0; |
| 302 } |
| 303 |
| 304 void CloudExternalDataManagerBase::Backend::RunCallback( |
| 305 const ExternalDataFetcher::FetchCallback& callback, |
| 306 scoped_ptr<std::string> data) const { |
| 307 content::BrowserThread::PostTask( |
| 308 content::BrowserThread::UI, FROM_HERE, |
| 309 base::Bind(RunCallbackOnUIThread, callback, base::Passed(&data))); |
| 310 } |
| 311 |
| 312 void CloudExternalDataManagerBase::Backend::StartDownload( |
| 313 const std::string& policy) { |
| 314 DCHECK(pending_downloads_.find(policy) != pending_downloads_.end()); |
| 315 if (!updater_) |
| 316 return; |
| 317 |
| 318 const MetadataEntry& metadata = metadata_[policy]; |
| 319 updater_->FetchExternalData( |
| 320 policy, |
| 321 ExternalPolicyDataUpdater::Request(metadata.url, |
| 322 metadata.hash, |
| 323 GetMaxExternalDataSize(policy)), |
| 324 base::Bind(&CloudExternalDataManagerBase::Backend::OnDownloadSuccess, |
| 325 base::Unretained(this), |
| 326 policy, |
| 327 metadata.hash)); |
| 328 } |
| 329 |
| 330 CloudExternalDataManagerBase::CloudExternalDataManagerBase( |
| 331 const PolicyDefinitionList* policy_definitions, |
| 332 scoped_refptr<base::SequencedTaskRunner> backend_task_runner) |
| 333 : backend_task_runner_(backend_task_runner), |
| 334 backend_(new Backend(policy_definitions, backend_task_runner_)) { |
| 335 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 336 } |
| 337 |
| 338 CloudExternalDataManagerBase::~CloudExternalDataManagerBase() { |
| 339 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 340 backend_task_runner_->DeleteSoon(FROM_HERE, backend_); |
| 341 } |
| 342 |
| 343 void CloudExternalDataManagerBase::SetExternalDataStore( |
| 344 scoped_ptr<CloudExternalDataStore> external_data_store) { |
| 345 backend_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 346 &Backend::SetExternalDataStore, |
| 347 base::Unretained(backend_), |
| 348 base::Passed(&external_data_store))); |
| 349 } |
| 350 |
| 351 void CloudExternalDataManagerBase::SetPolicyStore( |
| 352 CloudPolicyStore* policy_store) { |
| 353 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 354 CloudExternalDataManager::SetPolicyStore(policy_store); |
| 355 if (policy_store_ && policy_store_->is_initialized()) |
| 356 OnPolicyStoreLoaded(); |
| 357 } |
| 358 |
| 359 void CloudExternalDataManagerBase::OnPolicyStoreLoaded() { |
| 360 // Collect all external data references made by policies in |policy_store_| |
| 361 // and pass them to the |backend_|. |
| 362 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 363 scoped_ptr<Metadata> metadata(new Metadata); |
| 364 const PolicyMap& policy_map = policy_store_->policy_map(); |
| 365 for (PolicyMap::const_iterator it = policy_map.begin(); |
| 366 it != policy_map.end(); ++it) { |
| 367 if (!it->second.external_data_fetcher) { |
| 368 // Skip policies that do not reference external data. |
| 369 continue; |
| 370 } |
| 371 const base::DictionaryValue* dict = NULL; |
| 372 std::string url; |
| 373 std::string hex_hash; |
| 374 std::vector<uint8> hash; |
| 375 if (it->second.value && it->second.value->GetAsDictionary(&dict) && |
| 376 dict->GetStringWithoutPathExpansion("url", &url) && |
| 377 dict->GetStringWithoutPathExpansion("hash", &hex_hash) && |
| 378 !url.empty() && !hex_hash.empty() && |
| 379 base::HexStringToBytes(hex_hash, &hash)) { |
| 380 // Add the external data reference to |metadata| if it is valid (URL and |
| 381 // hash are not empty, hash can be decoded as a hex string). |
| 382 (*metadata)[it->first] = |
| 383 MetadataEntry(url, std::string(hash.begin(), hash.end())); |
| 384 } |
| 385 } |
| 386 |
| 387 backend_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 388 &Backend::OnMetadataUpdated, |
| 389 base::Unretained(backend_), |
| 390 base::Passed(&metadata))); |
| 391 } |
| 392 |
| 393 void CloudExternalDataManagerBase::Connect( |
| 394 scoped_refptr<net::URLRequestContextGetter> request_context) { |
| 395 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 396 backend_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 397 &Backend::Connect, base::Unretained(backend_), request_context)); |
| 398 } |
| 399 |
| 400 void CloudExternalDataManagerBase::Disconnect() { |
| 401 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 402 backend_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 403 &Backend::Disconnect, base::Unretained(backend_))); |
| 404 } |
| 405 |
| 406 void CloudExternalDataManagerBase::Fetch( |
| 407 const std::string& policy, |
| 408 const ExternalDataFetcher::FetchCallback& callback) { |
| 409 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 410 backend_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 411 &Backend::Fetch, base::Unretained(backend_), policy, callback)); |
| 412 } |
| 413 |
| 414 void CloudExternalDataManagerBase::FetchAll() { |
| 415 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 416 backend_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 417 &Backend::FetchAll, base::Unretained(backend_))); |
| 418 } |
| 419 |
| 420 } // namespace policy |
OLD | NEW |