Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / ui / toolbar / toolbar_model_impl.cc
blob50df4f2e268ce70cf1ae3ba36a9ec5fd7632466f
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"
40 #endif
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) {
56 if (!web_contents)
57 return NONE;
59 NavigationEntry* entry = web_contents->GetController().GetVisibleEntry();
60 if (!entry)
61 return NONE;
63 const SSLStatus& ssl = entry->GetSSL();
64 switch (ssl.security_style) {
65 case content::SECURITY_STYLE_UNKNOWN:
66 case content::SECURITY_STYLE_UNAUTHENTICATED:
67 return NONE;
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;
79 #endif
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))
88 return EV_SECURE;
89 return SECURE;
91 default:
92 NOTREACHED();
93 return NONE;
97 // ToolbarModelImpl Implementation.
98 base::string16 ToolbarModelImpl::GetText() const {
99 base::string16 search_terms(GetSearchTerms(false));
100 if (!search_terms.empty())
101 return search_terms;
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();
112 if (profile)
113 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
115 GURL url(GetURL());
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
120 // the space.
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();
129 GURL url(GetURL());
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::Component query(0, query_str.length()), key, value;
135 const char kChipKey[] = "sboxchip";
136 while (url::ExtractQueryKeyValue(query_str.c_str(), &query, &key, &value)) {
137 if (key.is_nonempty() && query_str.substr(key.begin, key.len) == kChipKey) {
138 return net::UnescapeAndDecodeUTF8URLComponent(
139 query_str.substr(value.begin, value.len),
140 net::UnescapeRule::NORMAL);
143 return base::string16();
146 GURL ToolbarModelImpl::GetURL() const {
147 const NavigationController* navigation_controller = GetNavigationController();
148 if (navigation_controller) {
149 const NavigationEntry* entry = navigation_controller->GetVisibleEntry();
150 if (entry)
151 return ShouldDisplayURL() ? entry->GetVirtualURL() : GURL();
154 return GURL(content::kAboutBlankURL);
157 bool ToolbarModelImpl::WouldOmitURLDueToOriginChip() const {
158 const char kInterstitialShownKey[] = "interstitial_shown";
160 // When users type URLs and hit enter, continue to show those URLs until
161 // the navigation commits or an interstitial is shown, because having the
162 // omnibox clear immediately feels like the input was ignored.
163 NavigationController* navigation_controller = GetNavigationController();
164 if (navigation_controller) {
165 NavigationEntry* pending_entry = navigation_controller->GetPendingEntry();
166 if (pending_entry) {
167 const NavigationEntry* visible_entry =
168 navigation_controller->GetVisibleEntry();
169 base::string16 unused;
170 // Keep track that we've shown the origin chip on an interstitial so it
171 // can be shown even after the interstitial was dismissed, to avoid
172 // showing the chip, removing it and then showing it again.
173 if (visible_entry &&
174 visible_entry->GetPageType() == content::PAGE_TYPE_INTERSTITIAL &&
175 !pending_entry->GetExtraData(kInterstitialShownKey, &unused))
176 pending_entry->SetExtraData(kInterstitialShownKey, base::string16());
177 const content::PageTransition transition_type =
178 pending_entry->GetTransitionType();
179 if ((transition_type & content::PAGE_TRANSITION_TYPED) != 0 &&
180 !pending_entry->GetExtraData(kInterstitialShownKey, &unused))
181 return false;
185 bool should_display_origin_chip =
186 chrome::ShouldDisplayOriginChip() || chrome::ShouldDisplayOriginChipV2();
187 return should_display_origin_chip && delegate_->InTabbedBrowser() &&
188 ShouldDisplayURL() && url_replacement_enabled();
191 bool ToolbarModelImpl::WouldPerformSearchTermReplacement(
192 bool ignore_editing) const {
193 return !GetSearchTerms(ignore_editing).empty();
196 bool ToolbarModelImpl::ShouldDisplayURL() const {
197 // Note: The order here is important.
198 // - The WebUI test must come before the extension scheme test because there
199 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In
200 // that case, we should prefer what the WebUI instance says.
201 // - The view-source test must come before the NTP test because of the case
202 // of view-source:chrome://newtab, which should display its URL despite what
203 // chrome://newtab says.
204 NavigationController* controller = GetNavigationController();
205 NavigationEntry* entry = controller ? controller->GetVisibleEntry() : NULL;
206 if (entry) {
207 if (entry->IsViewSourceMode() ||
208 entry->GetPageType() == content::PAGE_TYPE_INTERSTITIAL) {
209 return true;
212 GURL url = entry->GetURL();
213 GURL virtual_url = entry->GetVirtualURL();
214 if (url.SchemeIs(content::kChromeUIScheme) ||
215 virtual_url.SchemeIs(content::kChromeUIScheme)) {
216 if (!url.SchemeIs(content::kChromeUIScheme))
217 url = virtual_url;
218 return url.host() != chrome::kChromeUINewTabHost;
222 if (chrome::IsInstantNTP(delegate_->GetActiveWebContents()))
223 return false;
225 return true;
228 ToolbarModel::SecurityLevel ToolbarModelImpl::GetSecurityLevel(
229 bool ignore_editing) const {
230 // When editing, assume no security style.
231 return (input_in_progress() && !ignore_editing) ?
232 NONE : GetSecurityLevelForWebContents(delegate_->GetActiveWebContents());
235 int ToolbarModelImpl::GetIcon() const {
236 if (WouldPerformSearchTermReplacement(false)) {
237 // The secured version of the search icon is necessary if neither the search
238 // button nor origin chip are present to indicate the security state.
239 return (chrome::GetDisplaySearchButtonConditions() ==
240 chrome::DISPLAY_SEARCH_BUTTON_NEVER) &&
241 !chrome::ShouldDisplayOriginChipV2() ?
242 IDR_OMNIBOX_SEARCH_SECURED : IDR_OMNIBOX_SEARCH;
245 // When the original site chip experiment is running, the icon in the location
246 // bar, when not the search icon, should be the page icon.
247 if (chrome::ShouldDisplayOriginChip())
248 return GetIconForSecurityLevel(NONE);
250 return GetIconForSecurityLevel(GetSecurityLevel(false));
253 int ToolbarModelImpl::GetIconForSecurityLevel(SecurityLevel level) const {
254 static int icon_ids[NUM_SECURITY_LEVELS] = {
255 IDR_LOCATION_BAR_HTTP,
256 IDR_OMNIBOX_HTTPS_VALID,
257 IDR_OMNIBOX_HTTPS_VALID,
258 IDR_OMNIBOX_HTTPS_WARNING,
259 IDR_OMNIBOX_HTTPS_POLICY_WARNING,
260 IDR_OMNIBOX_HTTPS_INVALID,
262 DCHECK(arraysize(icon_ids) == NUM_SECURITY_LEVELS);
263 return icon_ids[level];
266 base::string16 ToolbarModelImpl::GetEVCertName() const {
267 DCHECK_EQ(EV_SECURE, GetSecurityLevel(false));
268 scoped_refptr<net::X509Certificate> cert;
269 // Note: Navigation controller and active entry are guaranteed non-NULL or
270 // the security level would be NONE.
271 content::CertStore::GetInstance()->RetrieveCert(
272 GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id, &cert);
273 return GetEVCertName(*cert.get());
276 // static
277 base::string16 ToolbarModelImpl::GetEVCertName(
278 const net::X509Certificate& cert) {
279 // EV are required to have an organization name and country.
280 if (cert.subject().organization_names.empty() ||
281 cert.subject().country_name.empty()) {
282 NOTREACHED();
283 return base::string16();
286 return l10n_util::GetStringFUTF16(
287 IDS_SECURE_CONNECTION_EV,
288 base::UTF8ToUTF16(cert.subject().organization_names[0]),
289 base::UTF8ToUTF16(cert.subject().country_name));
292 NavigationController* ToolbarModelImpl::GetNavigationController() const {
293 // This |current_tab| can be NULL during the initialization of the
294 // toolbar during window creation (i.e. before any tabs have been added
295 // to the window).
296 WebContents* current_tab = delegate_->GetActiveWebContents();
297 return current_tab ? &current_tab->GetController() : NULL;
300 Profile* ToolbarModelImpl::GetProfile() const {
301 NavigationController* navigation_controller = GetNavigationController();
302 return navigation_controller ?
303 Profile::FromBrowserContext(navigation_controller->GetBrowserContext()) :
304 NULL;
307 base::string16 ToolbarModelImpl::GetSearchTerms(bool ignore_editing) const {
308 if (!url_replacement_enabled() || (input_in_progress() && !ignore_editing))
309 return base::string16();
311 const WebContents* web_contents = delegate_->GetActiveWebContents();
312 base::string16 search_terms(chrome::GetSearchTerms(web_contents));
313 if (search_terms.empty()) {
314 // We mainly do this to enforce the subsequent DCHECK.
315 return base::string16();
318 // If the page is still loading and the security style is unknown, consider
319 // the page secure. Without this, after the user hit enter on some search
320 // terms, the omnibox would change to displaying the loading URL before
321 // changing back to the search terms once they could be extracted, thus
322 // causing annoying flicker.
323 DCHECK(web_contents);
324 const NavigationController& nav_controller = web_contents->GetController();
325 const NavigationEntry* entry = nav_controller.GetVisibleEntry();
326 if ((entry != nav_controller.GetLastCommittedEntry()) &&
327 (entry->GetSSL().security_style == content::SECURITY_STYLE_UNKNOWN))
328 return search_terms;
330 // If the URL is using a Google base URL specified via the command line, we
331 // bypass the security check below.
332 if (entry &&
333 google_util::StartsWithCommandLineGoogleBaseURL(entry->GetVirtualURL()))
334 return search_terms;
336 // Otherwise, extract search terms for HTTPS pages that do not have a security
337 // error.
338 ToolbarModel::SecurityLevel security_level = GetSecurityLevel(ignore_editing);
339 return ((security_level == NONE) || (security_level == SECURITY_ERROR)) ?
340 base::string16() : search_terms;