Chromium Code Reviews| 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_RETRIEVER_H_ | |
| 16 #define I18N_ADDRESSINPUT_COUNTRY_RULES_RETRIEVER_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 // Retrieves a ruleset for a country. Sample usage: | |
| 33 // class MyClass { | |
| 34 // public: | |
| 35 // MyClass() : retriever_(scoped_ptr<const Retriever>(...)) {} | |
| 36 // | |
| 37 // ~MyClass() {} | |
| 38 // | |
| 39 // void GetRuleset(const std::string& country_code) { | |
| 40 // retriever_.RetrieveRules( | |
| 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 // CountryRulesRetriever retriever_; | |
| 53 // | |
| 54 // DISALLOW_COPY_AND_ASSIGN(CountryRulesRetriever); | |
| 55 // }; | |
| 56 class CountryRulesRetriever { | |
|
Evan Stade
2014/01/08 21:08:59
please call this something else because this name
please use gerrit instead
2014/01/09 00:38:11
Done.
| |
| 57 public: | |
| 58 typedef i18n::addressinput::ScopedPtrCallback<std::string, Ruleset> Callback; | |
| 59 | |
| 60 explicit CountryRulesRetriever(scoped_ptr<Retriever> retriever); | |
| 61 | |
| 62 ~CountryRulesRetriever(); | |
| 63 | |
| 64 // Recursively retrieves all of the rules for |country_code| and its | |
| 65 // administrative areas, localities, and dependent localities. Also retrieves | |
| 66 // the language-specific rules. Abandons previous requests if invoked multiple | |
| 67 // times. Invokes the |rules_ready| callback when finished. | |
| 68 void RetrieveRules(const std::string& country_code, | |
| 69 scoped_ptr<Callback> rules_ready); | |
| 70 | |
| 71 private: | |
| 72 struct RequestData; | |
| 73 | |
| 74 // Callback for Retriever::Retrieve() method. | |
| 75 void OnDataReady(bool success, | |
| 76 const std::string& key, | |
| 77 const std::string& data); | |
| 78 | |
| 79 // Abandons all requests and clears all retrieved data. | |
| 80 void Reset(); | |
| 81 | |
| 82 // The data retriever that can download serialized rules for one sub-region at | |
| 83 // a time. | |
| 84 scoped_ptr<Retriever> retriever_; | |
| 85 | |
| 86 // A mapping of data keys (e.g., "data/CA/AB--fr") to information that helps | |
| 87 // to parse the response data and place it the correct location in the | |
| 88 // rulesets. | |
| 89 std::map<std::string, RequestData> requests_; | |
| 90 | |
| 91 // The country code for which to retrieve the ruleset. Passed to the callback | |
| 92 // method to identify the ruleset. Examples: "US", "CA", "CH", etc. | |
| 93 std::string country_code_; | |
| 94 | |
| 95 // The callback to invoke when the ruleset has been retrieved. | |
| 96 scoped_ptr<CountryRulesRetriever::Callback> rules_ready_; | |
| 97 | |
| 98 // The top-level ruleset for the country code. Passed to the callback method | |
| 99 // as the result of the query. | |
| 100 scoped_ptr<Ruleset> root_; | |
| 101 | |
| 102 // False if at least one request to Retriever failed. Passed to the callback | |
| 103 // method as an indication of whether ruleset was retrieved successfully. | |
| 104 bool success_; | |
|
Evan Stade
2014/01/08 21:08:59
you don't need this variable any more.
please use gerrit instead
2014/01/09 00:38:11
Done.
| |
| 105 | |
| 106 // The default language for the country code. This value is parsed from the | |
| 107 // country-level rule for the country code and is used to filter out the | |
| 108 // default language from the list of all supported languages for a country. | |
| 109 // For example, the list of supported languages for Canada is ["en", "fr"], | |
| 110 // but the default language is "en". Data requests for "data/CA/AB--fr" will | |
| 111 // succeed, but "data/CA/AB--en" will not return data. | |
| 112 std::string default_language_; | |
| 113 | |
| 114 // The list of all supported languages for the country code. This value is | |
| 115 // parsed from the country-level rule for the country and is used to download | |
| 116 // language-specific rules. | |
| 117 std::vector<std::string> languages_; | |
| 118 | |
| 119 DISALLOW_COPY_AND_ASSIGN(CountryRulesRetriever); | |
| 120 }; | |
| 121 | |
| 122 } // namespace addressinput | |
| 123 } // namespace i18n | |
| 124 | |
| 125 #endif // I18N_ADDRESSINPUT_COUNTRY_RULES_RETRIEVER_H_ | |
| OLD | NEW |