Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3067)

Unified Diff: third_party/libaddressinput/chromium/cpp/src/country_rules_retriever.cc

Issue 109323011: [rac] Download all rules for a country code in libaddressinput. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge smaller patches. Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..6eab254351772cb6b29b934e859bff1094e53125
--- /dev/null
+++ b/third_party/libaddressinput/chromium/cpp/src/country_rules_retriever.cc
@@ -0,0 +1,141 @@
+// Copyright (C) 2013 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 <string>
+
+#include "rule.h"
+#include "lookup_key.h"
+#include "rule_retriever.h"
+
+namespace i18n {
+namespace addressinput {
+
+namespace {
+
+class Helper {
+ public:
+ Helper(const std::string& country_code,
+ const CountryRulesRetriever::Callback& rules_ready,
+ const RuleRetriever& rule_retriever,
+ LookupKey* root)
+ : country_code_(country_code),
+ rules_ready_(rules_ready),
+ rule_retriever_(rule_retriever),
+ root_(root),
+ rule_ready_(BuildCallback(this, &Helper::OnRuleReady)),
+ num_requested_(1),
+ num_received_(0),
+ success_(true),
+ default_language_(),
+ languages_() {
+ assert(root_ != NULL);
+ rule_retriever_.RetrieveRule(*root_, *rule_ready_);
+ }
+
+ private:
+ ~Helper() {}
+
+ void OnRuleReady(bool success, const LookupKey& key, const Rule& rule) {
+ success_ &= success;
+ ++num_received_;
+ if (key.GetLevel() == COUNTRY && default_language_.empty()) {
+ default_language_ = rule.GetLanguage();
+ languages_ = rule.GetLanguages();
+ }
+
+ // TODO(rouslan): This rule copy can get expensive. If we decide to stick
+ // with this design, then we should change the RuleRetriever to return a
+ // pointer to the Rule object instead of a const-ref.
+ Rule* rule_copy = new Rule();
+ rule_copy->CopyFrom(rule);
+
+ // Takes ownership of |rule_copy|.
+ //
+ // TODO(rouslan): These const_casts seem unnecessary. Perhaps we should
+ // change the callbacks to return non-constant references to keys.
+ const_cast<LookupKey&>(key).SetRule(rule_copy);
+
+ if (default_language_.empty() || rule.GetLanguage() == default_language_) {
+ for (std::vector<std::string>::const_iterator\
+ lang_it = languages_.begin();
+ lang_it != languages_.end();
+ ++lang_it) {
+ if (*lang_it != default_language_) {
+ ++num_requested_;
+ rule_retriever_.RetrieveRule(
+ *const_cast<LookupKey&>(key).AddLanguageCodeKey(*lang_it),
+ *rule_ready_);
+ }
+ }
+
+ if (key.GetLevel() < DEPENDENT_LOCALITY) {
+ for (std::vector<std::string>::const_iterator
+ subkey_it = rule.GetSubKeys().begin();
+ subkey_it != rule.GetSubKeys().end();
+ ++subkey_it) {
+ ++num_requested_;
+ rule_retriever_.RetrieveRule(
+ *const_cast<LookupKey&>(key).AddSubKey(*subkey_it),
+ *rule_ready_);
+ }
+ }
+ }
+
+ if (num_received_ == num_requested_) {
+ rules_ready_(success_, country_code_, *root_);
+ delete this;
+ }
+ }
+
+ const std::string country_code_;
+ const CountryRulesRetriever::Callback& rules_ready_;
+ const RuleRetriever& rule_retriever_;
+ LookupKey* root_;
+ scoped_ptr<RuleRetriever::Callback> rule_ready_;
+ int num_requested_;
+ int num_received_;
+ bool success_;
+ std::string default_language_;
+ std::vector<std::string> languages_;
+
+ DISALLOW_COPY_AND_ASSIGN(Helper);
+};
+
+} // namespace
+
+CountryRulesRetriever::CountryRulesRetriever(
+ const RuleRetriever* rule_retriever)
+ : rule_retriever_(rule_retriever) {
+ assert(rule_retriever_ != NULL);
+}
+
+CountryRulesRetriever::~CountryRulesRetriever() {}
+
+void CountryRulesRetriever::RetrieveRules(const std::string& country_code,
+ const Callback& rules_ready,
+ LookupKey* lookup_key) const {
+ new Helper(country_code, rules_ready, *rule_retriever_, lookup_key);
+}
+
+} // namespace addressinput
+} // namespace i18n

Powered by Google App Engine
This is Rietveld 408576698