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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/toolbar/toolbar_model_impl.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/time/time.h"
10 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/search/search.h"
13 #include "chrome/browser/ssl/security_state_model.h"
14 #include "chrome/browser/ui/toolbar/toolbar_model_delegate.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/common/url_constants.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "components/google/core/browser/google_util.h"
19 #include "components/omnibox/browser/autocomplete_classifier.h"
20 #include "components/omnibox/browser/autocomplete_input.h"
21 #include "components/omnibox/browser/autocomplete_match.h"
22 #include "components/url_formatter/url_formatter.h"
23 #include "content/public/browser/cert_store.h"
24 #include "content/public/browser/navigation_controller.h"
25 #include "content/public/browser/navigation_entry.h"
26 #include "content/public/browser/web_contents.h"
27 #include "content/public/browser/web_ui.h"
28 #include "content/public/common/content_constants.h"
29 #include "content/public/common/ssl_status.h"
30 #include "grit/components_scaled_resources.h"
31 #include "grit/theme_resources.h"
32 #include "net/cert/cert_status_flags.h"
33 #include "net/cert/x509_certificate.h"
34 #include "net/ssl/ssl_connection_status_flags.h"
35 #include "ui/base/l10n/l10n_util.h"
37 using content::NavigationController
;
38 using content::NavigationEntry
;
39 using content::WebContents
;
41 ToolbarModelImpl::ToolbarModelImpl(ToolbarModelDelegate
* delegate
)
42 : delegate_(delegate
) {
45 ToolbarModelImpl::~ToolbarModelImpl() {
48 // ToolbarModelImpl Implementation.
49 base::string16
ToolbarModelImpl::GetText() const {
50 base::string16
search_terms(GetSearchTerms(false));
51 if (!search_terms
.empty())
54 return GetFormattedURL(NULL
);
57 base::string16
ToolbarModelImpl::GetFormattedURL(size_t* prefix_end
) const {
58 std::string languages
; // Empty if we don't have a |navigation_controller|.
59 Profile
* profile
= GetProfile();
61 languages
= profile
->GetPrefs()->GetString(prefs::kAcceptLanguages
);
64 if (url
.spec().length() > content::kMaxURLDisplayChars
)
65 url
= url
.IsStandard() ? url
.GetOrigin() : GURL(url
.scheme() + ":");
66 // Note that we can't unescape spaces here, because if the user copies this
67 // and pastes it into another program, that program may think the URL ends at
69 return AutocompleteInput::FormattedStringWithEquivalentMeaning(
70 url
, url_formatter::FormatUrl(
71 url
, languages
, url_formatter::kFormatUrlOmitAll
,
72 net::UnescapeRule::NORMAL
, nullptr, prefix_end
, nullptr),
73 ChromeAutocompleteSchemeClassifier(profile
));
76 base::string16
ToolbarModelImpl::GetCorpusNameForMobile() const {
77 if (!WouldPerformSearchTermReplacement(false))
78 return base::string16();
80 // If there is a query in the url fragment look for the corpus name there,
81 // otherwise look for the corpus name in the query parameters.
82 const std::string
& query_str(google_util::HasGoogleSearchQueryParam(
83 url
.ref()) ? url
.ref() : url
.query());
84 url::Component
query(0, query_str
.length()), key
, value
;
85 const char kChipKey
[] = "sboxchip";
86 while (url::ExtractQueryKeyValue(query_str
.c_str(), &query
, &key
, &value
)) {
87 if (key
.is_nonempty() && query_str
.substr(key
.begin
, key
.len
) == kChipKey
) {
88 return net::UnescapeAndDecodeUTF8URLComponent(
89 query_str
.substr(value
.begin
, value
.len
),
90 net::UnescapeRule::NORMAL
);
93 return base::string16();
96 GURL
ToolbarModelImpl::GetURL() const {
97 const NavigationController
* navigation_controller
= GetNavigationController();
98 if (navigation_controller
) {
99 const NavigationEntry
* entry
= navigation_controller
->GetVisibleEntry();
101 return ShouldDisplayURL() ? entry
->GetVirtualURL() : GURL();
104 return GURL(url::kAboutBlankURL
);
107 bool ToolbarModelImpl::WouldPerformSearchTermReplacement(
108 bool ignore_editing
) const {
109 return !GetSearchTerms(ignore_editing
).empty();
112 SecurityStateModel::SecurityLevel
ToolbarModelImpl::GetSecurityLevel(
113 bool ignore_editing
) const {
114 const content::WebContents
* web_contents
= delegate_
->GetActiveWebContents();
115 // If there is no active WebContents (which can happen during toolbar
116 // initialization), assume no security style.
118 return SecurityStateModel::NONE
;
119 const SecurityStateModel
* model
=
120 SecurityStateModel::FromWebContents(web_contents
);
122 // When editing, assume no security style.
123 return (input_in_progress() && !ignore_editing
)
124 ? SecurityStateModel::NONE
125 : model
->security_info().security_level
;
128 int ToolbarModelImpl::GetIcon() const {
129 if (WouldPerformSearchTermReplacement(false))
130 return IDR_OMNIBOX_SEARCH_SECURED
;
132 return GetIconForSecurityLevel(GetSecurityLevel(false));
135 int ToolbarModelImpl::GetIconForSecurityLevel(
136 SecurityStateModel::SecurityLevel level
) const {
138 case SecurityStateModel::NONE
:
139 return IDR_LOCATION_BAR_HTTP
;
140 case SecurityStateModel::EV_SECURE
:
141 case SecurityStateModel::SECURE
:
142 return IDR_OMNIBOX_HTTPS_VALID
;
143 case SecurityStateModel::SECURITY_WARNING
:
144 // Surface Dubious as Neutral.
145 return IDR_LOCATION_BAR_HTTP
;
146 case SecurityStateModel::SECURITY_POLICY_WARNING
:
147 return IDR_OMNIBOX_HTTPS_POLICY_WARNING
;
148 case SecurityStateModel::SECURITY_ERROR
:
149 return IDR_OMNIBOX_HTTPS_INVALID
;
153 return IDR_LOCATION_BAR_HTTP
;
156 base::string16
ToolbarModelImpl::GetEVCertName() const {
157 if (GetSecurityLevel(false) != SecurityStateModel::EV_SECURE
)
158 return base::string16();
160 // Note: Navigation controller and active entry are guaranteed non-NULL or
161 // the security level would be NONE.
162 scoped_refptr
<net::X509Certificate
> cert
;
163 content::CertStore::GetInstance()->RetrieveCert(
164 GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id
, &cert
);
166 // EV are required to have an organization name and country.
167 DCHECK(!cert
->subject().organization_names
.empty());
168 DCHECK(!cert
->subject().country_name
.empty());
169 return l10n_util::GetStringFUTF16(
170 IDS_SECURE_CONNECTION_EV
,
171 base::UTF8ToUTF16(cert
->subject().organization_names
[0]),
172 base::UTF8ToUTF16(cert
->subject().country_name
));
175 bool ToolbarModelImpl::ShouldDisplayURL() const {
176 // Note: The order here is important.
177 // - The WebUI test must come before the extension scheme test because there
178 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In
179 // that case, we should prefer what the WebUI instance says.
180 // - The view-source test must come before the NTP test because of the case
181 // of view-source:chrome://newtab, which should display its URL despite what
182 // chrome://newtab says.
183 NavigationController
* controller
= GetNavigationController();
184 NavigationEntry
* entry
= controller
? controller
->GetVisibleEntry() : NULL
;
186 if (entry
->IsViewSourceMode() ||
187 entry
->GetPageType() == content::PAGE_TYPE_INTERSTITIAL
) {
191 GURL url
= entry
->GetURL();
192 GURL virtual_url
= entry
->GetVirtualURL();
193 if (url
.SchemeIs(content::kChromeUIScheme
) ||
194 virtual_url
.SchemeIs(content::kChromeUIScheme
)) {
195 if (!url
.SchemeIs(content::kChromeUIScheme
))
197 return url
.host() != chrome::kChromeUINewTabHost
;
201 return !search::IsInstantNTP(delegate_
->GetActiveWebContents());
204 NavigationController
* ToolbarModelImpl::GetNavigationController() const {
205 // This |current_tab| can be NULL during the initialization of the
206 // toolbar during window creation (i.e. before any tabs have been added
208 WebContents
* current_tab
= delegate_
->GetActiveWebContents();
209 return current_tab
? ¤t_tab
->GetController() : NULL
;
212 Profile
* ToolbarModelImpl::GetProfile() const {
213 NavigationController
* navigation_controller
= GetNavigationController();
214 return navigation_controller
?
215 Profile::FromBrowserContext(navigation_controller
->GetBrowserContext()) :
219 base::string16
ToolbarModelImpl::GetSearchTerms(bool ignore_editing
) const {
220 if (!url_replacement_enabled() || (input_in_progress() && !ignore_editing
))
221 return base::string16();
223 const WebContents
* web_contents
= delegate_
->GetActiveWebContents();
224 base::string16
search_terms(search::GetSearchTerms(web_contents
));
225 if (search_terms
.empty()) {
226 // We mainly do this to enforce the subsequent DCHECK.
227 return base::string16();
230 // If the page is still loading and the security style is unknown, consider
231 // the page secure. Without this, after the user hit enter on some search
232 // terms, the omnibox would change to displaying the loading URL before
233 // changing back to the search terms once they could be extracted, thus
234 // causing annoying flicker.
235 DCHECK(web_contents
);
236 const NavigationController
& nav_controller
= web_contents
->GetController();
237 const NavigationEntry
* entry
= nav_controller
.GetVisibleEntry();
238 if ((entry
!= nav_controller
.GetLastCommittedEntry()) &&
239 (entry
->GetSSL().security_style
== content::SECURITY_STYLE_UNKNOWN
))
242 // If the URL is using a Google base URL specified via the command line, we
243 // bypass the security check below.
245 google_util::StartsWithCommandLineGoogleBaseURL(entry
->GetVirtualURL()))
248 // Otherwise, extract search terms for HTTPS pages that do not have a security
250 SecurityStateModel::SecurityLevel security_level
=
251 GetSecurityLevel(ignore_editing
);
252 return ((security_level
== SecurityStateModel::NONE
) ||
253 (security_level
== SecurityStateModel::SECURITY_ERROR
))