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