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

Commit

Permalink
Add WebXR settings and permission flow (#3019)
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro authored Mar 23, 2020
1 parent 8cd832d commit d6079e9
Show file tree
Hide file tree
Showing 39 changed files with 915 additions and 316 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@

import android.Manifest;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import org.mozilla.geckoview.GeckoSession;
import org.mozilla.vrbrowser.PlatformActivity;
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.browser.engine.Session;
import org.mozilla.vrbrowser.browser.engine.SessionState;
import org.mozilla.vrbrowser.browser.engine.SessionStore;
import org.mozilla.vrbrowser.db.SitePermission;
import org.mozilla.vrbrowser.ui.viewmodel.SitePermissionViewModel;
import org.mozilla.vrbrowser.ui.widgets.WidgetManagerDelegate;
import org.mozilla.vrbrowser.ui.widgets.dialogs.PermissionWidget;
import org.mozilla.vrbrowser.utils.DeviceType;
import org.mozilla.vrbrowser.utils.SystemUtils;
import org.mozilla.vrbrowser.utils.UrlUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PermissionDelegate implements GeckoSession.PermissionDelegate, WidgetManagerDelegate.PermissionListener {

Expand All @@ -30,12 +38,16 @@ public class PermissionDelegate implements GeckoSession.PermissionDelegate, Widg
private WidgetManagerDelegate mWidgetManager;
private GeckoSession.PermissionDelegate.Callback mCallback;
private PermissionWidget mPermissionWidget;
private SitePermissionViewModel mSitePermissionModel;
private List<SitePermission> mSitePermissions;

public PermissionDelegate(Context aContext, WidgetManagerDelegate aWidgetManager) {
mContext = aContext;
mWidgetManager = aWidgetManager;
mWidgetManager.addPermissionListener(this);
SessionStore.get().setPermissionDelegate(this);
mSitePermissionModel = new SitePermissionViewModel((Application)aContext.getApplicationContext());
mSitePermissionModel.getAll().observeForever(mSitePermissionObserver);
}

public void setParentWidgetHandle(int aHandle) {
Expand Down Expand Up @@ -72,7 +84,37 @@ public void handlePermission(final String aUri, final PermissionWidget.Permissio
mPermissionWidget.showPrompt(aUri, aType, aCallback);
}

private Observer<List<SitePermission>> mSitePermissionObserver = sites -> {
mSitePermissions = sites;
};

void handleWebXRPermission(GeckoSession aGeckoSession, final String aUri, final Callback aCallback) {
Session session = SessionStore.get().getSession(aGeckoSession);
if (session == null || !SettingsStore.getInstance(mContext).isWebXREnabled()) {
aCallback.reject();
return;
}
final String domain = UrlUtils.getHost(aUri);

@Nullable SitePermission site = null;
if (mSitePermissions != null) {
site = mSitePermissions.stream()
.filter(sitePermission -> sitePermission.category == SitePermission.SITE_PERMISSION_WEBXR &&
sitePermission.url.equalsIgnoreCase(domain))
.findFirst().orElse(null);
}

if (site == null || site.allowed) {
aCallback.grant();
session.setWebXRState(SessionState.WEBXR_ALLOWED);
} else {
aCallback.reject();
session.setWebXRState(SessionState.WEBXR_BLOCKED);
}
}

public void release() {
mSitePermissionModel.getAll().removeObserver(mSitePermissionObserver);
mWidgetManager.removePermissionListener(this);
SessionStore.get().setPermissionDelegate(null);
mCallback = null;
Expand Down Expand Up @@ -116,7 +158,7 @@ public void onAndroidPermissionsRequest(GeckoSession aSession, String[] permissi
public void onContentPermissionRequest(GeckoSession aSession, String aUri, int aType, Callback callback) {
Log.d(LOGTAG, "onContentPermissionRequest: " + aUri + " " + aType);
if (aType == PERMISSION_XR) {
callback.grant();
handleWebXRPermission(aSession, aUri, callback);
return;
}

Expand Down Expand Up @@ -231,7 +273,24 @@ public void reject() {
}
}

public boolean isPermissionDialogVisible() {
return mPermissionWidget != null && mPermissionWidget.isVisible();
public void setPermissionAllowed(String uri, @SitePermission.Category int category, boolean allowed) {
@Nullable SitePermission site = mSitePermissions.stream()
.filter((item) -> item.category == category && item.url.equals(uri))
.findFirst().orElse(null);
boolean wasAllowed = site == null || site.allowed;
if (allowed == wasAllowed) {
return;
}
if (allowed) {
mSitePermissions.removeIf(sitePermission -> sitePermission.url.equals(uri));
mSitePermissionModel.deleteSite(site);
} else {
if (site == null) {
site = new SitePermission(uri, false, SitePermission.SITE_PERMISSION_WEBXR);
mSitePermissions.add(site);
}
site.allowed = false;
mSitePermissionModel.insertSite(site);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.VRBrowserApplication;
import org.mozilla.vrbrowser.browser.engine.Session;
import org.mozilla.vrbrowser.db.PopUpSite;
import org.mozilla.vrbrowser.ui.viewmodel.PopUpsViewModel;
import org.mozilla.vrbrowser.db.SitePermission;
import org.mozilla.vrbrowser.ui.viewmodel.SitePermissionViewModel;
import org.mozilla.vrbrowser.ui.widgets.UIWidget;
import org.mozilla.vrbrowser.ui.widgets.WidgetPlacement;
import org.mozilla.vrbrowser.ui.widgets.WindowWidget;
import org.mozilla.vrbrowser.ui.widgets.dialogs.PopUpBlockDialogWidget;
import org.mozilla.vrbrowser.ui.widgets.dialogs.QuickPermissionWidget;
import org.mozilla.vrbrowser.ui.widgets.prompts.AlertPromptWidget;
import org.mozilla.vrbrowser.ui.widgets.prompts.AuthPromptWidget;
import org.mozilla.vrbrowser.ui.widgets.prompts.ChoicePromptWidget;
Expand Down Expand Up @@ -51,15 +52,15 @@ public interface PopUpDelegate {
private ConfirmPromptWidget mSlowScriptPrompt;
private Context mContext;
private WindowWidget mAttachedWindow;
private List<PopUpSite> mAllowedPopUpSites;
private PopUpsViewModel mViewModel;
private List<SitePermission> mAllowedPopUpSites;
private SitePermissionViewModel mViewModel;
private AppExecutors mExecutors;
private PopUpDelegate mPopupDelegate;

public PromptDelegate(@NonNull Context context) {
mContext = context;
mExecutors = ((VRBrowserApplication)context.getApplicationContext()).getExecutors();
mViewModel = new PopUpsViewModel(((Application)context.getApplicationContext()));
mViewModel = new SitePermissionViewModel(((Application)context.getApplicationContext()));
mAllowedPopUpSites = new ArrayList<>();
}

Expand All @@ -71,7 +72,7 @@ public void attachToWindow(@NonNull WindowWidget window) {

mAttachedWindow = window;
mAttachedWindow.addWindowListener(this);
mViewModel.getAll().observeForever(mObserver);
mViewModel.getAll(SitePermission.SITE_PERMISSION_POPUP).observeForever(mPopUpSiteObserver);

if (getSession() != null) {
setUpSession(getSession());
Expand All @@ -87,7 +88,7 @@ public void detachFromWindow() {
mAttachedWindow.removeWindowListener(this);
mAttachedWindow = null;
}
mViewModel.getAll().removeObserver(mObserver);
mViewModel.getAll(SitePermission.SITE_PERMISSION_POPUP).removeObserver(mPopUpSiteObserver);

clearPopUps();
}
Expand Down Expand Up @@ -262,8 +263,8 @@ public void dismiss() {
return result;
}

private Observer<List<PopUpSite>> mObserver = popUpSites -> {
mAllowedPopUpSites = popUpSites;
private Observer<List<SitePermission>> mPopUpSiteObserver = sites -> {
mAllowedPopUpSites = sites;
};

@Nullable
Expand All @@ -278,7 +279,7 @@ public GeckoResult<PromptResponse> onPopupPrompt(@NonNull GeckoSession geckoSess
final int sessionId = geckoSession.hashCode();
final String uri = mAttachedWindow.getSession().getCurrentUri();

Optional<PopUpSite> site = mAllowedPopUpSites.stream().filter((item) -> item.url.equals(uri)).findFirst();
Optional<SitePermission> site = mAllowedPopUpSites.stream().filter((item) -> item.url.equals(uri)).findFirst();
if (site.isPresent()) {
mAttachedWindow.postDelayed(() -> {
if (site.get().allowed) {
Expand Down Expand Up @@ -349,15 +350,16 @@ public boolean hasPendingPopUps(GeckoSession session) {

private void showPopUp(int sessionId, @NonNull Pair<String, LinkedList<PopUpRequest>> requests) {
String uri = requests.first;
Optional<PopUpSite> site = mAllowedPopUpSites.stream().filter((item) -> item.url.equals(uri)).findFirst();
Optional<SitePermission> site = mAllowedPopUpSites.stream().filter((item) -> item.url.equals(uri)).findFirst();
if (!site.isPresent()) {
mPopUpPrompt = new PopUpBlockDialogWidget(mContext);
mPopUpPrompt.setButtonsDelegate(index -> {
boolean allowed = index != PopUpBlockDialogWidget.NEGATIVE;
boolean askAgain = mPopUpPrompt.askAgain();
if (allowed && !askAgain) {
mAllowedPopUpSites.add(new PopUpSite(uri, allowed));
mViewModel.insertSite(uri, allowed);
SitePermission permission = new SitePermission(uri, allowed, SitePermission.SITE_PERMISSION_POPUP);
mAllowedPopUpSites.add(permission);
mViewModel.insertSite(permission);
}

if (allowed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ SettingsStore getInstance(final @NonNull Context aContext) {
public final static boolean AUTOPLAY_ENABLED = false;
public final static boolean DEBUG_LOGGING_DEFAULT = false;
public final static boolean POP_UPS_BLOCKING_DEFAULT = true;
public final static boolean WEBXR_ENABLED_DEFAULT = true;
public final static boolean TELEMETRY_STATUS_UPDATE_SENT_DEFAULT = false;
public final static boolean BOOKMARKS_SYNC_DEFAULT = true;
public final static boolean HISTORY_SYNC_DEFAULT = true;
Expand Down Expand Up @@ -593,6 +594,16 @@ public void setPopUpsBlockingEnabled(boolean isEnabled) {
editor.commit();
}

public boolean isWebXREnabled() {
return mPrefs.getBoolean(mContext.getString(R.string.settings_key_webxr), WEBXR_ENABLED_DEFAULT);
}

public void setWebXREnabled(boolean isEnabled) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(mContext.getString(R.string.settings_key_webxr), isEnabled);
editor.commit();
}

public void setWhatsNewDisplayed(boolean isEnabled) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(mContext.getString(R.string.settings_key_whats_new_displayed), isEnabled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class Session implements ContentBlocking.Delegate, GeckoSession.Navigatio
private transient CopyOnWriteArrayList<VideoAvailabilityListener> mVideoAvailabilityListeners;
private transient CopyOnWriteArrayList<BitmapChangedListener> mBitmapChangedListeners;
private transient CopyOnWriteArrayList<GeckoSession.SelectionActionDelegate> mSelectionActionListeners;
private transient CopyOnWriteArrayList<WebXRStateChangedListener> mWebXRStateListeners;

private SessionState mState;
private transient CopyOnWriteArrayList<Runnable> mQueuedCalls = new CopyOnWriteArrayList<>();
Expand All @@ -91,6 +92,10 @@ public interface BitmapChangedListener {
void onBitmapChanged(Session aSession, Bitmap aBitmap);
}

public interface WebXRStateChangedListener {
void onWebXRStateChanged(Session aSession, @SessionState.WebXRState int aWebXRState);
}

@IntDef(value = { SESSION_OPEN, SESSION_DO_NOT_OPEN})
public @interface SessionOpenModeFlags {}
public static final int SESSION_OPEN = 0;
Expand Down Expand Up @@ -121,6 +126,7 @@ private void initialize() {
mVideoAvailabilityListeners = new CopyOnWriteArrayList<>();
mSelectionActionListeners = new CopyOnWriteArrayList<>();
mBitmapChangedListeners = new CopyOnWriteArrayList<>();
mWebXRStateListeners = new CopyOnWriteArrayList<>();

if (mPrefs != null) {
mPrefs.registerOnSharedPreferenceChangeListener(this);
Expand Down Expand Up @@ -165,6 +171,7 @@ protected void shutdown() {
mVideoAvailabilityListeners.clear();
mSelectionActionListeners.clear();
mBitmapChangedListeners.clear();
mWebXRStateListeners.clear();

if (mPrefs != null) {
mPrefs.unregisterOnSharedPreferenceChangeListener(this);
Expand All @@ -185,6 +192,10 @@ private void dumpAllState() {
for (VideoAvailabilityListener listener: mVideoAvailabilityListeners) {
dumpState(listener);
}

for (WebXRStateChangedListener listener: mWebXRStateListeners) {
dumpState(listener);
}
}

private void dumpState(GeckoSession.NavigationDelegate aListener) {
Expand Down Expand Up @@ -215,6 +226,10 @@ private void dumpState(VideoAvailabilityListener aListener) {
aListener.onVideoAvailabilityChanged(mState.mMediaElements != null && mState.mMediaElements.size() > 0);
}

private void dumpState(WebXRStateChangedListener aListener) {
aListener.onWebXRStateChanged(this, mState.mWebXRState);
}

private void flushQueuedEvents() {
for (Runnable call: mQueuedCalls) {
call.run();
Expand Down Expand Up @@ -302,6 +317,15 @@ public void removeBitmapChangedListener(BitmapChangedListener aListener) {
mBitmapChangedListeners.remove(aListener);
}

public void addWebXRStateChangedListener(WebXRStateChangedListener aListener) {
mWebXRStateListeners.add(aListener);
dumpState(aListener);
}

public void removeWebXRStateChangedListener(WebXRStateChangedListener aListener) {
mWebXRStateListeners.remove(aListener);
}

private void setupSessionListeners(GeckoSession aSession) {
aSession.setNavigationDelegate(this);
aSession.setProgressDelegate(this);
Expand Down Expand Up @@ -757,6 +781,15 @@ public boolean isPrivateMode() {
return false;
}

public void setWebXRState(@SessionState.WebXRState int aWebXRState) {
if (aWebXRState != mState.mWebXRState) {
mState.mWebXRState = aWebXRState;
for (WebXRStateChangedListener listener: mWebXRStateListeners) {
dumpState(listener);
}
}
}

// Session Settings

protected void setServo(final boolean enabled) {
Expand Down Expand Up @@ -993,6 +1026,7 @@ public void onPageStart(@NonNull GeckoSession aSession, @NonNull String aUri) {
TelemetryWrapper.startPageLoadTime();
GleanMetricsService.startPageLoadTime();

setWebXRState(SessionState.WEBXR_UNUSED);
for (GeckoSession.ProgressDelegate listener : mProgressListeners) {
listener.onPageStart(aSession, aUri);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.mozilla.vrbrowser.browser.engine;

import androidx.annotation.IntDef;

import com.google.gson.Gson;
import com.google.gson.JsonParser;
import com.google.gson.TypeAdapter;
Expand All @@ -20,6 +22,12 @@

@JsonAdapter(SessionState.SessionStateAdapterFactory.class)
public class SessionState {
@IntDef(value = { WEBXR_UNUSED, WEBXR_ALLOWED, WEBXR_BLOCKED})
public @interface WebXRState {}
public static final int WEBXR_UNUSED = 0;
public static final int WEBXR_ALLOWED = 1;
public static final int WEBXR_BLOCKED = 2;

private transient boolean mIsActive;
public boolean mCanGoBack;
public boolean mCanGoForward;
Expand All @@ -34,6 +42,7 @@ public class SessionState {
public transient GeckoDisplay mDisplay;
public SessionSettings mSettings;
public transient ArrayList<Media> mMediaElements = new ArrayList<>();
public transient @WebXRState int mWebXRState = WEBXR_UNUSED;
@JsonAdapter(SessionState.GeckoSessionStateAdapter.class)
public GeckoSession.SessionState mSessionState;
public long mLastUse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.mozilla.vrbrowser.browser.HistoryStore;
import org.mozilla.vrbrowser.browser.PermissionDelegate;
import org.mozilla.vrbrowser.browser.Services;
import org.mozilla.vrbrowser.db.SitePermission;
import org.mozilla.vrbrowser.utils.SystemUtils;

import java.util.ArrayList;
Expand Down Expand Up @@ -136,6 +137,10 @@ public void suspendAllInactiveSessions() {
return mSessions.stream().filter(session -> session.getId().equals(aId)).findFirst().orElse(null);
}

public @Nullable Session getSession(GeckoSession aGeckoSession) {
return mSessions.stream().filter(session -> session.getGeckoSession() == aGeckoSession).findFirst().orElse(null);
}

public void setActiveSession(Session aSession) {
if (aSession != null) {
aSession.setActive(true);
Expand Down Expand Up @@ -311,4 +316,10 @@ public void onMediaPermissionRequest(@NonNull GeckoSession session, @NonNull Str
mPermissionDelegate.onMediaPermissionRequest(session, uri, video, audio, callback);
}
}

public void setPermissionAllowed(String uri, @SitePermission.Category int category, boolean allowed) {
if (mPermissionDelegate != null) {
mPermissionDelegate.setPermissionAllowed(uri, category, allowed);
}
}
}
Loading

0 comments on commit d6079e9

Please sign in to comment.