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 <string> | 26 #include <string> |
| 27 #include <utility> |
26 | 28 |
27 #include "lookup_key_util.h" | 29 #include "lookup_key_util.h" |
| 30 #include "util/stl_util.h" |
28 | 31 |
29 namespace i18n { | 32 namespace i18n { |
30 namespace addressinput { | 33 namespace addressinput { |
31 | 34 |
32 namespace { | |
33 | |
34 class Helper { | |
35 public: | |
36 // Does not take ownership of |storage|. | |
37 Helper(const std::string& key, | |
38 scoped_ptr<Retriever::Callback> retrieved, | |
39 const LookupKeyUtil& lookup_key_util, | |
40 const Downloader& downloader, | |
41 Storage* storage) | |
42 : retrieved_(retrieved.Pass()), | |
43 lookup_key_util_(lookup_key_util), | |
44 downloader_(downloader), | |
45 storage_(storage) { | |
46 assert(storage_ != NULL); | |
47 storage_->Get(key, BuildCallback(this, &Helper::OnDataReady)); | |
48 } | |
49 | |
50 private: | |
51 ~Helper() {} | |
52 | |
53 void OnDataReady(bool success, | |
54 const std::string& key, | |
55 const std::string& data) { | |
56 if (success) { | |
57 (*retrieved_)(success, key, data); | |
58 delete this; | |
59 } else { | |
60 downloader_.Download(lookup_key_util_.GetUrlForKey(key), | |
61 BuildCallback(this, &Helper::OnDownloaded)); | |
62 } | |
63 } | |
64 | |
65 void OnDownloaded(bool success, | |
66 const std::string& url, | |
67 const std::string& data) { | |
68 const std::string& key = lookup_key_util_.GetKeyForUrl(url); | |
69 if (success) { | |
70 // TODO(estade): this is really dangerous; storage_ is not owned. | |
71 storage_->Put(key, data); | |
72 } | |
73 (*retrieved_)(success, key, success ? data : std::string()); | |
74 delete this; | |
75 } | |
76 | |
77 scoped_ptr<Retriever::Callback> retrieved_; | |
78 const LookupKeyUtil& lookup_key_util_; | |
79 const Downloader& downloader_; | |
80 Storage* storage_; | |
81 | |
82 DISALLOW_COPY_AND_ASSIGN(Helper); | |
83 }; | |
84 | |
85 } // namespace | |
86 | |
87 Retriever::Retriever(const std::string& validation_data_url, | 35 Retriever::Retriever(const std::string& validation_data_url, |
88 scoped_ptr<const Downloader> downloader, | 36 scoped_ptr<const Downloader> downloader, |
89 scoped_ptr<Storage> storage) | 37 scoped_ptr<Storage> storage) |
90 : lookup_key_util_(validation_data_url), | 38 : lookup_key_util_(validation_data_url), |
91 downloader_(downloader.Pass()), | 39 downloader_(downloader.Pass()), |
92 storage_(storage.Pass()) { | 40 storage_(storage.Pass()) { |
93 assert(storage_ != NULL); | 41 assert(storage_ != NULL); |
94 assert(downloader_ != NULL); | 42 assert(downloader_ != NULL); |
95 } | 43 } |
96 | 44 |
97 Retriever::~Retriever() {} | 45 Retriever::~Retriever() { |
| 46 STLDeleteValues(&requests_); |
| 47 } |
98 | 48 |
99 void Retriever::Retrieve(const std::string& key, | 49 void Retriever::Retrieve(const std::string& key, |
100 scoped_ptr<Callback> retrieved) const { | 50 scoped_ptr<Callback> retrieved) { |
101 new Helper(key, | 51 assert(requests_.find(key) == requests_.end()); |
102 retrieved.Pass(), | 52 requests_.insert(std::make_pair(key, retrieved.release())); |
103 lookup_key_util_, | 53 storage_->Get(key, |
104 *downloader_, | 54 BuildCallback(this, &Retriever::OnDataRetrievedFromStorage)); |
105 storage_.get()); | 55 } |
| 56 |
| 57 void Retriever::OnDataRetrievedFromStorage(bool success, |
| 58 const std::string& key, |
| 59 const std::string& data) { |
| 60 if (success) { |
| 61 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); |
| 62 (*retrieved)(success, key, data); |
| 63 } else { |
| 64 downloader_->Download(lookup_key_util_.GetUrlForKey(key), |
| 65 BuildCallback(this, &Retriever::OnDownloaded)); |
| 66 } |
| 67 } |
| 68 |
| 69 void Retriever::OnDownloaded(bool success, |
| 70 const std::string& url, |
| 71 const std::string& data) { |
| 72 const std::string& key = lookup_key_util_.GetKeyForUrl(url); |
| 73 if (success) { |
| 74 storage_->Put(key, data); |
| 75 } |
| 76 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); |
| 77 (*retrieved)(success, key, success ? data : std::string()); |
| 78 } |
| 79 |
| 80 scoped_ptr<Retriever::Callback> Retriever::GetCallbackForKey( |
| 81 const std::string& key) { |
| 82 std::map<std::string, Callback*>::iterator iter = |
| 83 requests_.find(key); |
| 84 assert(iter != requests_.end()); |
| 85 scoped_ptr<Callback> callback(iter->second); |
| 86 requests_.erase(iter); |
| 87 return callback.Pass(); |
106 } | 88 } |
107 | 89 |
108 } // namespace addressinput | 90 } // namespace addressinput |
109 } // namespace i18n | 91 } // namespace i18n |
OLD | NEW |