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/builtin_provider.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/autocomplete/autocomplete_input.h"
12 #include "chrome/common/net/url_fixer_upper.h"
13 #include "chrome/common/url_constants.h"
17 // This list should be kept in sync with chrome/common/url_constants.h.
18 // Only include useful sub-pages, confirmation alerts are not useful.
19 const char* const kChromeSettingsSubPages[] = {
20 chrome::kAutofillSubPage,
21 chrome::kClearBrowserDataSubPage,
22 chrome::kContentSettingsSubPage,
23 chrome::kContentSettingsExceptionsSubPage,
24 chrome::kImportDataSubPage,
25 chrome::kLanguageOptionsSubPage,
26 chrome::kPasswordManagerSubPage,
27 chrome::kSearchEnginesSubPage,
28 chrome::kSyncSetupSubPage,
29 #if defined(OS_CHROMEOS)
30 chrome::kInternetOptionsSubPage,
36 const int BuiltinProvider::kRelevance = 860;
38 BuiltinProvider::BuiltinProvider(AutocompleteProviderListener* listener,
40 : AutocompleteProvider(listener, profile,
41 AutocompleteProvider::TYPE_BUILTIN) {
42 std::vector<std::string> builtins(
43 chrome::kChromeHostURLs,
44 chrome::kChromeHostURLs + chrome::kNumberOfChromeHostURLs);
45 std::sort(builtins.begin(), builtins.end());
46 for (std::vector<std::string>::iterator i(builtins.begin());
47 i != builtins.end(); ++i)
48 builtins_.push_back(ASCIIToUTF16(*i));
49 string16 settings(ASCIIToUTF16(chrome::kChromeUISettingsHost) +
51 for (size_t i = 0; i < arraysize(kChromeSettingsSubPages); i++)
52 builtins_.push_back(settings + ASCIIToUTF16(kChromeSettingsSubPages[i]));
55 void BuiltinProvider::Start(const AutocompleteInput& input,
56 bool minimal_changes) {
58 if ((input.type() == AutocompleteInput::INVALID) ||
59 (input.type() == AutocompleteInput::FORCED_QUERY) ||
60 (input.type() == AutocompleteInput::QUERY))
63 const string16 kAbout = ASCIIToUTF16(chrome::kAboutScheme) +
64 ASCIIToUTF16(content::kStandardSchemeSeparator);
65 const string16 kChrome = ASCIIToUTF16(chrome::kChromeUIScheme) +
66 ASCIIToUTF16(content::kStandardSchemeSeparator);
68 const int kUrl = ACMatchClassification::URL;
69 const int kMatch = kUrl | ACMatchClassification::MATCH;
71 string16 text = input.text();
72 bool starting_chrome = StartsWith(kChrome, text, false);
73 if (starting_chrome || StartsWith(kAbout, text, false)) {
74 ACMatchClassifications styles;
75 // Highlight the input portion matching "chrome://"; or if the user has
76 // input "about:" (with optional slashes), highlight the whole "chrome://".
77 const size_t kAboutSchemeLength = strlen(chrome::kAboutScheme);
78 bool highlight = starting_chrome || text.length() > kAboutSchemeLength;
79 styles.push_back(ACMatchClassification(0, highlight ? kMatch : kUrl));
80 size_t offset = starting_chrome ? text.length() : kChrome.length();
82 styles.push_back(ACMatchClassification(offset, kUrl));
83 // Include some common builtin chrome URLs as the user types the scheme.
84 AddMatch(ASCIIToUTF16(chrome::kChromeUIChromeURLsURL), string16(), styles);
85 AddMatch(ASCIIToUTF16(chrome::kChromeUISettingsURL), string16(), styles);
86 AddMatch(ASCIIToUTF16(chrome::kChromeUIVersionURL), string16(), styles);
88 // Match input about: or chrome: URL input against builtin chrome URLs.
89 GURL url = URLFixerUpper::FixupURL(UTF16ToUTF8(text), std::string());
90 // BuiltinProvider doesn't know how to suggest valid ?query or #fragment
91 // extensions to chrome: URLs.
92 if (url.SchemeIs(chrome::kChromeUIScheme) && url.has_host() &&
93 !url.has_query() && !url.has_ref()) {
94 // Include the path for sub-pages (e.g. "chrome://settings/browser").
95 string16 host_and_path = UTF8ToUTF16(url.host() + url.path());
96 TrimString(host_and_path, ASCIIToUTF16("/").c_str(), &host_and_path);
97 size_t match_length = kChrome.length() + host_and_path.length();
98 for (Builtins::const_iterator i(builtins_.begin());
99 (i != builtins_.end()) && (matches_.size() < kMaxMatches); ++i) {
100 if (StartsWith(*i, host_and_path, false)) {
101 ACMatchClassifications styles;
102 // Highlight the "chrome://" scheme, even for input "about:foo".
103 styles.push_back(ACMatchClassification(0, kMatch));
104 string16 match_string = kChrome + *i;
105 if (match_string.length() > match_length)
106 styles.push_back(ACMatchClassification(match_length, kUrl));
107 AddMatch(match_string, match_string.substr(match_length), styles);
113 for (size_t i = 0; i < matches_.size(); ++i)
114 matches_[i].relevance = kRelevance + matches_.size() - (i + 1);
115 if (!input.prevent_inline_autocomplete() && (matches_.size() == 1)) {
116 // If there's only one possible completion of the user's input and
117 // allowing completions is okay, give the match a high enough score to
118 // allow it to beat url-what-you-typed and be inlined.
119 matches_[0].relevance = 1250;
120 matches_[0].allowed_to_be_default_match = true;
124 BuiltinProvider::~BuiltinProvider() {}
126 void BuiltinProvider::AddMatch(const string16& match_string,
127 const string16& inline_completion,
128 const ACMatchClassifications& styles) {
129 AutocompleteMatch match(this, kRelevance, false,
130 AutocompleteMatchType::NAVSUGGEST);
131 match.fill_into_edit = match_string;
132 match.inline_autocompletion = inline_completion;
133 match.destination_url = GURL(match_string);
134 match.contents = match_string;
135 match.contents_class = styles;
136 matches_.push_back(match);