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/prefs/pref_service.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/autocomplete/autocomplete_classifier.h"
11 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
12 #include "chrome/browser/autocomplete/autocomplete_input.h"
13 #include "chrome/browser/autocomplete/autocomplete_match.h"
14 #include "chrome/browser/google/google_util.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 "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/generated_resources.h"
31 #include "grit/theme_resources.h"
32 #include "net/base/net_util.h"
33 #include "net/cert/cert_status_flags.h"
34 #include "net/cert/x509_certificate.h"
35 #include "ui/base/l10n/l10n_util.h"
37 #if defined(OS_CHROMEOS)
38 #include "chrome/browser/chromeos/policy/policy_cert_service.h"
39 #include "chrome/browser/chromeos/policy/policy_cert_service_factory.h"
42 using content::NavigationController
;
43 using content::NavigationEntry
;
44 using content::SSLStatus
;
45 using content::WebContents
;
47 ToolbarModelImpl::ToolbarModelImpl(ToolbarModelDelegate
* delegate
)
48 : delegate_(delegate
) {
51 ToolbarModelImpl::~ToolbarModelImpl() {
54 ToolbarModel::SecurityLevel
ToolbarModelImpl::GetSecurityLevelForWebContents(
55 content::WebContents
* web_contents
) {
59 NavigationEntry
* entry
= web_contents
->GetController().GetVisibleEntry();
63 const SSLStatus
& ssl
= entry
->GetSSL();
64 switch (ssl
.security_style
) {
65 case content::SECURITY_STYLE_UNKNOWN
:
66 case content::SECURITY_STYLE_UNAUTHENTICATED
:
69 case content::SECURITY_STYLE_AUTHENTICATION_BROKEN
:
70 return SECURITY_ERROR
;
72 case content::SECURITY_STYLE_AUTHENTICATED
: {
73 #if defined(OS_CHROMEOS)
74 policy::PolicyCertService
* service
=
75 policy::PolicyCertServiceFactory::GetForProfile(
76 Profile::FromBrowserContext(web_contents
->GetBrowserContext()));
77 if (service
&& service
->UsedPolicyCertificates())
78 return SECURITY_POLICY_WARNING
;
80 if (!!(ssl
.content_status
& SSLStatus::DISPLAYED_INSECURE_CONTENT
))
81 return SECURITY_WARNING
;
82 if (net::IsCertStatusError(ssl
.cert_status
)) {
83 DCHECK(net::IsCertStatusMinorError(ssl
.cert_status
));
84 return SECURITY_WARNING
;
86 if ((ssl
.cert_status
& net::CERT_STATUS_IS_EV
) &&
87 content::CertStore::GetInstance()->RetrieveCert(ssl
.cert_id
, NULL
))
97 // ToolbarModelImpl Implementation.
98 base::string16
ToolbarModelImpl::GetText() const {
99 base::string16
search_terms(GetSearchTerms(false));
100 if (!search_terms
.empty())
103 if (WouldOmitURLDueToOriginChip())
104 return base::string16();
106 std::string languages
; // Empty if we don't have a |navigation_controller|.
107 Profile
* profile
= GetProfile();
109 languages
= profile
->GetPrefs()->GetString(prefs::kAcceptLanguages
);
112 if (url
.spec().length() > content::kMaxURLDisplayChars
)
113 url
= url
.IsStandard() ? url
.GetOrigin() : GURL(url
.scheme() + ":");
114 // Note that we can't unescape spaces here, because if the user copies this
115 // and pastes it into another program, that program may think the URL ends at
117 return AutocompleteInput::FormattedStringWithEquivalentMeaning(
118 url
, net::FormatUrl(url
, languages
, net::kFormatUrlOmitAll
,
119 net::UnescapeRule::NORMAL
, NULL
, NULL
, NULL
));
122 base::string16
ToolbarModelImpl::GetCorpusNameForMobile() const {
123 if (!WouldPerformSearchTermReplacement(false))
124 return base::string16();
126 // If there is a query in the url fragment look for the corpus name there,
127 // otherwise look for the corpus name in the query parameters.
128 const std::string
& query_str(google_util::HasGoogleSearchQueryParam(
129 url
.ref()) ? url
.ref() : url
.query());
130 url_parse::Component
query(0, query_str
.length()), key
, value
;
131 const char kChipKey
[] = "sboxchip";
132 while (url_parse::ExtractQueryKeyValue(query_str
.c_str(), &query
, &key
,
134 if (key
.is_nonempty() && query_str
.substr(key
.begin
, key
.len
) == kChipKey
) {
135 return net::UnescapeAndDecodeUTF8URLComponent(
136 query_str
.substr(value
.begin
, value
.len
),
137 net::UnescapeRule::NORMAL
, NULL
);
140 return base::string16();
143 GURL
ToolbarModelImpl::GetURL() const {
144 const NavigationController
* navigation_controller
= GetNavigationController();
145 if (navigation_controller
) {
146 const NavigationEntry
* entry
= navigation_controller
->GetVisibleEntry();
148 return ShouldDisplayURL() ? entry
->GetVirtualURL() : GURL();
151 return GURL(content::kAboutBlankURL
);
154 bool ToolbarModelImpl::WouldOmitURLDueToOriginChip() const {
155 // When users type URLs and hit enter, continue to show those URLs until
156 // the navigation commits, because having the omnibox clear immediately
157 // feels like the input was ignored.
158 const NavigationController
* navigation_controller
= GetNavigationController();
159 if (navigation_controller
) {
160 const NavigationEntry
* entry
= navigation_controller
->GetPendingEntry();
162 (entry
->GetTransitionType() & content::PAGE_TRANSITION_TYPED
) != 0) {
167 bool should_display_origin_chip
=
168 chrome::ShouldDisplayOriginChip() || chrome::ShouldDisplayOriginChipV2();
169 return should_display_origin_chip
&& delegate_
->InTabbedBrowser() &&
170 ShouldDisplayURL() && url_replacement_enabled();
173 bool ToolbarModelImpl::WouldPerformSearchTermReplacement(
174 bool ignore_editing
) const {
175 return !GetSearchTerms(ignore_editing
).empty();
178 bool ToolbarModelImpl::ShouldDisplayURL() const {
179 // Note: The order here is important.
180 // - The WebUI test must come before the extension scheme test because there
181 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In
182 // that case, we should prefer what the WebUI instance says.
183 // - The view-source test must come before the NTP test because of the case
184 // of view-source:chrome://newtab, which should display its URL despite what
185 // chrome://newtab says.
186 NavigationController
* controller
= GetNavigationController();
187 NavigationEntry
* entry
= controller
? controller
->GetVisibleEntry() : NULL
;
189 if (entry
->IsViewSourceMode() ||
190 entry
->GetPageType() == content::PAGE_TYPE_INTERSTITIAL
) {
194 GURL url
= entry
->GetURL();
195 GURL virtual_url
= entry
->GetVirtualURL();
196 if (url
.SchemeIs(content::kChromeUIScheme
) ||
197 virtual_url
.SchemeIs(content::kChromeUIScheme
)) {
198 if (!url
.SchemeIs(content::kChromeUIScheme
))
200 return url
.host() != chrome::kChromeUINewTabHost
;
204 if (chrome::IsInstantNTP(delegate_
->GetActiveWebContents()))
210 ToolbarModel::SecurityLevel
ToolbarModelImpl::GetSecurityLevel(
211 bool ignore_editing
) const {
212 // When editing, assume no security style.
213 return (input_in_progress() && !ignore_editing
) ?
214 NONE
: GetSecurityLevelForWebContents(delegate_
->GetActiveWebContents());
217 int ToolbarModelImpl::GetIcon() const {
218 if (WouldPerformSearchTermReplacement(false)) {
219 return (chrome::GetDisplaySearchButtonConditions() ==
220 chrome::DISPLAY_SEARCH_BUTTON_NEVER
) ?
221 IDR_OMNIBOX_SEARCH_SECURED
: IDR_OMNIBOX_SEARCH
;
224 // When the original site chip experiment is running, the icon in the location
225 // bar, when not the search icon, should be the page icon.
226 if (chrome::ShouldDisplayOriginChip())
227 return GetIconForSecurityLevel(NONE
);
229 return GetIconForSecurityLevel(GetSecurityLevel(false));
232 int ToolbarModelImpl::GetIconForSecurityLevel(SecurityLevel level
) const {
233 static int icon_ids
[NUM_SECURITY_LEVELS
] = {
234 IDR_LOCATION_BAR_HTTP
,
235 IDR_OMNIBOX_HTTPS_VALID
,
236 IDR_OMNIBOX_HTTPS_VALID
,
237 IDR_OMNIBOX_HTTPS_WARNING
,
238 IDR_OMNIBOX_HTTPS_POLICY_WARNING
,
239 IDR_OMNIBOX_HTTPS_INVALID
,
241 DCHECK(arraysize(icon_ids
) == NUM_SECURITY_LEVELS
);
242 return icon_ids
[level
];
245 base::string16
ToolbarModelImpl::GetEVCertName() const {
246 DCHECK_EQ(EV_SECURE
, GetSecurityLevel(false));
247 scoped_refptr
<net::X509Certificate
> cert
;
248 // Note: Navigation controller and active entry are guaranteed non-NULL or
249 // the security level would be NONE.
250 content::CertStore::GetInstance()->RetrieveCert(
251 GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id
, &cert
);
252 return GetEVCertName(*cert
.get());
256 base::string16
ToolbarModelImpl::GetEVCertName(
257 const net::X509Certificate
& cert
) {
258 // EV are required to have an organization name and country.
259 if (cert
.subject().organization_names
.empty() ||
260 cert
.subject().country_name
.empty()) {
262 return base::string16();
265 return l10n_util::GetStringFUTF16(
266 IDS_SECURE_CONNECTION_EV
,
267 base::UTF8ToUTF16(cert
.subject().organization_names
[0]),
268 base::UTF8ToUTF16(cert
.subject().country_name
));
271 NavigationController
* ToolbarModelImpl::GetNavigationController() const {
272 // This |current_tab| can be NULL during the initialization of the
273 // toolbar during window creation (i.e. before any tabs have been added
275 WebContents
* current_tab
= delegate_
->GetActiveWebContents();
276 return current_tab
? ¤t_tab
->GetController() : NULL
;
279 Profile
* ToolbarModelImpl::GetProfile() const {
280 NavigationController
* navigation_controller
= GetNavigationController();
281 return navigation_controller
?
282 Profile::FromBrowserContext(navigation_controller
->GetBrowserContext()) :
286 base::string16
ToolbarModelImpl::GetSearchTerms(bool ignore_editing
) const {
287 if (!url_replacement_enabled() || (input_in_progress() && !ignore_editing
))
288 return base::string16();
290 const WebContents
* web_contents
= delegate_
->GetActiveWebContents();
291 base::string16
search_terms(chrome::GetSearchTerms(web_contents
));
292 if (search_terms
.empty()) {
293 // We mainly do this to enforce the subsequent DCHECK.
294 return base::string16();
297 // If the page is still loading and the security style is unknown, consider
298 // the page secure. Without this, after the user hit enter on some search
299 // terms, the omnibox would change to displaying the loading URL before
300 // changing back to the search terms once they could be extracted, thus
301 // causing annoying flicker.
302 DCHECK(web_contents
);
303 const NavigationController
& nav_controller
= web_contents
->GetController();
304 const NavigationEntry
* entry
= nav_controller
.GetVisibleEntry();
305 if ((entry
!= nav_controller
.GetLastCommittedEntry()) &&
306 (entry
->GetSSL().security_style
== content::SECURITY_STYLE_UNKNOWN
))
309 // If the URL is using a Google base URL specified via the command line, we
310 // bypass the security check below.
312 google_util::StartsWithCommandLineGoogleBaseURL(entry
->GetVirtualURL()))
315 // Otherwise, extract search terms for HTTPS pages that do not have a security
317 ToolbarModel::SecurityLevel security_level
= GetSecurityLevel(ignore_editing
);
318 return ((security_level
== NONE
) || (security_level
== SECURITY_ERROR
)) ?
319 base::string16() : search_terms
;