blob: 730f1a2277cfd08a6712c9fc550faf74b8055061 [file] [log] [blame]
[email protected]371dab12012-06-01 03:23:551// Copyright (c) 2012 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#include "chrome/browser/autocomplete/url_prefix.h"
6
7#include "base/basictypes.h"
[email protected]98570e12013-06-10 19:54:228#include "base/strings/string_util.h"
[email protected]135cb802013-06-09 16:44:209#include "base/strings/utf_string_conversions.h"
[email protected]973a886c2014-04-10 21:08:0610
11namespace {
12
13// Like URLPrefix::BestURLPrefix() except also handles the prefix of
14// "www.".
15const URLPrefix* BestURLPrefixWithWWWCase(
16 const base::string16& text,
17 const base::string16& prefix_suffix) {
18 CR_DEFINE_STATIC_LOCAL(URLPrefix, www_prefix,
19 (base::ASCIIToUTF16("www."), 1));
20 const URLPrefix* best_prefix = URLPrefix::BestURLPrefix(text, prefix_suffix);
21 if ((best_prefix == NULL) ||
22 (best_prefix->num_components < www_prefix.num_components)) {
23 if (URLPrefix::PrefixMatch(www_prefix, text, prefix_suffix))
24 best_prefix = &www_prefix;
25 }
26 return best_prefix;
27}
28
29} // namespace
[email protected]371dab12012-06-01 03:23:5530
[email protected]96920152013-12-04 21:00:1631URLPrefix::URLPrefix(const base::string16& prefix, size_t num_components)
[email protected]371dab12012-06-01 03:23:5532 : prefix(prefix),
33 num_components(num_components) {
34}
35
36// static
37const URLPrefixes& URLPrefix::GetURLPrefixes() {
38 CR_DEFINE_STATIC_LOCAL(URLPrefixes, prefixes, ());
39 if (prefixes.empty()) {
[email protected]670d3232013-12-24 17:58:5840 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("https://www."), 2));
41 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("http://www."), 2));
42 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://ftp."), 2));
43 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://www."), 2));
44 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("https://"), 1));
45 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("http://"), 1));
46 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://"), 1));
[email protected]b959d7d42013-12-13 17:26:3747 prefixes.push_back(URLPrefix(base::string16(), 0));
[email protected]371dab12012-06-01 03:23:5548 }
49 return prefixes;
50}
51
52// static
[email protected]96920152013-12-04 21:00:1653bool URLPrefix::IsURLPrefix(const base::string16& prefix) {
[email protected]371dab12012-06-01 03:23:5554 const URLPrefixes& list = GetURLPrefixes();
55 for (URLPrefixes::const_iterator i = list.begin(); i != list.end(); ++i)
56 if (i->prefix == prefix)
57 return true;
58 return false;
59}
60
61// static
[email protected]96920152013-12-04 21:00:1662const URLPrefix* URLPrefix::BestURLPrefix(const base::string16& text,
63 const base::string16& prefix_suffix) {
[email protected]371dab12012-06-01 03:23:5564 const URLPrefixes& list = GetURLPrefixes();
[email protected]d1f0a7f2012-06-05 10:26:4265 for (URLPrefixes::const_iterator i = list.begin(); i != list.end(); ++i)
[email protected]5b3cbb9b2013-12-07 23:30:3366 if (PrefixMatch(*i, text, prefix_suffix))
[email protected]d1f0a7f2012-06-05 10:26:4267 return &(*i);
68 return NULL;
[email protected]371dab12012-06-01 03:23:5569}
[email protected]5b3cbb9b2013-12-07 23:30:3370
71// static
72bool URLPrefix::PrefixMatch(const URLPrefix& prefix,
[email protected]4db2cf272013-12-10 21:53:4973 const base::string16& text,
74 const base::string16& prefix_suffix) {
[email protected]5b3cbb9b2013-12-07 23:30:3375 return StartsWith(text, prefix.prefix + prefix_suffix, false);
76}
[email protected]973a886c2014-04-10 21:08:0677
78// static
[email protected]b3a84892014-04-23 04:28:0779size_t URLPrefix::GetInlineAutocompleteOffset(
[email protected]13ff43162014-06-06 22:03:1280 const base::string16& input,
81 const base::string16& fixed_up_input,
[email protected]973a886c2014-04-10 21:08:0682 const bool allow_www_prefix_without_scheme,
[email protected]b3a84892014-04-23 04:28:0783 const base::string16& text) {
[email protected]973a886c2014-04-10 21:08:0684 const URLPrefix* best_prefix = allow_www_prefix_without_scheme ?
[email protected]13ff43162014-06-06 22:03:1285 BestURLPrefixWithWWWCase(text, input) : BestURLPrefix(text, input);
86 const base::string16* matching_string = &input;
[email protected]973a886c2014-04-10 21:08:0687 // If we failed to find a best_prefix initially, try again using a fixed-up
88 // version of the user input. This is especially useful to get about: URLs
89 // to inline against chrome:// shortcuts. (about: URLs are fixed up to the
90 // chrome:// scheme.)
[email protected]13ff43162014-06-06 22:03:1291 if (!best_prefix && !fixed_up_input.empty() && (fixed_up_input != input)) {
[email protected]973a886c2014-04-10 21:08:0692 best_prefix = allow_www_prefix_without_scheme ?
[email protected]13ff43162014-06-06 22:03:1293 BestURLPrefixWithWWWCase(text, fixed_up_input) :
94 BestURLPrefix(text, fixed_up_input);
95 matching_string = &fixed_up_input;
[email protected]973a886c2014-04-10 21:08:0696 }
[email protected]b3a84892014-04-23 04:28:0797 return (best_prefix != NULL) ?
98 (best_prefix->prefix.length() + matching_string->length()) :
99 base::string16::npos;
[email protected]973a886c2014-04-10 21:08:06100}