f0895f3a20239a3bcfffa1df8995a2d0ff4ca080
[platform/framework/web/crosswalk.git] / src / third_party / libaddressinput / src / cpp / src / validation_task.h
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_VALIDATION_TASK_H_
16 #define I18N_ADDRESSINPUT_VALIDATION_TASK_H_
17
18 #include <libaddressinput/address_field.h>
19 #include <libaddressinput/address_problem.h>
20 #include <libaddressinput/address_validator.h>
21 #include <libaddressinput/supplier.h>
22 #include <libaddressinput/util/basictypes.h>
23 #include <libaddressinput/util/scoped_ptr.h>
24
25 namespace i18n {
26 namespace addressinput {
27
28 class LookupKey;
29 class Rule;
30 struct AddressData;
31
32 // A ValidationTask object encapsulates the information necessary to perform
33 // validation of one particular address and call a callback when that has been
34 // done. Calling the Run() method will load required metadata, then perform
35 // validation, call the callback and delete the ValidationTask object itself.
36 class ValidationTask {
37  public:
38   ValidationTask(const AddressData& address,
39                  bool allow_postal,
40                  bool require_name,
41                  const FieldProblemMap* filter,
42                  FieldProblemMap* problems,
43                  const AddressValidator::Callback& validated);
44
45   ~ValidationTask();
46
47   // Calls supplier->Load(), with Validate() as callback.
48   void Run(Supplier* supplier) const;
49
50  private:
51   friend class ValidationTaskTest;
52
53   // Uses the address metadata of |hierarchy| to validate |address_|, writing
54   // problems found into |problems_|, then calls the |validated_| callback and
55   // deletes this ValidationTask object.
56   void Validate(bool success,
57                 const LookupKey& lookup_key,
58                 const Supplier::RuleHierarchy& hierarchy);
59
60   // Checks all fields for UNEXPECTED_FIELD problems.
61   void CheckUnexpectedField(const std::string& region_code) const;
62
63   // Checks all fields for MISSING_REQUIRED_FIELD problems.
64   void CheckMissingRequiredField(const std::string& region_code) const;
65
66   // Checks the hierarchical fields for UNKNOWN_VALUE problems.
67   void CheckUnknownValue(
68       const Supplier::RuleHierarchy& hierarchy) const;
69
70   // Checks the POSTAL_CODE field for problems.
71   void CheckPostalCodeFormatAndValue(
72       const Supplier::RuleHierarchy& hierarchy) const;
73
74   // Checks the STREET_ADDRESS field for USES_P_O_BOX problems.
75   void CheckUsesPoBox(
76       const Supplier::RuleHierarchy& hierarchy) const;
77
78   // Writes (|field|,|problem|) to |problems_|.
79   void ReportProblem(AddressField field, AddressProblem problem) const;
80
81   // Writes (|field|,|problem|) to |problems_|, if this pair should be reported.
82   void ReportProblemMaybe(AddressField field, AddressProblem problem) const;
83
84   // Returns whether (|field|,|problem|) should be reported.
85   bool ShouldReport(AddressField field, AddressProblem problem) const;
86
87   const AddressData& address_;
88   const bool allow_postal_;
89   const bool require_name_;
90   const FieldProblemMap* filter_;
91   FieldProblemMap* const problems_;
92   const AddressValidator::Callback& validated_;
93   const scoped_ptr<const Supplier::Callback> supplied_;
94   const scoped_ptr<LookupKey> lookup_key_;
95
96   DISALLOW_COPY_AND_ASSIGN(ValidationTask);
97 };
98
99 }  // namespace addressinput
100 }  // namespace i18n
101
102 #endif  // I18N_ADDRESSINPUT_VALIDATION_TASK_H_