summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autocomplete/url_prefix.cc
blob: 9174bd561cee68f36409dc57d428bd88cb42e9cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (c) 2012 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/autocomplete/url_prefix.h"

#include "base/basictypes.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"

URLPrefix::URLPrefix(const string16& prefix, size_t num_components)
    : prefix(prefix),
      num_components(num_components) {
}

// static
const URLPrefixes& URLPrefix::GetURLPrefixes() {
  CR_DEFINE_STATIC_LOCAL(URLPrefixes, prefixes, ());
  if (prefixes.empty()) {
    prefixes.push_back(URLPrefix(ASCIIToUTF16("https://www."), 2));
    prefixes.push_back(URLPrefix(ASCIIToUTF16("http://www."), 2));
    prefixes.push_back(URLPrefix(ASCIIToUTF16("ftp://ftp."), 2));
    prefixes.push_back(URLPrefix(ASCIIToUTF16("ftp://www."), 2));
    prefixes.push_back(URLPrefix(ASCIIToUTF16("https://"), 1));
    prefixes.push_back(URLPrefix(ASCIIToUTF16("http://"), 1));
    prefixes.push_back(URLPrefix(ASCIIToUTF16("ftp://"), 1));
    prefixes.push_back(URLPrefix(string16(), 0));
  }
  return prefixes;
}

// static
bool URLPrefix::IsURLPrefix(const string16& prefix) {
  const URLPrefixes& list = GetURLPrefixes();
  for (URLPrefixes::const_iterator i = list.begin(); i != list.end(); ++i)
    if (i->prefix == prefix)
      return true;
  return false;
}

// static
const URLPrefix* URLPrefix::BestURLPrefix(const string16& text,
                                          const string16& prefix_suffix) {
  const URLPrefixes& list = GetURLPrefixes();
  for (URLPrefixes::const_iterator i = list.begin(); i != list.end(); ++i)
    if (StartsWith(text, i->prefix + prefix_suffix, false))
      return &(*i);
  return NULL;
}