OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/chrome_toolbar_model_delegate.h" | 5 #include "chrome/browser/ui/toolbar/chrome_toolbar_model_delegate.h" |
6 | 6 |
| 7 #include "base/logging.h" |
7 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" | 8 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" |
8 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/search/search.h" |
| 11 #include "chrome/browser/ssl/chrome_security_state_model_client.h" |
9 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/common/url_constants.h" |
| 14 #include "components/google/core/browser/google_util.h" |
10 #include "components/omnibox/browser/autocomplete_input.h" | 15 #include "components/omnibox/browser/autocomplete_input.h" |
11 #include "components/prefs/pref_service.h" | 16 #include "components/prefs/pref_service.h" |
| 17 #include "content/public/browser/cert_store.h" |
12 #include "content/public/browser/navigation_controller.h" | 18 #include "content/public/browser/navigation_controller.h" |
13 #include "content/public/browser/navigation_entry.h" | 19 #include "content/public/browser/navigation_entry.h" |
14 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 21 #include "content/public/common/ssl_status.h" |
| 22 |
| 23 ChromeToolbarModelDelegate::ChromeToolbarModelDelegate() {} |
| 24 |
| 25 ChromeToolbarModelDelegate::~ChromeToolbarModelDelegate() {} |
15 | 26 |
16 std::string ChromeToolbarModelDelegate::GetAcceptLanguages() const { | 27 std::string ChromeToolbarModelDelegate::GetAcceptLanguages() const { |
17 Profile* profile = GetProfile(); | 28 Profile* profile = GetProfile(); |
18 return profile ? profile->GetPrefs()->GetString(prefs::kAcceptLanguages) | 29 return profile ? profile->GetPrefs()->GetString(prefs::kAcceptLanguages) |
19 : std::string(); | 30 : std::string(); |
20 } | 31 } |
21 | 32 |
22 base::string16 ChromeToolbarModelDelegate::FormattedStringWithEquivalentMeaning( | 33 base::string16 ChromeToolbarModelDelegate::FormattedStringWithEquivalentMeaning( |
23 const GURL& url, | 34 const GURL& url, |
24 const base::string16& formatted_url) const { | 35 const base::string16& formatted_url) const { |
25 return AutocompleteInput::FormattedStringWithEquivalentMeaning( | 36 return AutocompleteInput::FormattedStringWithEquivalentMeaning( |
26 url, formatted_url, ChromeAutocompleteSchemeClassifier(GetProfile())); | 37 url, formatted_url, ChromeAutocompleteSchemeClassifier(GetProfile())); |
27 } | 38 } |
28 | 39 |
29 ChromeToolbarModelDelegate::ChromeToolbarModelDelegate() {} | 40 bool ChromeToolbarModelDelegate::GetURL(GURL* url) const { |
| 41 DCHECK(url); |
| 42 content::NavigationEntry* entry = GetNavigationEntry(); |
| 43 if (!entry) |
| 44 return false; |
30 | 45 |
31 ChromeToolbarModelDelegate::~ChromeToolbarModelDelegate() {} | 46 *url = ShouldDisplayURL() ? entry->GetVirtualURL() : GURL(); |
| 47 return true; |
| 48 } |
| 49 |
| 50 bool ChromeToolbarModelDelegate::ShouldDisplayURL() const { |
| 51 // Note: The order here is important. |
| 52 // - The WebUI test must come before the extension scheme test because there |
| 53 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In |
| 54 // that case, we should prefer what the WebUI instance says. |
| 55 // - The view-source test must come before the NTP test because of the case |
| 56 // of view-source:chrome://newtab, which should display its URL despite what |
| 57 // chrome://newtab says. |
| 58 content::NavigationEntry* entry = GetNavigationEntry(); |
| 59 if (entry) { |
| 60 if (entry->IsViewSourceMode() || |
| 61 entry->GetPageType() == content::PAGE_TYPE_INTERSTITIAL) { |
| 62 return true; |
| 63 } |
| 64 |
| 65 GURL url = entry->GetURL(); |
| 66 GURL virtual_url = entry->GetVirtualURL(); |
| 67 if (url.SchemeIs(content::kChromeUIScheme) || |
| 68 virtual_url.SchemeIs(content::kChromeUIScheme)) { |
| 69 if (!url.SchemeIs(content::kChromeUIScheme)) |
| 70 url = virtual_url; |
| 71 return url.host() != chrome::kChromeUINewTabHost; |
| 72 } |
| 73 } |
| 74 |
| 75 return !search::IsInstantNTP(GetActiveWebContents()); |
| 76 } |
| 77 |
| 78 security_state::SecurityStateModel::SecurityLevel |
| 79 ChromeToolbarModelDelegate::GetSecurityLevel() const { |
| 80 content::WebContents* web_contents = GetActiveWebContents(); |
| 81 // If there is no active WebContents (which can happen during toolbar |
| 82 // initialization), assume no security style. |
| 83 if (!web_contents) |
| 84 return security_state::SecurityStateModel::NONE; |
| 85 auto* client = ChromeSecurityStateModelClient::FromWebContents(web_contents); |
| 86 return client->GetSecurityInfo().security_level; |
| 87 } |
| 88 |
| 89 base::string16 ChromeToolbarModelDelegate::GetSearchTerms( |
| 90 security_state::SecurityStateModel::SecurityLevel security_level) const { |
| 91 content::WebContents* web_contents = GetActiveWebContents(); |
| 92 base::string16 search_terms(search::GetSearchTerms(web_contents)); |
| 93 if (search_terms.empty()) { |
| 94 // We mainly do this to enforce the subsequent DCHECK. |
| 95 return base::string16(); |
| 96 } |
| 97 |
| 98 // If the page is still loading and the security style is unknown, consider |
| 99 // the page secure. Without this, after the user hit enter on some search |
| 100 // terms, the omnibox would change to displaying the loading URL before |
| 101 // changing back to the search terms once they could be extracted, thus |
| 102 // causing annoying flicker. |
| 103 DCHECK(web_contents); |
| 104 content::NavigationController& nav_controller = web_contents->GetController(); |
| 105 content::NavigationEntry* entry = nav_controller.GetVisibleEntry(); |
| 106 if ((entry != nav_controller.GetLastCommittedEntry()) && |
| 107 (entry->GetSSL().security_style == content::SECURITY_STYLE_UNKNOWN)) |
| 108 return search_terms; |
| 109 |
| 110 // If the URL is using a Google base URL specified via the command line, we |
| 111 // bypass the security check below. |
| 112 if (entry && |
| 113 google_util::StartsWithCommandLineGoogleBaseURL(entry->GetVirtualURL())) |
| 114 return search_terms; |
| 115 |
| 116 // Otherwise, extract search terms for HTTPS pages that do not have a security |
| 117 // error. |
| 118 bool extract_search_terms = |
| 119 (security_level != security_state::SecurityStateModel::NONE) && |
| 120 (security_level != security_state::SecurityStateModel::SECURITY_ERROR); |
| 121 return extract_search_terms ? search_terms : base::string16(); |
| 122 } |
| 123 |
| 124 scoped_refptr<net::X509Certificate> ChromeToolbarModelDelegate::GetCertificate() |
| 125 const { |
| 126 scoped_refptr<net::X509Certificate> cert; |
| 127 content::NavigationEntry* entry = GetNavigationEntry(); |
| 128 if (entry) { |
| 129 content::CertStore::GetInstance()->RetrieveCert(entry->GetSSL().cert_id, |
| 130 &cert); |
| 131 } |
| 132 return cert; |
| 133 } |
32 | 134 |
33 content::NavigationController* | 135 content::NavigationController* |
34 ChromeToolbarModelDelegate::GetNavigationController() const { | 136 ChromeToolbarModelDelegate::GetNavigationController() const { |
35 // This |current_tab| can be null during the initialization of the toolbar | 137 // This |current_tab| can be null during the initialization of the toolbar |
36 // during window creation (i.e. before any tabs have been added to the | 138 // during window creation (i.e. before any tabs have been added to the |
37 // window). | 139 // window). |
38 content::WebContents* current_tab = GetActiveWebContents(); | 140 content::WebContents* current_tab = GetActiveWebContents(); |
39 return current_tab ? ¤t_tab->GetController() : nullptr; | 141 return current_tab ? ¤t_tab->GetController() : nullptr; |
40 } | 142 } |
41 | 143 |
| 144 content::NavigationEntry* ChromeToolbarModelDelegate::GetNavigationEntry() |
| 145 const { |
| 146 content::NavigationController* controller = GetNavigationController(); |
| 147 return controller ? controller->GetVisibleEntry() : nullptr; |
| 148 } |
| 149 |
42 Profile* ChromeToolbarModelDelegate::GetProfile() const { | 150 Profile* ChromeToolbarModelDelegate::GetProfile() const { |
43 content::NavigationController* navigation_controller = | 151 content::NavigationController* controller = GetNavigationController(); |
44 GetNavigationController(); | 152 return controller |
45 return navigation_controller ? Profile::FromBrowserContext( | 153 ? Profile::FromBrowserContext(controller->GetBrowserContext()) |
46 navigation_controller->GetBrowserContext()) | 154 : nullptr; |
47 : nullptr; | |
48 } | 155 } |
OLD | NEW |