Upstream version 5.34.97.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / src / org / xwalk / core / client / XWalkDefaultClient.java
1 // Copyright (c) 2013 Intel Corporation. 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 package org.xwalk.core.client;
6
7 import android.app.Activity;
8 import android.app.AlertDialog;
9 import android.content.Context;
10 import android.content.DialogInterface;
11 import android.net.http.SslError;
12 import android.net.Uri;
13 import android.os.Message;
14 import android.view.View;
15 import android.view.ViewGroup;
16 import android.view.WindowManager;
17 import android.widget.EditText;
18 import android.widget.FrameLayout;
19 import android.widget.LinearLayout;
20 import android.widget.TextView;
21
22 import org.xwalk.core.HttpAuthDatabase;
23 import org.xwalk.core.XWalkHttpAuthHandler;
24 import org.xwalk.core.R;
25 import org.xwalk.core.SslErrorHandler;
26 import org.xwalk.core.XWalkClient;
27 import org.xwalk.core.XWalkView;
28
29 public class XWalkDefaultClient extends XWalkClient {
30
31     // Strings for displaying Dialog.
32     private static String mAlertTitle;
33     private static String mSslAlertTitle;
34     private static String mOKButton;
35     private static String mCancelButton;
36
37     private Context mContext;
38     private AlertDialog mDialog;
39     private XWalkView mView;
40     private HttpAuthDatabase mDatabase;
41     private static final String HTTP_AUTH_DATABASE_FILE = "http_auth.db";
42
43     public XWalkDefaultClient(Context context, XWalkView view) {
44         mDatabase = new HttpAuthDatabase(context.getApplicationContext(), HTTP_AUTH_DATABASE_FILE);
45         mContext = context;
46         mView = view;
47     }
48
49     /***
50      * Retrieve the HTTP authentication username and password for a given
51      * host & realm pair.
52      *
53      * @param host The host for which the credentials apply.
54      * @param realm The realm for which the credentials apply.
55      * @return String[] if found, String[0] is username, which can be null and
56      *         String[1] is password. Return null if it can't find anything.
57      */
58     public String[] getHttpAuthUsernamePassword(String host, String realm) {
59         return mDatabase.getHttpAuthUsernamePassword(host, realm);
60     }
61
62     @Override
63     public void onReceivedError(XWalkView view, int errorCode,
64             String description, String failingUrl) {
65         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
66         dialogBuilder.setTitle(android.R.string.dialog_alert_title)
67                 .setMessage(description)
68                 .setCancelable(false)
69                 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
70                     @Override
71                     public void onClick(DialogInterface dialog, int which) {
72                         dialog.dismiss();
73                     }
74                 });
75         mDialog = dialogBuilder.create();
76         mDialog.show();
77     }
78
79     @Override
80     public void onReceivedSslError(XWalkView view, SslErrorHandler handler,
81             SslError error) {
82         final SslErrorHandler sslHandler = handler;
83         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
84         dialogBuilder.setTitle(R.string.ssl_alert_title)
85                 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
86                     @Override
87                     public void onClick(DialogInterface dialog, int which) {
88                         sslHandler.proceed();
89                         dialog.dismiss();
90                     }
91                 })
92                 .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
93                     @Override
94                     public void onClick(DialogInterface dialog, int which) {
95                         sslHandler.cancel();
96                         dialog.cancel();
97                     }
98                 });
99         mDialog = dialogBuilder.create();
100         mDialog.show();
101     }
102
103     /***
104      * Set the HTTP authentication credentials for a given host and realm.
105      *
106      * @param host The host for the credentials.
107      * @param realm The realm for the credentials.
108      * @param username The username for the password. If it is null, it means
109      *                 password can't be saved.
110      * @param password The password
111      */
112     public void setHttpAuthUsernamePassword(String host, String realm,
113             String username, String password) {
114         mDatabase.setHttpAuthUsernamePassword(host, realm, username, password);
115     }
116
117     private void showHttpAuthDialog( final XWalkHttpAuthHandler handler,
118             final String host, final String realm) {
119         LinearLayout layout = new LinearLayout(mContext);
120         final EditText userNameEditText = new EditText(mContext);
121         final EditText passwordEditText = new EditText(mContext);
122         layout.setOrientation(LinearLayout.VERTICAL);
123         layout.setPaddingRelative(10, 0, 10, 20);
124         userNameEditText.setHint(R.string.http_auth_user_name);
125         passwordEditText.setHint(R.string.http_auth_password);
126         layout.addView(userNameEditText);
127         layout.addView(passwordEditText);
128
129         final Activity curActivity = mView.getActivity();
130         AlertDialog.Builder httpAuthDialog = new AlertDialog.Builder(curActivity);
131         httpAuthDialog.setTitle(R.string.http_auth_title)
132                 .setView(layout)
133                 .setCancelable(false)
134                 .setPositiveButton(R.string.http_auth_log_in, new DialogInterface.OnClickListener() {
135                     public void onClick(DialogInterface dialog, int whichButton) {
136                         String userName = userNameEditText.getText().toString();
137                         String password = passwordEditText.getText().toString();
138                         setHttpAuthUsernamePassword(host, realm, userName, password);
139                         handler.proceed(userName, password);
140                         dialog.dismiss();
141                     }
142                 })
143                 .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
144                     public void onClick(DialogInterface dialog, int whichButton) {
145                         handler.cancel();
146                         dialog.dismiss();
147                     }
148                 })
149                 .create().show();
150     }
151
152     @Override
153     public void onReceivedHttpAuthRequest(XWalkView view,
154             XWalkHttpAuthHandler handler, String host, String realm) {
155         if (view != null) {
156             showHttpAuthDialog(handler, host, realm);
157         }
158     }
159
160     @Override
161     public void onCloseWindow(XWalkView view) {
162         if (view != null && view.getActivity() != null) {
163             view.getActivity().finish();
164         }
165     }
166 }