8d80e9b7a543c2d70a03a293bf2fb8b2b98190da
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / 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.authentication;
19
20 import java.lang.ref.WeakReference;
21
22 import com.owncloud.android.utils.Log_OC;
23
24
25 import android.graphics.Bitmap;
26 import android.os.Handler;
27 import android.os.Message;
28 import android.view.View;
29 import android.webkit.CookieManager;
30 import android.webkit.WebView;
31 import android.webkit.WebViewClient;
32
33
34 /**
35 * Custom {@link WebViewClient} client aimed to catch the end of a single-sign-on process
36 * running in the {@link WebView} that is attached to.
37 *
38 * Assumes that the single-sign-on is kept thanks to a cookie set at the end of the
39 * authentication process.
40 *
41 * @author David A. Velasco
42 */
43 public class SsoWebViewClient extends WebViewClient {
44
45 private static final String TAG = SsoWebViewClient.class.getSimpleName();
46
47 public interface SsoWebViewClientListener {
48 public void onSsoFinished(String sessionCookie);
49 }
50
51 private Handler mListenerHandler;
52 private WeakReference<SsoWebViewClientListener> mListenerRef;
53 private String mTargetUrl;
54 private String mLastReloadedUrlAtError;
55
56 public SsoWebViewClient (Handler listenerHandler, SsoWebViewClientListener listener) {
57 mListenerHandler = listenerHandler;
58 mListenerRef = new WeakReference<SsoWebViewClient.SsoWebViewClientListener>(listener);
59 mTargetUrl = "fake://url.to.be.set";
60 mLastReloadedUrlAtError = null;
61 }
62
63 public String getTargetUrl() {
64 return mTargetUrl;
65 }
66
67 public void setTargetUrl(String targetUrl) {
68 mTargetUrl = targetUrl;
69 }
70
71 @Override
72 public void onPageStarted (WebView view, String url, Bitmap favicon) {
73 Log_OC.d(TAG, "onPageStarted : " + url);
74 super.onPageStarted(view, url, favicon);
75 }
76
77 @Override
78 public void onFormResubmission (WebView view, Message dontResend, Message resend) {
79 Log_OC.d(TAG, "onFormResubMission ");
80
81 // necessary to grant reload of last page when device orientation is changed after sending a form
82 resend.sendToTarget();
83 }
84
85 @Override
86 public boolean shouldOverrideUrlLoading(WebView view, String url) {
87 return false;
88 }
89
90 @Override
91 public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {
92 Log_OC.e(TAG, "onReceivedError : " + failingUrl + ", code " + errorCode + ", description: " + description);
93 if (!failingUrl.equals(mLastReloadedUrlAtError)) {
94 view.reload();
95 mLastReloadedUrlAtError = failingUrl;
96 } else {
97 mLastReloadedUrlAtError = null;
98 super.onReceivedError(view, errorCode, description, failingUrl);
99 }
100 }
101
102 @Override
103 public void onPageFinished (WebView view, String url) {
104 Log_OC.d(TAG, "onPageFinished : " + url);
105 mLastReloadedUrlAtError = null;
106 if (url.startsWith(mTargetUrl)) {
107 view.setVisibility(View.GONE);
108 CookieManager cookieManager = CookieManager.getInstance();
109 final String cookies = cookieManager.getCookie(url);
110 //Log_OC.d(TAG, "Cookies: " + cookies);
111 if (mListenerHandler != null && mListenerRef != null) {
112 // this is good idea because onPageFinished is not running in the UI thread
113 mListenerHandler.post(new Runnable() {
114 @Override
115 public void run() {
116 SsoWebViewClientListener listener = mListenerRef.get();
117 if (listener != null) {
118 listener.onSsoFinished(cookies);
119 }
120 }
121 });
122 }
123 }
124
125 }
126
127 /*
128 @Override
129 public void doUpdateVisitedHistory (WebView view, String url, boolean isReload) {
130 Log_OC.d(TAG, "doUpdateVisitedHistory : " + url);
131 }
132
133 @Override
134 public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
135 Log_OC.d(TAG, "onReceivedSslError : " + error);
136 }
137
138 @Override
139 public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) {
140 Log_OC.d(TAG, "onReceivedHttpAuthRequest : " + host);
141 }
142
143 @Override
144 public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
145 Log_OC.d(TAG, "shouldInterceptRequest : " + url);
146 return null;
147 }
148
149 @Override
150 public void onLoadResource (WebView view, String url) {
151 Log_OC.d(TAG, "onLoadResource : " + url);
152 }
153
154 @Override
155 public void onReceivedLoginRequest (WebView view, String realm, String account, String args) {
156 Log_OC.d(TAG, "onReceivedLoginRequest : " + realm + ", " + account + ", " + args);
157 }
158
159 @Override
160 public void onScaleChanged (WebView view, float oldScale, float newScale) {
161 Log_OC.d(TAG, "onScaleChanged : " + oldScale + " -> " + newScale);
162 super.onScaleChanged(view, oldScale, newScale);
163 }
164
165 @Override
166 public void onUnhandledKeyEvent (WebView view, KeyEvent event) {
167 Log_OC.d(TAG, "onUnhandledKeyEvent : " + event);
168 }
169
170 @Override
171 public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
172 Log_OC.d(TAG, "shouldOverrideKeyEvent : " + event);
173 return false;
174 }
175 */
176 }