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/autocomplete_provider.h" 6 7 #include "base/logging.h" 8 #include "base/prefs/pref_service.h" 9 #include "base/strings/utf_string_conversions.h" 10 #include "chrome/browser/autocomplete/autocomplete_input.h" 11 #include "chrome/browser/autocomplete/autocomplete_match.h" 12 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" 13 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/common/pref_names.h" 16 #include "components/bookmarks/browser/bookmark_model.h" 17 #include "components/url_fixer/url_fixer.h" 18 #include "content/public/common/url_constants.h" 19 #include "net/base/net_util.h" 20 #include "url/gurl.h" 21 22 // static 23 const size_t AutocompleteProvider::kMaxMatches = 3; 24 25 AutocompleteProvider::AutocompleteProvider( 26 AutocompleteProviderListener* listener, 27 Profile* profile, 28 Type type) 29 : profile_(profile), 30 listener_(listener), 31 done_(true), 32 type_(type) { 33 } 34 35 // static 36 const char* AutocompleteProvider::TypeToString(Type type) { 37 switch (type) { 38 case TYPE_BOOKMARK: 39 return "Bookmark"; 40 case TYPE_BUILTIN: 41 return "Builtin"; 42 case TYPE_EXTENSION_APP: 43 return "ExtensionApp"; 44 case TYPE_HISTORY_QUICK: 45 return "HistoryQuick"; 46 case TYPE_HISTORY_URL: 47 return "HistoryURL"; 48 case TYPE_KEYWORD: 49 return "Keyword"; 50 case TYPE_SEARCH: 51 return "Search"; 52 case TYPE_SHORTCUTS: 53 return "Shortcuts"; 54 case TYPE_ZERO_SUGGEST: 55 return "ZeroSuggest"; 56 default: 57 NOTREACHED() << "Unhandled AutocompleteProvider::Type " << type; 58 return "Unknown"; 59 } 60 } 61 62 void AutocompleteProvider::Stop(bool clear_cached_results) { 63 done_ = true; 64 } 65 66 const char* AutocompleteProvider::GetName() const { 67 return TypeToString(type_); 68 } 69 70 metrics::OmniboxEventProto_ProviderType AutocompleteProvider:: 71 AsOmniboxEventProviderType() const { 72 switch (type_) { 73 case TYPE_BOOKMARK: 74 return metrics::OmniboxEventProto::BOOKMARK; 75 case TYPE_BUILTIN: 76 return metrics::OmniboxEventProto::BUILTIN; 77 case TYPE_EXTENSION_APP: 78 return metrics::OmniboxEventProto::EXTENSION_APPS; 79 case TYPE_HISTORY_QUICK: 80 return metrics::OmniboxEventProto::HISTORY_QUICK; 81 case TYPE_HISTORY_URL: 82 return metrics::OmniboxEventProto::HISTORY_URL; 83 case TYPE_KEYWORD: 84 return metrics::OmniboxEventProto::KEYWORD; 85 case TYPE_SEARCH: 86 return metrics::OmniboxEventProto::SEARCH; 87 case TYPE_SHORTCUTS: 88 return metrics::OmniboxEventProto::SHORTCUTS; 89 case TYPE_ZERO_SUGGEST: 90 return metrics::OmniboxEventProto::ZERO_SUGGEST; 91 default: 92 NOTREACHED() << "Unhandled AutocompleteProvider::Type " << type_; 93 return metrics::OmniboxEventProto::UNKNOWN_PROVIDER; 94 } 95 } 96 97 void AutocompleteProvider::DeleteMatch(const AutocompleteMatch& match) { 98 DLOG(WARNING) << "The AutocompleteProvider '" << GetName() 99 << "' has not implemented DeleteMatch."; 100 } 101 102 void AutocompleteProvider::AddProviderInfo(ProvidersInfo* provider_info) const { 103 } 104 105 void AutocompleteProvider::ResetSession() { 106 } 107 108 base::string16 AutocompleteProvider::StringForURLDisplay(const GURL& url, 109 bool check_accept_lang, 110 bool trim_http) const { 111 std::string languages = (check_accept_lang && profile_) ? 112 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages) : std::string(); 113 return net::FormatUrl(url, languages, 114 net::kFormatUrlOmitAll & ~(trim_http ? 0 : net::kFormatUrlOmitHTTP), 115 net::UnescapeRule::SPACES, NULL, NULL, NULL); 116 } 117 118 AutocompleteProvider::~AutocompleteProvider() { 119 Stop(false); 120 } 121 122 void AutocompleteProvider::UpdateStarredStateOfMatches() { 123 if (matches_.empty()) 124 return; 125 126 if (!profile_) 127 return; 128 129 BookmarkModel* bookmark_model = BookmarkModelFactory::GetForProfile(profile_); 130 if (!bookmark_model || !bookmark_model->loaded()) 131 return; 132 133 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) 134 i->starred = bookmark_model->IsBookmarked(i->destination_url); 135 } 136 137 // static 138 AutocompleteProvider::FixupReturn AutocompleteProvider::FixupUserInput( 139 const AutocompleteInput& input) { 140 const base::string16& input_text = input.text(); 141 const FixupReturn failed(false, input_text); 142 143 // Fixup and canonicalize user input. 144 const GURL canonical_gurl( 145 url_fixer::FixupURL(base::UTF16ToUTF8(input_text), std::string())); 146 std::string canonical_gurl_str(canonical_gurl.possibly_invalid_spec()); 147 if (canonical_gurl_str.empty()) { 148 // This probably won't happen, but there are no guarantees. 149 return failed; 150 } 151 152 // If the user types a number, GURL will convert it to a dotted quad. 153 // However, if the parser did not mark this as a URL, then the user probably 154 // didn't intend this interpretation. Since this can break history matching 155 // for hostname beginning with numbers (e.g. input of "17173" will be matched 156 // against "0.0.67.21" instead of the original "17173", failing to find 157 // "17173.com"), swap the original hostname in for the fixed-up one. 158 if ((input.type() != metrics::OmniboxInputType::URL) && 159 canonical_gurl.HostIsIPAddress()) { 160 std::string original_hostname = 161 base::UTF16ToUTF8(input_text.substr(input.parts().host.begin, 162 input.parts().host.len)); 163 const url::Parsed& parts = 164 canonical_gurl.parsed_for_possibly_invalid_spec(); 165 // parts.host must not be empty when HostIsIPAddress() is true. 166 DCHECK(parts.host.is_nonempty()); 167 canonical_gurl_str.replace(parts.host.begin, parts.host.len, 168 original_hostname); 169 } 170 base::string16 output(base::UTF8ToUTF16(canonical_gurl_str)); 171 // Don't prepend a scheme when the user didn't have one. Since the fixer 172 // upper only prepends the "http" scheme, that's all we need to check for. 173 if (!AutocompleteInput::HasHTTPScheme(input_text)) 174 TrimHttpPrefix(&output); 175 176 // Make the number of trailing slashes on the output exactly match the input. 177 // Examples of why not doing this would matter: 178 // * The user types "a" and has this fixed up to "a/". Now no other sites 179 // beginning with "a" will match. 180 // * The user types "file:" and has this fixed up to "file://". Now inline 181 // autocomplete will append too few slashes, resulting in e.g. "file:/b..." 182 // instead of "file:///b..." 183 // * The user types "http:/" and has this fixed up to "http:". Now inline 184 // autocomplete will append too many slashes, resulting in e.g. 185 // "http:///c..." instead of "http://c...". 186 // NOTE: We do this after calling TrimHttpPrefix() since that can strip 187 // trailing slashes (if the scheme is the only thing in the input). It's not 188 // clear that the result of fixup really matters in this case, but there's no 189 // harm in making sure. 190 const size_t last_input_nonslash = 191 input_text.find_last_not_of(base::ASCIIToUTF16("/\\")); 192 const size_t num_input_slashes = 193 (last_input_nonslash == base::string16::npos) ? 194 input_text.length() : (input_text.length() - 1 - last_input_nonslash); 195 const size_t last_output_nonslash = 196 output.find_last_not_of(base::ASCIIToUTF16("/\\")); 197 const size_t num_output_slashes = 198 (last_output_nonslash == base::string16::npos) ? 199 output.length() : (output.length() - 1 - last_output_nonslash); 200 if (num_output_slashes < num_input_slashes) 201 output.append(num_input_slashes - num_output_slashes, '/'); 202 else if (num_output_slashes > num_input_slashes) 203 output.erase(output.length() - num_output_slashes + num_input_slashes); 204 if (output.empty()) 205 return failed; 206 207 return FixupReturn(true, output); 208 } 209 210 // static 211 size_t AutocompleteProvider::TrimHttpPrefix(base::string16* url) { 212 // Find any "http:". 213 if (!AutocompleteInput::HasHTTPScheme(*url)) 214 return 0; 215 size_t scheme_pos = 216 url->find(base::ASCIIToUTF16(url::kHttpScheme) + base::char16(':')); 217 DCHECK_NE(base::string16::npos, scheme_pos); 218 219 // Erase scheme plus up to two slashes. 220 size_t prefix_end = scheme_pos + strlen(url::kHttpScheme) + 1; 221 const size_t after_slashes = std::min(url->length(), prefix_end + 2); 222 while ((prefix_end < after_slashes) && ((*url)[prefix_end] == '/')) 223 ++prefix_end; 224 url->erase(scheme_pos, prefix_end - scheme_pos); 225 return (scheme_pos == 0) ? prefix_end : 0; 226 } 227