| Index: third_party/libaddressinput/chromium/cpp/src/retriever.cc
|
| diff --git a/third_party/libaddressinput/chromium/cpp/src/retriever.cc b/third_party/libaddressinput/chromium/cpp/src/retriever.cc
|
| index eebba3eb2d78b9b5d590f6aa8a4ba2819d39d7c1..e4b3c2c6e6ece050f9569436962266c4e22ef1ef 100644
|
| --- a/third_party/libaddressinput/chromium/cpp/src/retriever.cc
|
| +++ b/third_party/libaddressinput/chromium/cpp/src/retriever.cc
|
| @@ -28,16 +28,30 @@
|
|
|
| #include "fallback_data_store.h"
|
| #include "util/stl_util.h"
|
| +#include "wrapper.h"
|
|
|
| namespace i18n {
|
| namespace addressinput {
|
|
|
| +namespace {
|
| +
|
| +// The number of seconds after which data is considered stale. The staleness
|
| +// threshold is 30 days:
|
| +// 30 days *
|
| +// 24 hours per day *
|
| +// 60 minutes per hour *
|
| +// 60 seconds per minute.
|
| +static const double kStaleDataAgeInSeconds = 30.0 * 24.0 * 60.0 * 60.0;
|
| +
|
| +} // namespace
|
| +
|
| Retriever::Retriever(const std::string& validation_data_url,
|
| scoped_ptr<Downloader> downloader,
|
| scoped_ptr<Storage> storage)
|
| : validation_data_url_(validation_data_url),
|
| downloader_(downloader.Pass()),
|
| - storage_(storage.Pass()) {
|
| + storage_(storage.Pass()),
|
| + stale_data_() {
|
| assert(validation_data_url_.length() > 0);
|
| assert(validation_data_url_[validation_data_url_.length() - 1] == '/');
|
| assert(storage_ != NULL);
|
| @@ -66,14 +80,18 @@ void Retriever::Retrieve(const std::string& key,
|
| void Retriever::OnDataRetrievedFromStorage(bool success,
|
| const std::string& key,
|
| const std::string& stored_data) {
|
| - // TODO(rouslan): Add validation for data integrity and freshness. If a
|
| - // download fails, then it's OK to use stale data.
|
| - if (success) {
|
| + std::string unwrapped = stored_data;
|
| + double age_in_seconds = 0.0;
|
| + bool valid_format = Wrapper::Unwrap(&unwrapped, &age_in_seconds);
|
| + if (success && valid_format && age_in_seconds < kStaleDataAgeInSeconds) {
|
| scoped_ptr<Callback> retrieved = GetCallbackForKey(key);
|
| if (retrieved != NULL) {
|
| - (*retrieved)(success, key, stored_data);
|
| + (*retrieved)(success, key, unwrapped);
|
| }
|
| } else {
|
| + if (success && valid_format) {
|
| + stale_data_[key] = unwrapped;
|
| + }
|
| downloader_->Download(GetUrlForKey(key),
|
| BuildCallback(this, &Retriever::OnDownloaded));
|
| }
|
| @@ -84,13 +102,23 @@ void Retriever::OnDownloaded(bool success,
|
| const std::string& downloaded_data) {
|
| const std::string& key = GetKeyForUrl(url);
|
| std::string response;
|
| + std::map<std::string, std::string>::iterator stale_data_it =
|
| + stale_data_.find(key);
|
| +
|
| if (success) {
|
| - storage_->Put(key, downloaded_data);
|
| + storage_->Put(key, Wrapper::Wrap(downloaded_data));
|
| response = downloaded_data;
|
| + } else if (stale_data_it != stale_data_.end()) {
|
| + success = true;
|
| + response = stale_data_it->second;
|
| } else {
|
| success = FallbackDataStore::Get(key, &response);
|
| }
|
|
|
| + if (stale_data_it != stale_data_.end()) {
|
| + stale_data_.erase(stale_data_it);
|
| + }
|
| +
|
| scoped_ptr<Callback> retrieved = GetCallbackForKey(key);
|
| if (retrieved != NULL) {
|
| (*retrieved)(success, key, response);
|
|
|