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

Commit

Permalink
Receive FxA Tabs (#2068)
Browse files Browse the repository at this point in the history
* Receive FxA Tabs

* Correctly update captured image on background tabs
  • Loading branch information
MortimerGoro authored and keianhzo committed Oct 28, 2019
1 parent 55a0b24 commit 6d2aa2e
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,6 @@ public void openNewWindow(String uri) {
@Override
public void openNewTab(@NonNull String uri) {
mWindows.addBackgroundTab(mWindows.getFocusedWindow(), uri);
mTray.showTabAddedNotification();
}

@Override
Expand Down
18 changes: 10 additions & 8 deletions app/src/common/shared/org/mozilla/vrbrowser/browser/Services.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import androidx.work.WorkManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import mozilla.components.concept.sync.DeviceCapability
import mozilla.components.concept.sync.DeviceEvent
import mozilla.components.concept.sync.DeviceEventsObserver
import mozilla.components.concept.sync.DeviceType
import mozilla.components.concept.sync.*
import mozilla.components.lib.fetch.httpurlconnection.HttpURLConnectionClient
import mozilla.components.service.fxa.*
import mozilla.components.service.fxa.manager.FxaAccountManager
Expand All @@ -38,6 +35,11 @@ class Services(context: Context, places: Places): GeckoSession.NavigationDelegat
const val CLIENT_ID = "7ad9917f6c55fb77"
const val REDIRECT_URL = "https://accounts.firefox.com/oauth/success/$CLIENT_ID"
}
interface TabReceivedDelegate {
fun onTabsReceived(uri: List<TabData>)
}

var tabReceivedDelegate: TabReceivedDelegate? = null

// This makes bookmarks storage accessible to background sync workers.
init {
Expand Down Expand Up @@ -73,10 +75,10 @@ class Services(context: Context, places: Places): GeckoSession.NavigationDelegat
override fun onEvents(events: List<DeviceEvent>) {
CoroutineScope(Dispatchers.Main).launch {
Logger(logTag).info("Received ${events.size} device event(s)")
events.filterIsInstance(DeviceEvent.TabReceived::class.java).forEach {
// Just load the first tab that was sent.
// TODO Update when there is a push notifications API available
SessionStore.get().activeSession.loadUri(it.entries[0].url)
val events = events.filterIsInstance(DeviceEvent.TabReceived::class.java)
if (!events.isEmpty()) {
val tabs = events.map { event -> event.entries }.flatten()
tabReceivedDelegate?.onTabsReceived(tabs)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ public void captureBitmap(@NonNull GeckoDisplay aDisplay) {
});
}

public boolean hasCapturedBitmap() {
return BitmapCache.getInstance(mContext).hasBitmap(mState.mId);
}

public void purgeHistory() {
if (mState.mSession != null) {
mState.mSession.purgeHistory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void releaseWidget() {
@Override
public void show(int aShowFlags) {
super.show(aShowFlags);
mAdapter.updateTabs(SessionStore.get().getSortedSessions(mPrivateMode));
refreshTabs();
mWidgetManager.pushWorldBrightness(this, WidgetManagerDelegate.DEFAULT_DIM_BRIGHTNESS);
mTabsList.requestFocusFromTouch();
}
Expand All @@ -169,6 +169,10 @@ public void setTabDelegate(TabDelegate aDelegate) {
mTabDelegate = aDelegate;
}

public void refreshTabs() {
mAdapter.updateTabs(SessionStore.get().getSortedSessions(mPrivateMode));
}

public class TabAdapter extends RecyclerView.Adapter<TabAdapter.MyViewHolder> {
private ArrayList<Session> mTabs = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import org.mozilla.vrbrowser.ui.widgets.dialogs.ClearCacheDialogWidget;
import org.mozilla.vrbrowser.ui.widgets.dialogs.ContextMenuWidget;
import org.mozilla.vrbrowser.ui.widgets.dialogs.LibraryItemContextMenuWidget;
import org.mozilla.vrbrowser.ui.widgets.dialogs.MaxWindowsWidget;
import org.mozilla.vrbrowser.ui.widgets.dialogs.MessageDialogWidget;
import org.mozilla.vrbrowser.ui.widgets.dialogs.SelectionActionWidget;
import org.mozilla.vrbrowser.ui.widgets.prompts.AlertPromptWidget;
Expand Down Expand Up @@ -1459,7 +1458,7 @@ public void onPageStart(@NonNull GeckoSession geckoSession, @NonNull String s) {

@Override
public void onPageStop(@NonNull GeckoSession aSession, boolean b) {
if (mCaptureOnPageStop) {
if (mCaptureOnPageStop || !mSession.hasCapturedBitmap()) {
mCaptureOnPageStop = false;
captureImage();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.mozilla.vrbrowser.browser.Accounts;
import org.mozilla.vrbrowser.browser.Media;
import org.mozilla.vrbrowser.browser.PromptDelegate;
import org.mozilla.vrbrowser.browser.Services;
import org.mozilla.vrbrowser.browser.SettingsStore;
import org.mozilla.vrbrowser.browser.engine.Session;
import org.mozilla.vrbrowser.browser.engine.SessionState;
Expand All @@ -34,15 +35,18 @@
import java.io.Writer;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import mozilla.components.concept.sync.AccountObserver;
import mozilla.components.concept.sync.AuthType;
import mozilla.components.concept.sync.OAuthAccount;
import mozilla.components.concept.sync.Profile;
import mozilla.components.concept.sync.TabData;

public class Windows implements TrayListener, TopBarWidget.Delegate, TitleBarWidget.Delegate,
GeckoSession.ContentDelegate, WindowWidget.WindowListener, TabsWidget.TabDelegate {
GeckoSession.ContentDelegate, WindowWidget.WindowListener, TabsWidget.TabDelegate,
Services.TabReceivedDelegate {

private static final String LOGTAG = SystemUtils.createLogtag(Windows.class);

Expand Down Expand Up @@ -100,6 +104,7 @@ class WindowsState {
private PromptDelegate mPromptDelegate;
private TabsWidget mTabsWidget;
private Accounts mAccounts;
private Services mServices;

public enum WindowPlacement{
FRONT(0),
Expand Down Expand Up @@ -137,6 +142,8 @@ public Windows(Context aContext) {

mAccounts = ((VRBrowserApplication)mContext.getApplicationContext()).getAccounts();
mAccounts.addAccountListener(mAccountObserver);
mServices = ((VRBrowserApplication)mContext.getApplicationContext()).getServices();
mServices.setTabReceivedDelegate(this);

restoreWindows();
}
Expand Down Expand Up @@ -435,6 +442,7 @@ public void onDestroy() {
window.close();
}
mAccounts.removeAccountListener(mAccountObserver);
mServices.setTabReceivedDelegate(null);
}

public boolean isInPrivateMode() {
Expand Down Expand Up @@ -943,6 +951,10 @@ public void onTabsClicked() {
mTabsWidget.getPlacement().parentHandle = mFocusedWindow.getHandle();
mTabsWidget.attachToWindow(mFocusedWindow);
mTabsWidget.show(UIWidget.KEEP_FOCUS);
// If we're signed-in, poll for any new device events (e.g. received tabs)
// There's no push support right now, so this helps with the perception of speedy tab delivery.
((VRBrowserApplication)mContext.getApplicationContext()).getAccounts().refreshDevicesAsync();
((VRBrowserApplication)mContext.getApplicationContext()).getAccounts().pollForEventsAsync();
}
}

Expand Down Expand Up @@ -1105,7 +1117,7 @@ public void onTabSelect(Session aTab) {
}
}

public void addTab(WindowWidget targetWindow) {
public void addTab(WindowWidget targetWindow, @Nullable String aUri) {
Session session = SessionStore.get().createSession(targetWindow.getSession().isPrivateMode());
targetWindow.setFirstPaintReady(false);
targetWindow.setFirstDrawCallback(() -> {
Expand All @@ -1117,7 +1129,11 @@ public void addTab(WindowWidget targetWindow) {
mWidgetManager.updateWidget(targetWindow);
targetWindow.getSession().setActive(false);
targetWindow.setSession(session);
session.loadHomePage();
if (aUri != null) {
session.loadUri(aUri);
} else {
session.loadHomePage();
}
SessionStore.get().setActiveSession(session);
}

Expand All @@ -1126,11 +1142,12 @@ public void addBackgroundTab(WindowWidget targetWindow, String aUri) {
session.loadUri(aUri);
session.updateLastUse();
mFocusedWindow.getSession().updateLastUse();
mWidgetManager.getTray().showTabAddedNotification();
}

@Override
public void onTabAdd() {
addTab(mFocusedWindow);
addTab(mFocusedWindow, null);
}

@Override
Expand Down Expand Up @@ -1175,7 +1192,7 @@ public void onTabsClose(ArrayList<Session> aTabs) {
available.remove(0);
} else {
// We don't have more tabs available for the front window, load home.
addTab(window);
addTab(window, null);
}
}

Expand All @@ -1187,4 +1204,30 @@ public void onTabsClose(ArrayList<Session> aTabs) {

SessionStore.get().setActiveSession(targetWindow.getSession());
}

@Override
public void onTabsReceived(@NotNull List<TabData> aTabs) {
WindowWidget targetWindow = mFocusedWindow;
for (int i = aTabs.size() - 1; i >= 0; --i) {
Session session = SessionStore.get().createSession(targetWindow.getSession().isPrivateMode());
// Cache the provided data to avoid delays if the tabs are loaded at the same time the
// tabs panel is shown.
session.getSessionState().mTitle = aTabs.get(i).getTitle();
session.getSessionState().mUri = aTabs.get(i).getUrl();
session.loadUri(aTabs.get(i).getUrl());
session.updateLastUse();
if (i == 0) {
// Set the first received tab of the list the current one.
SessionStore.get().setActiveSession(session);
targetWindow.setSession(session);
}
}

mWidgetManager.getTray().showTabAddedNotification();

if (mTabsWidget != null && mTabsWidget.isVisible()) {
mTabsWidget.refreshTabs();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ public void removeBitmap(@NonNull String aKey) {
});
}

public boolean hasBitmap(@NonNull String aKey) {
return mMemoryCache.get(aKey) != null;
}

private void runIO(Runnable aRunnable) {
mIOExecutor.execute(() -> {
if (mDiskCache != null) {
Expand Down

0 comments on commit 6d2aa2e

Please sign in to comment.