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