OLD | NEW |
(Empty) | |
| 1 // Copyright (C) 2014 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 #ifndef I18N_ADDRESSINPUT_COUNTRY_RULES_AGGREGATOR_H_ |
| 16 #define I18N_ADDRESSINPUT_COUNTRY_RULES_AGGREGATOR_H_ |
| 17 |
| 18 #include <libaddressinput/callback.h> |
| 19 #include <libaddressinput/util/basictypes.h> |
| 20 #include <libaddressinput/util/scoped_ptr.h> |
| 21 |
| 22 #include <map> |
| 23 #include <string> |
| 24 #include <vector> |
| 25 |
| 26 namespace i18n { |
| 27 namespace addressinput { |
| 28 |
| 29 class Retriever; |
| 30 class Ruleset; |
| 31 |
| 32 // Aggregates a ruleset for a country. Sample usage: |
| 33 // class MyClass { |
| 34 // public: |
| 35 // MyClass() : aggregator_(scoped_ptr<const Retriever>(...)) {} |
| 36 // |
| 37 // ~MyClass() {} |
| 38 // |
| 39 // void GetRuleset(const std::string& country_code) { |
| 40 // aggregator_.AggregateRules( |
| 41 // country_code, |
| 42 // BuildScopedPtrCallback(this, &MyClass::OnRulesetReady)); |
| 43 // } |
| 44 // |
| 45 // void OnRulesetReady(bool success, |
| 46 // const std::string& country_code, |
| 47 // scoped_ptr<Ruleset> ruleset) { |
| 48 // ... |
| 49 // } |
| 50 // |
| 51 // private: |
| 52 // CountryRulesAggregator aggregator_; |
| 53 // |
| 54 // DISALLOW_COPY_AND_ASSIGN(MyClass); |
| 55 // }; |
| 56 class CountryRulesAggregator { |
| 57 public: |
| 58 typedef i18n::addressinput::ScopedPtrCallback<std::string, Ruleset> Callback; |
| 59 |
| 60 explicit CountryRulesAggregator(scoped_ptr<Retriever> retriever); |
| 61 ~CountryRulesAggregator(); |
| 62 |
| 63 // Recursively retrieves all of the rules for |country_code| and its |
| 64 // administrative areas, localities, and dependent localities. Also retrieves |
| 65 // the language-specific rules. Abandons previous requests if invoked multiple |
| 66 // times. Invokes the |rules_ready| callback when finished. |
| 67 void AggregateRules(const std::string& country_code, |
| 68 scoped_ptr<Callback> rules_ready); |
| 69 |
| 70 private: |
| 71 struct RequestData; |
| 72 |
| 73 // Callback for Retriever::Retrieve() method. |
| 74 void OnDataReady(bool success, |
| 75 const std::string& key, |
| 76 const std::string& data); |
| 77 |
| 78 // Abandons all requests and clears all retrieved data. |
| 79 void Reset(); |
| 80 |
| 81 // The data retriever that can download serialized rules for one sub-region at |
| 82 // a time. |
| 83 scoped_ptr<Retriever> retriever_; |
| 84 |
| 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 |
| 91 // method to identify the ruleset. Examples: "US", "CA", "CH", etc. |
| 92 std::string country_code_; |
| 93 |
| 94 // The callback to invoke when the ruleset has been retrieved. |
| 95 scoped_ptr<Callback> rules_ready_; |
| 96 |
| 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); |
| 115 }; |
| 116 |
| 117 } // namespace addressinput |
| 118 } // namespace i18n |
| 119 |
| 120 #endif // I18N_ADDRESSINPUT_COUNTRY_RULES_AGGREGATOR_H_ |
OLD | NEW |