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