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/chrome_autocomplete_scheme_classifier.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/search/search.h"
15 #include "chrome/browser/ssl/ssl_error_info.h"
16 #include "chrome/browser/ui/toolbar/toolbar_model_delegate.h"
17 #include "chrome/common/chrome_constants.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/common/url_constants.h"
21 #include "components/google/core/browser/google_util.h"
22 #include "components/omnibox/autocomplete_input.h"
23 #include "components/omnibox/autocomplete_match.h"
24 #include "content/public/browser/cert_store.h"
25 #include "content/public/browser/navigation_controller.h"
26 #include "content/public/browser/navigation_entry.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/browser/web_ui.h"
29 #include "content/public/common/content_constants.h"
30 #include "content/public/common/ssl_status.h"
31 #include "grit/component_scaled_resources.h"
32 #include "grit/generated_resources.h"
33 #include "grit/theme_resources.h"
34 #include "net/base/net_util.h"
35 #include "net/cert/cert_status_flags.h"
36 #include "net/cert/x509_certificate.h"
37 #include "ui/base/l10n/l10n_util.h"
39 #if defined(OS_CHROMEOS)
40 #include "chrome/browser/chromeos/policy/policy_cert_service.h"
41 #include "chrome/browser/chromeos/policy/policy_cert_service_factory.h"
44 using content::NavigationController
;
45 using content::NavigationEntry
;
46 using content::SSLStatus
;
47 using content::WebContents
;
49 ToolbarModelImpl::ToolbarModelImpl(ToolbarModelDelegate
* delegate
)
50 : delegate_(delegate
) {
53 ToolbarModelImpl::~ToolbarModelImpl() {
57 ToolbarModel::SecurityLevel
ToolbarModelImpl::GetSecurityLevelForWebContents(
58 content::WebContents
* web_contents
) {
62 NavigationEntry
* entry
= web_contents
->GetController().GetVisibleEntry();
66 const SSLStatus
& ssl
= entry
->GetSSL();
67 switch (ssl
.security_style
) {
68 case content::SECURITY_STYLE_UNKNOWN
:
69 case content::SECURITY_STYLE_UNAUTHENTICATED
:
72 case content::SECURITY_STYLE_AUTHENTICATION_BROKEN
:
73 return SECURITY_ERROR
;
75 case content::SECURITY_STYLE_AUTHENTICATED
: {
76 #if defined(OS_CHROMEOS)
77 policy::PolicyCertService
* service
=
78 policy::PolicyCertServiceFactory::GetForProfile(
79 Profile::FromBrowserContext(web_contents
->GetBrowserContext()));
80 if (service
&& service
->UsedPolicyCertificates())
81 return SECURITY_POLICY_WARNING
;
83 if (!!(ssl
.content_status
& SSLStatus::DISPLAYED_INSECURE_CONTENT
))
84 return SECURITY_WARNING
;
85 if (net::IsCertStatusError(ssl
.cert_status
)) {
86 DCHECK(net::IsCertStatusMinorError(ssl
.cert_status
));
87 return SECURITY_WARNING
;
89 if ((ssl
.cert_status
& net::CERT_STATUS_IS_EV
) &&
90 content::CertStore::GetInstance()->RetrieveCert(ssl
.cert_id
, NULL
))
100 // ToolbarModelImpl Implementation.
101 base::string16
ToolbarModelImpl::GetText() const {
102 base::string16
search_terms(GetSearchTerms(false));
103 if (!search_terms
.empty())
106 if (WouldOmitURLDueToOriginChip())
107 return base::string16();
109 return GetFormattedURL(NULL
);
112 base::string16
ToolbarModelImpl::GetFormattedURL(size_t* prefix_end
) const {
113 std::string languages
; // Empty if we don't have a |navigation_controller|.
114 Profile
* profile
= GetProfile();
116 languages
= profile
->GetPrefs()->GetString(prefs::kAcceptLanguages
);
119 if (url
.spec().length() > content::kMaxURLDisplayChars
)
120 url
= url
.IsStandard() ? url
.GetOrigin() : GURL(url
.scheme() + ":");
121 // Note that we can't unescape spaces here, because if the user copies this
122 // and pastes it into another program, that program may think the URL ends at
124 return AutocompleteInput::FormattedStringWithEquivalentMeaning(
125 url
, net::FormatUrl(url
, languages
, net::kFormatUrlOmitAll
,
126 net::UnescapeRule::NORMAL
, NULL
, prefix_end
, NULL
),
127 ChromeAutocompleteSchemeClassifier(profile
));
130 base::string16
ToolbarModelImpl::GetCorpusNameForMobile() const {
131 if (!WouldPerformSearchTermReplacement(false))
132 return base::string16();
134 // If there is a query in the url fragment look for the corpus name there,
135 // otherwise look for the corpus name in the query parameters.
136 const std::string
& query_str(google_util::HasGoogleSearchQueryParam(
137 url
.ref()) ? url
.ref() : url
.query());
138 url::Component
query(0, query_str
.length()), key
, value
;
139 const char kChipKey
[] = "sboxchip";
140 while (url::ExtractQueryKeyValue(query_str
.c_str(), &query
, &key
, &value
)) {
141 if (key
.is_nonempty() && query_str
.substr(key
.begin
, key
.len
) == kChipKey
) {
142 return net::UnescapeAndDecodeUTF8URLComponent(
143 query_str
.substr(value
.begin
, value
.len
),
144 net::UnescapeRule::NORMAL
);
147 return base::string16();
150 GURL
ToolbarModelImpl::GetURL() const {
151 const NavigationController
* navigation_controller
= GetNavigationController();
152 if (navigation_controller
) {
153 const NavigationEntry
* entry
= navigation_controller
->GetVisibleEntry();
155 return ShouldDisplayURL() ? entry
->GetVirtualURL() : GURL();
158 return GURL(url::kAboutBlankURL
);
161 bool ToolbarModelImpl::WouldPerformSearchTermReplacement(
162 bool ignore_editing
) const {
163 return !GetSearchTerms(ignore_editing
).empty();
166 ToolbarModel::SecurityLevel
ToolbarModelImpl::GetSecurityLevel(
167 bool ignore_editing
) const {
168 // When editing, assume no security style.
169 return (input_in_progress() && !ignore_editing
) ?
170 NONE
: GetSecurityLevelForWebContents(delegate_
->GetActiveWebContents());
173 int ToolbarModelImpl::GetIcon() const {
174 if (WouldPerformSearchTermReplacement(false)) {
175 // The secured version of the search icon is necessary if neither the search
176 // button nor origin chip are present to indicate the security state.
177 return (chrome::GetDisplaySearchButtonConditions() ==
178 chrome::DISPLAY_SEARCH_BUTTON_NEVER
) &&
179 !chrome::ShouldDisplayOriginChip() ?
180 IDR_OMNIBOX_SEARCH_SECURED
: IDR_OMNIBOX_SEARCH
;
183 return GetIconForSecurityLevel(GetSecurityLevel(false));
186 int ToolbarModelImpl::GetIconForSecurityLevel(SecurityLevel level
) const {
187 static int icon_ids
[NUM_SECURITY_LEVELS
] = {
188 IDR_LOCATION_BAR_HTTP
,
189 IDR_OMNIBOX_HTTPS_VALID
,
190 IDR_OMNIBOX_HTTPS_VALID
,
191 IDR_OMNIBOX_HTTPS_WARNING
,
192 IDR_OMNIBOX_HTTPS_POLICY_WARNING
,
193 IDR_OMNIBOX_HTTPS_INVALID
,
195 DCHECK(arraysize(icon_ids
) == NUM_SECURITY_LEVELS
);
196 return icon_ids
[level
];
199 base::string16
ToolbarModelImpl::GetEVCertName() const {
200 if (GetSecurityLevel(false) != EV_SECURE
)
201 return base::string16();
203 // Note: Navigation controller and active entry are guaranteed non-NULL or
204 // the security level would be NONE.
205 scoped_refptr
<net::X509Certificate
> cert
;
206 content::CertStore::GetInstance()->RetrieveCert(
207 GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id
, &cert
);
209 // EV are required to have an organization name and country.
210 DCHECK(!cert
->subject().organization_names
.empty());
211 DCHECK(!cert
->subject().country_name
.empty());
212 return l10n_util::GetStringFUTF16(
213 IDS_SECURE_CONNECTION_EV
,
214 base::UTF8ToUTF16(cert
->subject().organization_names
[0]),
215 base::UTF8ToUTF16(cert
->subject().country_name
));
218 bool ToolbarModelImpl::ShouldDisplayURL() const {
219 // Note: The order here is important.
220 // - The WebUI test must come before the extension scheme test because there
221 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In
222 // that case, we should prefer what the WebUI instance says.
223 // - The view-source test must come before the NTP test because of the case
224 // of view-source:chrome://newtab, which should display its URL despite what
225 // chrome://newtab says.
226 NavigationController
* controller
= GetNavigationController();
227 NavigationEntry
* entry
= controller
? controller
->GetVisibleEntry() : NULL
;
229 if (entry
->IsViewSourceMode() ||
230 entry
->GetPageType() == content::PAGE_TYPE_INTERSTITIAL
) {
234 GURL url
= entry
->GetURL();
235 GURL virtual_url
= entry
->GetVirtualURL();
236 if (url
.SchemeIs(content::kChromeUIScheme
) ||
237 virtual_url
.SchemeIs(content::kChromeUIScheme
)) {
238 if (!url
.SchemeIs(content::kChromeUIScheme
))
240 return url
.host() != chrome::kChromeUINewTabHost
;
244 return !chrome::IsInstantNTP(delegate_
->GetActiveWebContents());
247 bool ToolbarModelImpl::WouldOmitURLDueToOriginChip() const {
248 const char kInterstitialShownKey
[] = "interstitial_shown";
250 // When users type URLs and hit enter, continue to show those URLs until
251 // the navigation commits or an interstitial is shown, because having the
252 // omnibox clear immediately feels like the input was ignored.
253 NavigationController
* navigation_controller
= GetNavigationController();
254 if (navigation_controller
) {
255 NavigationEntry
* pending_entry
= navigation_controller
->GetPendingEntry();
257 const NavigationEntry
* visible_entry
=
258 navigation_controller
->GetVisibleEntry();
259 base::string16 unused
;
260 // Keep track that we've shown the origin chip on an interstitial so it
261 // can be shown even after the interstitial was dismissed, to avoid
262 // showing the chip, removing it and then showing it again.
264 visible_entry
->GetPageType() == content::PAGE_TYPE_INTERSTITIAL
&&
265 !pending_entry
->GetExtraData(kInterstitialShownKey
, &unused
))
266 pending_entry
->SetExtraData(kInterstitialShownKey
, base::string16());
267 const content::PageTransition transition_type
=
268 pending_entry
->GetTransitionType();
269 if ((transition_type
& content::PAGE_TRANSITION_TYPED
) != 0 &&
270 !pending_entry
->GetExtraData(kInterstitialShownKey
, &unused
))
275 if (!delegate_
->InTabbedBrowser() || !ShouldDisplayURL() ||
276 !url_replacement_enabled())
279 if (chrome::ShouldDisplayOriginChip())
282 const chrome::OriginChipCondition chip_condition
=
283 chrome::GetOriginChipCondition();
284 return (chip_condition
== chrome::ORIGIN_CHIP_ALWAYS
) ||
285 ((chip_condition
== chrome::ORIGIN_CHIP_ON_SRP
) &&
286 WouldPerformSearchTermReplacement(false));
289 NavigationController
* ToolbarModelImpl::GetNavigationController() const {
290 // This |current_tab| can be NULL during the initialization of the
291 // toolbar during window creation (i.e. before any tabs have been added
293 WebContents
* current_tab
= delegate_
->GetActiveWebContents();
294 return current_tab
? ¤t_tab
->GetController() : NULL
;
297 Profile
* ToolbarModelImpl::GetProfile() const {
298 NavigationController
* navigation_controller
= GetNavigationController();
299 return navigation_controller
?
300 Profile::FromBrowserContext(navigation_controller
->GetBrowserContext()) :
304 base::string16
ToolbarModelImpl::GetSearchTerms(bool ignore_editing
) const {
305 if (!url_replacement_enabled() || (input_in_progress() && !ignore_editing
))
306 return base::string16();
308 const WebContents
* web_contents
= delegate_
->GetActiveWebContents();
309 base::string16
search_terms(chrome::GetSearchTerms(web_contents
));
310 if (search_terms
.empty()) {
311 // We mainly do this to enforce the subsequent DCHECK.
312 return base::string16();
315 // If the page is still loading and the security style is unknown, consider
316 // the page secure. Without this, after the user hit enter on some search
317 // terms, the omnibox would change to displaying the loading URL before
318 // changing back to the search terms once they could be extracted, thus
319 // causing annoying flicker.
320 DCHECK(web_contents
);
321 const NavigationController
& nav_controller
= web_contents
->GetController();
322 const NavigationEntry
* entry
= nav_controller
.GetVisibleEntry();
323 if ((entry
!= nav_controller
.GetLastCommittedEntry()) &&
324 (entry
->GetSSL().security_style
== content::SECURITY_STYLE_UNKNOWN
))
327 // If the URL is using a Google base URL specified via the command line, we
328 // bypass the security check below.
330 google_util::StartsWithCommandLineGoogleBaseURL(entry
->GetVirtualURL()))
333 // Otherwise, extract search terms for HTTPS pages that do not have a security
335 ToolbarModel::SecurityLevel security_level
= GetSecurityLevel(ignore_editing
);
336 return ((security_level
== NONE
) || (security_level
== SECURITY_ERROR
)) ?
337 base::string16() : search_terms
;