| 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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 } | 62 } |
| 63 | 63 |
| 64 void BuiltinProvider::Start(const AutocompleteInput& input, | 64 void BuiltinProvider::Start(const AutocompleteInput& input, |
| 65 bool minimal_changes) { | 65 bool minimal_changes) { |
| 66 matches_.clear(); | 66 matches_.clear(); |
| 67 if ((input.type() == AutocompleteInput::INVALID) || | 67 if ((input.type() == AutocompleteInput::INVALID) || |
| 68 (input.type() == AutocompleteInput::FORCED_QUERY) || | 68 (input.type() == AutocompleteInput::FORCED_QUERY) || |
| 69 (input.type() == AutocompleteInput::QUERY)) | 69 (input.type() == AutocompleteInput::QUERY)) |
| 70 return; | 70 return; |
| 71 | 71 |
| 72 const size_t kAboutSchemeLength = strlen(content::kAboutScheme); |
| 72 const base::string16 kAbout = base::ASCIIToUTF16(content::kAboutScheme) + | 73 const base::string16 kAbout = base::ASCIIToUTF16(content::kAboutScheme) + |
| 73 base::ASCIIToUTF16(content::kStandardSchemeSeparator); | 74 base::ASCIIToUTF16(content::kStandardSchemeSeparator); |
| 74 const base::string16 kChrome = base::ASCIIToUTF16(content::kChromeUIScheme) + | 75 const base::string16 kChrome = base::ASCIIToUTF16(content::kChromeUIScheme) + |
| 75 base::ASCIIToUTF16(content::kStandardSchemeSeparator); | 76 base::ASCIIToUTF16(content::kStandardSchemeSeparator); |
| 76 | 77 |
| 77 const int kUrl = ACMatchClassification::URL; | 78 const int kUrl = ACMatchClassification::URL; |
| 78 const int kMatch = kUrl | ACMatchClassification::MATCH; | 79 const int kMatch = kUrl | ACMatchClassification::MATCH; |
| 79 | 80 |
| 80 base::string16 text = input.text(); | 81 base::string16 text = input.text(); |
| 81 bool starting_chrome = StartsWith(kChrome, text, false); | 82 bool starting_chrome = StartsWith(kChrome, text, false); |
| 82 if (starting_chrome || StartsWith(kAbout, text, false)) { | 83 if (starting_chrome || StartsWith(kAbout, text, false)) { |
| 83 ACMatchClassifications styles; | 84 ACMatchClassifications styles; |
| 84 // Highlight the input portion matching "chrome://"; or if the user has | 85 // Highlight the input portion matching "chrome://"; or if the user has |
| 85 // input "about:" (with optional slashes), highlight the whole "chrome://". | 86 // 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; | 87 bool highlight = starting_chrome || text.length() > kAboutSchemeLength; |
| 88 styles.push_back(ACMatchClassification(0, highlight ? kMatch : kUrl)); | 88 styles.push_back(ACMatchClassification(0, highlight ? kMatch : kUrl)); |
| 89 size_t offset = starting_chrome ? text.length() : kChrome.length(); | 89 size_t offset = starting_chrome ? text.length() : kChrome.length(); |
| 90 if (highlight) | 90 if (highlight) |
| 91 styles.push_back(ACMatchClassification(offset, kUrl)); | 91 styles.push_back(ACMatchClassification(offset, kUrl)); |
| 92 // Include some common builtin chrome URLs as the user types the scheme. | 92 // Include some common builtin chrome URLs as the user types the scheme. |
| 93 AddMatch(base::ASCIIToUTF16(chrome::kChromeUIChromeURLsURL), | 93 AddMatch(base::ASCIIToUTF16(chrome::kChromeUIChromeURLsURL), |
| 94 base::string16(), styles); | 94 base::string16(), styles); |
| 95 #if !defined(OS_ANDROID) | 95 #if !defined(OS_ANDROID) |
| 96 AddMatch(base::ASCIIToUTF16(chrome::kChromeUISettingsURL), | 96 AddMatch(base::ASCIIToUTF16(chrome::kChromeUISettingsURL), |
| 97 base::string16(), styles); | 97 base::string16(), styles); |
| 98 #endif | 98 #endif |
| 99 AddMatch(base::ASCIIToUTF16(chrome::kChromeUIVersionURL), | 99 AddMatch(base::ASCIIToUTF16(chrome::kChromeUIVersionURL), |
| 100 base::string16(), styles); | 100 base::string16(), styles); |
| 101 } else { | 101 } else { |
| 102 // Match input about: or chrome: URL input against builtin chrome URLs. | 102 // Match input about: or chrome: URL input against builtin chrome URLs. |
| 103 GURL url = URLFixerUpper::FixupURL(base::UTF16ToUTF8(text), std::string()); | 103 GURL url = URLFixerUpper::FixupURL(base::UTF16ToUTF8(text), std::string()); |
| 104 // BuiltinProvider doesn't know how to suggest valid ?query or #fragment | 104 // BuiltinProvider doesn't know how to suggest valid ?query or #fragment |
| 105 // extensions to chrome: URLs. | 105 // extensions to chrome: URLs. |
| 106 if (url.SchemeIs(content::kChromeUIScheme) && url.has_host() && | 106 if (url.SchemeIs(content::kChromeUIScheme) && url.has_host() && |
| 107 !url.has_query() && !url.has_ref()) { | 107 !url.has_query() && !url.has_ref()) { |
| 108 // Suggest about:blank for substrings, taking URL fixup into account. |
| 109 // Chrome does not support trailing slashes or paths for about:blank. |
| 110 const base::string16 blank_host = base::ASCIIToUTF16("blank"); |
| 111 const base::string16 host = base::UTF8ToUTF16(url.host()); |
| 112 if (StartsWith(text, base::ASCIIToUTF16(content::kAboutScheme), false) && |
| 113 StartsWith(blank_host, host, false) && (url.path().length() <= 1) && |
| 114 !EndsWith(text, base::ASCIIToUTF16("/"), false)) { |
| 115 ACMatchClassifications styles; |
| 116 styles.push_back(ACMatchClassification(0, kMatch)); |
| 117 base::string16 match = base::ASCIIToUTF16(content::kAboutBlankURL); |
| 118 // Measure the length of the matching host after the "about:" scheme. |
| 119 const size_t corrected_length = kAboutSchemeLength + 1 + host.length(); |
| 120 if (blank_host.length() > host.length()) |
| 121 styles.push_back(ACMatchClassification(corrected_length, kUrl)); |
| 122 AddMatch(match, match.substr(corrected_length), styles); |
| 123 } |
| 124 |
| 108 // Include the path for sub-pages (e.g. "chrome://settings/browser"). | 125 // Include the path for sub-pages (e.g. "chrome://settings/browser"). |
| 109 base::string16 host_and_path = base::UTF8ToUTF16(url.host() + url.path()); | 126 base::string16 host_and_path = base::UTF8ToUTF16(url.host() + url.path()); |
| 110 base::TrimString(host_and_path, base::ASCIIToUTF16("/").c_str(), | 127 base::TrimString(host_and_path, base::ASCIIToUTF16("/").c_str(), |
| 111 &host_and_path); | 128 &host_and_path); |
| 112 size_t match_length = kChrome.length() + host_and_path.length(); | 129 size_t match_length = kChrome.length() + host_and_path.length(); |
| 113 for (Builtins::const_iterator i(builtins_.begin()); | 130 for (Builtins::const_iterator i(builtins_.begin()); |
| 114 (i != builtins_.end()) && (matches_.size() < kMaxMatches); ++i) { | 131 (i != builtins_.end()) && (matches_.size() < kMaxMatches); ++i) { |
| 115 if (StartsWith(*i, host_and_path, false)) { | 132 if (StartsWith(*i, host_and_path, false)) { |
| 116 ACMatchClassifications styles; | 133 ACMatchClassifications styles; |
| 117 // Highlight the "chrome://" scheme, even for input "about:foo". | 134 // Highlight the "chrome://" scheme, even for input "about:foo". |
| (...skipping 26 matching lines...) Expand all Loading... |
| 144 const ACMatchClassifications& styles) { | 161 const ACMatchClassifications& styles) { |
| 145 AutocompleteMatch match(this, kRelevance, false, | 162 AutocompleteMatch match(this, kRelevance, false, |
| 146 AutocompleteMatchType::NAVSUGGEST); | 163 AutocompleteMatchType::NAVSUGGEST); |
| 147 match.fill_into_edit = match_string; | 164 match.fill_into_edit = match_string; |
| 148 match.inline_autocompletion = inline_completion; | 165 match.inline_autocompletion = inline_completion; |
| 149 match.destination_url = GURL(match_string); | 166 match.destination_url = GURL(match_string); |
| 150 match.contents = match_string; | 167 match.contents = match_string; |
| 151 match.contents_class = styles; | 168 match.contents_class = styles; |
| 152 matches_.push_back(match); | 169 matches_.push_back(match); |
| 153 } | 170 } |
| OLD | NEW |