90e8e6e6297d53aa7e3501c0ad5d1ab2edf92b31
[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 }
128 }
129
130
131 @Override
132 public void doUpdateVisitedHistory (WebView view, String url, boolean isReload) {
133 Log.d(TAG, "doUpdateVisitedHistory : " + url);
134 }
135
136 @Override
137 public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
138 Log.d(TAG, "onReceivedSslError : " + error);
139 handler.proceed();
140 }
141
142 @Override
143 public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) {
144 Log.d(TAG, "onReceivedHttpAuthRequest : " + host);
145 }
146
147 @Override
148 public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
149 Log.d(TAG, "shouldInterceptRequest : " + url);
150 return null;
151 }
152
153 @Override
154 public void onLoadResource (WebView view, String url) {
155 Log.d(TAG, "onLoadResource : " + url);
156 }
157
158 @Override
159 public void onReceivedLoginRequest (WebView view, String realm, String account, String args) {
160 Log.d(TAG, "onReceivedLoginRequest : " + realm + ", " + account + ", " + args);
161 }
162
163 @Override
164 public void onScaleChanged (WebView view, float oldScale, float newScale) {
165 Log.d(TAG, "onScaleChanged : " + oldScale + " -> " + newScale);
166 super.onScaleChanged(view, oldScale, newScale);
167 }
168
169 @Override
170 public void onUnhandledKeyEvent (WebView view, KeyEvent event) {
171 Log.d(TAG, "onUnhandledKeyEvent : " + event);
172 }
173
174 @Override
175 public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
176 Log.d(TAG, "shouldOverrideKeyEvent : " + event);
177 return false;
178 }
179
180 }