Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / autocomplete / url_prefix.cc
blobb304c7c56a5e5fd026a4085ba4e8adbe027ec392
1 // 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.
5 #include "chrome/browser/autocomplete/url_prefix.h"
7 #include "base/basictypes.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
11 URLPrefix::URLPrefix(const base::string16& prefix, size_t num_components)
12 : prefix(prefix),
13 num_components(num_components) {
16 // static
17 const URLPrefixes& URLPrefix::GetURLPrefixes() {
18 CR_DEFINE_STATIC_LOCAL(URLPrefixes, prefixes, ());
19 if (prefixes.empty()) {
20 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("https://www."), 2));
21 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("http://www."), 2));
22 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://ftp."), 2));
23 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://www."), 2));
24 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("https://"), 1));
25 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("http://"), 1));
26 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://"), 1));
27 prefixes.push_back(URLPrefix(base::string16(), 0));
29 return prefixes;
32 // static
33 bool URLPrefix::IsURLPrefix(const base::string16& prefix) {
34 const URLPrefixes& list = GetURLPrefixes();
35 for (URLPrefixes::const_iterator i = list.begin(); i != list.end(); ++i)
36 if (i->prefix == prefix)
37 return true;
38 return false;
41 // static
42 const URLPrefix* URLPrefix::BestURLPrefix(const base::string16& text,
43 const base::string16& prefix_suffix) {
44 const URLPrefixes& list = GetURLPrefixes();
45 for (URLPrefixes::const_iterator i = list.begin(); i != list.end(); ++i)
46 if (PrefixMatch(*i, text, prefix_suffix))
47 return &(*i);
48 return NULL;
51 // static
52 bool URLPrefix::PrefixMatch(const URLPrefix& prefix,
53 const base::string16& text,
54 const base::string16& prefix_suffix) {
55 return StartsWith(text, prefix.prefix + prefix_suffix, false);