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 is_prominent_search_term_ui_supported_(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; |
| 92 GetSearchTerms(&search_terms); | |
| 91 if (!search_terms.empty()) | 93 if (!search_terms.empty()) |
| 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 (GetSearchTermType() == SEARCH_TERM_NONE) |
| 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::SearchTermType ToolbarModelImpl::GetSearchTermType() const { |
| 143 return !GetSearchTerms().empty(); | 145 return GetSearchTerms(NULL); |
| 146 } | |
| 147 | |
| 148 void ToolbarModelImpl::SetIsProminentSearchTermUISupported(bool value) { | |
| 149 is_prominent_search_term_ui_supported_ = value; | |
| 144 } | 150 } |
| 145 | 151 |
| 146 bool ToolbarModelImpl::ShouldDisplayURL() const { | 152 bool ToolbarModelImpl::ShouldDisplayURL() const { |
| 147 // Note: The order here is important. | 153 // Note: The order here is important. |
| 148 // - The WebUI test must come before the extension scheme test because there | 154 // - 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 | 155 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In |
| 150 // that case, we should prefer what the WebUI instance says. | 156 // 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 | 157 // - 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 | 158 // of view-source:chrome://newtab, which should display its URL despite what |
| 153 // chrome://newtab says. | 159 // chrome://newtab says. |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 176 } | 182 } |
| 177 | 183 |
| 178 ToolbarModel::SecurityLevel ToolbarModelImpl::GetSecurityLevel() const { | 184 ToolbarModel::SecurityLevel ToolbarModelImpl::GetSecurityLevel() const { |
| 179 if (input_in_progress_) // When editing, assume no security style. | 185 if (input_in_progress_) // When editing, assume no security style. |
| 180 return NONE; | 186 return NONE; |
| 181 | 187 |
| 182 return GetSecurityLevelForWebContents(delegate_->GetActiveWebContents()); | 188 return GetSecurityLevelForWebContents(delegate_->GetActiveWebContents()); |
| 183 } | 189 } |
| 184 | 190 |
| 185 int ToolbarModelImpl::GetIcon() const { | 191 int ToolbarModelImpl::GetIcon() const { |
| 186 if (WouldReplaceSearchURLWithSearchTerms()) | 192 SearchTermType search_term_type = GetSearchTermType(); |
| 193 SecurityLevel security_level = GetSecurityLevel(); | |
| 194 bool is_secure = security_level == EV_SECURE || security_level == SECURE; | |
| 195 if (search_term_type == SEARCH_TERM_NORMAL || | |
| 196 (search_term_type == SEARCH_TERM_PROMINENT && is_secure)) | |
| 187 return IDR_OMNIBOX_SEARCH; | 197 return IDR_OMNIBOX_SEARCH; |
| 198 | |
| 188 static int icon_ids[NUM_SECURITY_LEVELS] = { | 199 static int icon_ids[NUM_SECURITY_LEVELS] = { |
| 189 IDR_LOCATION_BAR_HTTP, | 200 IDR_LOCATION_BAR_HTTP, |
| 190 IDR_OMNIBOX_HTTPS_VALID, | 201 IDR_OMNIBOX_HTTPS_VALID, |
| 191 IDR_OMNIBOX_HTTPS_VALID, | 202 IDR_OMNIBOX_HTTPS_VALID, |
| 192 IDR_OMNIBOX_HTTPS_WARNING, | 203 IDR_OMNIBOX_HTTPS_WARNING, |
| 193 IDR_OMNIBOX_HTTPS_INVALID, | 204 IDR_OMNIBOX_HTTPS_INVALID, |
| 194 }; | 205 }; |
| 195 DCHECK(arraysize(icon_ids) == NUM_SECURITY_LEVELS); | 206 DCHECK(arraysize(icon_ids) == NUM_SECURITY_LEVELS); |
| 196 return icon_ids[GetSecurityLevel()]; | 207 return icon_ids[security_level]; |
| 197 } | 208 } |
| 198 | 209 |
| 199 string16 ToolbarModelImpl::GetEVCertName() const { | 210 string16 ToolbarModelImpl::GetEVCertName() const { |
| 200 DCHECK_EQ(GetSecurityLevel(), EV_SECURE); | 211 DCHECK_EQ(GetSecurityLevel(), EV_SECURE); |
| 201 scoped_refptr<net::X509Certificate> cert; | 212 scoped_refptr<net::X509Certificate> cert; |
| 202 // Note: Navigation controller and active entry are guaranteed non-NULL or | 213 // Note: Navigation controller and active entry are guaranteed non-NULL or |
| 203 // the security level would be NONE. | 214 // the security level would be NONE. |
| 204 content::CertStore::GetInstance()->RetrieveCert( | 215 content::CertStore::GetInstance()->RetrieveCert( |
| 205 GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id, &cert); | 216 GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id, &cert); |
| 206 return GetEVCertName(*cert); | 217 return GetEVCertName(*cert); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 237 return current_tab ? ¤t_tab->GetController() : NULL; | 248 return current_tab ? ¤t_tab->GetController() : NULL; |
| 238 } | 249 } |
| 239 | 250 |
| 240 Profile* ToolbarModelImpl::GetProfile() const { | 251 Profile* ToolbarModelImpl::GetProfile() const { |
| 241 NavigationController* navigation_controller = GetNavigationController(); | 252 NavigationController* navigation_controller = GetNavigationController(); |
| 242 return navigation_controller ? | 253 return navigation_controller ? |
| 243 Profile::FromBrowserContext(navigation_controller->GetBrowserContext()) : | 254 Profile::FromBrowserContext(navigation_controller->GetBrowserContext()) : |
| 244 NULL; | 255 NULL; |
| 245 } | 256 } |
| 246 | 257 |
| 247 string16 ToolbarModelImpl::GetSearchTerms() const { | 258 ToolbarModel::SearchTermType ToolbarModelImpl::GetSearchTerms( |
| 248 const WebContents* contents = delegate_->GetActiveWebContents(); | 259 string16* search_terms_out) const { |
| 249 string16 search_terms = chrome::GetSearchTerms(contents); | 260 string16 search_terms = |
| 261 chrome::GetSearchTerms(delegate_->GetActiveWebContents()); | |
| 262 SearchTermType type = SEARCH_TERM_NORMAL; | |
| 250 | 263 |
| 251 // Don't extract search terms that the omnibox would treat as a navigation. | 264 ToolbarModel::SecurityLevel security_level = GetSecurityLevel(); |
| 252 // This might confuse users into believing that the search terms were the | 265 if (search_terms.empty()) { |
| 253 // URL of the current page, and could cause problems if users hit enter in | 266 type = SEARCH_TERM_NONE; |
| 254 // the omnibox expecting to reload the page. | 267 } else if (security_level != EV_SECURE && security_level != SECURE) { |
| 255 if (!search_terms.empty()) { | 268 type = SEARCH_TERM_PROMINENT; |
| 269 } else { | |
| 256 AutocompleteMatch match; | 270 AutocompleteMatch match; |
| 257 Profile* profile = | 271 Profile* profile = Profile::FromBrowserContext( |
| 258 Profile::FromBrowserContext(contents->GetBrowserContext()); | 272 delegate_->GetActiveWebContents()->GetBrowserContext()); |
| 259 AutocompleteClassifierFactory::GetForProfile(profile)->Classify( | 273 AutocompleteClassifierFactory::GetForProfile(profile)->Classify( |
| 260 search_terms, false, false, &match, NULL); | 274 search_terms, false, false, &match, NULL); |
| 261 if (!AutocompleteMatch::IsSearchType(match.type)) | 275 if (!AutocompleteMatch::IsSearchType(match.type)) |
| 262 search_terms.clear(); | 276 type = SEARCH_TERM_PROMINENT; |
| 263 } | 277 } |
| 264 | 278 |
| 265 return search_terms; | 279 if (type == SEARCH_TERM_PROMINENT && |
| 280 !is_prominent_search_term_ui_supported_) { | |
| 281 type = SEARCH_TERM_NONE; | |
| 282 search_terms = string16(); | |
|
sreeram
2013/04/25 16:37:10
search_terms.clear();
sail
2013/04/25 16:41:22
Done.
| |
| 283 } | |
| 284 | |
| 285 if (search_terms_out) | |
| 286 *search_terms_out = search_terms; | |
| 287 return type; | |
| 266 } | 288 } |
| OLD | NEW |