| OLD | NEW |
| 1 // Copyright (C) 2013 Google Inc. | 1 // Copyright (C) 2013 Google Inc. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "retriever.h" | 15 #include "retriever.h" |
| 16 | 16 |
| 17 #include <libaddressinput/callback.h> | 17 #include <libaddressinput/callback.h> |
| 18 #include <libaddressinput/downloader.h> | 18 #include <libaddressinput/downloader.h> |
| 19 #include <libaddressinput/storage.h> | 19 #include <libaddressinput/storage.h> |
| 20 #include <libaddressinput/util/basictypes.h> | 20 #include <libaddressinput/util/basictypes.h> |
| 21 #include <libaddressinput/util/scoped_ptr.h> | 21 #include <libaddressinput/util/scoped_ptr.h> |
| 22 | 22 |
| 23 #include <cassert> | 23 #include <cassert> |
| 24 #include <cstddef> | 24 #include <cstddef> |
| 25 #include <map> | 25 #include <map> |
| 26 #include <string> | 26 #include <string> |
| 27 #include <utility> | 27 #include <utility> |
| 28 | 28 |
| 29 #include "lookup_key_util.h" | |
| 30 #include "util/stl_util.h" | 29 #include "util/stl_util.h" |
| 31 | 30 |
| 32 namespace i18n { | 31 namespace i18n { |
| 33 namespace addressinput { | 32 namespace addressinput { |
| 34 | 33 |
| 35 Retriever::Retriever(const std::string& validation_data_url, | 34 Retriever::Retriever(const std::string& validation_data_url, |
| 36 scoped_ptr<Downloader> downloader, | 35 scoped_ptr<Downloader> downloader, |
| 37 scoped_ptr<Storage> storage) | 36 scoped_ptr<Storage> storage) |
| 38 : lookup_key_util_(validation_data_url), | 37 : validation_data_url_(validation_data_url), |
| 39 downloader_(downloader.Pass()), | 38 downloader_(downloader.Pass()), |
| 40 storage_(storage.Pass()) { | 39 storage_(storage.Pass()) { |
| 40 assert(validation_data_url_.length() > 0); |
| 41 assert(validation_data_url_[validation_data_url_.length() - 1] == '/'); |
| 41 assert(storage_ != NULL); | 42 assert(storage_ != NULL); |
| 42 assert(downloader_ != NULL); | 43 assert(downloader_ != NULL); |
| 43 } | 44 } |
| 44 | 45 |
| 45 Retriever::~Retriever() { | 46 Retriever::~Retriever() { |
| 46 STLDeleteValues(&requests_); | 47 STLDeleteValues(&requests_); |
| 47 } | 48 } |
| 48 | 49 |
| 49 void Retriever::Retrieve(const std::string& key, | 50 void Retriever::Retrieve(const std::string& key, |
| 50 scoped_ptr<Callback> retrieved) { | 51 scoped_ptr<Callback> retrieved) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 65 const std::string& key, | 66 const std::string& key, |
| 66 const std::string& data) { | 67 const std::string& data) { |
| 67 // TODO(rouslan): Add validation for data integrity and freshness. If a | 68 // TODO(rouslan): Add validation for data integrity and freshness. If a |
| 68 // download fails, then it's OK to use stale data. | 69 // download fails, then it's OK to use stale data. |
| 69 if (success) { | 70 if (success) { |
| 70 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); | 71 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); |
| 71 if (retrieved != NULL) { | 72 if (retrieved != NULL) { |
| 72 (*retrieved)(success, key, data); | 73 (*retrieved)(success, key, data); |
| 73 } | 74 } |
| 74 } else { | 75 } else { |
| 75 downloader_->Download(lookup_key_util_.GetUrlForKey(key), | 76 downloader_->Download(GetUrlForKey(key), |
| 76 BuildCallback(this, &Retriever::OnDownloaded)); | 77 BuildCallback(this, &Retriever::OnDownloaded)); |
| 77 } | 78 } |
| 78 } | 79 } |
| 79 | 80 |
| 80 void Retriever::OnDownloaded(bool success, | 81 void Retriever::OnDownloaded(bool success, |
| 81 const std::string& url, | 82 const std::string& url, |
| 82 const std::string& data) { | 83 const std::string& data) { |
| 83 const std::string& key = lookup_key_util_.GetKeyForUrl(url); | 84 const std::string& key = GetKeyForUrl(url); |
| 84 if (success) { | 85 if (success) { |
| 85 storage_->Put(key, data); | 86 storage_->Put(key, data); |
| 86 } | 87 } |
| 87 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); | 88 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); |
| 88 if (retrieved != NULL) { | 89 if (retrieved != NULL) { |
| 89 (*retrieved)(success, key, success ? data : std::string()); | 90 (*retrieved)(success, key, success ? data : std::string()); |
| 90 } | 91 } |
| 91 } | 92 } |
| 92 | 93 |
| 94 std::string Retriever::GetUrlForKey(const std::string& key) const { |
| 95 return validation_data_url_ + key; |
| 96 } |
| 97 |
| 98 std::string Retriever::GetKeyForUrl(const std::string& url) const { |
| 99 if (url.compare(0, validation_data_url_.length(), validation_data_url_) == 0) |
| 100 return url.substr(validation_data_url_.length()); |
| 101 |
| 102 return std::string(); |
| 103 } |
| 104 |
| 105 bool Retriever::IsValidationDataUrl(const std::string& url) const { |
| 106 return |
| 107 url.compare(0, validation_data_url_.length(), validation_data_url_) == 0; |
| 108 } |
| 109 |
| 93 scoped_ptr<Retriever::Callback> Retriever::GetCallbackForKey( | 110 scoped_ptr<Retriever::Callback> Retriever::GetCallbackForKey( |
| 94 const std::string& key) { | 111 const std::string& key) { |
| 95 std::map<std::string, Callback*>::iterator iter = | 112 std::map<std::string, Callback*>::iterator iter = |
| 96 requests_.find(key); | 113 requests_.find(key); |
| 97 if (iter == requests_.end()) { | 114 if (iter == requests_.end()) { |
| 98 // An abandonened request. | 115 // An abandonened request. |
| 99 return scoped_ptr<Callback>(); | 116 return scoped_ptr<Callback>(); |
| 100 } | 117 } |
| 101 scoped_ptr<Callback> callback(iter->second); | 118 scoped_ptr<Callback> callback(iter->second); |
| 102 requests_.erase(iter); | 119 requests_.erase(iter); |
| 103 return callback.Pass(); | 120 return callback.Pass(); |
| 104 } | 121 } |
| 105 | 122 |
| 106 } // namespace addressinput | 123 } // namespace addressinput |
| 107 } // namespace i18n | 124 } // namespace i18n |
| OLD | NEW |