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, |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include <map> | 24 #include <map> |
25 #include <string> | 25 #include <string> |
26 | 26 |
27 namespace i18n { | 27 namespace i18n { |
28 namespace addressinput { | 28 namespace addressinput { |
29 | 29 |
30 class Downloader; | 30 class Downloader; |
31 class Storage; | 31 class Storage; |
32 | 32 |
33 // Manages downloading data and caching it locally. Sample usage: | 33 // Manages downloading data and caching it locally. Sample usage: |
34 // Storage* storage = ...; | 34 // Storage* storage = new Storage; |
35 // Downloader* downloader = ...; | 35 // scoped_ptr<Downloader> downloader(new Downloader); |
36 // Retriever retriever("https://i18napis.appspot.com/ssl-aggregate-address/", | 36 // Retriever retriever("https://i18napis.appspot.com/ssl-aggregate-address/", |
37 // downloader, storage); | 37 // downloader.Pass(), storage); |
38 // retriever.Retrieve("data/CA/AB--fr", | 38 // retriever.Retrieve("data/CA/AB--fr", |
39 // BuildCallback(this, &MyClass::OnDataRetrieved)); | 39 // BuildCallback(this, &MyClass::OnDataRetrieved)); |
40 class Retriever { | 40 class Retriever { |
41 public: | 41 public: |
42 typedef i18n::addressinput::Callback<std::string, std::string> Callback; | 42 typedef i18n::addressinput::Callback<std::string, std::string> Callback; |
43 | 43 |
44 Retriever(const std::string& validation_data_url, | 44 Retriever(const std::string& validation_data_url, |
45 scoped_ptr<Downloader> downloader, | 45 scoped_ptr<Downloader> downloader, |
46 scoped_ptr<Storage> storage); | 46 Storage* storage); |
47 ~Retriever(); | 47 ~Retriever(); |
48 | 48 |
49 // Retrieves the data for |key| and invokes the |retrieved| callback. Checks | 49 // Retrieves the data for |key| and invokes the |retrieved| callback. Checks |
50 // for the data in storage first. If storage does not have the data for |key|, | 50 // for the data in storage first. If storage does not have the data for |key|, |
51 // then downloads the data and places it in storage. | 51 // then downloads the data and places it in storage. |
52 void Retrieve(const std::string& key, scoped_ptr<Callback> retrieved); | 52 void Retrieve(const std::string& key, scoped_ptr<Callback> retrieved); |
53 | 53 |
54 private: | 54 private: |
55 friend class RetrieverTest; | 55 friend class RetrieverTest; |
56 | 56 |
(...skipping 24 matching lines...) Expand all Loading... |
81 bool IsValidationDataUrl(const std::string& url) const; | 81 bool IsValidationDataUrl(const std::string& url) const; |
82 | 82 |
83 // Looks up the callback for |key| in |requests_|, removes it from the map and | 83 // Looks up the callback for |key| in |requests_|, removes it from the map and |
84 // invokes it with |key|, |success|, and |data| parameters. | 84 // invokes it with |key|, |success|, and |data| parameters. |
85 void InvokeCallbackForKey(const std::string& key, | 85 void InvokeCallbackForKey(const std::string& key, |
86 bool success, | 86 bool success, |
87 const std::string& data); | 87 const std::string& data); |
88 | 88 |
89 const std::string validation_data_url_; | 89 const std::string validation_data_url_; |
90 scoped_ptr<Downloader> downloader_; | 90 scoped_ptr<Downloader> downloader_; |
91 scoped_ptr<Storage> storage_; | 91 Storage* storage_; // weak. |
92 | 92 |
93 // Holds pending requests. The callback pointers are owned. | 93 // Holds pending requests. The callback pointers are owned. |
94 std::map<std::string, Callback*> requests_; | 94 std::map<std::string, Callback*> requests_; |
95 | 95 |
96 // Holds data from storage that has expired timestamps. If a download fails, | 96 // Holds data from storage that has expired timestamps. If a download fails, |
97 // then this data is used as fallback. | 97 // then this data is used as fallback. |
98 std::map<std::string, std::string> stale_data_; | 98 std::map<std::string, std::string> stale_data_; |
99 | 99 |
100 DISALLOW_COPY_AND_ASSIGN(Retriever); | 100 DISALLOW_COPY_AND_ASSIGN(Retriever); |
101 }; | 101 }; |
102 | 102 |
103 } // namespace addressinput | 103 } // namespace addressinput |
104 } // namespace i18n | 104 } // namespace i18n |
105 | 105 |
106 #endif // I18N_ADDRESSINPUT_RETRIEVER_H_ | 106 #endif // I18N_ADDRESSINPUT_RETRIEVER_H_ |
OLD | NEW |