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