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