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

Tabs polish #2028

Merged
merged 10 commits into from
Oct 23, 2019
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,9 @@ dependencies {
// SQLite helper to handle DBs from assets
implementation deps.sqlite.sqlite

// DiskLRUCache used to cache snapshots
implementation deps.disklrucache.disklrucache

// Testing
testImplementation deps.junit
androidTestImplementation deps.atsl.runner
Expand Down
14 changes: 14 additions & 0 deletions app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ protected void initializeWidgets() {
@Override
public void onFocusedWindowChanged(@NonNull WindowWidget aFocusedWindow, @Nullable WindowWidget aPrevFocusedWindow) {
attachToWindow(aFocusedWindow, aPrevFocusedWindow);
mTray.setAddWindowVisible(mWindows.canOpenNewWindow());
}
@Override
public void onWindowBorderChanged(@NonNull WindowWidget aChangeWindow) {
Expand All @@ -284,6 +285,7 @@ public void onWindowsMoved() {

@Override
public void onWindowClosed() {
mTray.setAddWindowVisible(mWindows.canOpenNewWindow());
updateWidget(mTray);
}
});
Expand All @@ -307,6 +309,7 @@ public void onWindowClosed() {

// Add widget listeners
mTray.addListeners(mWindows);
mTray.setAddWindowVisible(mWindows.canOpenNewWindow());

attachToWindow(mWindows.getFocusedWindow(), null);

Expand Down Expand Up @@ -1376,6 +1379,11 @@ public void setCPULevel(int aCPULevel) {
queueRunnable(() -> setCPULevelNative(aCPULevel));
}

@Override
public boolean canOpenNewWindow() {
return mWindows.canOpenNewWindow();
}

@Override
public void openNewWindow(String uri) {
WindowWidget newWindow = mWindows.addWindow();
Expand All @@ -1384,6 +1392,12 @@ public void openNewWindow(String uri) {
}
}

@Override
public void openNewTab(@NonNull String uri) {
mWindows.addBackgroundTab(mWindows.getFocusedWindow(), uri);
mTray.showTabAddedNotification();
}

@Override
public WindowWidget getFocusedWindow() {
return mWindows.getFocusedWindow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
import org.mozilla.vrbrowser.db.AppDatabase;
import org.mozilla.vrbrowser.db.DataRepository;
import org.mozilla.vrbrowser.telemetry.TelemetryWrapper;
import org.mozilla.vrbrowser.utils.BitmapCache;
import org.mozilla.vrbrowser.utils.LocaleUtils;

public class VRBrowserApplication extends Application {

private AppExecutors mAppExecutors;
private BitmapCache mBitmapCache;
private Places mPlaces;

@Override
Expand All @@ -26,6 +28,7 @@ public void onCreate() {

mAppExecutors = new AppExecutors();
mPlaces = new Places(this);
mBitmapCache = new BitmapCache(this, mAppExecutors.diskIO(), mAppExecutors.mainThread());

TelemetryWrapper.init(this);
}
Expand Down Expand Up @@ -57,4 +60,8 @@ public AppExecutors getExecutors() {
public DataRepository getRepository() {
return DataRepository.getInstance(getDatabase(), mAppExecutors);
}

public BitmapCache getBitmapCache() {
return mBitmapCache;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.mozilla.vrbrowser.browser;

import org.mozilla.geckoview.GeckoSession;
import org.mozilla.vrbrowser.browser.engine.Session;

public interface SessionChangeListener {
default void onNewSession(GeckoSession aSession) {};
default void onRemoveSession(GeckoSession aSession) {};
default void onCurrentSessionChange(GeckoSession aOldSession, GeckoSession aSession) {};
default void onNewTab(Session aTab) {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.mozilla.geckoview.AllowOrDeny;
import org.mozilla.geckoview.ContentBlocking;
import org.mozilla.geckoview.GeckoDisplay;
import org.mozilla.geckoview.GeckoResponse;
import org.mozilla.geckoview.GeckoResult;
import org.mozilla.geckoview.GeckoRuntime;
Expand All @@ -36,6 +37,7 @@
import org.mozilla.vrbrowser.browser.VideoAvailabilityListener;
import org.mozilla.vrbrowser.geolocation.GeolocationData;
import org.mozilla.vrbrowser.telemetry.TelemetryWrapper;
import org.mozilla.vrbrowser.utils.BitmapCache;
import org.mozilla.vrbrowser.utils.InternalPages;
import org.mozilla.vrbrowser.utils.SystemUtils;

Expand All @@ -44,6 +46,7 @@
import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;

import static org.mozilla.vrbrowser.utils.ServoUtils.createServoSession;
import static org.mozilla.vrbrowser.utils.ServoUtils.isInstanceOfServoSession;
Expand Down Expand Up @@ -77,23 +80,23 @@ public class Session implements ContentBlocking.Delegate, GeckoSession.Navigatio
private transient byte[] mPrivatePage;

public interface BitmapChangedListener {
void onBitmapChanged(Bitmap aBitmap);
void onBitmapChanged(Session aSession, Bitmap aBitmap);
}

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

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

setupSessionListeners(mState.mSession);
Expand Down Expand Up @@ -328,20 +331,20 @@ private void restore() {
dumpAllState();
}

private SessionState createSession() {
private SessionState createSession(boolean aOpen) {
SessionSettings settings = new SessionSettings.Builder()
.withDefaultSettings(mContext)
.build();

return createSession(settings);
return createSession(settings, aOpen);
}

private SessionState createSession(@NonNull SessionSettings aSettings) {
private SessionState createSession(@NonNull SessionSettings aSettings, boolean aOpen) {
SessionState state = new SessionState();
state.mSettings = aSettings;
state.mSession = createGeckoSession(aSettings);

Comment on lines +334 to 346
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use enum instead of a boolean so it is more obvious what is happening in the code.

if (!state.mSession.isOpen()) {
if (aOpen && !state.mSession.isOpen()) {
state.mSession.open(mRuntime);
}

Expand Down Expand Up @@ -376,7 +379,7 @@ private GeckoSession createGeckoSession(@NonNull SessionSettings aSettings) {
private void recreateSession() {
SessionState previous = mState;

mState = createSession(previous.mSettings);
mState = createSession(previous.mSettings, true);
if (previous.mSessionState != null)
mState.mSession.restoreState(previous.mSessionState);
if (previous.mSession != null) {
Expand All @@ -398,17 +401,18 @@ private void closeSession(@NonNull GeckoSession aSession) {
aSession.close();
}

public void setBitmap(Bitmap aBitmap, GeckoSession aSession) {
if (aSession == mState.mSession) {
mState.mBitmap = aBitmap;
for (BitmapChangedListener listener: mBitmapChangedListeners) {
listener.onBitmapChanged(mState.mBitmap);
public void captureBitmap(@NonNull GeckoDisplay aDisplay) {
aDisplay.capturePixels().then(bitmap -> {
if (bitmap != null) {
BitmapCache.getInstance(mContext).scaleBitmap(bitmap, 500, 280).thenAccept(scaledBitmap -> {
BitmapCache.getInstance(mContext).addBitmap(getId(), scaledBitmap);
for (BitmapChangedListener listener: mBitmapChangedListeners) {
listener.onBitmapChanged(Session.this, scaledBitmap);
}
});
}
}
}

public @Nullable Bitmap getBitmap() {
return mState.mBitmap;
return null;
});
}

public void purgeHistory() {
Expand Down Expand Up @@ -540,7 +544,7 @@ public void toggleServo() {
.withServo(!isInstanceOfServoSession(mState.mSession))
.build();

mState = createSession(settings);
mState = createSession(settings, true);
closeSession(previous.mSession);
loadUri(uri);
}
Expand All @@ -559,6 +563,10 @@ public GeckoSession getGeckoSession() {
return mState.mSession;
}

public String getId() {
return mState.mId;
}

public boolean isPrivateMode() {
if (mState.mSession != null) {
return mState.mSession.getSettings().getUsePrivateMode();
Expand Down Expand Up @@ -784,7 +792,10 @@ 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(mUsePrivateMode, mState.mSettings);
Session session = SessionStore.get().createSession(mUsePrivateMode, mState.mSettings, false);
for (SessionChangeListener listener: mSessionChangeListeners) {
listener.onNewTab(session);
}
return GeckoResult.fromValue(session.getGeckoSession());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.UUID;

@JsonAdapter(SessionState.SessionStateAdapterFactory.class)
public class SessionState {

public boolean mCanGoBack;
public boolean mCanGoForward;
public boolean mIsLoading;
public boolean mIsInputActive;
public transient @Nullable Bitmap mBitmap;
public transient GeckoSession.ProgressDelegate.SecurityInformation mSecurityInformation;
public String mUri = "";
public String mPreviousUri;
Expand All @@ -41,6 +40,7 @@ public class SessionState {
public GeckoSession.SessionState mSessionState;
public long mLastUse;
public String mRegion;
public String mId = UUID.randomUUID().toString();

public static class GeckoSessionStateAdapter extends TypeAdapter<GeckoSession.SessionState> {
@Override
Expand Down Expand Up @@ -82,6 +82,7 @@ public void write(JsonWriter out, T value) throws IOException {
out.name("mSettings").jsonValue(gson.toJson(session.mSettings));
out.name("mLastUse").value(session.mLastUse);
out.name("mRegion").value(session.mRegion);
out.name("mId").value(session.mId);
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 @@ -100,11 +100,11 @@ public void initializeStores(Context context) {
}

public Session createSession(boolean aPrivateMode) {
return createSession(aPrivateMode, null);
return createSession(aPrivateMode, null, true);
}

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

Expand Down
Loading