OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/autocomplete/builtin_provider.h" | 5 #include "chrome/browser/autocomplete/builtin_provider.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 const base::string16 kAbout = | 74 const base::string16 kAbout = |
75 base::ASCIIToUTF16(url::kAboutScheme) + | 75 base::ASCIIToUTF16(url::kAboutScheme) + |
76 base::ASCIIToUTF16(url::kStandardSchemeSeparator); | 76 base::ASCIIToUTF16(url::kStandardSchemeSeparator); |
77 const base::string16 kChrome = base::ASCIIToUTF16(content::kChromeUIScheme) + | 77 const base::string16 kChrome = base::ASCIIToUTF16(content::kChromeUIScheme) + |
78 base::ASCIIToUTF16(url::kStandardSchemeSeparator); | 78 base::ASCIIToUTF16(url::kStandardSchemeSeparator); |
79 | 79 |
80 const int kUrl = ACMatchClassification::URL; | 80 const int kUrl = ACMatchClassification::URL; |
81 const int kMatch = kUrl | ACMatchClassification::MATCH; | 81 const int kMatch = kUrl | ACMatchClassification::MATCH; |
82 | 82 |
83 base::string16 text = input.text(); | 83 base::string16 text = input.text(); |
84 bool starting_chrome = StartsWith(kChrome, text, false); | 84 bool starting_chrome = base::StartsWith(kChrome, text, false); |
85 if (starting_chrome || StartsWith(kAbout, text, false)) { | 85 if (starting_chrome || base::StartsWith(kAbout, text, false)) { |
86 ACMatchClassifications styles; | 86 ACMatchClassifications styles; |
87 // Highlight the input portion matching "chrome://"; or if the user has | 87 // Highlight the input portion matching "chrome://"; or if the user has |
88 // input "about:" (with optional slashes), highlight the whole "chrome://". | 88 // input "about:" (with optional slashes), highlight the whole "chrome://". |
89 bool highlight = starting_chrome || text.length() > kAboutSchemeLength; | 89 bool highlight = starting_chrome || text.length() > kAboutSchemeLength; |
90 styles.push_back(ACMatchClassification(0, highlight ? kMatch : kUrl)); | 90 styles.push_back(ACMatchClassification(0, highlight ? kMatch : kUrl)); |
91 size_t offset = starting_chrome ? text.length() : kChrome.length(); | 91 size_t offset = starting_chrome ? text.length() : kChrome.length(); |
92 if (highlight) | 92 if (highlight) |
93 styles.push_back(ACMatchClassification(offset, kUrl)); | 93 styles.push_back(ACMatchClassification(offset, kUrl)); |
94 // Include some common builtin chrome URLs as the user types the scheme. | 94 // Include some common builtin chrome URLs as the user types the scheme. |
95 AddMatch(base::ASCIIToUTF16(chrome::kChromeUIChromeURLsURL), | 95 AddMatch(base::ASCIIToUTF16(chrome::kChromeUIChromeURLsURL), |
96 base::string16(), styles); | 96 base::string16(), styles); |
97 #if !defined(OS_ANDROID) | 97 #if !defined(OS_ANDROID) |
98 AddMatch(base::ASCIIToUTF16(chrome::kChromeUISettingsURL), | 98 AddMatch(base::ASCIIToUTF16(chrome::kChromeUISettingsURL), |
99 base::string16(), styles); | 99 base::string16(), styles); |
100 #endif | 100 #endif |
101 AddMatch(base::ASCIIToUTF16(chrome::kChromeUIVersionURL), | 101 AddMatch(base::ASCIIToUTF16(chrome::kChromeUIVersionURL), |
102 base::string16(), styles); | 102 base::string16(), styles); |
103 } else { | 103 } else { |
104 // Match input about: or chrome: URL input against builtin chrome URLs. | 104 // Match input about: or chrome: URL input against builtin chrome URLs. |
105 GURL url = url_fixer::FixupURL(base::UTF16ToUTF8(text), std::string()); | 105 GURL url = url_fixer::FixupURL(base::UTF16ToUTF8(text), std::string()); |
106 // BuiltinProvider doesn't know how to suggest valid ?query or #fragment | 106 // BuiltinProvider doesn't know how to suggest valid ?query or #fragment |
107 // extensions to chrome: URLs. | 107 // extensions to chrome: URLs. |
108 if (url.SchemeIs(content::kChromeUIScheme) && url.has_host() && | 108 if (url.SchemeIs(content::kChromeUIScheme) && url.has_host() && |
109 !url.has_query() && !url.has_ref()) { | 109 !url.has_query() && !url.has_ref()) { |
110 // Suggest about:blank for substrings, taking URL fixup into account. | 110 // Suggest about:blank for substrings, taking URL fixup into account. |
111 // Chrome does not support trailing slashes or paths for about:blank. | 111 // Chrome does not support trailing slashes or paths for about:blank. |
112 const base::string16 blank_host = base::ASCIIToUTF16("blank"); | 112 const base::string16 blank_host = base::ASCIIToUTF16("blank"); |
113 const base::string16 host = base::UTF8ToUTF16(url.host()); | 113 const base::string16 host = base::UTF8ToUTF16(url.host()); |
114 if (StartsWith(text, base::ASCIIToUTF16(url::kAboutScheme), false) && | 114 if (base::StartsWith(text, base::ASCIIToUTF16(url::kAboutScheme), |
115 StartsWith(blank_host, host, false) && (url.path().length() <= 1) && | 115 false) && |
| 116 base::StartsWith(blank_host, host, false) && |
| 117 (url.path().length() <= 1) && |
116 !EndsWith(text, base::ASCIIToUTF16("/"), false)) { | 118 !EndsWith(text, base::ASCIIToUTF16("/"), false)) { |
117 ACMatchClassifications styles; | 119 ACMatchClassifications styles; |
118 styles.push_back(ACMatchClassification(0, kMatch)); | 120 styles.push_back(ACMatchClassification(0, kMatch)); |
119 base::string16 match = base::ASCIIToUTF16(url::kAboutBlankURL); | 121 base::string16 match = base::ASCIIToUTF16(url::kAboutBlankURL); |
120 // Measure the length of the matching host after the "about:" scheme. | 122 // Measure the length of the matching host after the "about:" scheme. |
121 const size_t corrected_length = kAboutSchemeLength + 1 + host.length(); | 123 const size_t corrected_length = kAboutSchemeLength + 1 + host.length(); |
122 if (blank_host.length() > host.length()) | 124 if (blank_host.length() > host.length()) |
123 styles.push_back(ACMatchClassification(corrected_length, kUrl)); | 125 styles.push_back(ACMatchClassification(corrected_length, kUrl)); |
124 AddMatch(match, match.substr(corrected_length), styles); | 126 AddMatch(match, match.substr(corrected_length), styles); |
125 } | 127 } |
126 | 128 |
127 // Include the path for sub-pages (e.g. "chrome://settings/browser"). | 129 // Include the path for sub-pages (e.g. "chrome://settings/browser"). |
128 base::string16 host_and_path = base::UTF8ToUTF16(url.host() + url.path()); | 130 base::string16 host_and_path = base::UTF8ToUTF16(url.host() + url.path()); |
129 base::TrimString(host_and_path, base::ASCIIToUTF16("/"), &host_and_path); | 131 base::TrimString(host_and_path, base::ASCIIToUTF16("/"), &host_and_path); |
130 size_t match_length = kChrome.length() + host_and_path.length(); | 132 size_t match_length = kChrome.length() + host_and_path.length(); |
131 for (Builtins::const_iterator i(builtins_.begin()); | 133 for (Builtins::const_iterator i(builtins_.begin()); |
132 (i != builtins_.end()) && (matches_.size() < kMaxMatches); ++i) { | 134 (i != builtins_.end()) && (matches_.size() < kMaxMatches); ++i) { |
133 if (StartsWith(*i, host_and_path, false)) { | 135 if (base::StartsWith(*i, host_and_path, false)) { |
134 ACMatchClassifications styles; | 136 ACMatchClassifications styles; |
135 // Highlight the "chrome://" scheme, even for input "about:foo". | 137 // Highlight the "chrome://" scheme, even for input "about:foo". |
136 styles.push_back(ACMatchClassification(0, kMatch)); | 138 styles.push_back(ACMatchClassification(0, kMatch)); |
137 base::string16 match_string = kChrome + *i; | 139 base::string16 match_string = kChrome + *i; |
138 if (match_string.length() > match_length) | 140 if (match_string.length() > match_length) |
139 styles.push_back(ACMatchClassification(match_length, kUrl)); | 141 styles.push_back(ACMatchClassification(match_length, kUrl)); |
140 AddMatch(match_string, match_string.substr(match_length), styles); | 142 AddMatch(match_string, match_string.substr(match_length), styles); |
141 } | 143 } |
142 } | 144 } |
143 } | 145 } |
(...skipping 18 matching lines...) Expand all Loading... |
162 const ACMatchClassifications& styles) { | 164 const ACMatchClassifications& styles) { |
163 AutocompleteMatch match(this, kRelevance, false, | 165 AutocompleteMatch match(this, kRelevance, false, |
164 AutocompleteMatchType::NAVSUGGEST); | 166 AutocompleteMatchType::NAVSUGGEST); |
165 match.fill_into_edit = match_string; | 167 match.fill_into_edit = match_string; |
166 match.inline_autocompletion = inline_completion; | 168 match.inline_autocompletion = inline_completion; |
167 match.destination_url = GURL(match_string); | 169 match.destination_url = GURL(match_string); |
168 match.contents = match_string; | 170 match.contents = match_string; |
169 match.contents_class = styles; | 171 match.contents_class = styles; |
170 matches_.push_back(match); | 172 matches_.push_back(match); |
171 } | 173 } |
OLD | NEW |