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

Commit

Permalink
Refactor session restore code. Fix nits.
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro committed Oct 14, 2019
1 parent 954191f commit 46fad5c
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ void loadFromIntent(final Intent intent) {
uri = Uri.parse(intent.getExtras().getString("url"));
}

Session activeStore = SessionStore.get().getActiveSession();
Session activeSession = SessionStore.get().getActiveSession();

Bundle extras = intent.getExtras();
if (extras != null && extras.containsKey("homepage")) {
Expand All @@ -465,10 +465,10 @@ void loadFromIntent(final Intent intent) {
}
}

if (activeStore != null) {
if (activeSession != null) {
if (uri != null) {
Log.d(LOGTAG, "Loading URI from intent: " + uri.toString());
activeStore.loadUri(uri.toString());
activeSession.loadUri(uri.toString());
} else {
mWindows.getFocusedWindow().loadHomeIfNotRestored();
}
Expand Down Expand Up @@ -998,8 +998,8 @@ private void handlePoorPerformance() {
if (window == null) {
return;
}
final String originalUrl = window.getSession().getCurrentUri();
if (mPoorPerformanceWhiteList.contains(originalUrl)) {
final String originalUri = window.getSession().getCurrentUri();
if (mPoorPerformanceWhiteList.contains(originalUri)) {
return;
}
window.getSession().loadHomePage();
Expand All @@ -1008,8 +1008,8 @@ private void handlePoorPerformance() {
@Override
public void confirm(int index) {
if (index == GeckoSession.PromptDelegate.ButtonPrompt.Type.NEGATIVE) {
mPoorPerformanceWhiteList.add(originalUrl);
window.getSession().loadUri(originalUrl);
mPoorPerformanceWhiteList.add(originalUri);
window.getSession().loadUri(originalUri);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public class Session implements ContentBlocking.Delegate, GeckoSession.Navigatio
SharedPreferences.OnSharedPreferenceChangeListener {

private static final String LOGTAG = SystemUtils.createLogtag(Session.class);
// You can test a local file using: "resource://android/assets/webvr/index.html"

private transient LinkedList<GeckoSession.NavigationDelegate> mNavigationListeners;
private transient LinkedList<GeckoSession.ProgressDelegate> mProgressListeners;
Expand All @@ -69,7 +68,6 @@ public class Session implements ContentBlocking.Delegate, GeckoSession.Navigatio
private transient GeckoSession.PermissionDelegate mPermissionDelegate;
private transient GeckoSession.PromptDelegate mPromptDelegate;
private transient GeckoSession.HistoryDelegate mHistoryDelegate;
private String mRegion;
private transient Context mContext;
private transient SharedPreferences mPrefs;
private transient GeckoRuntime mRuntime;
Expand All @@ -81,20 +79,31 @@ public interface BitmapChangedListener {
}

protected Session(Context aContext, GeckoRuntime aRuntime, boolean aUsePrivateMode) {
this(aContext, aRuntime, aUsePrivateMode, null);
}

protected Session(Context aContext, GeckoRuntime aRuntime, boolean aUsePrivateMode,
@Nullable SessionSettings aSettings) {
mContext = aContext;
mRuntime = aRuntime;
mUsePrivateMode = aUsePrivateMode;
initialize();
mState = createSession();
if (aSettings != null) {
mState = createSession(aSettings);
} else {
mState = createSession();
}

setupSessionListeners(mState.mSession);
}

protected Session(Context aContext, GeckoRuntime aRuntime, Session aFrom) {
protected Session(Context aContext, GeckoRuntime aRuntime, SessionState aRestoreState) {
mContext = aContext;
mRuntime = aRuntime;
mUsePrivateMode = aFrom.isPrivateMode();
mUsePrivateMode = false;
initialize();
mState = createSession(aFrom.mState.mSettings);
mState = aRestoreState;
restore();
setupSessionListeners(mState.mSession);
}

Expand Down Expand Up @@ -274,14 +283,25 @@ private void cleanSessionListeners(GeckoSession aSession) {
aSession.setHistoryDelegate(null);
}

public void restore(Session store) {
mRegion = store.mRegion;
if (store.mState.mLastUse > 0) {
mState.mLastUse = store.mState.mLastUse;
private void restore() {
SessionSettings settings = mState.mSettings;
if (settings == null) {
settings = new SessionSettings.Builder()
.withDefaultSettings(mContext)
.build();
}

mState.mSession = createGeckoSession(settings);
if (!mState.mSession.isOpen()) {
mState.mSession.open(mRuntime);
}
if (mState.mSession != null && store.mState.mSessionState != null) {
mState.mSessionState = store.mState.mSessionState;
mState.mSession.restoreState(store.mState.mSessionState);

if (mState.mSessionState != null) {
mState.mSession.restoreState(mState.mSessionState);
}

for (SessionChangeListener listener: mSessionChangeListeners) {
listener.onNewSession(mState.mSession);
}

if (mUsePrivateMode) {
Expand All @@ -305,27 +325,7 @@ private SessionState createSession() {
private SessionState createSession(@NonNull SessionSettings aSettings) {
SessionState state = new SessionState();
state.mSettings = aSettings;

GeckoSessionSettings geckoSettings = new GeckoSessionSettings.Builder()
.useMultiprocess(aSettings.isMultiprocessEnabled())
.usePrivateMode(mUsePrivateMode)
.useTrackingProtection(aSettings.isTrackingProtectionEnabled())
.build();

if (aSettings.isServoEnabled()) {
if (isServoAvailable()) {
mState.mSession = createServoSession(mContext);
} else {
Log.e(LOGTAG, "Attempt to create a ServoSession. Servo hasn't been enable at build time. Using a GeckoSession instead.");
state.mSession = new GeckoSession(geckoSettings);
}
} else {
state.mSession = new GeckoSession(geckoSettings);
}

state.mSession.getSettings().setSuspendMediaWhenInactive(aSettings.isSuspendMediaWhenInactiveEnabled());
state.mSession.getSettings().setUserAgentMode(aSettings.getUserAgentMode());
state.mSession.getSettings().setUserAgentOverride(aSettings.getUserAgentOverride());
state.mSession = createGeckoSession(aSettings);

if (!state.mSession.isOpen()) {
state.mSession.open(mRuntime);
Expand All @@ -338,6 +338,27 @@ private SessionState createSession(@NonNull SessionSettings aSettings) {
return state;
}

private GeckoSession createGeckoSession(@NonNull SessionSettings aSettings) {
GeckoSessionSettings geckoSettings = new GeckoSessionSettings.Builder()
.useMultiprocess(aSettings.isMultiprocessEnabled())
.usePrivateMode(mUsePrivateMode)
.useTrackingProtection(aSettings.isTrackingProtectionEnabled())
.build();

GeckoSession session;
if (aSettings.isServoEnabled() && isServoAvailable()) {
session = createServoSession(mContext);
} else {
session = new GeckoSession(geckoSettings);
}

session.getSettings().setSuspendMediaWhenInactive(aSettings.isSuspendMediaWhenInactiveEnabled());
session.getSettings().setUserAgentMode(aSettings.getUserAgentMode());
session.getSettings().setUserAgentOverride(aSettings.getUserAgentOverride());

return session;
}

private void recreateSession() {
SessionState previous = mState;

Expand Down Expand Up @@ -384,7 +405,7 @@ public void purgeHistory() {

public void setRegion(String aRegion) {
Log.d(LOGTAG, "Session setRegion: " + aRegion);
mRegion = aRegion != null ? aRegion.toLowerCase() : "worldwide";
mState.mRegion = aRegion != null ? aRegion.toLowerCase() : "worldwide";

// There is a region initialize and the home is already loaded
if (mState.mSession != null && isHomeUri(getCurrentUri())) {
Expand All @@ -394,8 +415,8 @@ public void setRegion(String aRegion) {

public String getHomeUri() {
String homepage = SettingsStore.getInstance(mContext).getHomepage();
if (homepage.equals(mContext.getString(R.string.homepage_url)) && mRegion != null) {
homepage = homepage + "?region=" + mRegion;
if (homepage.equals(mContext.getString(R.string.homepage_url)) && mState.mRegion != null) {
homepage = homepage + "?region=" + mState.mRegion;
}
return homepage;
}
Expand Down Expand Up @@ -638,6 +659,10 @@ public long getLastUse() {
return mState.mLastUse;
}

public @NonNull SessionState getSessionState() {
return mState;
}

// NavigationDelegate

@Override
Expand All @@ -654,7 +679,7 @@ public void onLocationChange(@NonNull GeckoSession aSession, String aUri) {
}

// The homepage finishes loading after the region has been updated
if (mRegion != null && aUri.equalsIgnoreCase(SettingsStore.getInstance(mContext).getHomepage())) {
if (mState.mRegion != null && aUri.equalsIgnoreCase(SettingsStore.getInstance(mContext).getHomepage())) {
aSession.loadUri("javascript:window.location.replace('" + getHomeUri() + "');");
}
}
Expand Down Expand Up @@ -745,7 +770,7 @@ public void onCanGoForward(@NonNull GeckoSession aSession, boolean aCanGoForward
public GeckoResult<GeckoSession> onNewSession(@NonNull GeckoSession aSession, @NonNull String aUri) {
Log.d(LOGTAG, "Session onNewSession: " + aUri);

Session session = SessionStore.get().createSession(this);
Session session = SessionStore.get().createSession(mUsePrivateMode, mState.mSettings);
return GeckoResult.fromValue(session.getGeckoSession());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class SessionState {
@JsonAdapter(SessionState.GeckoSessionStateAdapter.class)
public GeckoSession.SessionState mSessionState;
public long mLastUse;
public String mRegion;

public static class GeckoSessionStateAdapter extends TypeAdapter<GeckoSession.SessionState> {
@Override
Expand Down Expand Up @@ -80,6 +81,7 @@ public void write(JsonWriter out, T value) throws IOException {
out.name("mFullScreen").value(session.mFullScreen);
out.name("mSettings").jsonValue(gson.toJson(session.mSettings));
out.name("mLastUse").value(session.mLastUse);
out.name("mRegion").value(session.mRegion);
if (session.mSession != null) {
if (session.mSession.getSettings().getUsePrivateMode()) {
out.name("mSessionState").jsonValue(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,32 +99,26 @@ public void initializeStores(Context context) {
mHistoryStore = new HistoryStore(context);
}

public Session createSession(boolean privateMode) {
Session session = new Session(mContext, mRuntime, privateMode);
public Session createSession(boolean aPrivateMode) {
return createSession(aPrivateMode, null);
}

public Session createSession(boolean aPrivateMode, @Nullable SessionSettings aSettings) {
Session session = new Session(mContext, mRuntime, aPrivateMode, aSettings);
session.setPermissionDelegate(this);
mSessions.add(session);

return session;
}

public Session createSession(Session from) {
Session session = new Session(mContext, mRuntime, from);
public Session createSession(SessionState aRestoreState) {
Session session = new Session(mContext, mRuntime, aRestoreState);
session.setPermissionDelegate(this);
mSessions.add(session);

return session;
}

public ArrayList<Session> restoreSessions(ArrayList<Session> aSessions) {
ArrayList<Session> result = new ArrayList<>();
for (Session restore: aSessions) {
Session newSession = createSession(restore);
newSession.restore(restore);
result.add(newSession);
}
return result;
}

public void destroySession(Session aSession) {
mSessions.remove(aSession);
if (aSession != null) {
Expand All @@ -133,11 +127,10 @@ public void destroySession(Session aSession) {
}
}

public void setActiveStore(Session aSession) {
public void setActiveSession(Session aSession) {
mActiveSession = aSession;
}


public Session getActiveSession() {
return mActiveSession;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ public int getBorderWidth() {
public void setActiveWindow(boolean active) {
mActive = active;
if (active) {
SessionStore.get().setActiveStore(mSession);
SessionStore.get().setActiveSession(mSession);
GeckoSession session = mSession.getGeckoSession();
if (session != null) {
session.getTextInput().setView(this);
Expand Down
Loading

0 comments on commit 46fad5c

Please sign in to comment.