Chromium Code Reviews| Index: third_party/libaddressinput/chromium/cpp/src/country_rules_retriever.cc |
| diff --git a/third_party/libaddressinput/chromium/cpp/src/country_rules_retriever.cc b/third_party/libaddressinput/chromium/cpp/src/country_rules_retriever.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c0ccf26e836a7816bdf57f12b0fcb543fe337629 |
| --- /dev/null |
| +++ b/third_party/libaddressinput/chromium/cpp/src/country_rules_retriever.cc |
| @@ -0,0 +1,201 @@ |
| +// Copyright (C) 2014 Google Inc. |
| +// |
| +// Licensed under the Apache License, Version 2.0 (the "License"); |
| +// you may not use this file except in compliance with the License. |
| +// You may obtain a copy of the License at |
| +// |
| +// http://www.apache.org/licenses/LICENSE-2.0 |
| +// |
| +// Unless required by applicable law or agreed to in writing, software |
| +// distributed under the License is distributed on an "AS IS" BASIS, |
| +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| +// See the License for the specific language governing permissions and |
| +// limitations under the License. |
| + |
| +#include "country_rules_retriever.h" |
| + |
| +#include <libaddressinput/address_field.h> |
| +#include <libaddressinput/callback.h> |
| +#include <libaddressinput/util/basictypes.h> |
| +#include <libaddressinput/util/scoped_ptr.h> |
| + |
| +#include <cassert> |
| +#include <cstddef> |
| +#include <map> |
| +#include <string> |
| +#include <utility> |
| + |
| +#include "retriever.h" |
| +#include "rule.h" |
| +#include "ruleset.h" |
| +#include "util/stl_util.h" |
| + |
| +namespace i18n { |
| +namespace addressinput { |
| + |
| +// Information about data requests sent to Retriever. This data is not returned |
| +// as part of the ruleset, but is useful in constructing the ruleset |
| +// asynchronously. |
| +struct CountryRulesRetriever::RequestData { |
| + // Does not take ownership of |parent|. |
| + RequestData(Ruleset* parent, |
| + AddressField level, |
| + bool is_language_code, |
| + const std::string& id) |
| + : parent(parent), |
| + level(level), |
| + is_language_code(is_language_code), |
| + id(id) {} |
| + |
| + ~RequestData() {} |
| + |
| + // The parent ruleset of the data being downloaded. If NULL, then this is the |
| + // root ruleset at the COUNTRY level. The language-specific and sub-region |
| + // rules are added to this ruleset. Owned by |CountryRulesRetriever|. |
| + Ruleset* parent; |
| + |
| + // The level of the data being requested. Ranges from COUNTRY to |
| + // DEPENDENT_LOCALITY. If COUNTRY, then the rule should use default rules from |
| + // Rule::GetDefault(). |
| + AddressField level; |
| + |
| + // If true, then |id| is a language. The data received for this request should |
| + // be placed into a language-specific rule. |
| + bool is_language_code; |
| + |
| + // Can be a region name (e.g. "CA") or a language (e.g. "fr"). Used to add a |
| + // sub-region or a language-specific rule to |parent|. |
| + std::string id; |
| +}; |
| + |
| +CountryRulesRetriever::CountryRulesRetriever(scoped_ptr<Retriever> retriever) |
| + : retriever_(retriever.Pass()), |
| + requests_(), |
| + country_code_(), |
| + rules_ready_(), |
| + root_(), |
| + success_(true), |
| + default_language_(), |
| + languages_() { |
| + assert(retriever_ != NULL); |
| +} |
| + |
| +void CountryRulesRetriever::RetrieveRules(const std::string& country_code, |
| + scoped_ptr<Callback> rules_ready) { |
| + Reset(); |
| + country_code_ = country_code; |
| + rules_ready_.reset(rules_ready.release()); |
| + |
| + // Key construction: |
| + // https://code.google.com/p/libaddressinput/wiki/AddressValidationMetadata |
| + // Example of a country-level key: "data/CA". |
| + std::string key = "data/" + country_code_; |
| + requests_.insert(std::make_pair( |
| + key, RequestData(NULL, COUNTRY, false, std::string()))); |
| + |
| + retriever_->Retrieve( |
| + key, BuildCallback(this, &CountryRulesRetriever::OnDataReady)); |
| +} |
| + |
| +void CountryRulesRetriever::OnDataReady(bool success, |
| + const std::string& key, |
| + const std::string& data) { |
| + std::map<std::string, RequestData>::iterator request_it = |
| + requests_.find(key); |
| + if (request_it == requests_.end()) { |
| + return; // An abandoned request. |
| + } |
| + |
| + RequestData request = request_it->second; |
| + requests_.erase(request_it); |
| + |
| + success_ &= success; |
| + |
| + // All country-level rules are based on the default rule. |
| + scoped_ptr<Rule> rule(new Rule); |
| + if (request.level == COUNTRY) { |
| + rule->CopyFrom(Rule::GetDefault()); |
| + } |
| + |
| + success_ &= rule->ParseSerializedRule(data); |
| + |
| + // Place the rule in the correct location in the ruleset. |
| + Ruleset* ruleset = NULL; |
| + if (request.is_language_code) { |
| + assert(request.parent != NULL); |
| + request.parent->AddLanguageCode(request.id, rule.Pass()); |
| + ruleset = request.parent; |
| + } else if (request.level == COUNTRY) { |
| + // The default language and all supported languages for the country code are |
| + // in the country-level rule without a language code identifier. For |
| + // example: "data/CA". |
| + default_language_ = rule->GetLanguage(); |
| + languages_ = rule->GetLanguages(); |
| + |
| + root_.reset(new Ruleset(rule.Pass())); |
| + ruleset = root_.get(); |
| + } else { |
| + assert(request.parent != NULL); |
| + ruleset = request.parent->AddSubRegion(request.id, rule.Pass()); |
| + } |
| + |
| + if (!request.is_language_code) { |
| + // Retrieve the language-specific rules for this region. |
| + for (std::vector<std::string>::const_iterator |
| + lang_it = languages_.begin(); |
| + lang_it != languages_.end(); |
| + ++lang_it) { |
| + if (*lang_it == default_language_) { |
| + continue; |
| + } |
| + // Example of a language-specific key: "data/CA--fr". |
| + std::string language_code_key = key + "--" + *lang_it; |
| + requests_.insert(std::make_pair( |
| + key, RequestData(ruleset, request.level, true, *lang_it))); |
| + retriever_->Retrieve( |
| + language_code_key, |
| + BuildCallback(this, &CountryRulesRetriever::OnDataReady)); |
| + } |
| + |
| + if (request.level < DEPENDENT_LOCALITY) { |
| + // Retrieve the sub-region rules for this region. |
| + for (std::vector<std::string>::const_iterator |
| + subkey_it = ruleset->rule().GetSubKeys().begin(); |
| + subkey_it != ruleset->rule().GetSubKeys().end(); |
| + ++subkey_it) { |
| + // Example of a sub-region key: "data/CA/AB". |
| + std::string sub_region_key = key + "/" + *subkey_it; |
| + requests_.insert(std::make_pair( |
| + key, |
| + RequestData(ruleset, |
| + static_cast<AddressField>(request.level + 1), |
| + false, |
| + *subkey_it))); |
| + retriever_->Retrieve( |
| + sub_region_key, |
| + BuildCallback(this, &CountryRulesRetriever::OnDataReady)); |
| + } |
| + } |
| + } |
| + |
| + if (requests_.empty()) { |
| + (*rules_ready_)(success_, country_code_, root_.Pass()); |
| + Reset(); |
| + } |
| +} |
| + |
| +void CountryRulesRetriever::Reset() { |
| + requests_.clear(); |
| + country_code_.clear(); |
| + rules_ready_.reset(); |
| + root_.reset(); |
| + success_ = true; |
| + default_language_.clear(); |
| + languages_.clear(); |
| + rules_ready_.reset(); |
| + default_language_.clear(); |
| + languages_.clear(); |
|
please use gerrit instead
2014/01/07 23:00:42
Err, copy-paste error. Fix incoming.
please use gerrit instead
2014/01/07 23:03:42
Done.
|
| +} |
| + |
| +} // namespace addressinput |
| +} // namespace i18n |