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_
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"
18 class AutofillProfile
;
20 // A form group that stores phone number information.
21 class PhoneNumber
: public FormGroup
{
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
{
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.
71 base::string16 country_
;
73 base::string16 phone_
;
74 base::string16 whole_number_
;
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;
86 base::string16 number_
;
87 // Profile which stores the region used as hint when normalizing the number.
88 const AutofillProfile
* profile_
; // WEAK
91 mutable i18n::PhoneObject cached_parsed_phone_
;
94 } // namespace autofill
96 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_PHONE_NUMBER_H_