1 // Copyright (C) 2013 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "retriever.h"
16
17 #include <libaddressinput/callback.h>
18 #include <libaddressinput/downloader.h>
19 #include <libaddressinput/storage.h>
20 #include <libaddressinput/util/basictypes.h>
21 #include <libaddressinput/util/scoped_ptr.h>
22
23 #include <cassert>
24 #include <cstddef>
25 #include <string>
26
27 #include "lookup_key_util.h"
28
29 namespace i18n {
30 namespace addressinput {
31
32 namespace {
33
34 class Helper {
35 public:
36 // Does not take ownership of its parameters.
Helper(const std::string & key,const Retriever::Callback & retrieved,const LookupKeyUtil & lookup_key_util,const Downloader & downloader,Storage * storage)37 Helper(const std::string& key,
38 const Retriever::Callback& retrieved,
39 const LookupKeyUtil& lookup_key_util,
40 const Downloader& downloader,
41 Storage* storage)
42 : retrieved_(retrieved),
43 lookup_key_util_(lookup_key_util),
44 downloader_(downloader),
45 storage_(storage),
46 downloaded_(BuildCallback(this, &Helper::OnDownloaded)),
47 data_ready_(BuildCallback(this, &Helper::OnDataReady)) {
48 assert(storage_ != NULL);
49 storage_->Get(key, *data_ready_);
50 }
51
52 private:
~Helper()53 ~Helper() {}
54
OnDataReady(bool success,const std::string & key,const std::string & data)55 void OnDataReady(bool success,
56 const std::string& key,
57 const std::string& data) {
58 if (success) {
59 retrieved_(success, key, data);
60 delete this;
61 } else {
62 downloader_.Download(lookup_key_util_.GetUrlForKey(key),
63 *downloaded_);
64 }
65 }
66
OnDownloaded(bool success,const std::string & url,const std::string & data)67 void OnDownloaded(bool success,
68 const std::string& url,
69 const std::string& data) {
70 const std::string& key = lookup_key_util_.GetKeyForUrl(url);
71 if (success) {
72 storage_->Put(key, data);
73 }
74 retrieved_(success, key, success ? data : std::string());
75 delete this;
76 }
77
78 const Retriever::Callback& retrieved_;
79 const LookupKeyUtil& lookup_key_util_;
80 const Downloader& downloader_;
81 Storage* storage_;
82 scoped_ptr<Downloader::Callback> downloaded_;
83 scoped_ptr<Storage::Callback> data_ready_;
84
85 DISALLOW_COPY_AND_ASSIGN(Helper);
86 };
87
88 } // namespace
89
Retriever(const std::string & validation_data_url,const Downloader * downloader,Storage * storage)90 Retriever::Retriever(const std::string& validation_data_url,
91 const Downloader* downloader,
92 Storage* storage)
93 : lookup_key_util_(validation_data_url),
94 downloader_(downloader),
95 storage_(storage) {
96 assert(storage_ != NULL);
97 assert(downloader_ != NULL);
98 }
99
~Retriever()100 Retriever::~Retriever() {}
101
Retrieve(const std::string & key,const Callback & retrieved) const102 void Retriever::Retrieve(const std::string& key,
103 const Callback& retrieved) const {
104 new Helper(key, retrieved, lookup_key_util_, *downloader_, storage_.get());
105 }
106
107 } // namespace addressinput
108 } // namespace i18n
109