Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/ui/toolbar/toolbar_model_impl.h" | 5 #include "chrome/browser/ui/toolbar/toolbar_model_impl.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/autocomplete/autocomplete_classifier.h" | 10 #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 34 #include "net/cert/x509_certificate.h" | 34 #include "net/cert/x509_certificate.h" |
| 35 #include "ui/base/l10n/l10n_util.h" | 35 #include "ui/base/l10n/l10n_util.h" |
| 36 | 36 |
| 37 using content::NavigationController; | 37 using content::NavigationController; |
| 38 using content::NavigationEntry; | 38 using content::NavigationEntry; |
| 39 using content::SSLStatus; | 39 using content::SSLStatus; |
| 40 using content::WebContents; | 40 using content::WebContents; |
| 41 | 41 |
| 42 ToolbarModelImpl::ToolbarModelImpl(ToolbarModelDelegate* delegate) | 42 ToolbarModelImpl::ToolbarModelImpl(ToolbarModelDelegate* delegate) |
| 43 : delegate_(delegate), | 43 : delegate_(delegate), |
| 44 input_in_progress_(false) { | 44 input_in_progress_(false), |
| 45 supports_extraction_of_url_like_search_terms_(false) { | |
| 45 } | 46 } |
| 46 | 47 |
| 47 ToolbarModelImpl::~ToolbarModelImpl() { | 48 ToolbarModelImpl::~ToolbarModelImpl() { |
| 48 } | 49 } |
| 49 | 50 |
| 50 ToolbarModel::SecurityLevel ToolbarModelImpl::GetSecurityLevelForWebContents( | 51 ToolbarModel::SecurityLevel ToolbarModelImpl::GetSecurityLevelForWebContents( |
| 51 content::WebContents* web_contents) { | 52 content::WebContents* web_contents) { |
| 52 if (!web_contents) | 53 if (!web_contents) |
| 53 return NONE; | 54 return NONE; |
| 54 | 55 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 80 default: | 81 default: |
| 81 NOTREACHED(); | 82 NOTREACHED(); |
| 82 return NONE; | 83 return NONE; |
| 83 } | 84 } |
| 84 } | 85 } |
| 85 | 86 |
| 86 // ToolbarModelImpl Implementation. | 87 // ToolbarModelImpl Implementation. |
| 87 string16 ToolbarModelImpl::GetText( | 88 string16 ToolbarModelImpl::GetText( |
| 88 bool display_search_urls_as_search_terms) const { | 89 bool display_search_urls_as_search_terms) const { |
| 89 if (display_search_urls_as_search_terms) { | 90 if (display_search_urls_as_search_terms) { |
| 90 string16 search_terms = GetSearchTerms(); | 91 string16 search_terms( |
| 91 if (!search_terms.empty()) | 92 chrome::GetSearchTerms(delegate_->GetActiveWebContents())); |
| 93 if (GetSearchTermsTypeInternal(search_terms) != NO_SEARCH_TERMS) | |
| 92 return search_terms; | 94 return search_terms; |
| 93 } | 95 } |
| 94 std::string languages; // Empty if we don't have a |navigation_controller|. | 96 std::string languages; // Empty if we don't have a |navigation_controller|. |
| 95 Profile* profile = GetProfile(); | 97 Profile* profile = GetProfile(); |
| 96 if (profile) | 98 if (profile) |
| 97 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages); | 99 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 98 | 100 |
| 99 GURL url(GetURL()); | 101 GURL url(GetURL()); |
| 100 if (url.spec().length() > content::kMaxURLDisplayChars) | 102 if (url.spec().length() > content::kMaxURLDisplayChars) |
| 101 url = url.IsStandard() ? url.GetOrigin() : GURL(url.scheme() + ":"); | 103 url = url.IsStandard() ? url.GetOrigin() : GURL(url.scheme() + ":"); |
| 102 // Note that we can't unescape spaces here, because if the user copies this | 104 // Note that we can't unescape spaces here, because if the user copies this |
| 103 // and pastes it into another program, that program may think the URL ends at | 105 // and pastes it into another program, that program may think the URL ends at |
| 104 // the space. | 106 // the space. |
| 105 return AutocompleteInput::FormattedStringWithEquivalentMeaning( | 107 return AutocompleteInput::FormattedStringWithEquivalentMeaning( |
| 106 url, net::FormatUrl(url, languages, net::kFormatUrlOmitAll, | 108 url, net::FormatUrl(url, languages, net::kFormatUrlOmitAll, |
| 107 net::UnescapeRule::NORMAL, NULL, NULL, NULL)); | 109 net::UnescapeRule::NORMAL, NULL, NULL, NULL)); |
| 108 } | 110 } |
| 109 | 111 |
| 110 string16 ToolbarModelImpl::GetCorpusNameForMobile() const { | 112 string16 ToolbarModelImpl::GetCorpusNameForMobile() const { |
| 111 if (!WouldReplaceSearchURLWithSearchTerms()) | 113 if (GetSearchTermsType() == NO_SEARCH_TERMS) |
| 112 return string16(); | 114 return string16(); |
| 113 GURL url(GetURL()); | 115 GURL url(GetURL()); |
| 114 // If there is a query in the url fragment look for the corpus name there, | 116 // If there is a query in the url fragment look for the corpus name there, |
| 115 // otherwise look for the corpus name in the query parameters. | 117 // otherwise look for the corpus name in the query parameters. |
| 116 const std::string& query_str(google_util::HasGoogleSearchQueryParam( | 118 const std::string& query_str(google_util::HasGoogleSearchQueryParam( |
| 117 url.ref()) ? url.ref() : url.query()); | 119 url.ref()) ? url.ref() : url.query()); |
| 118 url_parse::Component query(0, query_str.length()), key, value; | 120 url_parse::Component query(0, query_str.length()), key, value; |
| 119 const char kChipKey[] = "sboxchip"; | 121 const char kChipKey[] = "sboxchip"; |
| 120 while (url_parse::ExtractQueryKeyValue(query_str.c_str(), &query, &key, | 122 while (url_parse::ExtractQueryKeyValue(query_str.c_str(), &query, &key, |
| 121 &value)) { | 123 &value)) { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 132 const NavigationController* navigation_controller = GetNavigationController(); | 134 const NavigationController* navigation_controller = GetNavigationController(); |
| 133 if (navigation_controller) { | 135 if (navigation_controller) { |
| 134 const NavigationEntry* entry = navigation_controller->GetVisibleEntry(); | 136 const NavigationEntry* entry = navigation_controller->GetVisibleEntry(); |
| 135 if (entry) | 137 if (entry) |
| 136 return ShouldDisplayURL() ? entry->GetVirtualURL() : GURL(); | 138 return ShouldDisplayURL() ? entry->GetVirtualURL() : GURL(); |
| 137 } | 139 } |
| 138 | 140 |
| 139 return GURL(chrome::kAboutBlankURL); | 141 return GURL(chrome::kAboutBlankURL); |
| 140 } | 142 } |
| 141 | 143 |
| 142 bool ToolbarModelImpl::WouldReplaceSearchURLWithSearchTerms() const { | 144 ToolbarModel::SearchTermsType ToolbarModelImpl::GetSearchTermsType() const { |
| 143 return !GetSearchTerms().empty(); | 145 return GetSearchTermsTypeInternal( |
| 146 chrome::GetSearchTerms(delegate_->GetActiveWebContents())); | |
| 147 } | |
| 148 | |
| 149 void ToolbarModelImpl::SetSupportsExtractionOfURLLikeSearchTerms(bool value) { | |
| 150 supports_extraction_of_url_like_search_terms_ = value; | |
| 144 } | 151 } |
| 145 | 152 |
| 146 bool ToolbarModelImpl::ShouldDisplayURL() const { | 153 bool ToolbarModelImpl::ShouldDisplayURL() const { |
| 147 // Note: The order here is important. | 154 // Note: The order here is important. |
| 148 // - The WebUI test must come before the extension scheme test because there | 155 // - The WebUI test must come before the extension scheme test because there |
| 149 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In | 156 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In |
| 150 // that case, we should prefer what the WebUI instance says. | 157 // that case, we should prefer what the WebUI instance says. |
| 151 // - The view-source test must come before the NTP test because of the case | 158 // - The view-source test must come before the NTP test because of the case |
| 152 // of view-source:chrome://newtab, which should display its URL despite what | 159 // of view-source:chrome://newtab, which should display its URL despite what |
| 153 // chrome://newtab says. | 160 // chrome://newtab says. |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 176 } | 183 } |
| 177 | 184 |
| 178 ToolbarModel::SecurityLevel ToolbarModelImpl::GetSecurityLevel() const { | 185 ToolbarModel::SecurityLevel ToolbarModelImpl::GetSecurityLevel() const { |
| 179 if (input_in_progress_) // When editing, assume no security style. | 186 if (input_in_progress_) // When editing, assume no security style. |
| 180 return NONE; | 187 return NONE; |
| 181 | 188 |
| 182 return GetSecurityLevelForWebContents(delegate_->GetActiveWebContents()); | 189 return GetSecurityLevelForWebContents(delegate_->GetActiveWebContents()); |
| 183 } | 190 } |
| 184 | 191 |
| 185 int ToolbarModelImpl::GetIcon() const { | 192 int ToolbarModelImpl::GetIcon() const { |
| 186 if (WouldReplaceSearchURLWithSearchTerms()) | 193 SearchTermsType search_terms_type = GetSearchTermsType(); |
| 194 SecurityLevel security_level = GetSecurityLevel(); | |
| 195 bool is_secure = security_level == EV_SECURE || security_level == SECURE; | |
| 196 if (search_terms_type == NORMAL_SEARCH_TERMS || | |
| 197 (search_terms_type == URL_LIKE_SEARCH_TERMS && is_secure)) | |
| 187 return IDR_OMNIBOX_SEARCH; | 198 return IDR_OMNIBOX_SEARCH; |
| 199 | |
| 188 static int icon_ids[NUM_SECURITY_LEVELS] = { | 200 static int icon_ids[NUM_SECURITY_LEVELS] = { |
| 189 IDR_LOCATION_BAR_HTTP, | 201 IDR_LOCATION_BAR_HTTP, |
| 190 IDR_OMNIBOX_HTTPS_VALID, | 202 IDR_OMNIBOX_HTTPS_VALID, |
| 191 IDR_OMNIBOX_HTTPS_VALID, | 203 IDR_OMNIBOX_HTTPS_VALID, |
| 192 IDR_OMNIBOX_HTTPS_WARNING, | 204 IDR_OMNIBOX_HTTPS_WARNING, |
| 193 IDR_OMNIBOX_HTTPS_INVALID, | 205 IDR_OMNIBOX_HTTPS_INVALID, |
| 194 }; | 206 }; |
| 195 DCHECK(arraysize(icon_ids) == NUM_SECURITY_LEVELS); | 207 DCHECK(arraysize(icon_ids) == NUM_SECURITY_LEVELS); |
| 196 return icon_ids[GetSecurityLevel()]; | 208 return icon_ids[security_level]; |
| 197 } | 209 } |
| 198 | 210 |
| 199 string16 ToolbarModelImpl::GetEVCertName() const { | 211 string16 ToolbarModelImpl::GetEVCertName() const { |
| 200 DCHECK_EQ(GetSecurityLevel(), EV_SECURE); | 212 DCHECK_EQ(GetSecurityLevel(), EV_SECURE); |
| 201 scoped_refptr<net::X509Certificate> cert; | 213 scoped_refptr<net::X509Certificate> cert; |
| 202 // Note: Navigation controller and active entry are guaranteed non-NULL or | 214 // Note: Navigation controller and active entry are guaranteed non-NULL or |
| 203 // the security level would be NONE. | 215 // the security level would be NONE. |
| 204 content::CertStore::GetInstance()->RetrieveCert( | 216 content::CertStore::GetInstance()->RetrieveCert( |
| 205 GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id, &cert); | 217 GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id, &cert); |
| 206 return GetEVCertName(*cert); | 218 return GetEVCertName(*cert); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 237 return current_tab ? ¤t_tab->GetController() : NULL; | 249 return current_tab ? ¤t_tab->GetController() : NULL; |
| 238 } | 250 } |
| 239 | 251 |
| 240 Profile* ToolbarModelImpl::GetProfile() const { | 252 Profile* ToolbarModelImpl::GetProfile() const { |
| 241 NavigationController* navigation_controller = GetNavigationController(); | 253 NavigationController* navigation_controller = GetNavigationController(); |
| 242 return navigation_controller ? | 254 return navigation_controller ? |
| 243 Profile::FromBrowserContext(navigation_controller->GetBrowserContext()) : | 255 Profile::FromBrowserContext(navigation_controller->GetBrowserContext()) : |
| 244 NULL; | 256 NULL; |
| 245 } | 257 } |
| 246 | 258 |
| 247 string16 ToolbarModelImpl::GetSearchTerms() const { | 259 ToolbarModel::SearchTermsType ToolbarModelImpl::GetSearchTermsTypeInternal( |
| 248 const WebContents* contents = delegate_->GetActiveWebContents(); | 260 const string16& search_terms) const { |
| 249 string16 search_terms = chrome::GetSearchTerms(contents); | 261 if (search_terms.empty()) |
| 262 return NO_SEARCH_TERMS; | |
| 250 | 263 |
| 251 // Don't extract search terms that the omnibox would treat as a navigation. | 264 AutocompleteMatch match; |
| 252 // This might confuse users into believing that the search terms were the | 265 AutocompleteClassifierFactory::GetForProfile(Profile::FromBrowserContext( |
| 253 // URL of the current page, and could cause problems if users hit enter in | 266 delegate_->GetActiveWebContents()->GetBrowserContext()))->Classify( |
| 254 // the omnibox expecting to reload the page. | 267 search_terms, false, false, &match, NULL); |
| 255 if (!search_terms.empty()) { | 268 // TODO(sail): Remove this once server side mixed content problems are fixed. |
| 256 AutocompleteMatch match; | 269 // For now treat mixed content similar to url shaped queries. |
|
Peter Kasting
2013/04/25 22:48:39
I don't think this TODO is correct, because I thin
sail
2013/04/25 23:39:16
Done.
| |
| 257 Profile* profile = | 270 ToolbarModel::SecurityLevel security_level = GetSecurityLevel(); |
| 258 Profile::FromBrowserContext(contents->GetBrowserContext()); | 271 if (AutocompleteMatch::IsSearchType(match.type) && |
| 259 AutocompleteClassifierFactory::GetForProfile(profile)->Classify( | 272 ((security_level == SECURE) || (security_level == EV_SECURE))) |
| 260 search_terms, false, false, &match, NULL); | 273 return NORMAL_SEARCH_TERMS; |
| 261 if (!AutocompleteMatch::IsSearchType(match.type)) | |
| 262 search_terms.clear(); | |
| 263 } | |
| 264 | 274 |
| 265 return search_terms; | 275 return supports_extraction_of_url_like_search_terms_? |
| 276 URL_LIKE_SEARCH_TERMS : NO_SEARCH_TERMS; | |
| 266 } | 277 } |
| OLD | NEW |