Bug 1443283 - Prevent crash when a null or open GeckoSession is registered with GeckoView.setSession() r=jchen,esawin
MozReview-Commit-ID: HV7xoz7fr6k
--- 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();