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 return GetFormattedURL();
109 base::string16 ToolbarModelImpl::GetFormattedURL() const {
110 std::string languages; // Empty if we don't have a |navigation_controller|.
111 Profile* profile = GetProfile();
113 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
116 if (url.spec().length() > content::kMaxURLDisplayChars)
117 url = url.IsStandard() ? url.GetOrigin() : GURL(url.scheme() + ":");
118 // Note that we can't unescape spaces here, because if the user copies this
119 // and pastes it into another program, that program may think the URL ends at
121 return AutocompleteInput::FormattedStringWithEquivalentMeaning(
122 url, net::FormatUrl(url, languages, net::kFormatUrlOmitAll,
123 net::UnescapeRule::NORMAL, NULL, NULL, NULL));
126 base::string16 ToolbarModelImpl::GetCorpusNameForMobile() const {
127 if (!WouldPerformSearchTermReplacement(false))
128 return base::string16();
130 // If there is a query in the url fragment look for the corpus name there,
131 // otherwise look for the corpus name in the query parameters.
132 const std::string& query_str(google_util::HasGoogleSearchQueryParam(
133 url.ref()) ? url.ref() : url.query());
134 url_parse::Component query(0, query_str.length()), key, value;
135 const char kChipKey[] = "sboxchip";
136 while (url_parse::ExtractQueryKeyValue(query_str.c_str(), &query, &key,
138 if (key.is_nonempty() && query_str.substr(key.begin, key.len) == kChipKey) {
139 return net::UnescapeAndDecodeUTF8URLComponent(
140 query_str.substr(value.begin, value.len),
141 net::UnescapeRule::NORMAL, NULL);
144 return base::string16();
147 GURL ToolbarModelImpl::GetURL() const {
148 const NavigationController* navigation_controller = GetNavigationController();
149 if (navigation_controller) {
150 const NavigationEntry* entry = navigation_controller->GetVisibleEntry();
152 return ShouldDisplayURL() ? entry->GetVirtualURL() : GURL();
155 return GURL(content::kAboutBlankURL);
158 bool ToolbarModelImpl::WouldOmitURLDueToOriginChip() const {
159 const char kInterstitialShownKey[] = "interstitial_shown";
161 // When users type URLs and hit enter, continue to show those URLs until
162 // the navigation commits or an interstitial is shown, because having the
163 // omnibox clear immediately feels like the input was ignored.
164 NavigationController* navigation_controller = GetNavigationController();
165 if (navigation_controller) {
166 NavigationEntry* pending_entry = navigation_controller->GetPendingEntry();
168 const NavigationEntry* visible_entry =
169 navigation_controller->GetVisibleEntry();
170 base::string16 unused;
171 // Keep track that we've shown the origin chip on an interstitial so it
172 // can be shown even after the interstitial was dismissed, to avoid
173 // showing the chip, removing it and then showing it again.
175 visible_entry->GetPageType() == content::PAGE_TYPE_INTERSTITIAL &&
176 !pending_entry->GetExtraData(kInterstitialShownKey, &unused))
177 pending_entry->SetExtraData(kInterstitialShownKey, base::string16());
178 const content::PageTransition transition_type =
179 pending_entry->GetTransitionType();
180 if ((transition_type & content::PAGE_TRANSITION_TYPED) != 0 &&
181 !pending_entry->GetExtraData(kInterstitialShownKey, &unused))
186 bool should_display_origin_chip =
187 chrome::ShouldDisplayOriginChip() || chrome::ShouldDisplayOriginChipV2();
188 return should_display_origin_chip && delegate_->InTabbedBrowser() &&
189 ShouldDisplayURL() && url_replacement_enabled();
192 bool ToolbarModelImpl::WouldPerformSearchTermReplacement(
193 bool ignore_editing) const {
194 return !GetSearchTerms(ignore_editing).empty();
197 bool ToolbarModelImpl::ShouldDisplayURL() const {
198 // Note: The order here is important.
199 // - The WebUI test must come before the extension scheme test because there
200 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In
201 // that case, we should prefer what the WebUI instance says.
202 // - The view-source test must come before the NTP test because of the case
203 // of view-source:chrome://newtab, which should display its URL despite what
204 // chrome://newtab says.
205 NavigationController* controller = GetNavigationController();
206 NavigationEntry* entry = controller ? controller->GetVisibleEntry() : NULL;
208 if (entry->IsViewSourceMode() ||
209 entry->GetPageType() == content::PAGE_TYPE_INTERSTITIAL) {
213 GURL url = entry->GetURL();
214 GURL virtual_url = entry->GetVirtualURL();
215 if (url.SchemeIs(content::kChromeUIScheme) ||
216 virtual_url.SchemeIs(content::kChromeUIScheme)) {
217 if (!url.SchemeIs(content::kChromeUIScheme))
219 return url.host() != chrome::kChromeUINewTabHost;
223 if (chrome::IsInstantNTP(delegate_->GetActiveWebContents()))
229 ToolbarModel::SecurityLevel ToolbarModelImpl::GetSecurityLevel(
230 bool ignore_editing) const {
231 // When editing, assume no security style.
232 return (input_in_progress() && !ignore_editing) ?
233 NONE : GetSecurityLevelForWebContents(delegate_->GetActiveWebContents());
236 int ToolbarModelImpl::GetIcon() const {
237 if (WouldPerformSearchTermReplacement(false)) {
238 return (chrome::GetDisplaySearchButtonConditions() ==
239 chrome::DISPLAY_SEARCH_BUTTON_NEVER) ?
240 IDR_OMNIBOX_SEARCH_SECURED : IDR_OMNIBOX_SEARCH;
243 // When the original site chip experiment is running, the icon in the location
244 // bar, when not the search icon, should be the page icon.
245 if (chrome::ShouldDisplayOriginChip())
246 return GetIconForSecurityLevel(NONE);
248 return GetIconForSecurityLevel(GetSecurityLevel(false));
251 int ToolbarModelImpl::GetIconForSecurityLevel(SecurityLevel level) const {
252 static int icon_ids[NUM_SECURITY_LEVELS] = {
253 IDR_LOCATION_BAR_HTTP,
254 IDR_OMNIBOX_HTTPS_VALID,
255 IDR_OMNIBOX_HTTPS_VALID,
256 IDR_OMNIBOX_HTTPS_WARNING,
257 IDR_OMNIBOX_HTTPS_POLICY_WARNING,
258 IDR_OMNIBOX_HTTPS_INVALID,
260 DCHECK(arraysize(icon_ids) == NUM_SECURITY_LEVELS);
261 return icon_ids[level];
264 base::string16 ToolbarModelImpl::GetEVCertName() const {
265 DCHECK_EQ(EV_SECURE, GetSecurityLevel(false));
266 scoped_refptr<net::X509Certificate> cert;
267 // Note: Navigation controller and active entry are guaranteed non-NULL or
268 // the security level would be NONE.
269 content::CertStore::GetInstance()->RetrieveCert(
270 GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id, &cert);
271 return GetEVCertName(*cert.get());
275 base::string16 ToolbarModelImpl::GetEVCertName(
276 const net::X509Certificate& cert) {
277 // EV are required to have an organization name and country.
278 if (cert.subject().organization_names.empty() ||
279 cert.subject().country_name.empty()) {
281 return base::string16();
284 return l10n_util::GetStringFUTF16(
285 IDS_SECURE_CONNECTION_EV,
286 base::UTF8ToUTF16(cert.subject().organization_names[0]),
287 base::UTF8ToUTF16(cert.subject().country_name));
290 NavigationController* ToolbarModelImpl::GetNavigationController() const {
291 // This |current_tab| can be NULL during the initialization of the
292 // toolbar during window creation (i.e. before any tabs have been added
294 WebContents* current_tab = delegate_->GetActiveWebContents();
295 return current_tab ? ¤t_tab->GetController() : NULL;
298 Profile* ToolbarModelImpl::GetProfile() const {
299 NavigationController* navigation_controller = GetNavigationController();
300 return navigation_controller ?
301 Profile::FromBrowserContext(navigation_controller->GetBrowserContext()) :
305 base::string16 ToolbarModelImpl::GetSearchTerms(bool ignore_editing) const {
306 if (!url_replacement_enabled() || (input_in_progress() && !ignore_editing))
307 return base::string16();
309 const WebContents* web_contents = delegate_->GetActiveWebContents();
310 base::string16 search_terms(chrome::GetSearchTerms(web_contents));
311 if (search_terms.empty()) {
312 // We mainly do this to enforce the subsequent DCHECK.
313 return base::string16();
316 // If the page is still loading and the security style is unknown, consider
317 // the page secure. Without this, after the user hit enter on some search
318 // terms, the omnibox would change to displaying the loading URL before
319 // changing back to the search terms once they could be extracted, thus
320 // causing annoying flicker.
321 DCHECK(web_contents);
322 const NavigationController& nav_controller = web_contents->GetController();
323 const NavigationEntry* entry = nav_controller.GetVisibleEntry();
324 if ((entry != nav_controller.GetLastCommittedEntry()) &&
325 (entry->GetSSL().security_style == content::SECURITY_STYLE_UNKNOWN))
328 // If the URL is using a Google base URL specified via the command line, we
329 // bypass the security check below.
331 google_util::StartsWithCommandLineGoogleBaseURL(entry->GetVirtualURL()))
334 // Otherwise, extract search terms for HTTPS pages that do not have a security
336 ToolbarModel::SecurityLevel security_level = GetSecurityLevel(ignore_editing);
337 return ((security_level == NONE) || (security_level == SECURITY_ERROR)) ?
338 base::string16() : search_terms;