summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authorjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-01-07 00:35:39 +0000
committerjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-01-07 00:35:39 +0000
commit3db3ff6a8ee531957ab460c5791243b4636d11e2 (patch)
treece81b1e2bf2bf1cb13f18c4111ec29febe2affbe /chrome
parente5e14c1ab452d48aaf401ed38c5b6e539e832373 (diff)
downloadchromium_src-3db3ff6a8ee531957ab460c5791243b4636d11e2.zip
chromium_src-3db3ff6a8ee531957ab460c5791243b4636d11e2.tar.gz
chromium_src-3db3ff6a8ee531957ab460c5791243b4636d11e2.tar.bz2
Add PhoneNumber, a form group that stores phone numbers.
BUG=none TEST=none Review URL: http://codereview.chromium.org/523100 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@35670 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/browser/autofill/fax_number.h42
-rw-r--r--chrome/browser/autofill/home_phone_number.h42
-rw-r--r--chrome/browser/autofill/phone_number.cc216
-rw-r--r--chrome/browser/autofill/phone_number.h81
-rwxr-xr-xchrome/chrome_browser.gypi4
5 files changed, 385 insertions, 0 deletions
diff --git a/chrome/browser/autofill/fax_number.h b/chrome/browser/autofill/fax_number.h
new file mode 100644
index 0000000..a034be6
--- /dev/null
+++ b/chrome/browser/autofill/fax_number.h
@@ -0,0 +1,42 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_AUTOFILL_FAX_NUMBER_H_
+#define CHROME_BROWSER_AUTOFILL_FAX_NUMBER_H_
+
+#include "chrome/browser/autofill/phone_number.h"
+
+class FormGroup;
+
+class FaxNumber : public PhoneNumber {
+ public:
+ virtual FormGroup* Clone() const { return new FaxNumber(*this); }
+
+ protected:
+ virtual AutoFillFieldType GetNumberType() const {
+ return PHONE_FAX_NUMBER;
+ }
+
+ virtual AutoFillFieldType GetCityCodeType() const {
+ return PHONE_FAX_CITY_CODE;
+ }
+
+ virtual AutoFillFieldType GetCountryCodeType() const {
+ return PHONE_FAX_COUNTRY_CODE;
+ }
+
+ virtual AutoFillFieldType GetCityAndNumberType() const {
+ return PHONE_FAX_CITY_AND_NUMBER;
+ }
+
+ virtual AutoFillFieldType GetWholeNumberType() const {
+ return PHONE_FAX_WHOLE_NUMBER;
+ }
+
+ private:
+ explicit FaxNumber(const FaxNumber& phone) : PhoneNumber(phone) {}
+ void operator=(const FaxNumber& phone);
+};
+
+#endif // CHROME_BROWSER_AUTOFILL_FAX_NUMBER_H_
diff --git a/chrome/browser/autofill/home_phone_number.h b/chrome/browser/autofill/home_phone_number.h
new file mode 100644
index 0000000..a3eaf9e
--- /dev/null
+++ b/chrome/browser/autofill/home_phone_number.h
@@ -0,0 +1,42 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_AUTOFILL_HOME_PHONE_NUMBER_H_
+#define CHROME_BROWSER_AUTOFILL_HOME_PHONE_NUMBER_H_
+
+#include "chrome/browser/autofill/phone_number.h"
+
+class FormGroup;
+
+class HomePhoneNumber : public PhoneNumber {
+ public:
+ virtual FormGroup* Clone() const { return new HomePhoneNumber(*this); }
+
+ protected:
+ virtual AutoFillFieldType GetNumberType() const {
+ return PHONE_HOME_NUMBER;
+ }
+
+ virtual AutoFillFieldType GetCityCodeType() const {
+ return PHONE_HOME_CITY_CODE;
+ }
+
+ virtual AutoFillFieldType GetCountryCodeType() const {
+ return PHONE_HOME_COUNTRY_CODE;
+ }
+
+ virtual AutoFillFieldType GetCityAndNumberType() const {
+ return PHONE_HOME_CITY_AND_NUMBER;
+ }
+
+ virtual AutoFillFieldType GetWholeNumberType() const {
+ return PHONE_HOME_WHOLE_NUMBER;
+ }
+
+ private:
+ explicit HomePhoneNumber(const HomePhoneNumber& phone) : PhoneNumber(phone) {}
+ void operator=(const HomePhoneNumber& phone);
+};
+
+#endif // CHROME_BROWSER_AUTOFILL_HOME_PHONE_NUMBER_H_
diff --git a/chrome/browser/autofill/phone_number.cc b/chrome/browser/autofill/phone_number.cc
new file mode 100644
index 0000000..97bb963
--- /dev/null
+++ b/chrome/browser/autofill/phone_number.cc
@@ -0,0 +1,216 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/autofill/phone_number.h"
+
+#include "base/basictypes.h"
+#include "base/string_util.h"
+#include "chrome/browser/autofill/autofill_type.h"
+#include "chrome/browser/autofill/field_types.h"
+
+static const string16 kPhoneNumberSeparators = ASCIIToUTF16(" .()-");
+
+static const AutoFillType::FieldTypeSubGroup kAutoFillPhoneTypes[] = {
+ AutoFillType::PHONE_NUMBER,
+ AutoFillType::PHONE_CITY_CODE,
+ AutoFillType::PHONE_COUNTRY_CODE,
+ AutoFillType::PHONE_CITY_AND_NUMBER,
+ AutoFillType::PHONE_WHOLE_NUMBER,
+};
+
+static const int kAutoFillPhoneLength = arraysize(kAutoFillPhoneTypes);
+
+void PhoneNumber::GetPossibleFieldTypes(const string16& text,
+ FieldTypeSet* possible_types) const {
+ string16 stripped_text(text);
+ StripPunctuation(&stripped_text);
+ if (!Validate(stripped_text))
+ return;
+
+ if (IsNumber(stripped_text))
+ possible_types->insert(GetNumberType());
+
+ if (IsCityCode(stripped_text))
+ possible_types->insert(GetCityCodeType());
+
+ if (IsCountryCode(stripped_text))
+ possible_types->insert(GetCountryCodeType());
+
+ if (IsCityAndNumber(stripped_text))
+ possible_types->insert(GetCityAndNumberType());
+
+ if (IsWholeNumber(stripped_text))
+ possible_types->insert(GetWholeNumberType());
+}
+
+string16 PhoneNumber::GetFieldText(const AutoFillType& type) const {
+ AutoFillFieldType field_type = type.field_type();
+ if (field_type == GetNumberType())
+ return number();
+
+ if (field_type == GetCityCodeType())
+ return city_code();
+
+ if (field_type == GetCountryCodeType())
+ return country_code();
+
+ if (field_type == GetCityAndNumberType())
+ return CityAndNumber();
+
+ if (field_type == GetWholeNumberType())
+ return WholeNumber();
+
+ return EmptyString16();
+}
+
+void PhoneNumber::FindInfoMatches(const AutoFillType& type,
+ const string16& info,
+ std::vector<string16>* matched_text) const {
+ if (matched_text == NULL) {
+ DLOG(ERROR) << "NULL matched vector passed in";
+ return;
+ }
+
+ string16 number(info);
+ StripPunctuation(&number);
+ if (!Validate(number))
+ return;
+
+ string16 match;
+ if (type.field_type() == UNKNOWN_TYPE) {
+ for (int i = 0; i < kAutoFillPhoneLength; ++i) {
+ if (FindInfoMatchesHelper(kAutoFillPhoneTypes[i], info, &match))
+ matched_text->push_back(match);
+ }
+ } else {
+ if (FindInfoMatchesHelper(type.subgroup(), info, &match))
+ matched_text->push_back(match);
+ }
+}
+
+void PhoneNumber::SetInfo(const AutoFillType& type, const string16& value) {
+ string16 number(value);
+ StripPunctuation(&number);
+ if (!Validate(number))
+ return;
+
+ FieldTypeSubGroup subgroup = type.subgroup();
+ if (subgroup == AutoFillType::PHONE_NUMBER)
+ set_number(number);
+ else if (subgroup == AutoFillType::PHONE_CITY_CODE)
+ set_city_code(number);
+ else if (subgroup == AutoFillType::PHONE_COUNTRY_CODE)
+ set_country_code(number);
+ else
+ NOTREACHED();
+ // TODO(jhawkins): Add extension support.
+ // else if (subgroup == AutoFillType::PHONE_EXTENSION)
+ // set_extension(number);
+}
+
+string16 PhoneNumber::WholeNumber() const {
+ string16 whole_number;
+ if (!country_code_.empty())
+ whole_number.append(country_code_);
+
+ if (!city_code_.empty())
+ whole_number.append(city_code_);
+
+ if (!number_.empty())
+ whole_number.append(number_);
+
+ return whole_number;
+}
+
+void PhoneNumber::set_number(const string16& number) {
+ string16 digits(number);
+ StripPunctuation(&digits);
+ number_ = digits;
+}
+
+PhoneNumber::PhoneNumber(const PhoneNumber& phone_number)
+ : country_code_(phone_number.country_code_),
+ city_code_(phone_number.city_code_),
+ number_(phone_number.number_),
+ extension_(phone_number.extension_) {
+}
+
+bool PhoneNumber::FindInfoMatchesHelper(const FieldTypeSubGroup& subgroup,
+ const string16& info,
+ string16* match) const {
+ if (match == NULL) {
+ DLOG(ERROR) << "NULL match string passed in";
+ return false;
+ }
+
+ match->clear();
+ if (subgroup == AutoFillType::PHONE_NUMBER &&
+ StartsWith(number(), info, true)) {
+ *match = number();
+ } else if (subgroup == AutoFillType::PHONE_CITY_CODE &&
+ StartsWith(city_code(), info, true)) {
+ *match = city_code();
+ } else if (subgroup == AutoFillType::PHONE_COUNTRY_CODE &&
+ StartsWith(country_code(), info, true)) {
+ *match = country_code();
+ } else if (subgroup == AutoFillType::PHONE_CITY_AND_NUMBER &&
+ StartsWith(CityAndNumber(), info, true)) {
+ *match = CityAndNumber();
+ } else if (subgroup == AutoFillType::PHONE_WHOLE_NUMBER &&
+ StartsWith(WholeNumber(), info, true)) {
+ *match = WholeNumber();
+ }
+
+ return !match->empty();
+}
+
+bool PhoneNumber::IsNumber(const string16& text) const {
+ if (text.length() <= number_.length())
+ return false;
+
+ return StartsWith(number_, text, false);
+}
+
+bool PhoneNumber::IsCityCode(const string16& text) const {
+ if (text.length() <= city_code_.length())
+ return false;
+
+ return StartsWith(city_code_, text, false);
+}
+
+bool PhoneNumber::IsCountryCode(const string16& text) const {
+ if (text.length() <= country_code_.length())
+ return false;
+
+ return StartsWith(country_code_, text, false);
+}
+
+bool PhoneNumber::IsCityAndNumber(const string16& text) const {
+ string16 city_and_number(CityAndNumber());
+ if (text.length() > city_and_number.length())
+ return false;
+
+ return StartsWith(city_and_number, text, false);
+}
+
+bool PhoneNumber::IsWholeNumber(const string16& text) const {
+ string16 whole_number(WholeNumber());
+ if (text.length() > whole_number.length())
+ return false;
+
+ return StartsWith(whole_number, text, false);
+}
+
+bool PhoneNumber::Validate(const string16& number) const {
+ for (size_t i = 0; i < number.length(); ++i) {
+ if (!IsAsciiDigit(number[i]))
+ return false;
+ }
+
+ return true;
+}
+
+void PhoneNumber::StripPunctuation(string16* number) const {
+ TrimString(*number, kPhoneNumberSeparators.c_str(), number);
+}
diff --git a/chrome/browser/autofill/phone_number.h b/chrome/browser/autofill/phone_number.h
new file mode 100644
index 0000000..225214e
--- /dev/null
+++ b/chrome/browser/autofill/phone_number.h
@@ -0,0 +1,81 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_AUTOFILL_PHONE_NUMBER_H_
+#define CHROME_BROWSER_AUTOFILL_PHONE_NUMBER_H_
+
+#include <vector>
+
+#include "base/string16.h"
+#include "chrome/browser/autofill/form_group.h"
+
+// A form group that stores phone number information.
+class PhoneNumber : public FormGroup {
+ public:
+ // FormGroup implementation:
+ virtual FormGroup* Clone() const = 0;
+ virtual void GetPossibleFieldTypes(const string16& text,
+ FieldTypeSet* possible_types) const;
+ virtual void FindInfoMatches(const AutoFillType& type,
+ const string16& info,
+ std::vector<string16>* matched_text) const;
+ virtual string16 GetFieldText(const AutoFillType& type) const;
+ virtual void SetInfo(const AutoFillType& type, const string16& value);
+
+ string16 country_code() const { return country_code_; }
+ string16 city_code() const { return city_code_; }
+ string16 number() const { return number_; }
+ string16 extension() const { return extension_; }
+ string16 CityAndNumber() const { return city_code_ + number_; }
+
+ // Returns the entire phone number as a string, without punctuation.
+ virtual string16 WholeNumber() const;
+
+ void set_country_code(const string16& country_code) {
+ country_code_ = country_code;
+ }
+ void set_city_code(const string16& city_code) { city_code_ = city_code; }
+ void set_number(const string16& number);
+ void set_extension(const string16& extension) { extension_ = extension; }
+
+ protected:
+ explicit PhoneNumber(const PhoneNumber& phone_number);
+ void operator=(const PhoneNumber& phone_number);
+
+ // A helper function for FindInfoMatches that only handles matching the info
+ // with the requested field type.
+ bool FindInfoMatchesHelper(const FieldTypeSubGroup& subgroup,
+ const string16& info,
+ string16* match) const;
+
+ // The numbers will be digits only (no punctuation), so any call to the IsX()
+ // functions should first call StripPunctuation on the text.
+ virtual bool IsNumber(const string16& text) const;
+ virtual bool IsCityCode(const string16& text) const;
+ virtual bool IsCountryCode(const string16& text) const;
+ virtual bool IsCityAndNumber(const string16& text) const;
+ virtual bool IsWholeNumber(const string16& text) const;
+
+ // The following functions should return the field type for each part of the
+ // phone number. Currently, these are either fax or home phone number types.
+ virtual AutoFillFieldType GetNumberType() const = 0;
+ virtual AutoFillFieldType GetCityCodeType() const = 0;
+ virtual AutoFillFieldType GetCountryCodeType() const = 0;
+ virtual AutoFillFieldType GetCityAndNumberType() const = 0;
+ virtual AutoFillFieldType GetWholeNumberType() const = 0;
+
+ // Verifies that |number| is a valid phone number.
+ bool Validate(const string16& number) const;
+
+ // Removes any punctuation characters from |number|.
+ void StripPunctuation(string16* number) const;
+
+ // The pieces of the phone number.
+ string16 country_code_;
+ string16 city_code_; // city or area code.
+ string16 number_;
+ string16 extension_;
+};
+
+#endif // CHROME_BROWSER_AUTOFILL_PHONE_NUMBER_H_
diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi
index e08ebb3..d072aba 100755
--- a/chrome/chrome_browser.gypi
+++ b/chrome/chrome_browser.gypi
@@ -98,18 +98,22 @@
'browser/autofill/credit_card_field.h',
'browser/autofill/credit_card.cc',
'browser/autofill/credit_card.h',
+ 'browser/autofill/fax_number.h',
'browser/autofill/field_types.h',
'browser/autofill/form_field.cc',
'browser/autofill/form_field.h',
'browser/autofill/form_group.h',
'browser/autofill/form_structure.cc',
'browser/autofill/form_structure.h',
+ 'browser/autofill/home_phone_number.h',
'browser/autofill/name_field.cc',
'browser/autofill/name_field.h',
'browser/autofill/personal_data_manager.cc',
'browser/autofill/personal_data_manager.h',
'browser/autofill/phone_field.cc',
'browser/autofill/phone_field.h',
+ 'browser/autofill/phone_number.cc',
+ 'browser/autofill/phone_number.h',
'browser/automation/automation_autocomplete_edit_tracker.h',
'browser/automation/automation_browser_tracker.h',
'browser/automation/extension_automation_constants.h',