Bug 1443283 - Prevent crash when a null or open GeckoSession is registered with GeckoView.setSession() r=jchen,esawin draft
authorRandall Barker <rbarker@mozilla.com>
Mon, 05 Mar 2018 12:41:34 -0800
changeset 764508 6f0cd562a1fa5816bc656a822b2eee6afe1128aa
parent 764507 0cb2dbce1571e0d999ecbb7fd135902e6733f3cd
push id101774
push userbmo:rbarker@mozilla.com
push dateWed, 07 Mar 2018 21:12:53 +0000
reviewersjchen, esawin
bugs1443283
milestone60.0a1
Bug 1443283 - Prevent crash when a null or open GeckoSession is registered with GeckoView.setSession() r=jchen,esawin MozReview-Commit-ID: HV7xoz7fr6k
mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoView.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoView.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoView.java
@@ -187,28 +187,39 @@ public class GeckoView extends FrameLayo
      * @param color Cover color.
      */
     public void coverUntilFirstPaint(final int color) {
         if (mSurfaceView != null) {
             mSurfaceView.setBackgroundColor(color);
         }
     }
 
+    public void releaseSession() {
+        if (mSession == null) {
+            return;
+        }
+
+        mSession.releaseDisplay(mDisplay.release());
+        mSession.getOverscrollEdgeEffect().setInvalidationCallback(null);
+        mSession.getCompositorController().setFirstPaintCallback(null);
+        mSession = null;
+    }
+
     public void setSession(final GeckoSession session) {
         if (mSession != null && mSession.isOpen()) {
             throw new IllegalStateException("Current session is open");
         }
 
-        if (mSession != null) {
-            mSession.releaseDisplay(mDisplay.release());
-        }
-        if (session != null) {
-            mDisplay.acquire(session.acquireDisplay());
+        releaseSession();
+        mSession = session;
+        if (mSession == null) {
+          return;
         }
 
+        mDisplay.acquire(session.acquireDisplay());
         final Context context = getContext();
         session.getOverscrollEdgeEffect().setTheme(context);
         session.getOverscrollEdgeEffect().setInvalidationCallback(new Runnable() {
             @Override
             public void run() {
                 if (Build.VERSION.SDK_INT >= 16) {
                     GeckoView.this.postInvalidateOnAnimation();
                 } else {
@@ -227,18 +238,16 @@ public class GeckoView extends FrameLayo
         }
 
         session.getCompositorController().setFirstPaintCallback(new Runnable() {
             @Override
             public void run() {
                 coverUntilFirstPaint(Color.TRANSPARENT);
             }
         });
-
-        mSession = session;
     }
 
     public GeckoSession getSession() {
         return mSession;
     }
 
     public EventDispatcher getEventDispatcher() {
         return mSession.getEventDispatcher();
@@ -270,17 +279,19 @@ public class GeckoView extends FrameLayo
 
         super.onAttachedToWindow();
     }
 
     @Override
     public void onDetachedFromWindow() {
         super.onDetachedFromWindow();
 
-        mSession.getTextInputController().setView(this);
+        if (mSession != null) {
+          mSession.getTextInputController().setView(null);
+        }
 
         if (mStateSaved) {
             // If we saved state earlier, we don't want to close the window.
             return;
         }
 
         if (mSession != null && mSession.isOpen()) {
             mSession.closeWindow();