| OLD | NEW |
| 1 // Copyright (C) 2014 Google Inc. | 1 // Copyright (C) 2014 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 #ifndef I18N_ADDRESSINPUT_COUNTRY_RULES_AGGREGATOR_H_ | 15 #ifndef I18N_ADDRESSINPUT_COUNTRY_RULES_AGGREGATOR_H_ |
| 16 #define I18N_ADDRESSINPUT_COUNTRY_RULES_AGGREGATOR_H_ | 16 #define I18N_ADDRESSINPUT_COUNTRY_RULES_AGGREGATOR_H_ |
| 17 | 17 |
| 18 #include <libaddressinput/callback.h> | 18 #include <libaddressinput/callback.h> |
| 19 #include <libaddressinput/util/basictypes.h> | 19 #include <libaddressinput/util/basictypes.h> |
| 20 #include <libaddressinput/util/scoped_ptr.h> | 20 #include <libaddressinput/util/scoped_ptr.h> |
| 21 | 21 |
| 22 #include <map> | |
| 23 #include <string> | 22 #include <string> |
| 24 #include <vector> | |
| 25 | 23 |
| 26 namespace i18n { | 24 namespace i18n { |
| 27 namespace addressinput { | 25 namespace addressinput { |
| 28 | 26 |
| 29 class Retriever; | 27 class Retriever; |
| 30 class Ruleset; | 28 class Ruleset; |
| 31 | 29 |
| 32 // Aggregates a ruleset for a country. Sample usage: | 30 // Aggregates a ruleset for a country. Sample usage: |
| 33 // class MyClass { | 31 // class MyClass { |
| 34 // public: | 32 // public: |
| (...skipping 26 matching lines...) Expand all Loading... |
| 61 ~CountryRulesAggregator(); | 59 ~CountryRulesAggregator(); |
| 62 | 60 |
| 63 // Recursively retrieves all of the rules for |country_code| and its | 61 // Recursively retrieves all of the rules for |country_code| and its |
| 64 // administrative areas, localities, and dependent localities. Also retrieves | 62 // administrative areas, localities, and dependent localities. Also retrieves |
| 65 // the language-specific rules. Abandons previous requests if invoked multiple | 63 // the language-specific rules. Abandons previous requests if invoked multiple |
| 66 // times. Invokes the |rules_ready| callback when finished. | 64 // times. Invokes the |rules_ready| callback when finished. |
| 67 void AggregateRules(const std::string& country_code, | 65 void AggregateRules(const std::string& country_code, |
| 68 scoped_ptr<Callback> rules_ready); | 66 scoped_ptr<Callback> rules_ready); |
| 69 | 67 |
| 70 private: | 68 private: |
| 71 struct RequestData; | |
| 72 | |
| 73 // Callback for Retriever::Retrieve() method. | 69 // Callback for Retriever::Retrieve() method. |
| 74 void OnDataReady(bool success, | 70 void OnDataReady(bool success, |
| 75 const std::string& key, | 71 const std::string& key, |
| 76 const std::string& data); | 72 const std::string& data); |
| 77 | 73 |
| 78 // Abandons all requests and clears all retrieved data. | 74 // Abandons all requests and clears all retrieved data. |
| 79 void Reset(); | 75 void Reset(); |
| 80 | 76 |
| 81 // The data retriever that can download serialized rules for one sub-region at | 77 // The data retriever that can download serialized rules for one sub-region at |
| 82 // a time. | 78 // a time. |
| 83 scoped_ptr<Retriever> retriever_; | 79 scoped_ptr<Retriever> retriever_; |
| 84 | 80 |
| 85 // A mapping of data keys (e.g., "data/CA/AB--fr") to information that helps | |
| 86 // to parse the response data and place it the correct location in the | |
| 87 // rulesets. | |
| 88 std::map<std::string, RequestData> requests_; | |
| 89 | |
| 90 // The country code for which to retrieve the ruleset. Passed to the callback | 81 // The country code for which to retrieve the ruleset. Passed to the callback |
| 91 // method to identify the ruleset. Examples: "US", "CA", "CH", etc. | 82 // method to identify the ruleset. Examples: "US", "CA", "CH", etc. |
| 92 std::string country_code_; | 83 std::string country_code_; |
| 93 | 84 |
| 85 // The key requested from retriever. For example, "data/US". |
| 86 std::string key_; |
| 87 |
| 94 // The callback to invoke when the ruleset has been retrieved. | 88 // The callback to invoke when the ruleset has been retrieved. |
| 95 scoped_ptr<Callback> rules_ready_; | 89 scoped_ptr<Callback> rules_ready_; |
| 96 | 90 |
| 97 // The top-level ruleset for the country code. Passed to the callback method | |
| 98 // as the result of the query. | |
| 99 scoped_ptr<Ruleset> root_; | |
| 100 | |
| 101 // The default language for the country code. This value is parsed from the | |
| 102 // country-level rule for the country code and is used to filter out the | |
| 103 // default language from the list of all supported languages for a country. | |
| 104 // For example, the list of supported languages for Canada is ["en", "fr"], | |
| 105 // but the default language is "en". Data requests for "data/CA/AB--fr" will | |
| 106 // succeed, but "data/CA/AB--en" will not return data. | |
| 107 std::string default_language_; | |
| 108 | |
| 109 // The list of all supported languages for the country code. This value is | |
| 110 // parsed from the country-level rule for the country and is used to download | |
| 111 // language-specific rules. | |
| 112 std::vector<std::string> languages_; | |
| 113 | |
| 114 DISALLOW_COPY_AND_ASSIGN(CountryRulesAggregator); | 91 DISALLOW_COPY_AND_ASSIGN(CountryRulesAggregator); |
| 115 }; | 92 }; |
| 116 | 93 |
| 117 } // namespace addressinput | 94 } // namespace addressinput |
| 118 } // namespace i18n | 95 } // namespace i18n |
| 119 | 96 |
| 120 #endif // I18N_ADDRESSINPUT_COUNTRY_RULES_AGGREGATOR_H_ | 97 #endif // I18N_ADDRESSINPUT_COUNTRY_RULES_AGGREGATOR_H_ |
| OLD | NEW |