| OLD | NEW |
| 1 // Copyright (C) 2013 Google Inc. | 1 // Copyright (C) 2013 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 #include "rule_retriever.h" | 15 #include "rule_retriever.h" |
| 16 | 16 |
| 17 #include <libaddressinput/address_field.h> |
| 17 #include <libaddressinput/callback.h> | 18 #include <libaddressinput/callback.h> |
| 18 #include <libaddressinput/util/basictypes.h> | 19 #include <libaddressinput/util/basictypes.h> |
| 19 #include <libaddressinput/util/scoped_ptr.h> | 20 #include <libaddressinput/util/scoped_ptr.h> |
| 20 | 21 |
| 21 #include <cassert> | 22 #include <cassert> |
| 22 #include <cstddef> | 23 #include <cstddef> |
| 23 #include <string> | 24 #include <string> |
| 24 | 25 |
| 26 #include "lookup_key.h" |
| 25 #include "retriever.h" | 27 #include "retriever.h" |
| 26 #include "rule.h" | 28 #include "rule.h" |
| 27 | 29 |
| 28 namespace i18n { | 30 namespace i18n { |
| 29 namespace addressinput { | 31 namespace addressinput { |
| 30 | 32 |
| 31 namespace { | 33 namespace { |
| 32 | 34 |
| 33 class Helper { | 35 class Helper { |
| 34 public: | 36 public: |
| 35 Helper(const std::string& key, | 37 Helper(const LookupKey& key, |
| 36 const RuleRetriever::Callback& rule_ready, | 38 const RuleRetriever::Callback& rule_ready, |
| 37 const Retriever& data_retriever) | 39 const Retriever& data_retriever) |
| 38 : rule_ready_(rule_ready), | 40 : key_(key), |
| 41 rule_ready_(rule_ready), |
| 39 data_retrieved_(BuildCallback(this, &Helper::OnDataRetrieved)) { | 42 data_retrieved_(BuildCallback(this, &Helper::OnDataRetrieved)) { |
| 40 data_retriever.Retrieve(key, *data_retrieved_); | 43 data_retriever.Retrieve(key_.GetKeyValue(), *data_retrieved_); |
| 41 } | 44 } |
| 42 | 45 |
| 43 private: | 46 private: |
| 44 ~Helper() {} | 47 ~Helper() {} |
| 45 | 48 |
| 46 void OnDataRetrieved(bool success, | 49 void OnDataRetrieved(bool success, |
| 47 const std::string& key, | 50 const std::string& key, |
| 48 const std::string& data) { | 51 const std::string& data) { |
| 49 Rule rule; | 52 Rule rule; |
| 50 if (!success) { | 53 if (!success) { |
| 51 rule_ready_(false, key, rule); | 54 rule_ready_(false, key_, rule); |
| 52 } else { | 55 } else { |
| 56 if (key_.GetLevel() == COUNTRY) { |
| 57 rule.CopyFrom(Rule::GetDefault()); |
| 58 } |
| 53 success = rule.ParseSerializedRule(data); | 59 success = rule.ParseSerializedRule(data); |
| 54 rule_ready_(success, key, rule); | 60 rule_ready_(success, key_, rule); |
| 55 } | 61 } |
| 56 delete this; | 62 delete this; |
| 57 } | 63 } |
| 58 | 64 |
| 65 const LookupKey& key_; |
| 59 const RuleRetriever::Callback& rule_ready_; | 66 const RuleRetriever::Callback& rule_ready_; |
| 60 scoped_ptr<Retriever::Callback> data_retrieved_; | 67 scoped_ptr<Retriever::Callback> data_retrieved_; |
| 61 | 68 |
| 62 DISALLOW_COPY_AND_ASSIGN(Helper); | 69 DISALLOW_COPY_AND_ASSIGN(Helper); |
| 63 }; | 70 }; |
| 64 | 71 |
| 65 } // namespace | 72 } // namespace |
| 66 | 73 |
| 67 RuleRetriever::RuleRetriever(const Retriever* retriever) | 74 RuleRetriever::RuleRetriever(const Retriever* retriever) |
| 68 : data_retriever_(retriever) { | 75 : data_retriever_(retriever) { |
| 69 assert(data_retriever_ != NULL); | 76 assert(data_retriever_ != NULL); |
| 70 } | 77 } |
| 71 | 78 |
| 72 RuleRetriever::~RuleRetriever() {} | 79 RuleRetriever::~RuleRetriever() {} |
| 73 | 80 |
| 74 void RuleRetriever::RetrieveRule(const std::string& key, | 81 void RuleRetriever::RetrieveRule(const LookupKey& key, |
| 75 const Callback& rule_ready) const { | 82 const Callback& rule_ready) const { |
| 76 new Helper(key, rule_ready, *data_retriever_); | 83 new Helper(key, rule_ready, *data_retriever_); |
| 77 } | 84 } |
| 78 | 85 |
| 79 } // namespace addressinput | 86 } // namespace addressinput |
| 80 } // namespace i18n | 87 } // namespace i18n |
| OLD | NEW |