blob: 6843a10fc27edba5d5f9c67802aba496102bf62a [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
19import android.app.AlertDialog;
20import android.content.Context;
21import android.content.DialogInterface;
22import android.content.res.Configuration;
23import android.net.http.SslCertificate;
24import android.net.http.SslError;
25import android.text.format.DateFormat;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.webkit.HttpAuthHandler;
29import android.webkit.SslErrorHandler;
30import android.webkit.WebView;
31import android.widget.LinearLayout;
32import android.widget.TextView;
33
34import java.util.Date;
35
36/**
37 * Displays page info
38 *
39 */
40public class PageDialogsHandler {
41
42 private Context mContext;
43 private Controller mController;
44 private boolean mPageInfoFromShowSSLCertificateOnError;
45 private Tab mPageInfoView;
46 private AlertDialog mPageInfoDialog;
47
48 // as SSLCertificateOnError has different style for landscape / portrait,
49 // we have to re-open it when configuration changed
50 private AlertDialog mSSLCertificateOnErrorDialog;
51 private WebView mSSLCertificateOnErrorView;
52 private SslErrorHandler mSSLCertificateOnErrorHandler;
53 private SslError mSSLCertificateOnErrorError;
54
55 // as SSLCertificate has different style for landscape / portrait, we
56 // have to re-open it when configuration changed
57 private AlertDialog mSSLCertificateDialog;
58 private Tab mSSLCertificateView;
59 private HttpAuthenticationDialog mHttpAuthenticationDialog;
60
61 public PageDialogsHandler(Context context, Controller controller) {
62 mContext = context;
63 mController = controller;
64 }
65
66 public void onConfigurationChanged(Configuration config) {
67 if (mPageInfoDialog != null) {
68 mPageInfoDialog.dismiss();
69 showPageInfo(mPageInfoView, mPageInfoFromShowSSLCertificateOnError);
70 }
71 if (mSSLCertificateDialog != null) {
72 mSSLCertificateDialog.dismiss();
73 showSSLCertificate(mSSLCertificateView);
74 }
75 if (mSSLCertificateOnErrorDialog != null) {
76 mSSLCertificateOnErrorDialog.dismiss();
77 showSSLCertificateOnError(mSSLCertificateOnErrorView, mSSLCertificateOnErrorHandler,
78 mSSLCertificateOnErrorError);
79 }
80 if (mHttpAuthenticationDialog != null) {
81 mHttpAuthenticationDialog.reshow();
82 }
83 }
84
85 /**
86 * Displays an http-authentication dialog.
87 */
88 void showHttpAuthentication(final Tab tab, final HttpAuthHandler handler, String host, String realm) {
89 mHttpAuthenticationDialog = new HttpAuthenticationDialog(mContext, host, realm);
90 mHttpAuthenticationDialog.setOkListener(new HttpAuthenticationDialog.OkListener() {
91 public void onOk(String host, String realm, String username, String password) {
92 setHttpAuthUsernamePassword(host, realm, username, password);
93 handler.proceed(username, password);
94 mHttpAuthenticationDialog = null;
95 }
96 });
97 mHttpAuthenticationDialog.setCancelListener(new HttpAuthenticationDialog.CancelListener() {
98 public void onCancel() {
99 handler.cancel();
100 mController.resetTitleAndRevertLockIcon(tab);
101 mHttpAuthenticationDialog = null;
102 }
103 });
104 mHttpAuthenticationDialog.show();
105 }
106
107 /**
108 * Set HTTP authentication password.
109 *
110 * @param host The host for the password
111 * @param realm The realm for the password
112 * @param username The username for the password. If it is null, it means
113 * password can't be saved.
114 * @param password The password
115 */
116 public void setHttpAuthUsernamePassword(String host, String realm,
117 String username,
118 String password) {
119 WebView w = mController.getCurrentTopWebView();
120 if (w != null) {
121 w.setHttpAuthUsernamePassword(host, realm, username, password);
122 }
123 }
124
125 /**
126 * Displays a page-info dialog.
127 * @param tab The tab to show info about
128 * @param fromShowSSLCertificateOnError The flag that indicates whether
129 * this dialog was opened from the SSL-certificate-on-error dialog or
130 * not. This is important, since we need to know whether to return to
131 * the parent dialog or simply dismiss.
132 */
133 void showPageInfo(final Tab tab,
134 final boolean fromShowSSLCertificateOnError) {
135 final LayoutInflater factory = LayoutInflater.from(mContext);
136
137 final View pageInfoView = factory.inflate(R.layout.page_info, null);
138
139 final WebView view = tab.getWebView();
140
141 String url = null;
142 String title = null;
143
144 if (view == null) {
145 url = tab.getUrl();
146 title = tab.getTitle();
147 } else if (view == mController.getCurrentWebView()) {
148 // Use the cached title and url if this is the current WebView
149 url = tab.getCurrentUrl();
150 title = tab.getCurrentTitle();
151 } else {
152 url = view.getUrl();
153 title = view.getTitle();
154 }
155
156 if (url == null) {
157 url = "";
158 }
159 if (title == null) {
160 title = "";
161 }
162
163 ((TextView) pageInfoView.findViewById(R.id.address)).setText(url);
164 ((TextView) pageInfoView.findViewById(R.id.title)).setText(title);
165
166 mPageInfoView = tab;
167 mPageInfoFromShowSSLCertificateOnError = fromShowSSLCertificateOnError;
168
169 AlertDialog.Builder alertDialogBuilder =
170 new AlertDialog.Builder(mContext)
171 .setTitle(R.string.page_info)
172 .setIcon(android.R.drawable.ic_dialog_info)
173 .setView(pageInfoView)
174 .setPositiveButton(
175 R.string.ok,
176 new DialogInterface.OnClickListener() {
177 public void onClick(DialogInterface dialog,
178 int whichButton) {
179 mPageInfoDialog = null;
180 mPageInfoView = null;
181
182 // if we came here from the SSL error dialog
183 if (fromShowSSLCertificateOnError) {
184 // go back to the SSL error dialog
185 showSSLCertificateOnError(
186 mSSLCertificateOnErrorView,
187 mSSLCertificateOnErrorHandler,
188 mSSLCertificateOnErrorError);
189 }
190 }
191 })
192 .setOnCancelListener(
193 new DialogInterface.OnCancelListener() {
194 public void onCancel(DialogInterface dialog) {
195 mPageInfoDialog = null;
196 mPageInfoView = null;
197
198 // if we came here from the SSL error dialog
199 if (fromShowSSLCertificateOnError) {
200 // go back to the SSL error dialog
201 showSSLCertificateOnError(
202 mSSLCertificateOnErrorView,
203 mSSLCertificateOnErrorHandler,
204 mSSLCertificateOnErrorError);
205 }
206 }
207 });
208
209 // if we have a main top-level page SSL certificate set or a certificate
210 // error
211 if (fromShowSSLCertificateOnError ||
212 (view != null && view.getCertificate() != null)) {
213 // add a 'View Certificate' button
214 alertDialogBuilder.setNeutralButton(
215 R.string.view_certificate,
216 new DialogInterface.OnClickListener() {
217 public void onClick(DialogInterface dialog,
218 int whichButton) {
219 mPageInfoDialog = null;
220 mPageInfoView = null;
221
222 // if we came here from the SSL error dialog
223 if (fromShowSSLCertificateOnError) {
224 // go back to the SSL error dialog
225 showSSLCertificateOnError(
226 mSSLCertificateOnErrorView,
227 mSSLCertificateOnErrorHandler,
228 mSSLCertificateOnErrorError);
229 } else {
230 // otherwise, display the top-most certificate from
231 // the chain
232 if (view.getCertificate() != null) {
233 showSSLCertificate(tab);
234 }
235 }
236 }
237 });
238 }
239
240 mPageInfoDialog = alertDialogBuilder.show();
241 }
242
243 /**
244 * Displays the main top-level page SSL certificate dialog
245 * (accessible from the Page-Info dialog).
246 * @param tab The tab to show certificate for.
247 */
248 private void showSSLCertificate(final Tab tab) {
249 final View certificateView =
250 inflateCertificateView(tab.getWebView().getCertificate());
251 if (certificateView == null) {
252 return;
253 }
254
255 LayoutInflater factory = LayoutInflater.from(mContext);
256
257 final LinearLayout placeholder =
258 (LinearLayout)certificateView.findViewById(R.id.placeholder);
259
260 LinearLayout ll = (LinearLayout) factory.inflate(
261 R.layout.ssl_success, placeholder);
262 ((TextView)ll.findViewById(R.id.success))
263 .setText(R.string.ssl_certificate_is_valid);
264
265 mSSLCertificateView = tab;
266 mSSLCertificateDialog =
267 new AlertDialog.Builder(mContext)
268 .setTitle(R.string.ssl_certificate).setIcon(
269 R.drawable.ic_dialog_browser_certificate_secure)
270 .setView(certificateView)
271 .setPositiveButton(R.string.ok,
272 new DialogInterface.OnClickListener() {
273 public void onClick(DialogInterface dialog,
274 int whichButton) {
275 mSSLCertificateDialog = null;
276 mSSLCertificateView = null;
277
278 showPageInfo(tab, false);
279 }
280 })
281 .setOnCancelListener(
282 new DialogInterface.OnCancelListener() {
283 public void onCancel(DialogInterface dialog) {
284 mSSLCertificateDialog = null;
285 mSSLCertificateView = null;
286
287 showPageInfo(tab, false);
288 }
289 })
290 .show();
291 }
292
293 /**
294 * Displays the SSL error certificate dialog.
295 * @param view The target web-view.
296 * @param handler The SSL error handler responsible for cancelling the
297 * connection that resulted in an SSL error or proceeding per user request.
298 * @param error The SSL error object.
299 */
300 void showSSLCertificateOnError(
301 final WebView view, final SslErrorHandler handler,
302 final SslError error) {
303
304 final View certificateView =
305 inflateCertificateView(error.getCertificate());
306 if (certificateView == null) {
307 return;
308 }
309
310 LayoutInflater factory = LayoutInflater.from(mContext);
311
312 final LinearLayout placeholder =
313 (LinearLayout)certificateView.findViewById(R.id.placeholder);
314
315 if (error.hasError(SslError.SSL_UNTRUSTED)) {
316 LinearLayout ll = (LinearLayout)factory
317 .inflate(R.layout.ssl_warning, placeholder);
318 ((TextView)ll.findViewById(R.id.warning))
319 .setText(R.string.ssl_untrusted);
320 }
321
322 if (error.hasError(SslError.SSL_IDMISMATCH)) {
323 LinearLayout ll = (LinearLayout)factory
324 .inflate(R.layout.ssl_warning, placeholder);
325 ((TextView)ll.findViewById(R.id.warning))
326 .setText(R.string.ssl_mismatch);
327 }
328
329 if (error.hasError(SslError.SSL_EXPIRED)) {
330 LinearLayout ll = (LinearLayout)factory
331 .inflate(R.layout.ssl_warning, placeholder);
332 ((TextView)ll.findViewById(R.id.warning))
333 .setText(R.string.ssl_expired);
334 }
335
336 if (error.hasError(SslError.SSL_NOTYETVALID)) {
337 LinearLayout ll = (LinearLayout)factory
338 .inflate(R.layout.ssl_warning, placeholder);
339 ((TextView)ll.findViewById(R.id.warning))
340 .setText(R.string.ssl_not_yet_valid);
341 }
342
343 mSSLCertificateOnErrorHandler = handler;
344 mSSLCertificateOnErrorView = view;
345 mSSLCertificateOnErrorError = error;
346 mSSLCertificateOnErrorDialog =
347 new AlertDialog.Builder(mContext)
348 .setTitle(R.string.ssl_certificate).setIcon(
349 R.drawable.ic_dialog_browser_certificate_partially_secure)
350 .setView(certificateView)
351 .setPositiveButton(R.string.ok,
352 new DialogInterface.OnClickListener() {
353 public void onClick(DialogInterface dialog,
354 int whichButton) {
355 mSSLCertificateOnErrorDialog = null;
356 mSSLCertificateOnErrorView = null;
357 mSSLCertificateOnErrorHandler = null;
358 mSSLCertificateOnErrorError = null;
359
360 view.getWebViewClient().onReceivedSslError(
361 view, handler, error);
362 }
363 })
364 .setNeutralButton(R.string.page_info_view,
365 new DialogInterface.OnClickListener() {
366 public void onClick(DialogInterface dialog,
367 int whichButton) {
368 mSSLCertificateOnErrorDialog = null;
369
370 // do not clear the dialog state: we will
371 // need to show the dialog again once the
372 // user is done exploring the page-info details
373
374 showPageInfo(mController.getTabControl()
375 .getTabFromView(view),
376 true);
377 }
378 })
379 .setOnCancelListener(
380 new DialogInterface.OnCancelListener() {
381 public void onCancel(DialogInterface dialog) {
382 mSSLCertificateOnErrorDialog = null;
383 mSSLCertificateOnErrorView = null;
384 mSSLCertificateOnErrorHandler = null;
385 mSSLCertificateOnErrorError = null;
386
387 view.getWebViewClient().onReceivedSslError(
388 view, handler, error);
389 }
390 })
391 .show();
392 }
393
394 /**
395 * Inflates the SSL certificate view (helper method).
396 * @param certificate The SSL certificate.
397 * @return The resultant certificate view with issued-to, issued-by,
398 * issued-on, expires-on, and possibly other fields set.
399 * If the input certificate is null, returns null.
400 */
401 private View inflateCertificateView(SslCertificate certificate) {
402 if (certificate == null) {
403 return null;
404 }
405
406 LayoutInflater factory = LayoutInflater.from(mContext);
407
408 View certificateView = factory.inflate(
409 R.layout.ssl_certificate, null);
410
411 // issued to:
412 SslCertificate.DName issuedTo = certificate.getIssuedTo();
413 if (issuedTo != null) {
414 ((TextView) certificateView.findViewById(R.id.to_common))
415 .setText(issuedTo.getCName());
416 ((TextView) certificateView.findViewById(R.id.to_org))
417 .setText(issuedTo.getOName());
418 ((TextView) certificateView.findViewById(R.id.to_org_unit))
419 .setText(issuedTo.getUName());
420 }
421
422 // issued by:
423 SslCertificate.DName issuedBy = certificate.getIssuedBy();
424 if (issuedBy != null) {
425 ((TextView) certificateView.findViewById(R.id.by_common))
426 .setText(issuedBy.getCName());
427 ((TextView) certificateView.findViewById(R.id.by_org))
428 .setText(issuedBy.getOName());
429 ((TextView) certificateView.findViewById(R.id.by_org_unit))
430 .setText(issuedBy.getUName());
431 }
432
433 // issued on:
434 String issuedOn = formatCertificateDate(
435 certificate.getValidNotBeforeDate());
436 ((TextView) certificateView.findViewById(R.id.issued_on))
437 .setText(issuedOn);
438
439 // expires on:
440 String expiresOn = formatCertificateDate(
441 certificate.getValidNotAfterDate());
442 ((TextView) certificateView.findViewById(R.id.expires_on))
443 .setText(expiresOn);
444
445 return certificateView;
446 }
447
448 /**
449 * Formats the certificate date to a properly localized date string.
450 * @return Properly localized version of the certificate date string and
451 * the "" if it fails to localize.
452 */
453 private String formatCertificateDate(Date certificateDate) {
454 if (certificateDate == null) {
455 return "";
456 }
457 String formattedDate = DateFormat.getDateFormat(mContext)
458 .format(certificateDate);
459 if (formattedDate == null) {
460 return "";
461 }
462 return formattedDate;
463 }
464
465}