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