ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / ui / toolbar / toolbar_model_impl.cc
blob6f2c5426d247e77bf1e0c3b93f1f6073b77d20e7
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/chrome_autocomplete_scheme_classifier.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/search/search.h"
13 #include "chrome/browser/ssl/connection_security.h"
14 #include "chrome/browser/ui/toolbar/toolbar_model_delegate.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/common/url_constants.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "components/google/core/browser/google_util.h"
19 #include "components/omnibox/browser/autocomplete_classifier.h"
20 #include "components/omnibox/browser/autocomplete_input.h"
21 #include "components/omnibox/browser/autocomplete_match.h"
22 #include "components/url_formatter/url_formatter.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/cert/cert_status_flags.h"
33 #include "net/cert/x509_certificate.h"
34 #include "net/ssl/ssl_connection_status_flags.h"
35 #include "ui/base/l10n/l10n_util.h"
37 using content::NavigationController;
38 using content::NavigationEntry;
39 using content::WebContents;
41 ToolbarModelImpl::ToolbarModelImpl(ToolbarModelDelegate* delegate)
42 : delegate_(delegate) {
45 ToolbarModelImpl::~ToolbarModelImpl() {
48 // ToolbarModelImpl Implementation.
49 base::string16 ToolbarModelImpl::GetText() const {
50 base::string16 search_terms(GetSearchTerms(false));
51 if (!search_terms.empty())
52 return search_terms;
54 return GetFormattedURL(NULL);
57 base::string16 ToolbarModelImpl::GetFormattedURL(size_t* prefix_end) const {
58 std::string languages; // Empty if we don't have a |navigation_controller|.
59 Profile* profile = GetProfile();
60 if (profile)
61 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
63 GURL url(GetURL());
64 if (url.spec().length() > content::kMaxURLDisplayChars)
65 url = url.IsStandard() ? url.GetOrigin() : GURL(url.scheme() + ":");
66 // Note that we can't unescape spaces here, because if the user copies this
67 // and pastes it into another program, that program may think the URL ends at
68 // the space.
69 return AutocompleteInput::FormattedStringWithEquivalentMeaning(
70 url, url_formatter::FormatUrl(
71 url, languages, url_formatter::kFormatUrlOmitAll,
72 net::UnescapeRule::NORMAL, nullptr, prefix_end, nullptr),
73 ChromeAutocompleteSchemeClassifier(profile));
76 base::string16 ToolbarModelImpl::GetCorpusNameForMobile() const {
77 if (!WouldPerformSearchTermReplacement(false))
78 return base::string16();
79 GURL url(GetURL());
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();
100 if (entry)
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 connection_security::SecurityLevel ToolbarModelImpl::GetSecurityLevel(
113 bool ignore_editing) const {
114 // When editing, assume no security style.
115 return (input_in_progress() && !ignore_editing)
116 ? connection_security::NONE
117 : connection_security::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 connection_security::SecurityLevel level) const {
130 switch (level) {
131 case connection_security::NONE:
132 return IDR_LOCATION_BAR_HTTP;
133 case connection_security::EV_SECURE:
134 case connection_security::SECURE:
135 return IDR_OMNIBOX_HTTPS_VALID;
136 case connection_security::SECURITY_WARNING:
137 // Surface Dubious as Neutral.
138 return IDR_LOCATION_BAR_HTTP;
139 case connection_security::SECURITY_POLICY_WARNING:
140 return IDR_OMNIBOX_HTTPS_POLICY_WARNING;
141 case connection_security::SECURITY_ERROR:
142 return IDR_OMNIBOX_HTTPS_INVALID;
145 NOTREACHED();
146 return IDR_LOCATION_BAR_HTTP;
149 base::string16 ToolbarModelImpl::GetEVCertName() const {
150 if (GetSecurityLevel(false) != connection_security::EV_SECURE)
151 return base::string16();
153 // Note: Navigation controller and active entry are guaranteed non-NULL or
154 // the security level would be NONE.
155 scoped_refptr<net::X509Certificate> cert;
156 content::CertStore::GetInstance()->RetrieveCert(
157 GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id, &cert);
159 // EV are required to have an organization name and country.
160 DCHECK(!cert->subject().organization_names.empty());
161 DCHECK(!cert->subject().country_name.empty());
162 return l10n_util::GetStringFUTF16(
163 IDS_SECURE_CONNECTION_EV,
164 base::UTF8ToUTF16(cert->subject().organization_names[0]),
165 base::UTF8ToUTF16(cert->subject().country_name));
168 bool ToolbarModelImpl::ShouldDisplayURL() const {
169 // Note: The order here is important.
170 // - The WebUI test must come before the extension scheme test because there
171 // can be WebUIs that have extension schemes (e.g. the bookmark manager). In
172 // that case, we should prefer what the WebUI instance says.
173 // - The view-source test must come before the NTP test because of the case
174 // of view-source:chrome://newtab, which should display its URL despite what
175 // chrome://newtab says.
176 NavigationController* controller = GetNavigationController();
177 NavigationEntry* entry = controller ? controller->GetVisibleEntry() : NULL;
178 if (entry) {
179 if (entry->IsViewSourceMode() ||
180 entry->GetPageType() == content::PAGE_TYPE_INTERSTITIAL) {
181 return true;
184 GURL url = entry->GetURL();
185 GURL virtual_url = entry->GetVirtualURL();
186 if (url.SchemeIs(content::kChromeUIScheme) ||
187 virtual_url.SchemeIs(content::kChromeUIScheme)) {
188 if (!url.SchemeIs(content::kChromeUIScheme))
189 url = virtual_url;
190 return url.host() != chrome::kChromeUINewTabHost;
194 return !search::IsInstantNTP(delegate_->GetActiveWebContents());
197 NavigationController* ToolbarModelImpl::GetNavigationController() const {
198 // This |current_tab| can be NULL during the initialization of the
199 // toolbar during window creation (i.e. before any tabs have been added
200 // to the window).
201 WebContents* current_tab = delegate_->GetActiveWebContents();
202 return current_tab ? &current_tab->GetController() : NULL;
205 Profile* ToolbarModelImpl::GetProfile() const {
206 NavigationController* navigation_controller = GetNavigationController();
207 return navigation_controller ?
208 Profile::FromBrowserContext(navigation_controller->GetBrowserContext()) :
209 NULL;
212 base::string16 ToolbarModelImpl::GetSearchTerms(bool ignore_editing) const {
213 if (!url_replacement_enabled() || (input_in_progress() && !ignore_editing))
214 return base::string16();
216 const WebContents* web_contents = delegate_->GetActiveWebContents();
217 base::string16 search_terms(search::GetSearchTerms(web_contents));
218 if (search_terms.empty()) {
219 // We mainly do this to enforce the subsequent DCHECK.
220 return base::string16();
223 // If the page is still loading and the security style is unknown, consider
224 // the page secure. Without this, after the user hit enter on some search
225 // terms, the omnibox would change to displaying the loading URL before
226 // changing back to the search terms once they could be extracted, thus
227 // causing annoying flicker.
228 DCHECK(web_contents);
229 const NavigationController& nav_controller = web_contents->GetController();
230 const NavigationEntry* entry = nav_controller.GetVisibleEntry();
231 if ((entry != nav_controller.GetLastCommittedEntry()) &&
232 (entry->GetSSL().security_style == content::SECURITY_STYLE_UNKNOWN))
233 return search_terms;
235 // If the URL is using a Google base URL specified via the command line, we
236 // bypass the security check below.
237 if (entry &&
238 google_util::StartsWithCommandLineGoogleBaseURL(entry->GetVirtualURL()))
239 return search_terms;
241 // Otherwise, extract search terms for HTTPS pages that do not have a security
242 // error.
243 connection_security::SecurityLevel security_level =
244 GetSecurityLevel(ignore_editing);
245 return ((security_level == connection_security::NONE) ||
246 (security_level == connection_security::SECURITY_ERROR))
247 ? base::string16()
248 : search_terms;