blob: be5a5ef40e19123a14d3a0a41329af2062d4dbcf [file] [log] [blame]
[email protected]ebeb8612014-06-17 01:54:501// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]371dab12012-06-01 03:23:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]b1c5ab682014-08-07 11:53:175#include "components/omnibox/url_prefix.h"
[email protected]371dab12012-06-01 03:23:556
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));
[email protected]670d3232013-12-24 17:58:5842 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://www."), 2));
43 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("https://"), 1));
44 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("http://"), 1));
45 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://"), 1));
[email protected]b959d7d42013-12-13 17:26:3746 prefixes.push_back(URLPrefix(base::string16(), 0));
[email protected]371dab12012-06-01 03:23:5547 }
48 return prefixes;
49}
50
51// static
[email protected]96920152013-12-04 21:00:1652bool URLPrefix::IsURLPrefix(const base::string16& prefix) {
[email protected]371dab12012-06-01 03:23:5553 const URLPrefixes& list = GetURLPrefixes();
54 for (URLPrefixes::const_iterator i = list.begin(); i != list.end(); ++i)
55 if (i->prefix == prefix)
56 return true;
57 return false;
58}
59
60// static
[email protected]96920152013-12-04 21:00:1661const URLPrefix* URLPrefix::BestURLPrefix(const base::string16& text,
62 const base::string16& prefix_suffix) {
[email protected]371dab12012-06-01 03:23:5563 const URLPrefixes& list = GetURLPrefixes();
[email protected]d1f0a7f2012-06-05 10:26:4264 for (URLPrefixes::const_iterator i = list.begin(); i != list.end(); ++i)
[email protected]5b3cbb9b2013-12-07 23:30:3365 if (PrefixMatch(*i, text, prefix_suffix))
[email protected]d1f0a7f2012-06-05 10:26:4266 return &(*i);
67 return NULL;
[email protected]371dab12012-06-01 03:23:5568}
[email protected]5b3cbb9b2013-12-07 23:30:3369
70// static
71bool URLPrefix::PrefixMatch(const URLPrefix& prefix,
[email protected]4db2cf272013-12-10 21:53:4972 const base::string16& text,
73 const base::string16& prefix_suffix) {
[email protected]5b3cbb9b2013-12-07 23:30:3374 return StartsWith(text, prefix.prefix + prefix_suffix, false);
75}
[email protected]973a886c2014-04-10 21:08:0676
77// static
[email protected]b3a84892014-04-23 04:28:0778size_t URLPrefix::GetInlineAutocompleteOffset(
[email protected]13ff43162014-06-06 22:03:1279 const base::string16& input,
80 const base::string16& fixed_up_input,
[email protected]973a886c2014-04-10 21:08:0681 const bool allow_www_prefix_without_scheme,
[email protected]b3a84892014-04-23 04:28:0782 const base::string16& text) {
[email protected]973a886c2014-04-10 21:08:0683 const URLPrefix* best_prefix = allow_www_prefix_without_scheme ?
[email protected]13ff43162014-06-06 22:03:1284 BestURLPrefixWithWWWCase(text, input) : BestURLPrefix(text, input);
85 const base::string16* matching_string = &input;
[email protected]973a886c2014-04-10 21:08:0686 // If we failed to find a best_prefix initially, try again using a fixed-up
87 // version of the user input. This is especially useful to get about: URLs
88 // to inline against chrome:// shortcuts. (about: URLs are fixed up to the
89 // chrome:// scheme.)
[email protected]13ff43162014-06-06 22:03:1290 if (!best_prefix && !fixed_up_input.empty() && (fixed_up_input != input)) {
[email protected]973a886c2014-04-10 21:08:0691 best_prefix = allow_www_prefix_without_scheme ?
[email protected]13ff43162014-06-06 22:03:1292 BestURLPrefixWithWWWCase(text, fixed_up_input) :
93 BestURLPrefix(text, fixed_up_input);
94 matching_string = &fixed_up_input;
[email protected]973a886c2014-04-10 21:08:0695 }
[email protected]b3a84892014-04-23 04:28:0796 return (best_prefix != NULL) ?
97 (best_prefix->prefix.length() + matching_string->length()) :
98 base::string16::npos;
[email protected]973a886c2014-04-10 21:08:0699}