Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(157)

Side by Side Diff: chrome/browser/ui/toolbar/chrome_toolbar_model_delegate.cc

Issue 1653013002: Abstract ToolbarModelImpl dependencies on //content. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@1
Patch Set: Fix compilation on Mac Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/prefs/pref_service.h" 8 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" 9 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
9 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/search/search.h"
12 #include "chrome/browser/ssl/chrome_security_state_model_client.h"
10 #include "chrome/common/pref_names.h" 13 #include "chrome/common/pref_names.h"
14 #include "chrome/common/url_constants.h"
15 #include "components/google/core/browser/google_util.h"
11 #include "components/omnibox/browser/autocomplete_input.h" 16 #include "components/omnibox/browser/autocomplete_input.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() {}
15 24
16 std::string ChromeToolbarModelDelegate::GetAcceptLanguages() const { 25 std::string ChromeToolbarModelDelegate::GetAcceptLanguages() const {
17 Profile* profile = GetProfile(); 26 Profile* profile = GetProfile();
18 return profile ? profile->GetPrefs()->GetString(prefs::kAcceptLanguages) 27 return profile ? profile->GetPrefs()->GetString(prefs::kAcceptLanguages)
19 : std::string(); 28 : std::string();
20 } 29 }
21 30
22 base::string16 ChromeToolbarModelDelegate::FormattedStringWithEquivalentMeaning( 31 base::string16 ChromeToolbarModelDelegate::FormattedStringWithEquivalentMeaning(
23 const GURL& url, 32 const GURL& url,
24 const base::string16& formatted_url) const { 33 const base::string16& formatted_url) const {
25 return AutocompleteInput::FormattedStringWithEquivalentMeaning( 34 return AutocompleteInput::FormattedStringWithEquivalentMeaning(
26 url, formatted_url, ChromeAutocompleteSchemeClassifier(GetProfile())); 35 url, formatted_url, ChromeAutocompleteSchemeClassifier(GetProfile()));
27 } 36 }
28 37
29 ChromeToolbarModelDelegate::~ChromeToolbarModelDelegate() {} 38 bool ChromeToolbarModelDelegate::GetURL(GURL* url) const {
39 DCHECK(url);
40 content::NavigationEntry* entry = GetNavigationEntry();
41 if (entry) {
Peter Kasting 2016/02/02 23:51:30 Nit: Reverse conditional and early-return to avoid
sdefresne 2016/02/03 13:14:17 Done.
42 *url = ShouldDisplayURL() ? entry->GetVirtualURL() : GURL();
43 return true;
44 }
45 return false;
46 }
47
48 bool ChromeToolbarModelDelegate::ShouldDisplayURL() const {
49 // Note: The order here is important.
50 // - The WebUI test must come before the extension scheme test because there
51 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In
52 // that case, we should prefer what the WebUI instance says.
53 // - The view-source test must come before the NTP test because of the case
54 // of view-source:chrome://newtab, which should display its URL despite what
55 // chrome://newtab says.
56 content::NavigationEntry* entry = GetNavigationEntry();
57 if (entry) {
58 if (entry->IsViewSourceMode() ||
59 entry->GetPageType() == content::PAGE_TYPE_INTERSTITIAL) {
60 return true;
61 }
62
63 GURL url = entry->GetURL();
64 GURL virtual_url = entry->GetVirtualURL();
65 if (url.SchemeIs(content::kChromeUIScheme) ||
66 virtual_url.SchemeIs(content::kChromeUIScheme)) {
67 if (!url.SchemeIs(content::kChromeUIScheme))
68 url = virtual_url;
69 return url.host() != chrome::kChromeUINewTabHost;
70 }
71 }
72
73 return !search::IsInstantNTP(GetActiveWebContents());
74 }
75
76 security_state::SecurityStateModel::SecurityLevel
77 ChromeToolbarModelDelegate::GetSecurityLevel() const {
78 content::WebContents* web_contents = GetActiveWebContents();
79 // If there is no active WebContents (which can happen during toolbar
80 // initialization), assume no security style.
81 if (!web_contents)
82 return security_state::SecurityStateModel::NONE;
83 return ChromeSecurityStateModelClient::FromWebContents(web_contents)
Peter Kasting 2016/02/02 23:51:30 Nit: Shorter: auto* client = ChromeSecurityStat
sdefresne 2016/02/03 13:14:17 Done.
84 ->GetSecurityInfo()
85 .security_level;
86 }
87
88 base::string16 ChromeToolbarModelDelegate::GetSearchTerms(
89 security_state::SecurityStateModel::SecurityLevel security_level) const {
90 content::WebContents* web_contents = GetActiveWebContents();
91 base::string16 search_terms(search::GetSearchTerms(web_contents));
92 if (search_terms.empty()) {
93 // We mainly do this to enforce the subsequent DCHECK.
94 return base::string16();
95 }
96
97 // If the page is still loading and the security style is unknown, consider
98 // the page secure. Without this, after the user hit enter on some search
99 // terms, the omnibox would change to displaying the loading URL before
100 // changing back to the search terms once they could be extracted, thus
101 // causing annoying flicker.
102 DCHECK(web_contents);
103 content::NavigationController& nav_controller = web_contents->GetController();
104 content::NavigationEntry* entry = nav_controller.GetVisibleEntry();
105 if ((entry != nav_controller.GetLastCommittedEntry()) &&
106 (entry->GetSSL().security_style == content::SECURITY_STYLE_UNKNOWN))
107 return search_terms;
108
109 // If the URL is using a Google base URL specified via the command line, we
110 // bypass the security check below.
111 if (entry &&
112 google_util::StartsWithCommandLineGoogleBaseURL(entry->GetVirtualURL()))
113 return search_terms;
114
115 // Otherwise, extract search terms for HTTPS pages that do not have a security
116 // error.
117 return ((security_level == security_state::SecurityStateModel::NONE) ||
118 (security_level ==
119 security_state::SecurityStateModel::SECURITY_ERROR))
120 ? base::string16()
121 : search_terms;
Peter Kasting 2016/02/02 23:51:30 Nit: Shorter: bool extract_search_terms =
sdefresne 2016/02/03 13:14:17 Done.
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 }
30 134
31 content::NavigationController* 135 content::NavigationController*
32 ChromeToolbarModelDelegate::GetNavigationController() const { 136 ChromeToolbarModelDelegate::GetNavigationController() const {
33 // 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
34 // 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
35 // window). 139 // window).
36 content::WebContents* current_tab = GetActiveWebContents(); 140 content::WebContents* current_tab = GetActiveWebContents();
37 return current_tab ? &current_tab->GetController() : nullptr; 141 return current_tab ? &current_tab->GetController() : nullptr;
38 } 142 }
39 143
144 content::NavigationEntry* ChromeToolbarModelDelegate::GetNavigationEntry()
145 const {
146 content::NavigationController* controller = GetNavigationController();
147 return controller ? controller->GetVisibleEntry() : nullptr;
148 }
149
40 Profile* ChromeToolbarModelDelegate::GetProfile() const { 150 Profile* ChromeToolbarModelDelegate::GetProfile() const {
41 content::NavigationController* navigation_controller = 151 content::NavigationController* controller = GetNavigationController();
42 GetNavigationController(); 152 return controller
43 return navigation_controller ? Profile::FromBrowserContext( 153 ? Profile::FromBrowserContext(controller->GetBrowserContext())
44 navigation_controller->GetBrowserContext()) 154 : nullptr;
45 : nullptr;
46 } 155 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698