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/form_group.h"
14#include "components/autofill/core/browser/phone_number_i18n.h"
15
16namespace autofill {
17
18class AutofillProfile;
19
20// A form group that stores phone number information.
21class PhoneNumber : public FormGroup {
22 public:
23  explicit PhoneNumber(AutofillProfile* profile);
24  PhoneNumber(const PhoneNumber& number);
25  virtual ~PhoneNumber();
26
27  PhoneNumber& operator=(const PhoneNumber& number);
28
29  void set_profile(AutofillProfile* profile) { profile_ = profile; }
30
31  // FormGroup implementation:
32  virtual void GetMatchingTypes(
33      const base::string16& text,
34      const std::string& app_locale,
35      ServerFieldTypeSet* matching_types) const OVERRIDE;
36  virtual base::string16 GetRawInfo(ServerFieldType type) const OVERRIDE;
37  virtual void SetRawInfo(ServerFieldType type,
38                          const base::string16& value) OVERRIDE;
39  virtual base::string16 GetInfo(const AutofillType& type,
40                           const std::string& app_locale) const OVERRIDE;
41  virtual bool SetInfo(const AutofillType& 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(const AutofillType& 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(
82      ServerFieldTypeSet* supported_types) const OVERRIDE;
83
84  // Updates the cached parsed number if the profile's region has changed
85  // since the last time the cache was updated.
86  void UpdateCacheIfNeeded(const std::string& app_locale) const;
87
88  // The phone number.
89  base::string16 number_;
90  // Profile which stores the region used as hint when normalizing the number.
91  const AutofillProfile* profile_;  // WEAK
92
93  // Cached number.
94  mutable i18n::PhoneObject cached_parsed_phone_;
95};
96
97}  // namespace autofill
98
99#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_PHONE_NUMBER_H_
100