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/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/autocomplete/autocomplete_classifier.h" | 10 #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 using content::SSLStatus; | 44 using content::SSLStatus; |
45 using content::WebContents; | 45 using content::WebContents; |
46 | 46 |
47 ToolbarModelImpl::ToolbarModelImpl(ToolbarModelDelegate* delegate) | 47 ToolbarModelImpl::ToolbarModelImpl(ToolbarModelDelegate* delegate) |
48 : delegate_(delegate) { | 48 : delegate_(delegate) { |
49 } | 49 } |
50 | 50 |
51 ToolbarModelImpl::~ToolbarModelImpl() { | 51 ToolbarModelImpl::~ToolbarModelImpl() { |
52 } | 52 } |
53 | 53 |
| 54 // static |
54 ToolbarModel::SecurityLevel ToolbarModelImpl::GetSecurityLevelForWebContents( | 55 ToolbarModel::SecurityLevel ToolbarModelImpl::GetSecurityLevelForWebContents( |
55 content::WebContents* web_contents) { | 56 content::WebContents* web_contents) { |
56 if (!web_contents) | 57 if (!web_contents) |
57 return NONE; | 58 return NONE; |
58 | 59 |
59 NavigationEntry* entry = web_contents->GetController().GetVisibleEntry(); | 60 NavigationEntry* entry = web_contents->GetController().GetVisibleEntry(); |
60 if (!entry) | 61 if (!entry) |
61 return NONE; | 62 return NONE; |
62 | 63 |
63 const SSLStatus& ssl = entry->GetSSL(); | 64 const SSLStatus& ssl = entry->GetSSL(); |
(...skipping 23 matching lines...) Expand all Loading... |
87 content::CertStore::GetInstance()->RetrieveCert(ssl.cert_id, NULL)) | 88 content::CertStore::GetInstance()->RetrieveCert(ssl.cert_id, NULL)) |
88 return EV_SECURE; | 89 return EV_SECURE; |
89 return SECURE; | 90 return SECURE; |
90 } | 91 } |
91 default: | 92 default: |
92 NOTREACHED(); | 93 NOTREACHED(); |
93 return NONE; | 94 return NONE; |
94 } | 95 } |
95 } | 96 } |
96 | 97 |
| 98 // static |
| 99 base::string16 ToolbarModelImpl::GetEVCertName( |
| 100 const net::X509Certificate& cert) { |
| 101 // EV are required to have an organization name and country. |
| 102 if (cert.subject().organization_names.empty() || |
| 103 cert.subject().country_name.empty()) { |
| 104 NOTREACHED(); |
| 105 return base::string16(); |
| 106 } |
| 107 |
| 108 return l10n_util::GetStringFUTF16( |
| 109 IDS_SECURE_CONNECTION_EV, |
| 110 base::UTF8ToUTF16(cert.subject().organization_names[0]), |
| 111 base::UTF8ToUTF16(cert.subject().country_name)); |
| 112 } |
| 113 |
97 // ToolbarModelImpl Implementation. | 114 // ToolbarModelImpl Implementation. |
98 base::string16 ToolbarModelImpl::GetText() const { | 115 base::string16 ToolbarModelImpl::GetText() const { |
99 base::string16 search_terms(GetSearchTerms(false)); | 116 base::string16 search_terms(GetSearchTerms(false)); |
100 if (!search_terms.empty()) | 117 if (!search_terms.empty()) |
101 return search_terms; | 118 return search_terms; |
102 | 119 |
103 if (WouldOmitURLDueToOriginChip()) | 120 if (WouldOmitURLDueToOriginChip()) |
104 return base::string16(); | 121 return base::string16(); |
105 | 122 |
106 return GetFormattedURL(); | 123 return GetFormattedURL(); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 const NavigationController* navigation_controller = GetNavigationController(); | 164 const NavigationController* navigation_controller = GetNavigationController(); |
148 if (navigation_controller) { | 165 if (navigation_controller) { |
149 const NavigationEntry* entry = navigation_controller->GetVisibleEntry(); | 166 const NavigationEntry* entry = navigation_controller->GetVisibleEntry(); |
150 if (entry) | 167 if (entry) |
151 return ShouldDisplayURL() ? entry->GetVirtualURL() : GURL(); | 168 return ShouldDisplayURL() ? entry->GetVirtualURL() : GURL(); |
152 } | 169 } |
153 | 170 |
154 return GURL(content::kAboutBlankURL); | 171 return GURL(content::kAboutBlankURL); |
155 } | 172 } |
156 | 173 |
157 bool ToolbarModelImpl::WouldOmitURLDueToOriginChip() const { | |
158 const char kInterstitialShownKey[] = "interstitial_shown"; | |
159 | |
160 // When users type URLs and hit enter, continue to show those URLs until | |
161 // the navigation commits or an interstitial is shown, because having the | |
162 // omnibox clear immediately feels like the input was ignored. | |
163 NavigationController* navigation_controller = GetNavigationController(); | |
164 if (navigation_controller) { | |
165 NavigationEntry* pending_entry = navigation_controller->GetPendingEntry(); | |
166 if (pending_entry) { | |
167 const NavigationEntry* visible_entry = | |
168 navigation_controller->GetVisibleEntry(); | |
169 base::string16 unused; | |
170 // Keep track that we've shown the origin chip on an interstitial so it | |
171 // can be shown even after the interstitial was dismissed, to avoid | |
172 // showing the chip, removing it and then showing it again. | |
173 if (visible_entry && | |
174 visible_entry->GetPageType() == content::PAGE_TYPE_INTERSTITIAL && | |
175 !pending_entry->GetExtraData(kInterstitialShownKey, &unused)) | |
176 pending_entry->SetExtraData(kInterstitialShownKey, base::string16()); | |
177 const content::PageTransition transition_type = | |
178 pending_entry->GetTransitionType(); | |
179 if ((transition_type & content::PAGE_TRANSITION_TYPED) != 0 && | |
180 !pending_entry->GetExtraData(kInterstitialShownKey, &unused)) | |
181 return false; | |
182 } | |
183 } | |
184 | |
185 bool should_display_origin_chip = | |
186 chrome::ShouldDisplayOriginChip() || chrome::ShouldDisplayOriginChipV2(); | |
187 return should_display_origin_chip && delegate_->InTabbedBrowser() && | |
188 ShouldDisplayURL() && url_replacement_enabled(); | |
189 } | |
190 | |
191 bool ToolbarModelImpl::WouldPerformSearchTermReplacement( | 174 bool ToolbarModelImpl::WouldPerformSearchTermReplacement( |
192 bool ignore_editing) const { | 175 bool ignore_editing) const { |
193 return !GetSearchTerms(ignore_editing).empty(); | 176 return !GetSearchTerms(ignore_editing).empty(); |
194 } | 177 } |
195 | 178 |
196 bool ToolbarModelImpl::ShouldDisplayURL() const { | |
197 // Note: The order here is important. | |
198 // - The WebUI test must come before the extension scheme test because there | |
199 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In | |
200 // that case, we should prefer what the WebUI instance says. | |
201 // - The view-source test must come before the NTP test because of the case | |
202 // of view-source:chrome://newtab, which should display its URL despite what | |
203 // chrome://newtab says. | |
204 NavigationController* controller = GetNavigationController(); | |
205 NavigationEntry* entry = controller ? controller->GetVisibleEntry() : NULL; | |
206 if (entry) { | |
207 if (entry->IsViewSourceMode() || | |
208 entry->GetPageType() == content::PAGE_TYPE_INTERSTITIAL) { | |
209 return true; | |
210 } | |
211 | |
212 GURL url = entry->GetURL(); | |
213 GURL virtual_url = entry->GetVirtualURL(); | |
214 if (url.SchemeIs(content::kChromeUIScheme) || | |
215 virtual_url.SchemeIs(content::kChromeUIScheme)) { | |
216 if (!url.SchemeIs(content::kChromeUIScheme)) | |
217 url = virtual_url; | |
218 return url.host() != chrome::kChromeUINewTabHost; | |
219 } | |
220 } | |
221 | |
222 if (chrome::IsInstantNTP(delegate_->GetActiveWebContents())) | |
223 return false; | |
224 | |
225 return true; | |
226 } | |
227 | |
228 ToolbarModel::SecurityLevel ToolbarModelImpl::GetSecurityLevel( | 179 ToolbarModel::SecurityLevel ToolbarModelImpl::GetSecurityLevel( |
229 bool ignore_editing) const { | 180 bool ignore_editing) const { |
230 // When editing, assume no security style. | 181 // When editing, assume no security style. |
231 return (input_in_progress() && !ignore_editing) ? | 182 return (input_in_progress() && !ignore_editing) ? |
232 NONE : GetSecurityLevelForWebContents(delegate_->GetActiveWebContents()); | 183 NONE : GetSecurityLevelForWebContents(delegate_->GetActiveWebContents()); |
233 } | 184 } |
234 | 185 |
235 int ToolbarModelImpl::GetIcon() const { | 186 int ToolbarModelImpl::GetIcon() const { |
236 if (WouldPerformSearchTermReplacement(false)) { | 187 if (WouldPerformSearchTermReplacement(false)) { |
237 // The secured version of the search icon is necessary if neither the search | 188 // The secured version of the search icon is necessary if neither the search |
(...skipping 28 matching lines...) Expand all Loading... |
266 base::string16 ToolbarModelImpl::GetEVCertName() const { | 217 base::string16 ToolbarModelImpl::GetEVCertName() const { |
267 DCHECK_EQ(EV_SECURE, GetSecurityLevel(false)); | 218 DCHECK_EQ(EV_SECURE, GetSecurityLevel(false)); |
268 scoped_refptr<net::X509Certificate> cert; | 219 scoped_refptr<net::X509Certificate> cert; |
269 // Note: Navigation controller and active entry are guaranteed non-NULL or | 220 // Note: Navigation controller and active entry are guaranteed non-NULL or |
270 // the security level would be NONE. | 221 // the security level would be NONE. |
271 content::CertStore::GetInstance()->RetrieveCert( | 222 content::CertStore::GetInstance()->RetrieveCert( |
272 GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id, &cert); | 223 GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id, &cert); |
273 return GetEVCertName(*cert.get()); | 224 return GetEVCertName(*cert.get()); |
274 } | 225 } |
275 | 226 |
276 // static | 227 bool ToolbarModelImpl::ShouldDisplayURL() const { |
277 base::string16 ToolbarModelImpl::GetEVCertName( | 228 // Note: The order here is important. |
278 const net::X509Certificate& cert) { | 229 // - The WebUI test must come before the extension scheme test because there |
279 // EV are required to have an organization name and country. | 230 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In |
280 if (cert.subject().organization_names.empty() || | 231 // that case, we should prefer what the WebUI instance says. |
281 cert.subject().country_name.empty()) { | 232 // - The view-source test must come before the NTP test because of the case |
282 NOTREACHED(); | 233 // of view-source:chrome://newtab, which should display its URL despite what |
283 return base::string16(); | 234 // chrome://newtab says. |
| 235 NavigationController* controller = GetNavigationController(); |
| 236 NavigationEntry* entry = controller ? controller->GetVisibleEntry() : NULL; |
| 237 if (entry) { |
| 238 if (entry->IsViewSourceMode() || |
| 239 entry->GetPageType() == content::PAGE_TYPE_INTERSTITIAL) { |
| 240 return true; |
| 241 } |
| 242 |
| 243 GURL url = entry->GetURL(); |
| 244 GURL virtual_url = entry->GetVirtualURL(); |
| 245 if (url.SchemeIs(content::kChromeUIScheme) || |
| 246 virtual_url.SchemeIs(content::kChromeUIScheme)) { |
| 247 if (!url.SchemeIs(content::kChromeUIScheme)) |
| 248 url = virtual_url; |
| 249 return url.host() != chrome::kChromeUINewTabHost; |
| 250 } |
284 } | 251 } |
285 | 252 |
286 return l10n_util::GetStringFUTF16( | 253 if (chrome::IsInstantNTP(delegate_->GetActiveWebContents())) |
287 IDS_SECURE_CONNECTION_EV, | 254 return false; |
288 base::UTF8ToUTF16(cert.subject().organization_names[0]), | 255 |
289 base::UTF8ToUTF16(cert.subject().country_name)); | 256 return true; |
| 257 } |
| 258 |
| 259 bool ToolbarModelImpl::WouldOmitURLDueToOriginChip() const { |
| 260 const char kInterstitialShownKey[] = "interstitial_shown"; |
| 261 |
| 262 // When users type URLs and hit enter, continue to show those URLs until |
| 263 // the navigation commits or an interstitial is shown, because having the |
| 264 // omnibox clear immediately feels like the input was ignored. |
| 265 NavigationController* navigation_controller = GetNavigationController(); |
| 266 if (navigation_controller) { |
| 267 NavigationEntry* pending_entry = navigation_controller->GetPendingEntry(); |
| 268 if (pending_entry) { |
| 269 const NavigationEntry* visible_entry = |
| 270 navigation_controller->GetVisibleEntry(); |
| 271 base::string16 unused; |
| 272 // Keep track that we've shown the origin chip on an interstitial so it |
| 273 // can be shown even after the interstitial was dismissed, to avoid |
| 274 // showing the chip, removing it and then showing it again. |
| 275 if (visible_entry && |
| 276 visible_entry->GetPageType() == content::PAGE_TYPE_INTERSTITIAL && |
| 277 !pending_entry->GetExtraData(kInterstitialShownKey, &unused)) |
| 278 pending_entry->SetExtraData(kInterstitialShownKey, base::string16()); |
| 279 const content::PageTransition transition_type = |
| 280 pending_entry->GetTransitionType(); |
| 281 if ((transition_type & content::PAGE_TRANSITION_TYPED) != 0 && |
| 282 !pending_entry->GetExtraData(kInterstitialShownKey, &unused)) |
| 283 return false; |
| 284 } |
| 285 } |
| 286 |
| 287 bool should_display_origin_chip = |
| 288 chrome::ShouldDisplayOriginChip() || chrome::ShouldDisplayOriginChipV2(); |
| 289 return should_display_origin_chip && delegate_->InTabbedBrowser() && |
| 290 ShouldDisplayURL() && url_replacement_enabled(); |
290 } | 291 } |
291 | 292 |
292 NavigationController* ToolbarModelImpl::GetNavigationController() const { | 293 NavigationController* ToolbarModelImpl::GetNavigationController() const { |
293 // This |current_tab| can be NULL during the initialization of the | 294 // This |current_tab| can be NULL during the initialization of the |
294 // toolbar during window creation (i.e. before any tabs have been added | 295 // toolbar during window creation (i.e. before any tabs have been added |
295 // to the window). | 296 // to the window). |
296 WebContents* current_tab = delegate_->GetActiveWebContents(); | 297 WebContents* current_tab = delegate_->GetActiveWebContents(); |
297 return current_tab ? ¤t_tab->GetController() : NULL; | 298 return current_tab ? ¤t_tab->GetController() : NULL; |
298 } | 299 } |
299 | 300 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 if (entry && | 333 if (entry && |
333 google_util::StartsWithCommandLineGoogleBaseURL(entry->GetVirtualURL())) | 334 google_util::StartsWithCommandLineGoogleBaseURL(entry->GetVirtualURL())) |
334 return search_terms; | 335 return search_terms; |
335 | 336 |
336 // Otherwise, extract search terms for HTTPS pages that do not have a security | 337 // Otherwise, extract search terms for HTTPS pages that do not have a security |
337 // error. | 338 // error. |
338 ToolbarModel::SecurityLevel security_level = GetSecurityLevel(ignore_editing); | 339 ToolbarModel::SecurityLevel security_level = GetSecurityLevel(ignore_editing); |
339 return ((security_level == NONE) || (security_level == SECURITY_ERROR)) ? | 340 return ((security_level == NONE) || (security_level == SECURITY_ERROR)) ? |
340 base::string16() : search_terms; | 341 base::string16() : search_terms; |
341 } | 342 } |
OLD | NEW |