Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / autofill / core / browser / phone_number.h
blob0b69b19bf4b469a9c567145710a9a18e8864b156
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_PHONE_NUMBER_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_PHONE_NUMBER_H_
8 #include <string>
9 #include <vector>
11 #include "base/gtest_prod_util.h"
12 #include "base/strings/string16.h"
13 #include "components/autofill/core/browser/form_group.h"
14 #include "components/autofill/core/browser/phone_number_i18n.h"
16 namespace autofill {
18 class AutofillProfile;
20 // A form group that stores phone number information.
21 class PhoneNumber : public FormGroup {
22 public:
23 explicit PhoneNumber(AutofillProfile* profile);
24 PhoneNumber(const PhoneNumber& number);
25 ~PhoneNumber() override;
27 PhoneNumber& operator=(const PhoneNumber& number);
29 void set_profile(AutofillProfile* profile) { profile_ = profile; }
31 // FormGroup implementation:
32 void GetMatchingTypes(const base::string16& text,
33 const std::string& app_locale,
34 ServerFieldTypeSet* matching_types) const override;
35 base::string16 GetRawInfo(ServerFieldType type) const override;
36 void SetRawInfo(ServerFieldType type, const base::string16& value) override;
37 base::string16 GetInfo(const AutofillType& type,
38 const std::string& app_locale) const override;
39 bool SetInfo(const AutofillType& type,
40 const base::string16& value,
41 const std::string& app_locale) override;
43 // Size and offset of the prefix and suffix portions of phone numbers.
44 static const size_t kPrefixOffset = 0;
45 static const size_t kPrefixLength = 3;
46 static const size_t kSuffixOffset = 3;
47 static const size_t kSuffixLength = 4;
49 // The class used to combine home phone parts into a whole number.
50 class PhoneCombineHelper {
51 public:
52 PhoneCombineHelper();
53 ~PhoneCombineHelper();
55 // If |type| is a phone field type, saves the |value| accordingly and
56 // returns true. For all other field types returs false.
57 bool SetInfo(const AutofillType& type, const base::string16& value);
59 // Parses the number built up from pieces stored via SetInfo() according to
60 // the specified |profile|'s country code, falling back to the given
61 // |app_locale| if the |profile| has no associated country code. Returns
62 // true if parsing was successful, false otherwise.
63 bool ParseNumber(const AutofillProfile& profile,
64 const std::string& app_locale,
65 base::string16* value);
67 // Returns true if both |phone_| and |whole_number_| are empty.
68 bool IsEmpty() const;
70 private:
71 base::string16 country_;
72 base::string16 city_;
73 base::string16 phone_;
74 base::string16 whole_number_;
77 private:
78 // FormGroup:
79 void GetSupportedTypes(ServerFieldTypeSet* supported_types) const override;
81 // Updates the cached parsed number if the profile's region has changed
82 // since the last time the cache was updated.
83 void UpdateCacheIfNeeded(const std::string& app_locale) const;
85 // The phone number.
86 base::string16 number_;
87 // Profile which stores the region used as hint when normalizing the number.
88 const AutofillProfile* profile_; // WEAK
90 // Cached number.
91 mutable i18n::PhoneObject cached_parsed_phone_;
94 } // namespace autofill
96 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_PHONE_NUMBER_H_