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

Commit

Permalink
Refactor Session to include GeckoDisplay
Browse files Browse the repository at this point in the history
  • Loading branch information
bluemarvin committed Nov 14, 2019
1 parent ebf452b commit 1ca05b9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class Session implements ContentBlocking.Delegate, GeckoSession.Navigatio
private transient LinkedList<VideoAvailabilityListener> mVideoAvailabilityListeners;
private transient LinkedList<BitmapChangedListener> mBitmapChangedListeners;
private transient LinkedList<GeckoSession.SelectionActionDelegate> mSelectionActionListeners;
private transient UserAgentOverride mUserAgentOverride;
private static transient UserAgentOverride mUserAgentOverride;

private SessionState mState;
private LinkedList<Runnable> mQueuedCalls = new LinkedList<>();
Expand All @@ -81,7 +81,6 @@ public class Session implements ContentBlocking.Delegate, GeckoSession.Navigatio
private transient SharedPreferences mPrefs;
private transient GeckoRuntime mRuntime;
private transient byte[] mPrivatePage;
private boolean mIsActive;


public interface BitmapChangedListener {
Expand Down Expand Up @@ -138,6 +137,7 @@ protected void shutdown() {
if (mState.mSession.isOpen()) {
mState.mSession.close();
}
mState.mDisplay = null;
mState.mSession = null;
}

Expand Down Expand Up @@ -324,15 +324,15 @@ private void cleanSessionListeners(GeckoSession aSession) {
}

public void suspend() {
if (mIsActive) {
if (mState.mIsActive) {
Log.e(LOGTAG, "Active Sessions can not be suspended");
return;
}
if (mState.mSession == null) {
return;
}
Log.d(LOGTAG, "Suspending Session: " + mState.mId);
closeSession(mState.mSession);
closeSession(mState);
mState.mSession = null;
}

Expand All @@ -350,7 +350,7 @@ private void restore() {
if (!mState.mSession.isOpen()) {
mState.mSession.open(mRuntime);
}

if (mState.mSessionState != null) {
mState.mSession.restoreState(mState.mSessionState);
}
Expand All @@ -363,7 +363,7 @@ private void restore() {
(mState.mSessionState != null && mState.mSessionState.size() == 0)) {
loadHomePage();
} else if (mState.mUri != null && mState.mUri.contains(".youtube.com")) {
mState.mSession.loadUri(mState.mUri);
mState.mSession.loadUri(mState.mUri, GeckoSession.LOAD_FLAGS_REPLACE_HISTORY);
}

dumpAllState();
Expand Down Expand Up @@ -413,24 +413,35 @@ private void recreateSession() {
mState.mSession.restoreState(previous.mSessionState);
}
if (previous.mSession != null) {
closeSession(previous.mSession);
closeSession(previous);
}

for (SessionChangeListener listener : mSessionChangeListeners) {
listener.onCurrentSessionChange(previous.mSession, mState.mSession);
}
}

private void closeSession(@NonNull GeckoSession aSession) {
cleanSessionListeners(aSession);
aSession.setActive(false);
aSession.stop();
aSession.close();
mIsActive = false;
private void closeSession(@NonNull SessionState aState) {
if (aState.mSession == null) {
return;
}
cleanSessionListeners(aState.mSession);
aState.mSession.setActive(false);
aState.mSession.stop();
if (aState.mDisplay != null) {
aState.mDisplay.surfaceDestroyed();
aState.mSession.releaseDisplay(aState.mDisplay);
aState.mDisplay = null;
}
aState.mSession.close();
aState.mIsActive = false;
}

public void captureBitmap(@NonNull GeckoDisplay aDisplay) {
aDisplay.capturePixels().then(bitmap -> {
public void captureBitmap() {
if (mState.mDisplay == null) {
return;
}
mState.mDisplay.capturePixels().then(bitmap -> {
if (bitmap != null) {
BitmapCache.getInstance(mContext).scaleBitmap(bitmap, 500, 280).thenAccept(scaledBitmap -> {
BitmapCache.getInstance(mContext).addBitmap(getId(), scaledBitmap);
Expand Down Expand Up @@ -512,10 +523,16 @@ public Boolean isHomeUri(String aUri) {
}

public String getCurrentUri() {
if (mState.mUri == null) {
return "";
}
return mState.mUri;
}

public String getCurrentTitle() {
if (mState.mTitle == null) {
return "";
}
return mState.mTitle;
}

Expand Down Expand Up @@ -578,7 +595,7 @@ public void goForward() {

public void setActive(boolean aActive) {
// Flush the events queued while the session was inactive
if (mState.mSession != null && !mIsActive && aActive) {
if (mState.mSession != null && !mState.mIsActive && aActive) {
flushQueuedEvents();
}

Expand All @@ -588,7 +605,7 @@ public void setActive(boolean aActive) {
restore();
}

mIsActive = aActive;
mState.mIsActive = aActive;

for (SessionChangeListener listener: mSessionChangeListeners) {
listener.onActiveStateChange(this, aActive);
Expand Down Expand Up @@ -642,7 +659,7 @@ public void toggleServo() {
.build();

mState = createSession(settings, SESSION_OPEN);
closeSession(previous.mSession);
closeSession(previous);
loadUri(uri);
}

Expand Down Expand Up @@ -687,7 +704,7 @@ public int getUaMode() {
}

public boolean isActive() {
return mIsActive;
return mState.mIsActive;
}

private static final String M_PREFIX = "m.";
Expand Down Expand Up @@ -1401,4 +1418,32 @@ public void onActiveStateChange(Session aSession, boolean aActive) {
}
}
}

// Display functions
public void releaseDisplay() {
surfaceDestroyed();
if (mState.mDisplay != null) {
if (mState.mSession != null) {
mState.mSession.releaseDisplay(mState.mDisplay);
}
mState.mDisplay = null;
}
}

public void surfaceDestroyed() {
if (mState.mDisplay != null) {
mState.mDisplay.surfaceDestroyed();
}
}

public void surfaceChanged(@NonNull final Surface surface, final int left, final int top,
final int width, final int height) {
if (mState.mSession == null) {
return;
}
if (mState.mDisplay == null) {
mState.mDisplay = mState.mSession.acquireDisplay();
}
mState.mDisplay.surfaceChanged(surface, left, top, width, height);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.google.gson.stream.JsonWriter;

import org.json.JSONException;
import org.mozilla.geckoview.GeckoDisplay;
import org.mozilla.geckoview.GeckoSession;
import org.mozilla.vrbrowser.browser.Media;

Expand All @@ -24,6 +25,7 @@

@JsonAdapter(SessionState.SessionStateAdapterFactory.class)
public class SessionState {
public transient boolean mIsActive;
public boolean mCanGoBack;
public boolean mCanGoForward;
public boolean mIsLoading;
Expand All @@ -34,6 +36,7 @@ public class SessionState {
public String mTitle = "";
public transient boolean mFullScreen;
public transient GeckoSession mSession;
public transient GeckoDisplay mDisplay;
public SessionSettings mSettings;
public transient ArrayList<Media> mMediaElements = new ArrayList<>();
@JsonAdapter(SessionState.GeckoSessionStateAdapter.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ default void onBookmarksShown(WindowWidget aWindow) {}
default void onBookmarksHidden(WindowWidget aWindow) {}
}

private GeckoDisplay mDisplay;
private Surface mSurface;
private int mWidth;
private int mHeight;
Expand Down Expand Up @@ -521,15 +520,15 @@ public void hideHistory(boolean switchSurface) {
}

public void pauseCompositor() {
if (mDisplay == null) {
if (mSession == null) {
return;
}

mDisplay.surfaceDestroyed();
mSession.surfaceDestroyed();
}

public void resumeCompositor() {
if (mDisplay == null) {
if (mSession == null) {
return;
}
if (mSurface == null) {
Expand Down Expand Up @@ -712,11 +711,6 @@ public void setSurfaceTexture(SurfaceTexture aTexture, final int aWidth, final i
mTexture = aTexture;
aTexture.setDefaultBufferSize(aWidth, aHeight);
mSurface = new Surface(aTexture);
if (mDisplay == null) {
mDisplay = session.acquireDisplay();
} else {
Log.e(LOGTAG, "GeckoDisplay was not null in BrowserWidget.setSurfaceTexture()");
}
callSurfaceChanged();
}
}
Expand All @@ -727,32 +721,21 @@ public void setSurface(Surface aSurface, final int aWidth, final int aHeight, Ru
super.setSurface(aSurface, aWidth, aHeight, aFirstDrawCallback);

} else {
GeckoSession session = mSession.getGeckoSession();
if (session == null) {
return;
}
mWidth = aWidth;
mHeight = aHeight;
mSurface = aSurface;
mFirstDrawCallback = aFirstDrawCallback;
if (mDisplay == null) {
mDisplay = session.acquireDisplay();
} else {
Log.e(LOGTAG, "GeckoDisplay was not null in BrowserWidget.setSurfaceTexture()");
}
if (mSurface != null) {
callSurfaceChanged();
} else {
mDisplay.surfaceDestroyed();
mSession.surfaceDestroyed();
}
}
}

private void callSurfaceChanged() {
if (mDisplay != null) {
mDisplay.surfaceChanged(mSurface, mBorderWidth, mBorderWidth, mWidth - mBorderWidth * 2, mHeight - mBorderWidth * 2);
}
if (mSession != null) {
mSession.surfaceChanged(mSurface, mBorderWidth, mBorderWidth, mWidth - mBorderWidth * 2, mHeight - mBorderWidth * 2);
mSession.updateLastUse();
}
}
Expand Down Expand Up @@ -938,12 +921,8 @@ public void releaseWidget() {
cleanListeners(mSession);
GeckoSession session = mSession.getGeckoSession();

if (mDisplay != null) {
mDisplay.surfaceDestroyed();
if (session != null) {
session.releaseDisplay(mDisplay);
}
mDisplay = null;
if (mSession != null) {
mSession.releaseDisplay();
}
if (session != null) {
session.getTextInput().setView(null);
Expand Down Expand Up @@ -996,8 +975,12 @@ public void setVisible(boolean aVisible) {
if (mWidgetPlacement.visible == aVisible) {
return;
}

if (!mIsInVRVideoMode) {
mSession.setActive(aVisible);
if (aVisible) {
callSurfaceChanged();
}
}
mWidgetPlacement.visible = aVisible;
if (!aVisible) {
Expand Down Expand Up @@ -1030,6 +1013,7 @@ public void setSession(@NonNull Session aSession) {
Session oldSession = mSession;
if (oldSession != null) {
cleanListeners(oldSession);
oldSession.releaseDisplay();
}

mSession = aSession;
Expand All @@ -1046,15 +1030,6 @@ public void setSession(@NonNull Session aSession) {
hideLibraryPanels();
}

public void releaseDisplay(GeckoSession aSession) {
if (aSession != null && mDisplay != null) {
Log.d(LOGTAG, "Detach from previous session: " + aSession.hashCode());
aSession.getTextInput().setView(null);
mDisplay.surfaceDestroyed();
aSession.releaseDisplay(mDisplay);
mDisplay = null;
}
}

public void showPopUps() {
if (mPromptDelegate != null) {
Expand All @@ -1073,12 +1048,9 @@ public boolean hasPendingPopUps() {
// Session.GeckoSessionChange
@Override
public void onCurrentSessionChange(GeckoSession aOldSession, GeckoSession aSession) {
Log.d(LOGTAG, "onCurrentSessionChange: " + this.toString());
Log.d(LOGTAG, "onCurrentSessionChange: " + this.hashCode());

releaseDisplay(aOldSession);
mWidgetManager.setIsServoSession(isInstanceOfServoSession(aSession));

mDisplay = aSession.acquireDisplay();
Log.d(LOGTAG, "surfaceChanged: " + aSession.hashCode());
callSurfaceChanged();
aSession.getTextInput().setView(this);
Expand Down Expand Up @@ -1551,9 +1523,7 @@ public void onPageStop(@NonNull GeckoSession aSession, boolean b) {
}

public void captureImage() {
if (mDisplay != null) {
mSession.captureBitmap(mDisplay);
}
mSession.captureBitmap();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1137,8 +1137,8 @@ public void onTabSelect(Session aTab) {
// Move session between windows
Session moveFrom = windowToMove.getSession();
Session moveTo = targetWindow.getSession();
windowToMove.releaseDisplay(moveFrom.getGeckoSession());
targetWindow.releaseDisplay(moveTo.getGeckoSession());
moveFrom.surfaceDestroyed();
moveTo.surfaceDestroyed();
windowToMove.setSession(moveTo);
targetWindow.setSession(moveFrom);
SessionStore.get().setActiveSession(targetWindow.getSession());
Expand Down

0 comments on commit 1ca05b9

Please sign in to comment.