This source file includes following definitions.
- GetMaximumContentLength
- GetIntentURL
- FindContent
#include "content/renderer/android/phone_number_detector.h"
#include <algorithm>
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/renderer/android_content_detection_prefixes.h"
#include "net/base/escape.h"
#include "third_party/libphonenumber/src/phonenumber_api.h"
#include "third_party/libphonenumber/src/phonenumbers/phonenumbermatch.h"
#include "third_party/libphonenumber/src/phonenumbers/phonenumbermatcher.h"
#include "third_party/libphonenumber/src/phonenumbers/region_code.h"
using i18n::phonenumbers::PhoneNumberMatch;
using i18n::phonenumbers::PhoneNumberMatcher;
using i18n::phonenumbers::PhoneNumberUtil;
using i18n::phonenumbers::RegionCode;
namespace {
const size_t kMaximumTelephoneLength = 20;
}
namespace content {
PhoneNumberDetector::PhoneNumberDetector()
: region_code_(RegionCode::GetUnknown()) {
}
PhoneNumberDetector::PhoneNumberDetector(const std::string& region)
: region_code_(region.empty() ? RegionCode::GetUnknown()
: StringToUpperASCII(region)) {
}
PhoneNumberDetector::~PhoneNumberDetector() {
}
size_t PhoneNumberDetector::GetMaximumContentLength() {
return kMaximumTelephoneLength;
}
GURL PhoneNumberDetector::GetIntentURL(const std::string& content_text) {
if (content_text.empty())
return GURL();
return GURL(kPhoneNumberPrefix +
net::EscapeQueryParamValue(content_text, true));
}
bool PhoneNumberDetector::FindContent(
const base::string16::const_iterator& begin,
const base::string16::const_iterator& end,
size_t* start_pos,
size_t* end_pos,
std::string* content_text) {
base::string16 utf16_input = base::string16(begin, end);
std::string utf8_input = base::UTF16ToUTF8(utf16_input);
PhoneNumberMatcher matcher(utf8_input, region_code_);
if (matcher.HasNext()) {
PhoneNumberMatch match;
matcher.Next(&match);
PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance();
phone_util->FormatNumberForMobileDialing(match.number(), region_code_,
false,
content_text);
if (content_text->empty())
return false;
*start_pos =
base::UTF8ToUTF16(utf8_input.substr(0, match.start())).length();
*end_pos = *start_pos + base::UTF8ToUTF16(match.raw_string()).length();
return true;
}
return false;
}
}