| 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" |
| 9 #include "chrome/browser/browser_about_handler.h" | 10 #include "chrome/browser/browser_about_handler.h" |
| 10 #include "chrome/browser/net/url_fixer_upper.h" | 11 #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 | |
| 38 | 12 |
| 39 const int BuiltinProvider::kRelevance = 575; | 13 const int BuiltinProvider::kRelevance = 575; |
| 40 | 14 |
| 41 BuiltinProvider::BuiltinProvider(ACProviderListener* listener, | 15 BuiltinProvider::BuiltinProvider(ACProviderListener* listener, |
| 42 Profile* profile) | 16 Profile* profile) |
| 43 : AutocompleteProvider(listener, profile, "Builtin") { | 17 : AutocompleteProvider(listener, profile, "Builtin") { |
| 44 std::vector<std::string> builtins(ChromePaths()); | 18 std::vector<std::string> builtins(ChromePaths()); |
| 45 for (std::vector<std::string>::iterator i(builtins.begin()); | 19 for (std::vector<std::string>::iterator i(builtins.begin()); |
| 46 i != builtins.end(); ++i) | 20 i != builtins.end(); ++i) |
| 47 builtins_.push_back(ASCIIToUTF16(*i)); | 21 builtins_.push_back(ASCIIToUTF16("about:") + 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])); | |
| 52 } | 22 } |
| 53 | 23 |
| 54 BuiltinProvider::~BuiltinProvider() {} | 24 BuiltinProvider::~BuiltinProvider() {} |
| 55 | 25 |
| 56 void BuiltinProvider::Start(const AutocompleteInput& input, | 26 void BuiltinProvider::Start(const AutocompleteInput& input, |
| 57 bool minimal_changes) { | 27 bool minimal_changes) { |
| 58 matches_.clear(); | 28 matches_.clear(); |
| 59 if ((input.type() == AutocompleteInput::INVALID) || | 29 if ((input.type() == AutocompleteInput::INVALID) || |
| 60 (input.type() == AutocompleteInput::FORCED_QUERY) || | 30 (input.type() == AutocompleteInput::FORCED_QUERY) || |
| 61 (input.type() == AutocompleteInput::QUERY) || | 31 (input.type() == AutocompleteInput::QUERY) || |
| 62 (input.matches_requested() == AutocompleteInput::BEST_MATCH)) | 32 (input.matches_requested() == AutocompleteInput::BEST_MATCH)) |
| 63 return; | 33 return; |
| 64 | 34 for (Builtins::const_iterator i(builtins_.begin()); |
| 65 static const string16 kAbout = ASCIIToUTF16(chrome::kAboutScheme) + | 35 (i != builtins_.end()) && (matches_.size() < kMaxMatches); ++i) { |
| 66 ASCIIToUTF16(chrome::kStandardSchemeSeparator); | 36 if (StartsWith(*i, input.text(), false)) { |
| 67 static const string16 kChrome = ASCIIToUTF16(chrome::kChromeUIScheme) + | 37 AutocompleteMatch match(this, kRelevance, false, |
| 68 ASCIIToUTF16(chrome::kStandardSchemeSeparator); | 38 AutocompleteMatch::NAVSUGGEST); |
| 69 | 39 match.fill_into_edit = *i; |
| 70 static const int kUrl = ACMatchClassification::URL; | 40 match.destination_url = GURL(*i); |
| 71 static const int kMatch = kUrl | ACMatchClassification::MATCH; | 41 match.contents = match.fill_into_edit; |
| 72 | 42 match.contents_class.push_back(ACMatchClassification(0, |
| 73 string16 text = input.text(); | 43 ACMatchClassification::MATCH | ACMatchClassification::URL)); |
| 74 bool starting_chrome = StartsWith(kChrome, text, false); | 44 if (match.contents.length() > input.text().length()) { |
| 75 if (starting_chrome || StartsWith(kAbout, text, false)) { | 45 match.contents_class.push_back( |
| 76 ACMatchClassifications styles; | 46 ACMatchClassification(input.text().length(), |
| 77 // Highlight the input portion matching "chrome://"; or if the user has | 47 ACMatchClassification::URL)); |
| 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 } | |
| 108 } | 48 } |
| 49 matches_.push_back(match); |
| 109 } | 50 } |
| 110 } | 51 } |
| 111 | |
| 112 for (size_t i = 0; i < matches_.size(); ++i) | 52 for (size_t i = 0; i < matches_.size(); ++i) |
| 113 matches_[i].relevance = kRelevance + matches_.size() - (i + 1); | 53 matches_[i].relevance = kRelevance + matches_.size() - (i + 1); |
| 114 } | 54 } |
| 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 |