blob: f1652f58eb354dd15beb3ec441db1d4b49568d85 [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]d1f0a7f2012-06-05 10:26:428#include "base/string_util.h"
[email protected]371dab12012-06-01 03:23:559#include "base/utf_string_conversions.h"
10
11URLPrefix::URLPrefix(const string16& prefix, size_t num_components)
12 : prefix(prefix),
13 num_components(num_components) {
14}
15
16// static
17const URLPrefixes& URLPrefix::GetURLPrefixes() {
18 CR_DEFINE_STATIC_LOCAL(URLPrefixes, prefixes, ());
19 if (prefixes.empty()) {
20 prefixes.push_back(URLPrefix(ASCIIToUTF16("https://www."), 2));
21 prefixes.push_back(URLPrefix(ASCIIToUTF16("http://www."), 2));
22 prefixes.push_back(URLPrefix(ASCIIToUTF16("ftp://ftp."), 2));
23 prefixes.push_back(URLPrefix(ASCIIToUTF16("ftp://www."), 2));
24 prefixes.push_back(URLPrefix(ASCIIToUTF16("https://"), 1));
25 prefixes.push_back(URLPrefix(ASCIIToUTF16("http://"), 1));
26 prefixes.push_back(URLPrefix(ASCIIToUTF16("ftp://"), 1));
27 prefixes.push_back(URLPrefix(string16(), 0));
28 }
29 return prefixes;
30}
31
32// static
33bool URLPrefix::IsURLPrefix(const 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;
39}
40
41// static
42const URLPrefix* URLPrefix::BestURLPrefix(const string16& text,
43 const string16& prefix_suffix) {
[email protected]371dab12012-06-01 03:23:5544 const URLPrefixes& list = GetURLPrefixes();
[email protected]d1f0a7f2012-06-05 10:26:4245 for (URLPrefixes::const_iterator i = list.begin(); i != list.end(); ++i)
46 if (StartsWith(text, i->prefix + prefix_suffix, false))
47 return &(*i);
48 return NULL;
[email protected]371dab12012-06-01 03:23:5549}