- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / toolbar / toolbar_model_impl.cc
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.
4
5 #include "chrome/browser/ui/toolbar/toolbar_model_impl.h"
6
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/policy/profile_policy_connector.h"
16 #include "chrome/browser/policy/profile_policy_connector_factory.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/search/search.h"
19 #include "chrome/browser/ssl/ssl_error_info.h"
20 #include "chrome/browser/ui/toolbar/toolbar_model_delegate.h"
21 #include "chrome/common/chrome_constants.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/pref_names.h"
24 #include "chrome/common/url_constants.h"
25 #include "content/public/browser/cert_store.h"
26 #include "content/public/browser/navigation_controller.h"
27 #include "content/public/browser/navigation_entry.h"
28 #include "content/public/browser/web_contents.h"
29 #include "content/public/browser/web_ui.h"
30 #include "content/public/common/content_constants.h"
31 #include "content/public/common/ssl_status.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"
38
39 using content::NavigationController;
40 using content::NavigationEntry;
41 using content::SSLStatus;
42 using content::WebContents;
43
44 ToolbarModelImpl::ToolbarModelImpl(ToolbarModelDelegate* delegate)
45     : delegate_(delegate) {
46 }
47
48 ToolbarModelImpl::~ToolbarModelImpl() {
49 }
50
51 ToolbarModel::SecurityLevel ToolbarModelImpl::GetSecurityLevelForWebContents(
52       content::WebContents* web_contents) {
53   if (!web_contents)
54     return NONE;
55
56   NavigationEntry* entry = web_contents->GetController().GetVisibleEntry();
57   if (!entry)
58     return NONE;
59
60   const SSLStatus& ssl = entry->GetSSL();
61   switch (ssl.security_style) {
62     case content::SECURITY_STYLE_UNKNOWN:
63     case content::SECURITY_STYLE_UNAUTHENTICATED:
64       return NONE;
65
66     case content::SECURITY_STYLE_AUTHENTICATION_BROKEN:
67       return SECURITY_ERROR;
68
69     case content::SECURITY_STYLE_AUTHENTICATED:
70       if (policy::ProfilePolicyConnectorFactory::GetForProfile(
71           Profile::FromBrowserContext(web_contents->GetBrowserContext()))->
72           UsedPolicyCertificates())
73         return SECURITY_POLICY_WARNING;
74       if (!!(ssl.content_status & SSLStatus::DISPLAYED_INSECURE_CONTENT))
75         return SECURITY_WARNING;
76       if (net::IsCertStatusError(ssl.cert_status)) {
77         DCHECK(net::IsCertStatusMinorError(ssl.cert_status));
78         return SECURITY_WARNING;
79       }
80       if ((ssl.cert_status & net::CERT_STATUS_IS_EV) &&
81           content::CertStore::GetInstance()->RetrieveCert(ssl.cert_id, NULL))
82         return EV_SECURE;
83       return SECURE;
84
85     default:
86       NOTREACHED();
87       return NONE;
88   }
89 }
90
91 // ToolbarModelImpl Implementation.
92 string16 ToolbarModelImpl::GetText(bool allow_search_term_replacement) const {
93   if (allow_search_term_replacement) {
94     string16 search_terms(GetSearchTerms(false));
95     if (!search_terms.empty())
96       return search_terms;
97   }
98   std::string languages;  // Empty if we don't have a |navigation_controller|.
99   Profile* profile = GetProfile();
100   if (profile)
101     languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
102
103   GURL url(GetURL());
104   if (url.spec().length() > content::kMaxURLDisplayChars)
105     url = url.IsStandard() ? url.GetOrigin() : GURL(url.scheme() + ":");
106   // Note that we can't unescape spaces here, because if the user copies this
107   // and pastes it into another program, that program may think the URL ends at
108   // the space.
109   return AutocompleteInput::FormattedStringWithEquivalentMeaning(
110       url, net::FormatUrl(url, languages, net::kFormatUrlOmitAll,
111                           net::UnescapeRule::NORMAL, NULL, NULL, NULL));
112 }
113
114 string16 ToolbarModelImpl::GetCorpusNameForMobile() const {
115   if (!WouldPerformSearchTermReplacement(false))
116     return string16();
117   GURL url(GetURL());
118   // If there is a query in the url fragment look for the corpus name there,
119   // otherwise look for the corpus name in the query parameters.
120   const std::string& query_str(google_util::HasGoogleSearchQueryParam(
121       url.ref()) ? url.ref() : url.query());
122   url_parse::Component query(0, query_str.length()), key, value;
123   const char kChipKey[] = "sboxchip";
124   while (url_parse::ExtractQueryKeyValue(query_str.c_str(), &query, &key,
125                                          &value)) {
126     if (key.is_nonempty() && query_str.substr(key.begin, key.len) == kChipKey) {
127       return net::UnescapeAndDecodeUTF8URLComponent(
128           query_str.substr(value.begin, value.len),
129           net::UnescapeRule::NORMAL, NULL);
130     }
131   }
132   return string16();
133 }
134
135 GURL ToolbarModelImpl::GetURL() const {
136   const NavigationController* navigation_controller = GetNavigationController();
137   if (navigation_controller) {
138     const NavigationEntry* entry = navigation_controller->GetVisibleEntry();
139     if (entry)
140       return ShouldDisplayURL() ? entry->GetVirtualURL() : GURL();
141   }
142
143   return GURL(content::kAboutBlankURL);
144 }
145
146 bool ToolbarModelImpl::WouldPerformSearchTermReplacement(
147     bool ignore_editing) const {
148   return !GetSearchTerms(ignore_editing).empty();
149 }
150
151 bool ToolbarModelImpl::ShouldDisplayURL() const {
152   // Note: The order here is important.
153   // - The WebUI test must come before the extension scheme test because there
154   //   can be WebUIs that have extension schemes (e.g. the bookmark manager). In
155   //   that case, we should prefer what the WebUI instance says.
156   // - The view-source test must come before the NTP test because of the case
157   //   of view-source:chrome://newtab, which should display its URL despite what
158   //   chrome://newtab says.
159   NavigationController* controller = GetNavigationController();
160   NavigationEntry* entry = controller ? controller->GetVisibleEntry() : NULL;
161   if (entry) {
162     if (entry->IsViewSourceMode() ||
163         entry->GetPageType() == content::PAGE_TYPE_INTERSTITIAL) {
164       return true;
165     }
166
167     GURL url = entry->GetURL();
168     GURL virtual_url = entry->GetVirtualURL();
169     if (url.SchemeIs(chrome::kChromeUIScheme) ||
170         virtual_url.SchemeIs(chrome::kChromeUIScheme)) {
171       if (!url.SchemeIs(chrome::kChromeUIScheme))
172         url = virtual_url;
173       return url.host() != chrome::kChromeUINewTabHost;
174     }
175   }
176
177   if (chrome::IsInstantNTP(delegate_->GetActiveWebContents()))
178     return false;
179
180   return true;
181 }
182
183 ToolbarModel::SecurityLevel ToolbarModelImpl::GetSecurityLevel(
184     bool ignore_editing) const {
185   // When editing, assume no security style.
186   return (input_in_progress() && !ignore_editing) ?
187       NONE : GetSecurityLevelForWebContents(delegate_->GetActiveWebContents());
188 }
189
190 int ToolbarModelImpl::GetIcon() const {
191   if (WouldPerformSearchTermReplacement(false))
192     return IDR_OMNIBOX_SEARCH_SECURED;
193
194   static int icon_ids[NUM_SECURITY_LEVELS] = {
195     IDR_LOCATION_BAR_HTTP,
196     IDR_OMNIBOX_HTTPS_VALID,
197     IDR_OMNIBOX_HTTPS_VALID,
198     IDR_OMNIBOX_HTTPS_WARNING,
199     IDR_OMNIBOX_HTTPS_POLICY_WARNING,
200     IDR_OMNIBOX_HTTPS_INVALID,
201   };
202   DCHECK(arraysize(icon_ids) == NUM_SECURITY_LEVELS);
203   return icon_ids[GetSecurityLevel(false)];
204 }
205
206 string16 ToolbarModelImpl::GetEVCertName() const {
207   DCHECK_EQ(EV_SECURE, GetSecurityLevel(false));
208   scoped_refptr<net::X509Certificate> cert;
209   // Note: Navigation controller and active entry are guaranteed non-NULL or
210   // the security level would be NONE.
211   content::CertStore::GetInstance()->RetrieveCert(
212       GetNavigationController()->GetVisibleEntry()->GetSSL().cert_id, &cert);
213   return GetEVCertName(*cert.get());
214 }
215
216 // static
217 string16 ToolbarModelImpl::GetEVCertName(const net::X509Certificate& cert) {
218   // EV are required to have an organization name and country.
219   if (cert.subject().organization_names.empty() ||
220       cert.subject().country_name.empty()) {
221     NOTREACHED();
222     return string16();
223   }
224
225   return l10n_util::GetStringFUTF16(
226       IDS_SECURE_CONNECTION_EV,
227       UTF8ToUTF16(cert.subject().organization_names[0]),
228       UTF8ToUTF16(cert.subject().country_name));
229 }
230
231 NavigationController* ToolbarModelImpl::GetNavigationController() const {
232   // This |current_tab| can be NULL during the initialization of the
233   // toolbar during window creation (i.e. before any tabs have been added
234   // to the window).
235   WebContents* current_tab = delegate_->GetActiveWebContents();
236   return current_tab ? &current_tab->GetController() : NULL;
237 }
238
239 Profile* ToolbarModelImpl::GetProfile() const {
240   NavigationController* navigation_controller = GetNavigationController();
241   return navigation_controller ?
242       Profile::FromBrowserContext(navigation_controller->GetBrowserContext()) :
243       NULL;
244 }
245
246 string16 ToolbarModelImpl::GetSearchTerms(bool ignore_editing) const {
247   if (!search_term_replacement_enabled() ||
248       (input_in_progress() && !ignore_editing))
249     return string16();
250
251   const WebContents* web_contents = delegate_->GetActiveWebContents();
252   string16 search_terms(chrome::GetSearchTerms(web_contents));
253   if (search_terms.empty())
254     return string16();  // We mainly do this to enforce the subsequent DCHECK.
255
256   // If the page is still loading and the security style is unknown, consider
257   // the page secure.  Without this, after the user hit enter on some search
258   // terms, the omnibox would change to displaying the loading URL before
259   // changing back to the search terms once they could be extracted, thus
260   // causing annoying flicker.
261   DCHECK(web_contents);
262   const NavigationController& nav_controller = web_contents->GetController();
263   const NavigationEntry* entry = nav_controller.GetVisibleEntry();
264   if ((entry != nav_controller.GetLastCommittedEntry()) &&
265       (entry->GetSSL().security_style == content::SECURITY_STYLE_UNKNOWN))
266     return search_terms;
267
268   // If the URL is using a Google base URL specified via the command line, we
269   // bypass the security check below.
270   if (entry &&
271       google_util::StartsWithCommandLineGoogleBaseURL(entry->GetVirtualURL()))
272     return search_terms;
273
274   // Otherwise, extract search terms for HTTPS pages that do not have a security
275   // error.
276   ToolbarModel::SecurityLevel security_level = GetSecurityLevel(ignore_editing);
277   return ((security_level == NONE) || (security_level == SECURITY_ERROR)) ?
278       string16() : search_terms;
279 }