| 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 15 matching lines...) Expand all Loading... |
| 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 = ...; |
| 35 // Downloader* downloader = ...; | 35 // Downloader* downloader = ...; |
| 36 // Retriever retriever("https://i18napis.appspot.com/ssl-address/", | 36 // Retriever retriever("https://i18napis.appspot.com/ssl-aggregate-address/", |
| 37 // downloader, storage); | 37 // downloader, 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 scoped_ptr<Storage> storage); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 58 void OnDataRetrievedFromStorage(bool success, | 58 void OnDataRetrievedFromStorage(bool success, |
| 59 const std::string& key, | 59 const std::string& key, |
| 60 const std::string& stored_data); | 60 const std::string& stored_data); |
| 61 | 61 |
| 62 // Callback for when a rule is retrieved by |downloader_|. | 62 // Callback for when a rule is retrieved by |downloader_|. |
| 63 void OnDownloaded(bool success, | 63 void OnDownloaded(bool success, |
| 64 const std::string& url, | 64 const std::string& url, |
| 65 const std::string& downloaded_data); | 65 const std::string& downloaded_data); |
| 66 | 66 |
| 67 // Returns the URL where the |key| can be retrieved. For example, returns | 67 // Returns the URL where the |key| can be retrieved. For example, returns |
| 68 // "https://i18napis.appspot.com/ssl-address/data/US" for input "data/US". | 68 // "https://i18napis.appspot.com/ssl-aggregate-address/data/US" for input |
| 69 // Assumes that the input string is a valid URL segment. | 69 // "data/US". Assumes that the input string is a valid URL segment. |
| 70 std::string GetUrlForKey(const std::string& key) const; | 70 std::string GetUrlForKey(const std::string& key) const; |
| 71 | 71 |
| 72 // Returns the key for the |url|. For example, returns "data/US" for | 72 // Returns the key for the |url|. For example, returns "data/US" for |
| 73 // "https://i18napis.appspot.com/ssl-address/data/US". If the |url| does not | 73 // "https://i18napis.appspot.com/ssl-aggregate-address/data/US". If the |url| |
| 74 // start with |validation_data_url| that was passed to the constructor, then | 74 // does not start with |validation_data_url| that was passed to the |
| 75 // returns an empty string. (This can happen if the user of the library | 75 // constructor, then returns an empty string. (This can happen if the user of |
| 76 // returns a bad URL in their Downloader implementation.) | 76 // the library returns a bad URL in their Downloader implementation.) |
| 77 std::string GetKeyForUrl(const std::string& url) const; | 77 std::string GetKeyForUrl(const std::string& url) const; |
| 78 | 78 |
| 79 // Returns true if the |url| starts with |validation_data_url| that was passed | 79 // Returns true if the |url| starts with |validation_data_url| that was passed |
| 80 // to the constructor. | 80 // to the constructor. |
| 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 | 83 // Looks up the callback for |key| in |requests_|, removes it from the map |
| 84 // and returns it. Assumes that |key| is in fact in |requests_|. | 84 // and returns it. Assumes that |key| is in fact in |requests_|. |
| 85 scoped_ptr<Callback> GetCallbackForKey(const std::string& key); | 85 scoped_ptr<Callback> GetCallbackForKey(const std::string& key); |
| 86 | 86 |
| 87 const std::string validation_data_url_; | 87 const std::string validation_data_url_; |
| 88 scoped_ptr<Downloader> downloader_; | 88 scoped_ptr<Downloader> downloader_; |
| 89 scoped_ptr<Storage> storage_; | 89 scoped_ptr<Storage> storage_; |
| 90 // Holds pending requests. The callback pointers are owned. | 90 // Holds pending requests. The callback pointers are owned. |
| 91 std::map<std::string, Callback*> requests_; | 91 std::map<std::string, Callback*> requests_; |
| 92 | 92 |
| 93 DISALLOW_COPY_AND_ASSIGN(Retriever); | 93 DISALLOW_COPY_AND_ASSIGN(Retriever); |
| 94 }; | 94 }; |
| 95 | 95 |
| 96 } // namespace addressinput | 96 } // namespace addressinput |
| 97 } // namespace i18n | 97 } // namespace i18n |
| 98 | 98 |
| 99 #endif // I18N_ADDRESSINPUT_RETRIEVER_H_ | 99 #endif // I18N_ADDRESSINPUT_RETRIEVER_H_ |
| OLD | NEW |