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 3881d83da0e13ec91d7f3506195873055385f50a..c1ad5306fe49dbe6ed8023ace14a0faf1713ecfa 100644 |
--- a/third_party/libaddressinput/chromium/cpp/src/retriever.cc |
+++ b/third_party/libaddressinput/chromium/cpp/src/retriever.cc |
@@ -22,68 +22,16 @@ |
#include <cassert> |
#include <cstddef> |
+#include <map> |
#include <string> |
+#include <utility> |
#include "lookup_key_util.h" |
+#include "util/stl_util.h" |
namespace i18n { |
namespace addressinput { |
-namespace { |
- |
-class Helper { |
- public: |
- // Does not take ownership of |storage|. |
- Helper(const std::string& key, |
- scoped_ptr<Retriever::Callback> retrieved, |
- const LookupKeyUtil& lookup_key_util, |
- const Downloader& downloader, |
- Storage* storage) |
- : retrieved_(retrieved.Pass()), |
- lookup_key_util_(lookup_key_util), |
- downloader_(downloader), |
- storage_(storage) { |
- assert(storage_ != NULL); |
- storage_->Get(key, BuildCallback(this, &Helper::OnDataReady)); |
- } |
- |
- private: |
- ~Helper() {} |
- |
- void OnDataReady(bool success, |
- const std::string& key, |
- const std::string& data) { |
- if (success) { |
- (*retrieved_)(success, key, data); |
- delete this; |
- } else { |
- downloader_.Download(lookup_key_util_.GetUrlForKey(key), |
- BuildCallback(this, &Helper::OnDownloaded)); |
- } |
- } |
- |
- void OnDownloaded(bool success, |
- const std::string& url, |
- const std::string& data) { |
- const std::string& key = lookup_key_util_.GetKeyForUrl(url); |
- if (success) { |
- // TODO(estade): this is really dangerous; storage_ is not owned. |
- storage_->Put(key, data); |
- } |
- (*retrieved_)(success, key, success ? data : std::string()); |
- delete this; |
- } |
- |
- scoped_ptr<Retriever::Callback> retrieved_; |
- const LookupKeyUtil& lookup_key_util_; |
- const Downloader& downloader_; |
- Storage* storage_; |
- |
- DISALLOW_COPY_AND_ASSIGN(Helper); |
-}; |
- |
-} // namespace |
- |
Retriever::Retriever(const std::string& validation_data_url, |
scoped_ptr<const Downloader> downloader, |
scoped_ptr<Storage> storage) |
@@ -94,15 +42,49 @@ Retriever::Retriever(const std::string& validation_data_url, |
assert(downloader_ != NULL); |
} |
-Retriever::~Retriever() {} |
+Retriever::~Retriever() { |
+ STLDeleteValues(&requests_); |
+} |
void Retriever::Retrieve(const std::string& key, |
- scoped_ptr<Callback> retrieved) const { |
- new Helper(key, |
- retrieved.Pass(), |
- lookup_key_util_, |
- *downloader_, |
- storage_.get()); |
+ scoped_ptr<Callback> retrieved) { |
+ assert(requests_.find(key) == requests_.end()); |
+ requests_.insert(std::make_pair(key, retrieved.release())); |
+ storage_->Get(key, |
+ BuildCallback(this, &Retriever::OnDataRetrievedFromStorage)); |
+} |
+ |
+void Retriever::OnDataRetrievedFromStorage(bool success, |
+ const std::string& key, |
+ const std::string& data) { |
+ if (success) { |
+ scoped_ptr<Callback> retrieved = GetCallbackForKey(key); |
+ (*retrieved)(success, key, data); |
+ } else { |
+ downloader_->Download(lookup_key_util_.GetUrlForKey(key), |
+ BuildCallback(this, &Retriever::OnDownloaded)); |
+ } |
+} |
+ |
+void Retriever::OnDownloaded(bool success, |
+ const std::string& url, |
+ const std::string& data) { |
+ const std::string& key = lookup_key_util_.GetKeyForUrl(url); |
+ if (success) { |
+ storage_->Put(key, data); |
+ } |
+ scoped_ptr<Callback> retrieved = GetCallbackForKey(key); |
+ (*retrieved)(success, key, success ? data : std::string()); |
+} |
+ |
+scoped_ptr<Retriever::Callback> Retriever::GetCallbackForKey( |
+ const std::string& key) { |
+ std::map<std::string, Callback*>::iterator iter = |
+ requests_.find(key); |
+ assert(iter != requests_.end()); |
+ scoped_ptr<Callback> callback(iter->second); |
+ requests_.erase(iter); |
+ return callback.Pass(); |
} |
} // namespace addressinput |