OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/autocomplete/autocomplete_match.h" | |
10 #include "chrome/browser/browser_about_handler.h" | 9 #include "chrome/browser/browser_about_handler.h" |
11 #include "chrome/browser/net/url_fixer_upper.h" | 10 #include "chrome/browser/net/url_fixer_upper.h" |
| 11 #include "chrome/common/url_constants.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 // This list should be kept in sync with chrome/common/url_constants.h. |
| 16 const char* kChromeSettingsSubPages[] = { |
| 17 chrome::kAdvancedOptionsSubPage, |
| 18 chrome::kAutofillSubPage, |
| 19 chrome::kBrowserOptionsSubPage, |
| 20 chrome::kClearBrowserDataSubPage, |
| 21 chrome::kContentSettingsSubPage, |
| 22 chrome::kContentSettingsExceptionsSubPage, |
| 23 chrome::kImportDataSubPage, |
| 24 chrome::kInstantConfirmPage, |
| 25 chrome::kLanguageOptionsSubPage, |
| 26 chrome::kPersonalOptionsSubPage, |
| 27 chrome::kPasswordManagerSubPage, |
| 28 chrome::kSearchEnginesSubPage, |
| 29 chrome::kSyncSetupSubPage, |
| 30 #if defined(OS_CHROMEOS) |
| 31 chrome::kAboutOptionsSubPage, |
| 32 chrome::kInternetOptionsSubPage, |
| 33 chrome::kSystemOptionsSubPage, |
| 34 #endif |
| 35 }; |
| 36 |
| 37 } // namespace |
12 | 38 |
13 const int BuiltinProvider::kRelevance = 575; | 39 const int BuiltinProvider::kRelevance = 575; |
14 | 40 |
15 BuiltinProvider::BuiltinProvider(ACProviderListener* listener, | 41 BuiltinProvider::BuiltinProvider(ACProviderListener* listener, |
16 Profile* profile) | 42 Profile* profile) |
17 : AutocompleteProvider(listener, profile, "Builtin") { | 43 : AutocompleteProvider(listener, profile, "Builtin") { |
18 std::vector<std::string> builtins(ChromePaths()); | 44 std::vector<std::string> builtins(ChromePaths()); |
19 for (std::vector<std::string>::iterator i(builtins.begin()); | 45 for (std::vector<std::string>::iterator i(builtins.begin()); |
20 i != builtins.end(); ++i) | 46 i != builtins.end(); ++i) |
21 builtins_.push_back(ASCIIToUTF16("about:") + ASCIIToUTF16(*i)); | 47 builtins_.push_back(ASCIIToUTF16(*i)); |
| 48 string16 settings(ASCIIToUTF16(chrome::kChromeUISettingsHost) + |
| 49 ASCIIToUTF16("/")); |
| 50 for (size_t i = 0; i < arraysize(kChromeSettingsSubPages); i++) |
| 51 builtins_.push_back(settings + ASCIIToUTF16(kChromeSettingsSubPages[i])); |
22 } | 52 } |
23 | 53 |
24 BuiltinProvider::~BuiltinProvider() {} | 54 BuiltinProvider::~BuiltinProvider() {} |
25 | 55 |
26 void BuiltinProvider::Start(const AutocompleteInput& input, | 56 void BuiltinProvider::Start(const AutocompleteInput& input, |
27 bool minimal_changes) { | 57 bool minimal_changes) { |
28 matches_.clear(); | 58 matches_.clear(); |
29 if ((input.type() == AutocompleteInput::INVALID) || | 59 if ((input.type() == AutocompleteInput::INVALID) || |
30 (input.type() == AutocompleteInput::FORCED_QUERY) || | 60 (input.type() == AutocompleteInput::FORCED_QUERY) || |
31 (input.type() == AutocompleteInput::QUERY) || | 61 (input.type() == AutocompleteInput::QUERY) || |
32 (input.matches_requested() == AutocompleteInput::BEST_MATCH)) | 62 (input.matches_requested() == AutocompleteInput::BEST_MATCH)) |
33 return; | 63 return; |
34 for (Builtins::const_iterator i(builtins_.begin()); | 64 |
35 (i != builtins_.end()) && (matches_.size() < kMaxMatches); ++i) { | 65 static const string16 kAbout = ASCIIToUTF16(chrome::kAboutScheme) + |
36 if (StartsWith(*i, input.text(), false)) { | 66 ASCIIToUTF16(chrome::kStandardSchemeSeparator); |
37 AutocompleteMatch match(this, kRelevance, false, | 67 static const string16 kChrome = ASCIIToUTF16(chrome::kChromeUIScheme) + |
38 AutocompleteMatch::NAVSUGGEST); | 68 ASCIIToUTF16(chrome::kStandardSchemeSeparator); |
39 match.fill_into_edit = *i; | 69 |
40 match.destination_url = GURL(*i); | 70 static const int kUrl = ACMatchClassification::URL; |
41 match.contents = match.fill_into_edit; | 71 static const int kMatch = kUrl | ACMatchClassification::MATCH; |
42 match.contents_class.push_back(ACMatchClassification(0, | 72 |
43 ACMatchClassification::MATCH | ACMatchClassification::URL)); | 73 string16 text = input.text(); |
44 if (match.contents.length() > input.text().length()) { | 74 bool starting_chrome = StartsWith(kChrome, text, false); |
45 match.contents_class.push_back( | 75 if (starting_chrome || StartsWith(kAbout, text, false)) { |
46 ACMatchClassification(input.text().length(), | 76 ACMatchClassifications styles; |
47 ACMatchClassification::URL)); | 77 // Highlight the input portion matching "chrome://"; or if the user has |
| 78 // input "about:" (with optional slashes), highlight the whole "chrome://". |
| 79 static const size_t kAboutSchemeLength = strlen(chrome::kAboutScheme); |
| 80 bool highlight = starting_chrome || text.length() > kAboutSchemeLength; |
| 81 styles.push_back(ACMatchClassification(0, highlight ? kMatch : kUrl)); |
| 82 size_t offset = starting_chrome ? text.length() : kChrome.length(); |
| 83 if (highlight) |
| 84 styles.push_back(ACMatchClassification(offset, kUrl)); |
| 85 // Include some common builtin chrome URLs as the user types the scheme. |
| 86 AddMatch(ASCIIToUTF16(chrome::kChromeUIChromeURLsURL), styles); |
| 87 AddMatch(ASCIIToUTF16(chrome::kChromeUISettingsURL), styles); |
| 88 AddMatch(ASCIIToUTF16(chrome::kChromeUIVersionURL), styles); |
| 89 } else { |
| 90 // Match input about: or chrome: URL input against builtin chrome URLs. |
| 91 GURL url = URLFixerUpper::FixupURL(UTF16ToUTF8(text), std::string()); |
| 92 if (url.SchemeIs(chrome::kChromeUIScheme) && url.has_host()) { |
| 93 // Include the path for sub-pages (e.g. "chrome://settings/browser"). |
| 94 string16 host_and_path = UTF8ToUTF16(url.host() + url.path()); |
| 95 TrimString(host_and_path, ASCIIToUTF16("/").c_str(), &host_and_path); |
| 96 size_t match_length = kChrome.length() + host_and_path.length(); |
| 97 for (Builtins::const_iterator i(builtins_.begin()); |
| 98 (i != builtins_.end()) && (matches_.size() < kMaxMatches); ++i) { |
| 99 if (StartsWith(*i, host_and_path, false)) { |
| 100 ACMatchClassifications styles; |
| 101 // Highlight the "chrome://" scheme, even for input "about:foo". |
| 102 styles.push_back(ACMatchClassification(0, kMatch)); |
| 103 string16 match_string = kChrome + *i; |
| 104 if (match_string.length() > match_length) |
| 105 styles.push_back(ACMatchClassification(match_length, kUrl)); |
| 106 AddMatch(match_string, styles); |
| 107 } |
48 } | 108 } |
49 matches_.push_back(match); | |
50 } | 109 } |
51 } | 110 } |
| 111 |
52 for (size_t i = 0; i < matches_.size(); ++i) | 112 for (size_t i = 0; i < matches_.size(); ++i) |
53 matches_[i].relevance = kRelevance + matches_.size() - (i + 1); | 113 matches_[i].relevance = kRelevance + matches_.size() - (i + 1); |
54 } | 114 } |
| 115 |
| 116 void BuiltinProvider::AddMatch(const string16& match_string, |
| 117 const ACMatchClassifications& styles) { |
| 118 AutocompleteMatch match(this, kRelevance, false, |
| 119 AutocompleteMatch::NAVSUGGEST); |
| 120 match.fill_into_edit = match_string; |
| 121 match.destination_url = GURL(match_string); |
| 122 match.contents = match_string; |
| 123 match.contents_class = styles; |
| 124 matches_.push_back(match); |
| 125 } |
OLD | NEW |