| 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 "fallback_data_store.h" |
| 29 #include "lookup_key_util.h" | 30 #include "lookup_key_util.h" |
| 30 #include "util/stl_util.h" | 31 #include "util/stl_util.h" |
| 31 | 32 |
| 32 namespace i18n { | 33 namespace i18n { |
| 33 namespace addressinput { | 34 namespace addressinput { |
| 34 | 35 |
| 35 Retriever::Retriever(const std::string& validation_data_url, | 36 Retriever::Retriever(const std::string& validation_data_url, |
| 36 scoped_ptr<Downloader> downloader, | 37 scoped_ptr<Downloader> downloader, |
| 37 scoped_ptr<Storage> storage) | 38 scoped_ptr<Storage> storage) |
| 38 : lookup_key_util_(validation_data_url), | 39 : lookup_key_util_(validation_data_url), |
| (...skipping 17 matching lines...) Expand all Loading... |
| 56 requests_.erase(request_it); | 57 requests_.erase(request_it); |
| 57 } | 58 } |
| 58 | 59 |
| 59 requests_[key] = retrieved.release(); | 60 requests_[key] = retrieved.release(); |
| 60 storage_->Get(key, | 61 storage_->Get(key, |
| 61 BuildCallback(this, &Retriever::OnDataRetrievedFromStorage)); | 62 BuildCallback(this, &Retriever::OnDataRetrievedFromStorage)); |
| 62 } | 63 } |
| 63 | 64 |
| 64 void Retriever::OnDataRetrievedFromStorage(bool success, | 65 void Retriever::OnDataRetrievedFromStorage(bool success, |
| 65 const std::string& key, | 66 const std::string& key, |
| 66 const std::string& data) { | 67 const std::string& stored_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, stored_data); |
| 73 } | 74 } |
| 74 } else { | 75 } else { |
| 75 downloader_->Download(lookup_key_util_.GetUrlForKey(key), | 76 downloader_->Download(lookup_key_util_.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& downloaded_data) { |
| 84 std::string response; |
| 83 const std::string& key = lookup_key_util_.GetKeyForUrl(url); | 85 const std::string& key = lookup_key_util_.GetKeyForUrl(url); |
| 84 if (success) { | 86 if (success) { |
| 85 storage_->Put(key, data); | 87 storage_->Put(key, downloaded_data); |
| 88 response = downloaded_data; |
| 89 } else { |
| 90 success = FallbackDataStore::Get(key, &response); |
| 86 } | 91 } |
| 92 |
| 87 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); | 93 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); |
| 88 if (retrieved != NULL) { | 94 if (retrieved != NULL) { |
| 89 (*retrieved)(success, key, success ? data : std::string()); | 95 (*retrieved)(success, key, response); |
| 90 } | 96 } |
| 91 } | 97 } |
| 92 | 98 |
| 93 scoped_ptr<Retriever::Callback> Retriever::GetCallbackForKey( | 99 scoped_ptr<Retriever::Callback> Retriever::GetCallbackForKey( |
| 94 const std::string& key) { | 100 const std::string& key) { |
| 95 std::map<std::string, Callback*>::iterator iter = | 101 std::map<std::string, Callback*>::iterator iter = |
| 96 requests_.find(key); | 102 requests_.find(key); |
| 97 if (iter == requests_.end()) { | 103 if (iter == requests_.end()) { |
| 98 // An abandonened request. | 104 // An abandonened request. |
| 99 return scoped_ptr<Callback>(); | 105 return scoped_ptr<Callback>(); |
| 100 } | 106 } |
| 101 scoped_ptr<Callback> callback(iter->second); | 107 scoped_ptr<Callback> callback(iter->second); |
| 102 requests_.erase(iter); | 108 requests_.erase(iter); |
| 103 return callback.Pass(); | 109 return callback.Pass(); |
| 104 } | 110 } |
| 105 | 111 |
| 106 } // namespace addressinput | 112 } // namespace addressinput |
| 107 } // namespace i18n | 113 } // namespace i18n |
| OLD | NEW |