phone_number.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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.
4
5#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_PHONE_NUMBER_H_
6#define COMPONENTS_AUTOFILL_CORE_BROWSER_PHONE_NUMBER_H_
7
8#include <string>
9#include <vector>
10
11#include "base/gtest_prod_util.h"
12#include "base/strings/string16.h"
13#include "components/autofill/core/browser/autofill_type.h"
14#include "components/autofill/core/browser/form_group.h"
15#include "components/autofill/core/browser/phone_number_i18n.h"
16
17namespace autofill {
18
19class AutofillProfile;
20
21// A form group that stores phone number information.
22class PhoneNumber : public FormGroup {
23 public:
24  explicit PhoneNumber(AutofillProfile* profile);
25  PhoneNumber(const PhoneNumber& number);
26  virtual ~PhoneNumber();
27
28  PhoneNumber& operator=(const PhoneNumber& number);
29
30  void set_profile(AutofillProfile* profile) { profile_ = profile; }
31
32  // FormGroup implementation:
33  virtual void GetMatchingTypes(const base::string16& text,
34                                const std::string& app_locale,
35                                FieldTypeSet* matching_types) const OVERRIDE;
36  virtual base::string16 GetRawInfo(AutofillFieldType type) const OVERRIDE;
37  virtual void SetRawInfo(AutofillFieldType type,
38                          const base::string16& value) OVERRIDE;
39  virtual base::string16 GetInfo(AutofillFieldType type,
40                           const std::string& app_locale) const OVERRIDE;
41  virtual bool SetInfo(AutofillFieldType type,
42                       const base::string16& value,
43                       const std::string& app_locale) OVERRIDE;
44
45  // Size and offset of the prefix and suffix portions of phone numbers.
46  static const size_t kPrefixOffset = 0;
47  static const size_t kPrefixLength = 3;
48  static const size_t kSuffixOffset = 3;
49  static const size_t kSuffixLength = 4;
50
51  // The class used to combine home phone parts into a whole number.
52  class PhoneCombineHelper {
53   public:
54    PhoneCombineHelper();
55    ~PhoneCombineHelper();
56
57    // If |type| is a phone field type, saves the |value| accordingly and
58    // returns true.  For all other field types returs false.
59    bool SetInfo(AutofillFieldType type, const base::string16& value);
60
61    // Parses the number built up from pieces stored via SetInfo() according to
62    // the specified |profile|'s country code, falling back to the given
63    // |app_locale| if the |profile| has no associated country code.  Returns
64    // true if parsing was successful, false otherwise.
65    bool ParseNumber(const AutofillProfile& profile,
66                     const std::string& app_locale,
67                     base::string16* value);
68
69    // Returns true if both |phone_| and |whole_number_| are empty.
70    bool IsEmpty() const;
71
72   private:
73    base::string16 country_;
74    base::string16 city_;
75    base::string16 phone_;
76    base::string16 whole_number_;
77  };
78
79 private:
80  // FormGroup:
81  virtual void GetSupportedTypes(FieldTypeSet* supported_types) const OVERRIDE;
82
83  // Updates the cached parsed number if the profile's region has changed
84  // since the last time the cache was updated.
85  void UpdateCacheIfNeeded(const std::string& app_locale) const;
86
87  // The phone number.
88  base::string16 number_;
89  // Profile which stores the region used as hint when normalizing the number.
90  const AutofillProfile* profile_;  // WEAK
91
92  // Cached number.
93  mutable i18n::PhoneObject cached_parsed_phone_;
94};
95
96}  // namespace autofill
97
98#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_PHONE_NUMBER_H_
99