Merge pull request #287 from LukeOwncloud/setup_md
[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 super.onPageStarted(view, url, favicon);
74 }
75
76 @Override
77 public void onFormResubmission (WebView view, Message dontResend, Message resend) {
78 Log_OC.d(TAG, "onFormResubMission ");
79
80 // necessary to grant reload of last page when device orientation is changed after sending a form
81 resend.sendToTarget();
82 }
83
84 @Override
85 public boolean shouldOverrideUrlLoading(WebView view, String url) {
86 return false;
87 }
88
89 @Override
90 public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {
91 Log_OC.e(TAG, "onReceivedError : " + failingUrl + ", code " + errorCode + ", description: " + description);
92 if (!failingUrl.equals(mLastReloadedUrlAtError)) {
93 view.reload();
94 mLastReloadedUrlAtError = failingUrl;
95 } else {
96 mLastReloadedUrlAtError = null;
97 super.onReceivedError(view, errorCode, description, failingUrl);
98 }
99 }
100
101 @Override
102 public void onPageFinished (WebView view, String url) {
103 Log_OC.d(TAG, "onPageFinished : " + url);
104 mLastReloadedUrlAtError = null;
105 if (url.startsWith(mTargetUrl)) {
106 view.setVisibility(View.GONE);
107 CookieManager cookieManager = CookieManager.getInstance();
108 final String cookies = cookieManager.getCookie(url);
109 //Log_OC.d(TAG, "Cookies: " + cookies);
110 if (mListenerHandler != null && mListenerRef != null) {
111 // this is good idea because onPageFinished is not running in the UI thread
112 mListenerHandler.post(new Runnable() {
113 @Override
114 public void run() {
115 SsoWebViewClientListener listener = mListenerRef.get();
116 if (listener != null) {
117 listener.onSsoFinished(cookies);
118 }
119 }
120 });
121 }
122 }
123
124 }
125
126 /*
127 @Override
128 public void doUpdateVisitedHistory (WebView view, String url, boolean isReload) {
129 Log_OC.d(TAG, "doUpdateVisitedHistory : " + url);
130 }
131
132 @Override
133 public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
134 Log_OC.d(TAG, "onReceivedSslError : " + error);
135 }
136
137 @Override
138 public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) {
139 Log_OC.d(TAG, "onReceivedHttpAuthRequest : " + host);
140 }
141
142 @Override
143 public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
144 Log_OC.d(TAG, "shouldInterceptRequest : " + url);
145 return null;
146 }
147
148 @Override
149 public void onLoadResource (WebView view, String url) {
150 Log_OC.d(TAG, "onLoadResource : " + url);
151 }
152
153 @Override
154 public void onReceivedLoginRequest (WebView view, String realm, String account, String args) {
155 Log_OC.d(TAG, "onReceivedLoginRequest : " + realm + ", " + account + ", " + args);
156 }
157
158 @Override
159 public void onScaleChanged (WebView view, float oldScale, float newScale) {
160 Log_OC.d(TAG, "onScaleChanged : " + oldScale + " -> " + newScale);
161 super.onScaleChanged(view, oldScale, newScale);
162 }
163
164 @Override
165 public void onUnhandledKeyEvent (WebView view, KeyEvent event) {
166 Log_OC.d(TAG, "onUnhandledKeyEvent : " + event);
167 }
168
169 @Override
170 public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
171 Log_OC.d(TAG, "shouldOverrideKeyEvent : " + event);
172 return false;
173 }
174 */
175 }