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 "util/stl_util.h" | 30 #include "util/stl_util.h" |
30 | 31 |
31 namespace i18n { | 32 namespace i18n { |
32 namespace addressinput { | 33 namespace addressinput { |
33 | 34 |
34 Retriever::Retriever(const std::string& validation_data_url, | 35 Retriever::Retriever(const std::string& validation_data_url, |
35 scoped_ptr<Downloader> downloader, | 36 scoped_ptr<Downloader> downloader, |
36 scoped_ptr<Storage> storage) | 37 scoped_ptr<Storage> storage) |
37 : validation_data_url_(validation_data_url), | 38 : validation_data_url_(validation_data_url), |
38 downloader_(downloader.Pass()), | 39 downloader_(downloader.Pass()), |
(...skipping 18 matching lines...) Expand all Loading... |
57 requests_.erase(request_it); | 58 requests_.erase(request_it); |
58 } | 59 } |
59 | 60 |
60 requests_[key] = retrieved.release(); | 61 requests_[key] = retrieved.release(); |
61 storage_->Get(key, | 62 storage_->Get(key, |
62 BuildCallback(this, &Retriever::OnDataRetrievedFromStorage)); | 63 BuildCallback(this, &Retriever::OnDataRetrievedFromStorage)); |
63 } | 64 } |
64 | 65 |
65 void Retriever::OnDataRetrievedFromStorage(bool success, | 66 void Retriever::OnDataRetrievedFromStorage(bool success, |
66 const std::string& key, | 67 const std::string& key, |
67 const std::string& data) { | 68 const std::string& stored_data) { |
68 // TODO(rouslan): Add validation for data integrity and freshness. If a | 69 // TODO(rouslan): Add validation for data integrity and freshness. If a |
69 // download fails, then it's OK to use stale data. | 70 // download fails, then it's OK to use stale data. |
70 if (success) { | 71 if (success) { |
71 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); | 72 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); |
72 if (retrieved != NULL) { | 73 if (retrieved != NULL) { |
73 (*retrieved)(success, key, data); | 74 (*retrieved)(success, key, stored_data); |
74 } | 75 } |
75 } else { | 76 } else { |
76 downloader_->Download(GetUrlForKey(key), | 77 downloader_->Download(GetUrlForKey(key), |
77 BuildCallback(this, &Retriever::OnDownloaded)); | 78 BuildCallback(this, &Retriever::OnDownloaded)); |
78 } | 79 } |
79 } | 80 } |
80 | 81 |
81 void Retriever::OnDownloaded(bool success, | 82 void Retriever::OnDownloaded(bool success, |
82 const std::string& url, | 83 const std::string& url, |
83 const std::string& data) { | 84 const std::string& downloaded_data) { |
84 const std::string& key = GetKeyForUrl(url); | 85 const std::string& key = GetKeyForUrl(url); |
| 86 std::string response; |
85 if (success) { | 87 if (success) { |
86 storage_->Put(key, data); | 88 storage_->Put(key, downloaded_data); |
| 89 response = downloaded_data; |
| 90 } else { |
| 91 success = FallbackDataStore::Get(key, &response); |
87 } | 92 } |
| 93 |
88 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); | 94 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); |
89 if (retrieved != NULL) { | 95 if (retrieved != NULL) { |
90 (*retrieved)(success, key, success ? data : std::string()); | 96 (*retrieved)(success, key, response); |
91 } | 97 } |
92 } | 98 } |
93 | 99 |
94 std::string Retriever::GetUrlForKey(const std::string& key) const { | 100 std::string Retriever::GetUrlForKey(const std::string& key) const { |
95 return validation_data_url_ + key; | 101 return validation_data_url_ + key; |
96 } | 102 } |
97 | 103 |
98 std::string Retriever::GetKeyForUrl(const std::string& url) const { | 104 std::string Retriever::GetKeyForUrl(const std::string& url) const { |
99 if (url.compare(0, validation_data_url_.length(), validation_data_url_) == 0) | 105 if (url.compare(0, validation_data_url_.length(), validation_data_url_) == 0) |
100 return url.substr(validation_data_url_.length()); | 106 return url.substr(validation_data_url_.length()); |
(...skipping 14 matching lines...) Expand all Loading... |
115 // An abandonened request. | 121 // An abandonened request. |
116 return scoped_ptr<Callback>(); | 122 return scoped_ptr<Callback>(); |
117 } | 123 } |
118 scoped_ptr<Callback> callback(iter->second); | 124 scoped_ptr<Callback> callback(iter->second); |
119 requests_.erase(iter); | 125 requests_.erase(iter); |
120 return callback.Pass(); | 126 return callback.Pass(); |
121 } | 127 } |
122 | 128 |
123 } // namespace addressinput | 129 } // namespace addressinput |
124 } // namespace i18n | 130 } // namespace i18n |
OLD | NEW |