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/command_line.h"
8 #include "base/metrics/field_trial.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/time/time.h"
12 #include "chrome/browser/autocomplete/autocomplete_classifier.h"
13 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
14 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/search/search.h"
17 #include "chrome/browser/ssl/ssl_error_info.h"
18 #include "chrome/browser/ui/toolbar/toolbar_model_delegate.h"
19 #include "chrome/common/chrome_constants.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/pref_names.h"
22 #include "chrome/common/url_constants.h"
23 #include "chrome/grit/generated_resources.h"
24 #include "components/google/core/browser/google_util.h"
25 #include "components/omnibox/autocomplete_input.h"
26 #include "components/omnibox/autocomplete_match.h"
27 #include "content/public/browser/cert_store.h"
28 #include "content/public/browser/navigation_controller.h"
29 #include "content/public/browser/navigation_entry.h"
30 #include "content/public/browser/web_contents.h"
31 #include "content/public/browser/web_ui.h"
32 #include "content/public/common/content_constants.h"
33 #include "content/public/common/ssl_status.h"
34 #include "grit/components_scaled_resources.h"
35 #include "grit/theme_resources.h"
36 #include "net/base/net_util.h"
37 #include "net/cert/cert_status_flags.h"
38 #include "net/cert/x509_certificate.h"
39 #include "net/ssl/ssl_connection_status_flags.h"
40 #include "ui/base/l10n/l10n_util.h"
42 #if defined(OS_CHROMEOS)
43 #include "chrome/browser/chromeos/policy/policy_cert_service.h"
44 #include "chrome/browser/chromeos/policy/policy_cert_service_factory.h"
47 using content::NavigationController
;
48 using content::NavigationEntry
;
49 using content::SSLStatus
;
50 using content::WebContents
;
54 // Converts a SHA-1 field trial group into the appropriate SecurityLevel.
55 bool GetSecurityLevelForFieldTrialGroup(const std::string
& group
,
56 ToolbarModel::SecurityLevel
* level
) {
58 *level
= ToolbarModel::SECURITY_ERROR
;
59 else if (group
== "Warning")
60 *level
= ToolbarModel::SECURITY_WARNING
;
61 else if (group
== "HTTP")
62 *level
= ToolbarModel::NONE
;
68 ToolbarModel::SecurityLevel
GetSecurityLevelForNonSecureFieldTrial() {
69 std::string choice
= base::CommandLine::ForCurrentProcess()->
70 GetSwitchValueASCII(switches::kMarkNonSecureAs
);
71 if (choice
== switches::kMarkNonSecureAsNeutral
)
72 return ToolbarModel::NONE
;
73 if (choice
== switches::kMarkNonSecureAsDubious
)
74 return ToolbarModel::SECURITY_WARNING
;
75 if (choice
== switches::kMarkNonSecureAsNonSecure
)
76 return ToolbarModel::SECURITY_ERROR
;
78 std::string group
= base::FieldTrialList::FindFullName("MarkNonSecureAs");
79 if (group
== switches::kMarkNonSecureAsNeutral
)
80 return ToolbarModel::NONE
;
81 if (group
== switches::kMarkNonSecureAsDubious
)
82 return ToolbarModel::SECURITY_WARNING
;
83 if (group
== switches::kMarkNonSecureAsNonSecure
)
84 return ToolbarModel::SECURITY_ERROR
;
86 return ToolbarModel::NONE
;
91 ToolbarModelImpl::ToolbarModelImpl(ToolbarModelDelegate
* delegate
)
92 : delegate_(delegate
) {
95 ToolbarModelImpl::~ToolbarModelImpl() {
99 ToolbarModel::SecurityLevel
ToolbarModelImpl::GetSecurityLevelForWebContents(
100 content::WebContents
* web_contents
) {
104 NavigationEntry
* entry
= web_contents
->GetController().GetVisibleEntry();
108 const SSLStatus
& ssl
= entry
->GetSSL();
109 switch (ssl
.security_style
) {
110 case content::SECURITY_STYLE_UNKNOWN
:
113 case content::SECURITY_STYLE_UNAUTHENTICATED
: {
114 const GURL
& url
= entry
->GetURL();
115 if (url
.SchemeIs("http") || url
.SchemeIs("ftp"))
116 return GetSecurityLevelForNonSecureFieldTrial();
120 case content::SECURITY_STYLE_AUTHENTICATION_BROKEN
:
121 return SECURITY_ERROR
;
123 case content::SECURITY_STYLE_AUTHENTICATED
: {
124 #if defined(OS_CHROMEOS)
125 policy::PolicyCertService
* service
=
126 policy::PolicyCertServiceFactory::GetForProfile(
127 Profile::FromBrowserContext(web_contents
->GetBrowserContext()));
128 if (service
&& service
->UsedPolicyCertificates())
129 return SECURITY_POLICY_WARNING
;
131 if (!!(ssl
.content_status
& SSLStatus::DISPLAYED_INSECURE_CONTENT
))
132 return SECURITY_WARNING
;
133 scoped_refptr
<net::X509Certificate
> cert
;
134 if (content::CertStore::GetInstance()->RetrieveCert(ssl
.cert_id
, &cert
) &&
135 (ssl
.cert_status
& net::CERT_STATUS_SHA1_SIGNATURE_PRESENT
)) {
136 // The internal representation of the dates for UI treatment of SHA-1.
137 // See http://crbug.com/401365 for details
138 static const int64_t kJanuary2017
= INT64_C(13127702400000000);
139 static const int64_t kJune2016
= INT64_C(13109213000000000);
140 static const int64_t kJanuary2016
= INT64_C(13096080000000000);
142 ToolbarModel::SecurityLevel security_level
= NONE
;
143 // Gated behind a field trial, so that it is possible to adjust the
144 // UI treatment (to be more or less severe, as necessary) over the
145 // course of multiple releases.
146 // See http://crbug.com/401365 for the timeline, with the end state
147 // being that > kJanuary2017 = Error, and > kJanuary2016 =
148 // Warning, and kJune2016 disappearing entirely.
149 if (cert
->valid_expiry() >=
150 base::Time::FromInternalValue(kJanuary2017
) &&
151 GetSecurityLevelForFieldTrialGroup(
152 base::FieldTrialList::FindFullName("SHA1ToolbarUIJanuary2017"),
154 return security_level
;
156 if (cert
->valid_expiry() >= base::Time::FromInternalValue(kJune2016
) &&
157 GetSecurityLevelForFieldTrialGroup(
158 base::FieldTrialList::FindFullName("SHA1ToolbarUIJune2016"),
160 return security_level
;
162 if (cert
->valid_expiry() >=
163 base::Time::FromInternalValue(kJanuary2016
) &&
164 GetSecurityLevelForFieldTrialGroup(
165 base::FieldTrialList::FindFullName("SHA1ToolbarUIJanuary2016"),
167 return security_level
;
170 if (net::IsCertStatusError(ssl
.cert_status
)) {
171 DCHECK(net::IsCertStatusMinorError(ssl
.cert_status
));
172 return SECURITY_WARNING
;
174 if (net::SSLConnectionStatusToVersion(ssl
.connection_status
) ==
175 net::SSL_CONNECTION_VERSION_SSL3
) {
176 // SSLv3 will be removed in the future.
177 return SECURITY_WARNING
;
179 if ((ssl
.cert_status
& net::CERT_STATUS_IS_EV
) && cert
.get())
189 // ToolbarModelImpl Implementation.
190 base::string16
ToolbarModelImpl::GetText() const {
191 base::string16
search_terms(GetSearchTerms(false));
192 if (!search_terms
.empty())
195 return GetFormattedURL(NULL
);
198 base::string16
ToolbarModelImpl::GetFormattedURL(size_t* prefix_end
) const {
199 std::string languages
; // Empty if we don't have a |navigation_controller|.
200 Profile
* profile
= GetProfile();
202 languages
= profile
->GetPrefs()->GetString(prefs::kAcceptLanguages
);
205 if (url
.spec().length() > content::kMaxURLDisplayChars
)
206 url
= url
.IsStandard() ? url
.GetOrigin() : GURL(url
.scheme() + ":");
207 // Note that we can't unescape spaces here, because if the user copies this
208 // and pastes it into another program, that program may think the URL ends at
210 return AutocompleteInput::FormattedStringWithEquivalentMeaning(
211 url
, net::FormatUrl(url
, languages
, net::kFormatUrlOmitAll
,
212 net::UnescapeRule::NORMAL
, NULL
, prefix_end
, NULL
),
213 ChromeAutocompleteSchemeClassifier(profile
));
216 base::string16
ToolbarModelImpl::GetCorpusNameForMobile() const {
217 if (!WouldPerformSearchTermReplacement(false))
218 return base::string16();
220 // If there is a query in the url fragment look for the corpus name there,
221 // otherwise look for the corpus name in the query parameters.
222 const std::string
& query_str(google_util::HasGoogleSearchQueryParam(
223 url
.ref()) ? url
.ref() : url
.query());
224 url::Component
query(0, query_str
.length()), key
, value
;
225 const char kChipKey
[] = "sboxchip";
226 while (url::ExtractQueryKeyValue(query_str
.c_str(), &query
, &key
, &value
)) {
227 if (key
.is_nonempty() && query_str
.substr(key
.begin
, key
.len
) == kChipKey
) {
228 return net::UnescapeAndDecodeUTF8URLComponent(
229 query_str
.substr(value
.begin
, value
.len
),
230 net::UnescapeRule::NORMAL
);
233 return base::string16();
236 GURL
ToolbarModelImpl::GetURL() const {
237 const NavigationController
* navigation_controller
= GetNavigationController();
238 if (navigation_controller
) {
239 const NavigationEntry
* entry
= navigation_controller
->GetVisibleEntry();
241 return ShouldDisplayURL() ? entry
->GetVirtualURL() : GURL();
244 return GURL(url::kAboutBlankURL
);
247 bool ToolbarModelImpl::WouldPerformSearchTermReplacement(
248 bool ignore_editing
) const {
249 return !GetSearchTerms(ignore_editing
).empty();
252 ToolbarModel::SecurityLevel
ToolbarModelImpl::GetSecurityLevel(
253 bool ignore_editing
) const {
254 // When editing, assume no security style.
255 return (input_in_progress() && !ignore_editing
) ?
256 NONE
: GetSecurityLevelForWebContents(delegate_
->GetActiveWebContents());
259 int ToolbarModelImpl::GetIcon() const {
260 if (WouldPerformSearchTermReplacement(false))
261 return IDR_OMNIBOX_SEARCH_SECURED
;
263 return GetIconForSecurityLevel(GetSecurityLevel(false));
266 int ToolbarModelImpl::GetIconForSecurityLevel(SecurityLevel level
) const {
267 static int icon_ids
[NUM_SECURITY_LEVELS
] = {
268 IDR_LOCATION_BAR_HTTP
,
269 IDR_OMNIBOX_HTTPS_VALID
,
270 IDR_OMNIBOX_HTTPS_VALID
,
271 IDR_OMNIBOX_HTTPS_WARNING
,
272 IDR_OMNIBOX_HTTPS_POLICY_WARNING
,
273 IDR_OMNIBOX_HTTPS_INVALID
,
275 DCHECK(arraysize(icon_ids
) == NUM_SECURITY_LEVELS
);
276 return icon_ids
[level
];
279 base::string16
ToolbarModelImpl::GetEVCertName() const {
280 if (GetSecurityLevel(false) != EV_SECURE
)
281 return base::string16();
283 // Note: Navigation controller and active entry are guaranteed non-NULL or
284 // the security level would be NONE.
285 scoped_refptr
<net::X509Certificate
> cert
;
286 content::CertStore::GetInstance()->RetrieveCert(
287 GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id
, &cert
);
289 // EV are required to have an organization name and country.
290 DCHECK(!cert
->subject().organization_names
.empty());
291 DCHECK(!cert
->subject().country_name
.empty());
292 return l10n_util::GetStringFUTF16(
293 IDS_SECURE_CONNECTION_EV
,
294 base::UTF8ToUTF16(cert
->subject().organization_names
[0]),
295 base::UTF8ToUTF16(cert
->subject().country_name
));
298 bool ToolbarModelImpl::ShouldDisplayURL() const {
299 // Note: The order here is important.
300 // - The WebUI test must come before the extension scheme test because there
301 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In
302 // that case, we should prefer what the WebUI instance says.
303 // - The view-source test must come before the NTP test because of the case
304 // of view-source:chrome://newtab, which should display its URL despite what
305 // chrome://newtab says.
306 NavigationController
* controller
= GetNavigationController();
307 NavigationEntry
* entry
= controller
? controller
->GetVisibleEntry() : NULL
;
309 if (entry
->IsViewSourceMode() ||
310 entry
->GetPageType() == content::PAGE_TYPE_INTERSTITIAL
) {
314 GURL url
= entry
->GetURL();
315 GURL virtual_url
= entry
->GetVirtualURL();
316 if (url
.SchemeIs(content::kChromeUIScheme
) ||
317 virtual_url
.SchemeIs(content::kChromeUIScheme
)) {
318 if (!url
.SchemeIs(content::kChromeUIScheme
))
320 return url
.host() != chrome::kChromeUINewTabHost
;
324 return !chrome::IsInstantNTP(delegate_
->GetActiveWebContents());
327 NavigationController
* ToolbarModelImpl::GetNavigationController() const {
328 // This |current_tab| can be NULL during the initialization of the
329 // toolbar during window creation (i.e. before any tabs have been added
331 WebContents
* current_tab
= delegate_
->GetActiveWebContents();
332 return current_tab
? ¤t_tab
->GetController() : NULL
;
335 Profile
* ToolbarModelImpl::GetProfile() const {
336 NavigationController
* navigation_controller
= GetNavigationController();
337 return navigation_controller
?
338 Profile::FromBrowserContext(navigation_controller
->GetBrowserContext()) :
342 base::string16
ToolbarModelImpl::GetSearchTerms(bool ignore_editing
) const {
343 if (!url_replacement_enabled() || (input_in_progress() && !ignore_editing
))
344 return base::string16();
346 const WebContents
* web_contents
= delegate_
->GetActiveWebContents();
347 base::string16
search_terms(chrome::GetSearchTerms(web_contents
));
348 if (search_terms
.empty()) {
349 // We mainly do this to enforce the subsequent DCHECK.
350 return base::string16();
353 // If the page is still loading and the security style is unknown, consider
354 // the page secure. Without this, after the user hit enter on some search
355 // terms, the omnibox would change to displaying the loading URL before
356 // changing back to the search terms once they could be extracted, thus
357 // causing annoying flicker.
358 DCHECK(web_contents
);
359 const NavigationController
& nav_controller
= web_contents
->GetController();
360 const NavigationEntry
* entry
= nav_controller
.GetVisibleEntry();
361 if ((entry
!= nav_controller
.GetLastCommittedEntry()) &&
362 (entry
->GetSSL().security_style
== content::SECURITY_STYLE_UNKNOWN
))
365 // If the URL is using a Google base URL specified via the command line, we
366 // bypass the security check below.
368 google_util::StartsWithCommandLineGoogleBaseURL(entry
->GetVirtualURL()))
371 // Otherwise, extract search terms for HTTPS pages that do not have a security
373 ToolbarModel::SecurityLevel security_level
= GetSecurityLevel(ignore_editing
);
374 return ((security_level
== NONE
) || (security_level
== SECURITY_ERROR
)) ?
375 base::string16() : search_terms
;