04f8d95d0cc938971af23de1b4d839b064b22242
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / accounts / SsoWebViewClient.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.oc_framework.accounts;
19
20 import java.lang.ref.WeakReference;
21
22 import android.graphics.Bitmap;
23 import android.net.http.SslError;
24 import android.os.Handler;
25 import android.os.Message;
26 import android.util.Log;
27 import android.view.KeyEvent;
28 import android.view.View;
29 import android.webkit.CookieManager;
30 import android.webkit.HttpAuthHandler;
31 import android.webkit.SslErrorHandler;
32 import android.webkit.WebResourceResponse;
33 import android.webkit.WebView;
34 import android.webkit.WebViewClient;
35
36
37 /**
38 * Custom {@link WebViewClient} client aimed to catch the end of a single-sign-on process
39 * running in the {@link WebView} that is attached to.
40 *
41 * Assumes that the single-sign-on is kept thanks to a cookie set at the end of the
42 * authentication process.
43 *
44 * @author David A. Velasco
45 */
46 public class SsoWebViewClient extends WebViewClient {
47
48 private static final String TAG = SsoWebViewClient.class.getSimpleName();
49
50 public interface SsoWebViewClientListener {
51 public void onSsoFinished(String sessionCookie);
52 }
53
54 private Handler mListenerHandler;
55 private WeakReference<SsoWebViewClientListener> mListenerRef;
56 private String mTargetUrl;
57 private String mLastReloadedUrlAtError;
58
59 public SsoWebViewClient (Handler listenerHandler, SsoWebViewClientListener listener) {
60 mListenerHandler = listenerHandler;
61 mListenerRef = new WeakReference<SsoWebViewClient.SsoWebViewClientListener>(listener);
62 mTargetUrl = "fake://url.to.be.set";
63 mLastReloadedUrlAtError = null;
64 }
65
66 public String getTargetUrl() {
67 return mTargetUrl;
68 }
69
70 public void setTargetUrl(String targetUrl) {
71 mTargetUrl = targetUrl;
72 }
73
74 @Override
75 public void onPageStarted (WebView view, String url, Bitmap favicon) {
76 Log.d(TAG, "onPageStarted : " + url);
77 super.onPageStarted(view, url, favicon);
78 }
79
80 @Override
81 public void onFormResubmission (WebView view, Message dontResend, Message resend) {
82 Log.d(TAG, "onFormResubMission ");
83
84 // necessary to grant reload of last page when device orientation is changed after sending a form
85 resend.sendToTarget();
86 }
87
88 @Override
89 public boolean shouldOverrideUrlLoading(WebView view, String url) {
90 return false;
91 }
92
93 @Override
94 public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {
95 Log.e(TAG, "onReceivedError : " + failingUrl + ", code " + errorCode + ", description: " + description);
96 if (!failingUrl.equals(mLastReloadedUrlAtError)) {
97 view.reload();
98 mLastReloadedUrlAtError = failingUrl;
99 } else {
100 mLastReloadedUrlAtError = null;
101 super.onReceivedError(view, errorCode, description, failingUrl);
102 }
103 }
104
105 @Override
106 public void onPageFinished (WebView view, String url) {
107 Log.d(TAG, "onPageFinished : " + url);
108 mLastReloadedUrlAtError = null;
109 if (url.startsWith(mTargetUrl)) {
110 view.setVisibility(View.GONE);
111 CookieManager cookieManager = CookieManager.getInstance();
112 final String cookies = cookieManager.getCookie(url);
113 Log.d(TAG, "Cookies: " + cookies);
114 if (mListenerHandler != null && mListenerRef != null) {
115 // this is good idea because onPageFinished is not running in the UI thread
116 mListenerHandler.post(new Runnable() {
117 @Override
118 public void run() {
119 SsoWebViewClientListener listener = mListenerRef.get();
120 if (listener != null) {
121 // Send Cookies to the listener
122 listener.onSsoFinished(cookies);
123 }
124 }
125 });
126 }
127 } else {
128 Log.d(TAG, "URL==> " + url + " mTarget==> " + mTargetUrl);
129 }
130 }
131
132
133 @Override
134 public void doUpdateVisitedHistory (WebView view, String url, boolean isReload) {
135 Log.d(TAG, "doUpdateVisitedHistory : " + url);
136 }
137
138 @Override
139 public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
140 Log.d(TAG, "onReceivedSslError : " + error);
141 handler.proceed();
142 }
143
144 @Override
145 public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) {
146 Log.d(TAG, "onReceivedHttpAuthRequest : " + host);
147 }
148
149 @Override
150 public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
151 Log.d(TAG, "shouldInterceptRequest : " + url);
152 return null;
153 }
154
155 @Override
156 public void onLoadResource (WebView view, String url) {
157 Log.d(TAG, "onLoadResource : " + url);
158 }
159
160 @Override
161 public void onReceivedLoginRequest (WebView view, String realm, String account, String args) {
162 Log.d(TAG, "onReceivedLoginRequest : " + realm + ", " + account + ", " + args);
163 }
164
165 @Override
166 public void onScaleChanged (WebView view, float oldScale, float newScale) {
167 Log.d(TAG, "onScaleChanged : " + oldScale + " -> " + newScale);
168 super.onScaleChanged(view, oldScale, newScale);
169 }
170
171 @Override
172 public void onUnhandledKeyEvent (WebView view, KeyEvent event) {
173 Log.d(TAG, "onUnhandledKeyEvent : " + event);
174 }
175
176 @Override
177 public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
178 Log.d(TAG, "shouldOverrideKeyEvent : " + event);
179 return false;
180 }
181
182 }