From b0863a25d9c1d82c999095a98b9f00ec615c9ad0 Mon Sep 17 00:00:00 2001 From: Manuel Martin Date: Tue, 5 Nov 2019 22:42:23 +0100 Subject: [PATCH] Queue History events while the session is inactive (#2163) * Queue History events while the session is inactive Revert new line * Better null checking and exception handling * Exception handling for real --- .../vrbrowser/browser/engine/Session.java | 90 +++++++++++++++++-- 1 file changed, 83 insertions(+), 7 deletions(-) diff --git a/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/Session.java b/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/Session.java index d9e9ef505..a667a898a 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/Session.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/Session.java @@ -9,7 +9,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.graphics.Bitmap; -import android.graphics.SurfaceTexture; +import android.os.Handler; import android.preference.PreferenceManager; import android.util.Log; import android.view.Surface; @@ -47,9 +47,11 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.LinkedList; +import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; +import static java.util.Objects.requireNonNull; import static org.mozilla.vrbrowser.utils.ServoUtils.createServoSession; import static org.mozilla.vrbrowser.utils.ServoUtils.isInstanceOfServoSession; import static org.mozilla.vrbrowser.utils.ServoUtils.isServoAvailable; @@ -72,6 +74,7 @@ public class Session implements ContentBlocking.Delegate, GeckoSession.Navigatio private transient UserAgentOverride mUserAgentOverride; private SessionState mState; + private LinkedList mQueuedCalls = new LinkedList<>(); private transient GeckoSession.PermissionDelegate mPermissionDelegate; private transient GeckoSession.PromptDelegate mPromptDelegate; private transient GeckoSession.HistoryDelegate mHistoryDelegate; @@ -82,6 +85,7 @@ public class Session implements ContentBlocking.Delegate, GeckoSession.Navigatio private transient byte[] mPrivatePage; private boolean mIsActive; + public interface BitmapChangedListener { void onBitmapChanged(Session aSession, Bitmap aBitmap); } @@ -163,6 +167,7 @@ protected void shutdown() { } } + mQueuedCalls.clear(); mNavigationListeners.clear(); mProgressListeners.clear(); mContentListeners.clear(); @@ -221,6 +226,13 @@ private void dumpState(VideoAvailabilityListener aListener) { aListener.onVideoAvailabilityChanged(mState.mMediaElements != null && mState.mMediaElements.size() > 0); } + private void flushQueuedEvents() { + for (Runnable call: mQueuedCalls) { + call.run(); + } + mQueuedCalls.clear(); + } + public void setPermissionDelegate(GeckoSession.PermissionDelegate aDelegate) { mPermissionDelegate = aDelegate; } @@ -567,9 +579,15 @@ public void goForward() { } public void setActive(boolean aActive) { + // Flush the events queued while the session was inactive + if (mState.mSession != null && !mIsActive && aActive) { + flushQueuedEvents(); + } + if (mState.mSession != null) { mState.mSession.setActive(aActive); } + mIsActive = aActive; for (SessionChangeListener listener: mSessionChangeListeners) { @@ -1212,16 +1230,51 @@ public void onMediaRemove(@NonNull GeckoSession aSession, @NonNull MediaElement @Override public void onHistoryStateChange(@NonNull GeckoSession aSession, @NonNull GeckoSession.HistoryDelegate.HistoryList historyList) { - if (mState.mSession == aSession && mHistoryDelegate != null) { - mHistoryDelegate.onHistoryStateChange(aSession, historyList); + if (mState.mSession == aSession) { + if (mHistoryDelegate != null) { + mHistoryDelegate.onHistoryStateChange(aSession, historyList); + + } else { + mQueuedCalls.add(() -> { + new Handler(mContext.getMainLooper()).postDelayed(() -> { + if (mHistoryDelegate != null) { + mHistoryDelegate.onHistoryStateChange(aSession, historyList); + } + }, 100); + }); + } } } @Nullable @Override public GeckoResult onVisited(@NonNull GeckoSession aSession, @NonNull String url, @Nullable String lastVisitedURL, int flags) { - if (mState.mSession == aSession && mHistoryDelegate != null) { - return mHistoryDelegate.onVisited(aSession, url, lastVisitedURL, flags); + if (mState.mSession == aSession) { + if (mHistoryDelegate != null) { + return mHistoryDelegate.onVisited(aSession, url, lastVisitedURL, flags); + + } else { + final GeckoResult response = new GeckoResult<>(); + mQueuedCalls.add(() -> { + if (mHistoryDelegate != null) { + try { + requireNonNull(mHistoryDelegate.onVisited(aSession, url, lastVisitedURL, flags)).then(aBoolean -> { + response.complete(aBoolean); + return null; + + }).exceptionally(throwable -> { + Log.d(LOGTAG, "Null GeckoResult from onVisited"); + return null; + }); + + } catch (NullPointerException e) { + e.printStackTrace(); + } + } + }); + + return response; + } } return GeckoResult.fromValue(false); @@ -1230,8 +1283,31 @@ public GeckoResult onVisited(@NonNull GeckoSession aSession, @NonNull S @UiThread @Nullable public GeckoResult getVisited(@NonNull GeckoSession aSession, @NonNull String[] urls) { - if (mState.mSession == aSession && mHistoryDelegate != null) { - return mHistoryDelegate.getVisited(aSession, urls); + if (mState.mSession == aSession) { + if (mHistoryDelegate != null) { + return mHistoryDelegate.getVisited(aSession, urls); + + } else { + final GeckoResult response = new GeckoResult<>(); + mQueuedCalls.add(() -> { + if (mHistoryDelegate != null) { + try { + requireNonNull(mHistoryDelegate.getVisited(aSession, urls)).then(aBoolean -> { + response.complete(aBoolean); + return null; + + }).exceptionally(throwable -> { + Log.d(LOGTAG, "Null GeckoResult from getVisited"); + return null; + }); + + } catch (NullPointerException e) { + e.printStackTrace(); + } + } + }); + return response; + } } return GeckoResult.fromValue(new boolean[]{});