SsoWebViewClient.java 5.9 KB

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