Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Queue History events while the session is inactive #2163

Merged
merged 3 commits into from
Nov 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -72,6 +74,7 @@ public class Session implements ContentBlocking.Delegate, GeckoSession.Navigatio
private transient UserAgentOverride mUserAgentOverride;

private SessionState mState;
private LinkedList<Runnable> mQueuedCalls = new LinkedList<>();
private transient GeckoSession.PermissionDelegate mPermissionDelegate;
private transient GeckoSession.PromptDelegate mPromptDelegate;
private transient GeckoSession.HistoryDelegate mHistoryDelegate;
Expand All @@ -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);
}
Expand Down Expand Up @@ -163,6 +167,7 @@ protected void shutdown() {
}
}

mQueuedCalls.clear();
mNavigationListeners.clear();
mProgressListeners.clear();
mContentListeners.clear();
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<Boolean> 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<Boolean> 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);
Expand All @@ -1230,8 +1283,31 @@ public GeckoResult<Boolean> onVisited(@NonNull GeckoSession aSession, @NonNull S
@UiThread
@Nullable
public GeckoResult<boolean[]> 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<boolean[]> 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[]{});
Expand Down