Skip to content

Commit

Permalink
Allow popup tabs to live long enough to initialize before suspending (M…
Browse files Browse the repository at this point in the history
  • Loading branch information
bluemarvin authored and Alexandre Lissy committed Jan 21, 2020
1 parent 7be09df commit 46df150
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
Expand Up @@ -62,7 +62,7 @@ public class Session implements ContentBlocking.Delegate, GeckoSession.Navigatio

private static final String LOGTAG = SystemUtils.createLogtag(Session.class);
private static UserAgentOverride sUserAgentOverride;

private static final long KEEP_ALIVE_DURATION_MS = 1000; // 1 second.

private transient LinkedList<GeckoSession.NavigationDelegate> mNavigationListeners;
private transient LinkedList<GeckoSession.ProgressDelegate> mProgressListeners;
Expand All @@ -83,7 +83,7 @@ public class Session implements ContentBlocking.Delegate, GeckoSession.Navigatio
private transient GeckoRuntime mRuntime;
private transient byte[] mPrivatePage;
private transient boolean mFirstContentfulPaint;

private transient long mKeepAlive;

public interface BitmapChangedListener {
void onBitmapChanged(Session aSession, Bitmap aBitmap);
Expand Down Expand Up @@ -334,6 +334,11 @@ public void suspend() {
if (mState.mSession == null) {
return;
}
if (mKeepAlive > System.currentTimeMillis()) {
Log.e(LOGTAG, "Unable to suspend activity with active keep alive time.");
return;
}

Log.d(LOGTAG, "Suspending Session: " + mState.mId);
closeSession(mState);
mState.mSession = null;
Expand Down Expand Up @@ -688,6 +693,7 @@ public String getId() {
return mState.mId;
}


public boolean isPrivateMode() {
if (mState.mSession != null) {
return mState.mSession.getSettings().getUsePrivateMode();
Expand Down Expand Up @@ -916,10 +922,12 @@ public void onCanGoForward(@NonNull GeckoSession aSession, boolean aCanGoForward

@Override
public GeckoResult<GeckoSession> onNewSession(@NonNull GeckoSession aSession, @NonNull String aUri) {
Log.d(LOGTAG, "Session onStackSession: " + aUri);
mKeepAlive = System.currentTimeMillis() + KEEP_ALIVE_DURATION_MS;
Log.d(LOGTAG, "onNewSession: " + aUri);

Session session = SessionStore.get().createSession(mState.mSettings, SESSION_DO_NOT_OPEN);
session.mState.mParentId = mState.mId;
session.mKeepAlive = mKeepAlive;
for (SessionChangeListener listener: new LinkedList<>(mSessionChangeListeners)) {
listener.onStackSession(session);
}
Expand Down Expand Up @@ -1427,6 +1435,16 @@ public void onActiveStateChange(Session aSession, boolean aActive) {
}
}

@Override
public void onStackSession(Session aSession) {
if (aSession.equals(this)) {
return;
}
for (SessionChangeListener listener : mSessionChangeListeners) {
listener.onStackSession(aSession);
}
}

// Display functions
public void releaseDisplay() {
surfaceDestroyed();
Expand Down
Expand Up @@ -229,7 +229,12 @@ public Session getActiveSession() {
public ArrayList<Session> getSortedSessions(boolean aPrivateMode) {
ArrayList<Session> result = new ArrayList<>(mSessions);
result.removeIf(session -> session.isPrivateMode() != aPrivateMode);
result.sort((o1, o2) -> (int)(o2.getLastUse() - o1.getLastUse()));
result.sort((o1, o2) -> {
if (o2.getLastUse() < o1.getLastUse()) {
return -1;
}
return o2.getLastUse() == o1.getLastUse() ? 0 : 1;
});
return result;
}

Expand Down
Expand Up @@ -931,6 +931,17 @@ public void removeWindowListener(WindowListener aListener) {
mListeners.remove(aListener);
}

public void waitForFirstPaint() {
setFirstPaintReady(false);
setFirstDrawCallback(() -> {
if (!isFirstPaintReady()) {
setFirstPaintReady(true);
mWidgetManager.updateWidget(WindowWidget.this);
}
});
mWidgetManager.updateWidget(this);
}

@Override
public void handleResizeEvent(float aWorldWidth, float aWorldHeight) {
int width = getWindowWidth(aWorldWidth);
Expand Down Expand Up @@ -1100,6 +1111,8 @@ public void onCurrentSessionChange(GeckoSession aOldSession, GeckoSession aSessi
@Override
public void onStackSession(Session aSession) {
// e.g. tab opened via window.open()
aSession.updateLastUse();
waitForFirstPaint();
Session current = mSession;
setSession(aSession);
SessionStore.get().setActiveSession(aSession);
Expand Down
Expand Up @@ -982,14 +982,7 @@ public void onTabsClicked() {

private void setFirstPaint(@NonNull final WindowWidget aWindow, @NonNull final Session aSession) {
if (aSession.getGeckoSession() == null) {
aWindow.setFirstPaintReady(false);
aWindow.setFirstDrawCallback(() -> {
if (!aWindow.isFirstPaintReady()) {
aWindow.setFirstPaintReady(true);
mWidgetManager.updateWidget(aWindow);
}
});
mWidgetManager.updateWidget(aWindow);
aWindow.waitForFirstPaint();
} else {
// If the new session has a GeckoSession there won't be a first paint event.
// So trigger the first paint callback in case the window is grayed out
Expand Down

0 comments on commit 46df150

Please sign in to comment.