| 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..35a06b9dbab31342bc66c43c7c46d42f523dd219 100644
|
| --- a/third_party/libaddressinput/chromium/cpp/src/retriever.cc
|
| +++ b/third_party/libaddressinput/chromium/cpp/src/retriever.cc
|
| @@ -22,12 +22,14 @@
|
|
|
| #include <cassert>
|
| #include <cstddef>
|
| +#include <ctime>
|
| #include <map>
|
| #include <string>
|
| #include <utility>
|
|
|
| #include "fallback_data_store.h"
|
| #include "util/stl_util.h"
|
| +#include "validating_util.h"
|
|
|
| namespace i18n {
|
| namespace addressinput {
|
| @@ -37,7 +39,8 @@ Retriever::Retriever(const std::string& validation_data_url,
|
| 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 +69,24 @@ 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;
|
| + ValidatingUtil::TimestampStatus timestamp_status =
|
| + ValidatingUtil::UnwrapTimestamp(&unwrapped, time(NULL));
|
| + bool checksum_valid = ValidatingUtil::UnwrapChecksum(&unwrapped);
|
| +
|
| + if (success &&
|
| + timestamp_status == ValidatingUtil::TIMESTAMP_VALID &&
|
| + checksum_valid) {
|
| scoped_ptr<Callback> retrieved = GetCallbackForKey(key);
|
| if (retrieved != NULL) {
|
| - (*retrieved)(success, key, stored_data);
|
| + (*retrieved)(success, key, unwrapped);
|
| }
|
| } else {
|
| + if (success &&
|
| + timestamp_status == ValidatingUtil::TIMESTAMP_STALE &&
|
| + checksum_valid) {
|
| + stale_data_[key] = unwrapped;
|
| + }
|
| downloader_->Download(GetUrlForKey(key),
|
| BuildCallback(this, &Retriever::OnDownloaded));
|
| }
|
| @@ -84,11 +97,21 @@ 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, ValidatingUtil::Wrap(downloaded_data, time(NULL)));
|
| response = downloaded_data;
|
| + } else if (stale_data_it != stale_data_.end()) {
|
| + success = true;
|
| + response = stale_data_it->second;
|
| } else {
|
| - success = FallbackDataStore::Get(key, &response);
|
| + success = FallbackDataStore::Get(key, &response);
|
| + }
|
| +
|
| + if (stale_data_it != stale_data_.end()) {
|
| + stale_data_.erase(stale_data_it);
|
| }
|
|
|
| scoped_ptr<Callback> retrieved = GetCallbackForKey(key);
|
|
|