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/prefs/pref_service.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/time/time.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/connection_security_helper.h"
16 #include "chrome/browser/ui/toolbar/toolbar_model_delegate.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/common/url_constants.h"
19 #include "chrome/grit/generated_resources.h"
20 #include "components/google/core/browser/google_util.h"
21 #include "components/omnibox/autocomplete_input.h"
22 #include "components/omnibox/autocomplete_match.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/components_scaled_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 "net/ssl/ssl_connection_status_flags.h"
36 #include "ui/base/l10n/l10n_util.h"
38 using content::NavigationController
;
39 using content::NavigationEntry
;
40 using content::WebContents
;
42 ToolbarModelImpl::ToolbarModelImpl(ToolbarModelDelegate
* delegate
)
43 : delegate_(delegate
) {
46 ToolbarModelImpl::~ToolbarModelImpl() {
49 // ToolbarModelImpl Implementation.
50 base::string16
ToolbarModelImpl::GetText() const {
51 base::string16
search_terms(GetSearchTerms(false));
52 if (!search_terms
.empty())
55 return GetFormattedURL(NULL
);
58 base::string16
ToolbarModelImpl::GetFormattedURL(size_t* prefix_end
) const {
59 std::string languages
; // Empty if we don't have a |navigation_controller|.
60 Profile
* profile
= GetProfile();
62 languages
= profile
->GetPrefs()->GetString(prefs::kAcceptLanguages
);
65 if (url
.spec().length() > content::kMaxURLDisplayChars
)
66 url
= url
.IsStandard() ? url
.GetOrigin() : GURL(url
.scheme() + ":");
67 // Note that we can't unescape spaces here, because if the user copies this
68 // and pastes it into another program, that program may think the URL ends at
70 return AutocompleteInput::FormattedStringWithEquivalentMeaning(
71 url
, net::FormatUrl(url
, languages
, net::kFormatUrlOmitAll
,
72 net::UnescapeRule::NORMAL
, NULL
, prefix_end
, NULL
),
73 ChromeAutocompleteSchemeClassifier(profile
));
76 base::string16
ToolbarModelImpl::GetCorpusNameForMobile() const {
77 if (!WouldPerformSearchTermReplacement(false))
78 return base::string16();
80 // If there is a query in the url fragment look for the corpus name there,
81 // otherwise look for the corpus name in the query parameters.
82 const std::string
& query_str(google_util::HasGoogleSearchQueryParam(
83 url
.ref()) ? url
.ref() : url
.query());
84 url::Component
query(0, query_str
.length()), key
, value
;
85 const char kChipKey
[] = "sboxchip";
86 while (url::ExtractQueryKeyValue(query_str
.c_str(), &query
, &key
, &value
)) {
87 if (key
.is_nonempty() && query_str
.substr(key
.begin
, key
.len
) == kChipKey
) {
88 return net::UnescapeAndDecodeUTF8URLComponent(
89 query_str
.substr(value
.begin
, value
.len
),
90 net::UnescapeRule::NORMAL
);
93 return base::string16();
96 GURL
ToolbarModelImpl::GetURL() const {
97 const NavigationController
* navigation_controller
= GetNavigationController();
98 if (navigation_controller
) {
99 const NavigationEntry
* entry
= navigation_controller
->GetVisibleEntry();
101 return ShouldDisplayURL() ? entry
->GetVirtualURL() : GURL();
104 return GURL(url::kAboutBlankURL
);
107 bool ToolbarModelImpl::WouldPerformSearchTermReplacement(
108 bool ignore_editing
) const {
109 return !GetSearchTerms(ignore_editing
).empty();
112 ConnectionSecurityHelper::SecurityLevel
ToolbarModelImpl::GetSecurityLevel(
113 bool ignore_editing
) const {
114 // When editing, assume no security style.
115 return (input_in_progress() && !ignore_editing
)
116 ? ConnectionSecurityHelper::NONE
117 : ConnectionSecurityHelper::GetSecurityLevelForWebContents(
118 delegate_
->GetActiveWebContents());
121 int ToolbarModelImpl::GetIcon() const {
122 if (WouldPerformSearchTermReplacement(false))
123 return IDR_OMNIBOX_SEARCH_SECURED
;
125 return GetIconForSecurityLevel(GetSecurityLevel(false));
128 int ToolbarModelImpl::GetIconForSecurityLevel(
129 ConnectionSecurityHelper::SecurityLevel level
) const {
131 case ConnectionSecurityHelper::NONE
:
132 return IDR_LOCATION_BAR_HTTP
;
133 case ConnectionSecurityHelper::EV_SECURE
:
134 case ConnectionSecurityHelper::SECURE
:
135 return IDR_OMNIBOX_HTTPS_VALID
;
136 case ConnectionSecurityHelper::SECURITY_WARNING
:
137 return IDR_OMNIBOX_HTTPS_WARNING
;
138 case ConnectionSecurityHelper::SECURITY_POLICY_WARNING
:
139 return IDR_OMNIBOX_HTTPS_POLICY_WARNING
;
140 case ConnectionSecurityHelper::SECURITY_ERROR
:
141 return IDR_OMNIBOX_HTTPS_INVALID
;
145 return IDR_LOCATION_BAR_HTTP
;
148 base::string16
ToolbarModelImpl::GetEVCertName() const {
149 if (GetSecurityLevel(false) != ConnectionSecurityHelper::EV_SECURE
)
150 return base::string16();
152 // Note: Navigation controller and active entry are guaranteed non-NULL or
153 // the security level would be NONE.
154 scoped_refptr
<net::X509Certificate
> cert
;
155 content::CertStore::GetInstance()->RetrieveCert(
156 GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id
, &cert
);
158 // EV are required to have an organization name and country.
159 DCHECK(!cert
->subject().organization_names
.empty());
160 DCHECK(!cert
->subject().country_name
.empty());
161 return l10n_util::GetStringFUTF16(
162 IDS_SECURE_CONNECTION_EV
,
163 base::UTF8ToUTF16(cert
->subject().organization_names
[0]),
164 base::UTF8ToUTF16(cert
->subject().country_name
));
167 bool ToolbarModelImpl::ShouldDisplayURL() const {
168 // Note: The order here is important.
169 // - The WebUI test must come before the extension scheme test because there
170 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In
171 // that case, we should prefer what the WebUI instance says.
172 // - The view-source test must come before the NTP test because of the case
173 // of view-source:chrome://newtab, which should display its URL despite what
174 // chrome://newtab says.
175 NavigationController
* controller
= GetNavigationController();
176 NavigationEntry
* entry
= controller
? controller
->GetVisibleEntry() : NULL
;
178 if (entry
->IsViewSourceMode() ||
179 entry
->GetPageType() == content::PAGE_TYPE_INTERSTITIAL
) {
183 GURL url
= entry
->GetURL();
184 GURL virtual_url
= entry
->GetVirtualURL();
185 if (url
.SchemeIs(content::kChromeUIScheme
) ||
186 virtual_url
.SchemeIs(content::kChromeUIScheme
)) {
187 if (!url
.SchemeIs(content::kChromeUIScheme
))
189 return url
.host() != chrome::kChromeUINewTabHost
;
193 return !chrome::IsInstantNTP(delegate_
->GetActiveWebContents());
196 NavigationController
* ToolbarModelImpl::GetNavigationController() const {
197 // This |current_tab| can be NULL during the initialization of the
198 // toolbar during window creation (i.e. before any tabs have been added
200 WebContents
* current_tab
= delegate_
->GetActiveWebContents();
201 return current_tab
? ¤t_tab
->GetController() : NULL
;
204 Profile
* ToolbarModelImpl::GetProfile() const {
205 NavigationController
* navigation_controller
= GetNavigationController();
206 return navigation_controller
?
207 Profile::FromBrowserContext(navigation_controller
->GetBrowserContext()) :
211 base::string16
ToolbarModelImpl::GetSearchTerms(bool ignore_editing
) const {
212 if (!url_replacement_enabled() || (input_in_progress() && !ignore_editing
))
213 return base::string16();
215 const WebContents
* web_contents
= delegate_
->GetActiveWebContents();
216 base::string16
search_terms(chrome::GetSearchTerms(web_contents
));
217 if (search_terms
.empty()) {
218 // We mainly do this to enforce the subsequent DCHECK.
219 return base::string16();
222 // If the page is still loading and the security style is unknown, consider
223 // the page secure. Without this, after the user hit enter on some search
224 // terms, the omnibox would change to displaying the loading URL before
225 // changing back to the search terms once they could be extracted, thus
226 // causing annoying flicker.
227 DCHECK(web_contents
);
228 const NavigationController
& nav_controller
= web_contents
->GetController();
229 const NavigationEntry
* entry
= nav_controller
.GetVisibleEntry();
230 if ((entry
!= nav_controller
.GetLastCommittedEntry()) &&
231 (entry
->GetSSL().security_style
== content::SECURITY_STYLE_UNKNOWN
))
234 // If the URL is using a Google base URL specified via the command line, we
235 // bypass the security check below.
237 google_util::StartsWithCommandLineGoogleBaseURL(entry
->GetVirtualURL()))
240 // Otherwise, extract search terms for HTTPS pages that do not have a security
242 ConnectionSecurityHelper::SecurityLevel security_level
=
243 GetSecurityLevel(ignore_editing
);
244 return ((security_level
== ConnectionSecurityHelper::NONE
) ||
245 (security_level
== ConnectionSecurityHelper::SECURITY_ERROR
))