Fixed comments in PR#246
[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: " + 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 super.onReceivedError(view, errorCode, description, failingUrl);
115 }
116 }
117
118 @Override
119 public void onPageFinished (WebView view, String url) {
120 Log_OC.d(TAG, "onPageFinished : " + url);
121 mLastReloadedUrlAtError = null;
122 }
123
124 /*
125 @Override
126 public void doUpdateVisitedHistory (WebView view, String url, boolean isReload) {
127 Log_OC.d(TAG, "doUpdateVisitedHistory : " + url);
128 }
129
130 @Override
131 public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
132 Log_OC.d(TAG, "onReceivedSslError : " + error);
133 }
134
135 @Override
136 public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) {
137 Log_OC.d(TAG, "onReceivedHttpAuthRequest : " + host);
138 }
139
140 @Override
141 public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
142 Log_OC.d(TAG, "shouldInterceptRequest : " + url);
143 return null;
144 }
145
146 @Override
147 public void onLoadResource (WebView view, String url) {
148 Log_OC.d(TAG, "onLoadResource : " + url);
149 }
150
151 @Override
152 public void onReceivedLoginRequest (WebView view, String realm, String account, String args) {
153 Log_OC.d(TAG, "onReceivedLoginRequest : " + realm + ", " + account + ", " + args);
154 }
155
156 @Override
157 public void onScaleChanged (WebView view, float oldScale, float newScale) {
158 Log_OC.d(TAG, "onScaleChanged : " + oldScale + " -> " + newScale);
159 super.onScaleChanged(view, oldScale, newScale);
160 }
161
162 @Override
163 public void onUnhandledKeyEvent (WebView view, KeyEvent event) {
164 Log_OC.d(TAG, "onUnhandledKeyEvent : " + event);
165 }
166
167 @Override
168 public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
169 Log_OC.d(TAG, "shouldOverrideKeyEvent : " + event);
170 return false;
171 }
172 */
173 }