Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use legacy User-Agent by default, add more settings #440

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class GeckoWebExtension(
val activeSession: Session = SessionStore.get().activeSession;
val session: Session = SessionStore.get().createWebExtensionSession(false, activeSession.isPrivateMode);
session.setParentSession(activeSession)
session.uaMode = GeckoSessionSettings.USER_AGENT_MODE_DESKTOP
session.setUaMode(GeckoSessionSettings.USER_AGENT_MODE_DESKTOP, true)
val geckoEngineSession = WolvicEngineSession(session)
ext.metaData?.optionsPageUrl?.let { optionsPageUrl ->
tabHandler.onNewTab(
Expand Down
12 changes: 12 additions & 0 deletions app/src/common/shared/com/igalia/wolvic/browser/SettingsStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ SettingsStore getInstance(final @NonNull Context aContext) {
public final static boolean SPEECH_DATA_COLLECTION_DEFAULT = false;
public final static boolean SPEECH_DATA_COLLECTION_REVIEWED_DEFAULT = false;
public final static int UA_MODE_DEFAULT = WSessionSettings.USER_AGENT_MODE_VR;
public final static boolean USE_WOLVIC_UA_DEFAULT = false;
public final static int INPUT_MODE_DEFAULT = 1;
public final static float DISPLAY_DENSITY_DEFAULT = 1.0f;
public final static int WINDOW_WIDTH_DEFAULT = 800;
Expand Down Expand Up @@ -342,6 +343,17 @@ public void setUaMode(int mode) {
editor.commit();
}

public boolean getUseWolvicUA() {
return mPrefs.getBoolean(
mContext.getString(R.string.settings_key_use_wolvic_ua), USE_WOLVIC_UA_DEFAULT);
}

public void setUseWolvicUA(boolean useWolvicUA) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(mContext.getString(R.string.settings_key_use_wolvic_ua), useWolvicUA);
editor.commit();
}

public int getInputMode() {
return mPrefs.getInt(
mContext.getString(R.string.settings_key_input_mode), INPUT_MODE_DEFAULT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class WolvicWebExtensionRuntime(
val activeSession = SessionStore.get().activeSession
val session: Session = SessionStore.get().createWebExtensionSession(activeSession.isPrivateMode);
session.setParentSession(activeSession)
session.uaMode = WSessionSettings.USER_AGENT_MODE_DESKTOP
session.setUaMode(WSessionSettings.USER_AGENT_MODE_DESKTOP, true)
val engineSession = WolvicEngineSession(session)
(context as WidgetManagerDelegate).windows.onTabSelect(session)
return webExtensionDelegate?.onToggleActionPopup(extension, engineSession, action)
Expand Down
51 changes: 31 additions & 20 deletions app/src/common/shared/com/igalia/wolvic/browser/engine/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ public class Session implements WContentBlocking.Delegate, WSession.NavigationDe
private static UriOverride sUserAgentOverride;
private static UriOverride sDesktopModeOverrides;
private static final long KEEP_ALIVE_DURATION_MS = 1000; // 1 second.
private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (Android 10; Mobile VR; rv:105.0) Gecko/105.0 Wolvic/" + BuildConfig.VERSION_NAME;

private static final String WOLVIC_USER_AGENT_MOBILE = "Mozilla/5.0 (Android 10; Mobile; rv:105.0) Gecko/105.0 Wolvic/" + BuildConfig.VERSION_NAME;
// This matches GeckoViewSettings.jsm
private static final String WOLVIC_USER_AGENT_VR = WOLVIC_USER_AGENT_MOBILE.replace("Mobile", "Mobile VR");

private transient CopyOnWriteArrayList<WSession.NavigationDelegate> mNavigationListeners;
private transient CopyOnWriteArrayList<WSession.ProgressDelegate> mProgressListeners;
Expand Down Expand Up @@ -193,12 +196,11 @@ private void initialize() {
mDrmStateStateListeners = new CopyOnWriteArrayList<>();
mMedia = new Media();

mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
if (mPrefs != null) {
mPrefs.registerOnSharedPreferenceChangeListener(this);
}

mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);

InternalPages.PageResources pageResources = InternalPages.PageResources.create(R.raw.private_mode, R.raw.private_style);
mPrivatePage = InternalPages.createAboutPage(mContext, pageResources);

Expand Down Expand Up @@ -1025,10 +1027,15 @@ private boolean trySetUaMode(int mode) {
return true;
}

public void setUaMode(int mode) {
public void setUaMode(int mode, boolean reload) {
// the UA mode value did not change
if (!trySetUaMode(mode))
return;

// the value did change, but we don't need to force a reload
if (!reload)
return;

String overrideUri = mode == WSessionSettings.USER_AGENT_MODE_DESKTOP ? checkForMobileSite(mState.mUri) : null;
if (overrideUri != null) {
mState.mSession.loadUri(overrideUri, WSession.LOAD_FLAGS_BYPASS_CACHE | WSession.LOAD_FLAGS_REPLACE_HISTORY);
Expand Down Expand Up @@ -1135,18 +1142,17 @@ WResult<WAllowOrDeny> onLoadRequest(@NonNull WSession aSession, @NonNull LoadReq
Log.d(LOGTAG, "onLoadRequest: " + uri);

if (aSession == mState.mSession) {
Log.d(LOGTAG, "Testing for UA override");

String userAgentOverride = sUserAgentOverride.lookupOverride(uri);

// Here we set a default user agent if no override was found, BUT
// only if UA Mode is not Desktop, because the UA override
// takes precedence over mode overrides so if we set it, it will
// invalidate desktop mode UA. For VR and Mobile modes we don't change
// the UA so they are not affected.
if (mState.mSettings.getUserAgentMode() != WSessionSettings.USER_AGENT_MODE_DESKTOP &&
userAgentOverride == null) {
userAgentOverride = DEFAULT_USER_AGENT;
Log.d(LOGTAG, "User-Agent override: " + userAgentOverride);

// Update the User-Agent according to the current UA settings,
// unless we are in Desktop mode (which uses its own User-Agent value).
int mode = mState.mSettings.getUserAgentMode();
if (mode != WSessionSettings.USER_AGENT_MODE_DESKTOP
&& userAgentOverride == null
&& SettingsStore.getInstance(mContext).getUseWolvicUA()) {
userAgentOverride = (mode == WSessionSettings.USER_AGENT_MODE_MOBILE) ? WOLVIC_USER_AGENT_MOBILE : WOLVIC_USER_AGENT_VR;
}

aSession.getSettings().setUserAgentOverride(userAgentOverride);
Expand Down Expand Up @@ -1738,12 +1744,17 @@ public void onMediaPermissionRequest(@NonNull WSession aSession, @NonNull String

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (mContext != null) {
if (key.equals(mContext.getString(R.string.settings_key_geolocation_data))) {
GeolocationData data = GeolocationData.parse(sharedPreferences.getString(key, null));
if (data != null) {
setRegion(data.getCountryCode());
}
if (mContext == null)
return;

if (key.equals(mContext.getString(R.string.settings_key_geolocation_data))) {
GeolocationData data = GeolocationData.parse(sharedPreferences.getString(key, null));
if (data != null) {
setRegion(data.getCountryCode());
}
} else if (key.equals(mContext.getString(R.string.settings_key_user_agent_version))) {
if (mState.mSettings.getUserAgentMode() != WSessionSettings.USER_AGENT_MODE_DESKTOP) {
setUaMode(SettingsStore.getInstance(mContext).getUaMode(), false);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1229,11 +1229,11 @@ public void onSwitchMode() {
if (uaMode == WSessionSettings.USER_AGENT_MODE_DESKTOP) {
final int defaultUaMode = SettingsStore.getInstance(mAppContext).getUaMode();
mHamburgerMenu.setUAMode(defaultUaMode);
mAttachedWindow.getSession().setUaMode(defaultUaMode);
mAttachedWindow.getSession().setUaMode(defaultUaMode, true);

} else {
mHamburgerMenu.setUAMode(WSessionSettings.USER_AGENT_MODE_DESKTOP);
mAttachedWindow.getSession().setUaMode(WSessionSettings.USER_AGENT_MODE_DESKTOP);
mAttachedWindow.getSession().setUaMode(WSessionSettings.USER_AGENT_MODE_DESKTOP, true);
}

hideMenu();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package com.igalia.wolvic.ui.widgets.settings;

import android.content.Context;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;

Expand All @@ -16,8 +17,10 @@
import com.igalia.wolvic.browser.SettingsStore;
import com.igalia.wolvic.browser.engine.SessionStore;
import com.igalia.wolvic.databinding.OptionsDeveloperBinding;
import com.igalia.wolvic.ui.views.settings.RadioGroupSetting;
import com.igalia.wolvic.ui.views.settings.SwitchSetting;
import com.igalia.wolvic.ui.widgets.WidgetManagerDelegate;
import com.igalia.wolvic.ui.widgets.WidgetPlacement;

class DeveloperOptionsView extends SettingsView {

Expand Down Expand Up @@ -76,6 +79,13 @@ protected void updateUI() {

mBinding.localAddonSwitch.setOnCheckedChangeListener(mLocalAddonListener);
setLocalAddon(SettingsStore.getInstance(getContext()).isLocalAddonAllowed(), false);

mBinding.wolvicUserAgentSwitch.setOnCheckedChangeListener(mUseWolvicUAListener);
setUseWolvicUA(SettingsStore.getInstance(getContext()).getUseWolvicUA(), false);

int uaMode = SettingsStore.getInstance(getContext()).getUaMode();
mBinding.uaRadio.setOnCheckedChangeListener(mUaModeListener);
setUaMode(mBinding.uaRadio.getIdForValue(uaMode), false);
}

private SwitchSetting.OnCheckedChangeListener mRemoteDebuggingListener = (compoundButton, value, doApply) -> {
Expand Down Expand Up @@ -106,6 +116,13 @@ protected void updateUI() {
setLocalAddon(value, doApply);
};

private SwitchSetting.OnCheckedChangeListener mUseWolvicUAListener = (compoundButton, enabled, apply) ->
setUseWolvicUA(enabled, true);

private RadioGroupSetting.OnCheckedChangeListener mUaModeListener = (radioGroup, checkedId, doApply) -> {
setUaMode(checkedId, true);
};

private OnClickListener mResetListener = (view) -> {
boolean restart = false;
if (mBinding.remoteDebuggingSwitch.isChecked() != SettingsStore.REMOTE_DEBUGGING_DEFAULT) {
Expand Down Expand Up @@ -139,6 +156,14 @@ protected void updateUI() {
setLocalAddon(SettingsStore.LOCAL_ADDON_ALLOWED, true);
}

if (mBinding.wolvicUserAgentSwitch.isChecked() != SettingsStore.USE_WOLVIC_UA_DEFAULT) {
setUseWolvicUA(SettingsStore.USE_WOLVIC_UA_DEFAULT, true);
}

if (!mBinding.uaRadio.getValueForId(mBinding.uaRadio.getCheckedRadioButtonId()).equals(SettingsStore.UA_MODE_DEFAULT)) {
setUaMode(mBinding.uaRadio.getIdForValue(SettingsStore.UA_MODE_DEFAULT), true);
}

if (restart) {
showRestartDialog();
}
Expand Down Expand Up @@ -219,9 +244,38 @@ private void setLocalAddon(boolean value, boolean doApply) {
}
}

private void setUseWolvicUA(boolean value, boolean doApply) {
mBinding.wolvicUserAgentSwitch.setOnCheckedChangeListener(null);
mBinding.wolvicUserAgentSwitch.setValue(value, false);
mBinding.wolvicUserAgentSwitch.setOnCheckedChangeListener(mUseWolvicUAListener);

if (doApply) {
SettingsStore.getInstance(getContext()).setUseWolvicUA(value);

if (value != SettingsStore.USE_WOLVIC_UA_DEFAULT) {
showAlert(getContext().getString(R.string.developer_options_wolvic_user_agent_warning_title),
getContext().getString(R.string.developer_options_wolvic_user_agent_warning_body));
}
}
}

private void setUaMode(int checkId, boolean doApply) {
mBinding.uaRadio.setOnCheckedChangeListener(null);
mBinding.uaRadio.setChecked(checkId, doApply);
mBinding.uaRadio.setOnCheckedChangeListener(mUaModeListener);

SettingsStore.getInstance(getContext()).setUaMode((Integer) mBinding.uaRadio.getValueForId(checkId));
}

@Override
public Point getDimensions() {
return new Point(WidgetPlacement.dpDimension(getContext(), R.dimen.settings_dialog_width),
WidgetPlacement.dpDimension(getContext(), R.dimen.developer_options_height));
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't see the relationship of this with anything else.

Is it something that was missing? Does it fix any particular issue? If that's the case it should go in a different PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This makes the dialog a bit taller, which is needed after adding the two settings for the User-Agent.

The alternative was to keep the dialog with the same height, but that looks worse:

08b9cbb2b6a8529c27325e8a27f43551


@Override
protected SettingViewType getType() {
return SettingViewType.LANGUAGE_VOICE;
return SettingViewType.DEVELOPER;
Copy link
Member

Choose a reason for hiding this comment

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

Same comment as above.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is a little issue that I saw when changing the dialog, it seems too small for its own PR but I can open one if needed.

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ protected void updateUI() {
mBinding.curvedDisplaySwitch.setOnCheckedChangeListener(mCurvedDisplayListener);
setCurvedDisplay(SettingsStore.getInstance(getContext()).getCylinderDensity() > 0.0f, false);

int uaMode = SettingsStore.getInstance(getContext()).getUaMode();
mBinding.uaRadio.setOnCheckedChangeListener(mUaModeListener);
setUaMode(mBinding.uaRadio.getIdForValue(uaMode), false);

int msaaLevel = SettingsStore.getInstance(getContext()).getMSAALevel();
mBinding.msaaRadio.setOnCheckedChangeListener(mMSSAChangeListener);
setMSAAMode(mBinding.msaaRadio.getIdForValue(msaaLevel), false);
Expand Down Expand Up @@ -123,10 +119,6 @@ public boolean isEditing() {
return editing;
}

private RadioGroupSetting.OnCheckedChangeListener mUaModeListener = (radioGroup, checkedId, doApply) -> {
setUaMode(checkedId, true);
};

private RadioGroupSetting.OnCheckedChangeListener mMSSAChangeListener = (radioGroup, checkedId, doApply) -> {
setMSAAMode(checkedId, true);
};
Expand Down Expand Up @@ -178,17 +170,13 @@ public boolean isEditing() {
private OnClickListener mResetListener = (view) -> {
boolean restart = false;

if (!mBinding.uaRadio.getValueForId(mBinding.uaRadio.getCheckedRadioButtonId()).equals(SettingsStore.UA_MODE_DEFAULT)) {
setUaMode(mBinding.uaRadio.getIdForValue(SettingsStore.UA_MODE_DEFAULT), true);
}
if (!mBinding.msaaRadio.getValueForId(mBinding.msaaRadio.getCheckedRadioButtonId()).equals(SettingsStore.MSAA_DEFAULT_LEVEL)) {
setMSAAMode(mBinding.msaaRadio.getIdForValue(SettingsStore.MSAA_DEFAULT_LEVEL), true);
}

restart = restart | setDisplayDensity(SettingsStore.DISPLAY_DENSITY_DEFAULT);
restart = restart | setDisplayDpi(SettingsStore.DISPLAY_DPI_DEFAULT);


setHomepage(mDefaultHomepageUrl);
setAutoplay(SettingsStore.AUTOPLAY_ENABLED, true);
setCurvedDisplay(false, true);
Expand Down Expand Up @@ -227,14 +215,6 @@ private void setHomepage(String newHomepage) {
mBinding.homepageEdit.setOnClickListener(mHomepageListener);
}

private void setUaMode(int checkId, boolean doApply) {
mBinding.uaRadio.setOnCheckedChangeListener(null);
mBinding.uaRadio.setChecked(checkId, doApply);
mBinding.uaRadio.setOnCheckedChangeListener(mUaModeListener);

SettingsStore.getInstance(getContext()).setUaMode((Integer)mBinding.uaRadio.getValueForId(checkId));
}

private void setMSAAMode(int checkedId, boolean doApply) {
mBinding.msaaRadio.setOnCheckedChangeListener(null);
mBinding.msaaRadio.setChecked(checkedId, doApply);
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/layout/options_developer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@
android:layout_height="wrap_content"
app:description="@string/allow_local_addon_switch" />

<com.igalia.wolvic.ui.views.settings.SwitchSetting
android:id="@+id/wolvic_user_agent_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:description="@string/developer_options_wolvic_user_agent" />

<com.igalia.wolvic.ui.views.settings.RadioGroupSetting
android:id="@+id/ua_radio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:description="@string/developer_options_ua_mode"
app:options="@array/developer_options_ua_modes"
app:values="@array/developer_options_ua_mode_values" />

</LinearLayout>
</com.igalia.wolvic.ui.views.CustomScrollView>

Expand Down
14 changes: 3 additions & 11 deletions app/src/main/res/layout/options_display.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@
android:layout_height="wrap_content"
app:description="@string/developer_options_curved_display" />

<com.igalia.wolvic.ui.views.settings.RadioGroupSetting
android:id="@+id/ua_radio"
<com.igalia.wolvic.ui.views.settings.SwitchSetting
android:id="@+id/autoplaySwitch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:description="@string/developer_options_ua_mode"
app:options="@array/developer_options_ua_modes"
app:values="@array/developer_options_ua_mode_values" />
app:description="@string/security_options_autoplay" />

<com.igalia.wolvic.ui.views.settings.RadioGroupSetting
android:id="@+id/msaa_radio"
Expand All @@ -60,12 +58,6 @@
app:options="@array/developer_options_msaa"
app:values="@array/developer_options_msaa_mode_values" />

<com.igalia.wolvic.ui.views.settings.SwitchSetting
android:id="@+id/autoplaySwitch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:description="@string/security_options_autoplay" />

<com.igalia.wolvic.ui.views.settings.SingleEditSetting
android:id="@+id/homepage_edit"
android:layout_width="match_parent"
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/res/values-es-rES/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,16 @@
<string name="developer_options_msaa_2">2x</string>
<!-- This string is used to label the MSAA radio button that enables two times MSAA in Immersive Mode. -->
<string name="developer_options_msaa_4">4x</string>
<!-- This string is used to label a switch that allows the user to use
a Wolvic-specific User-Agent value. -->
<string name="developer_options_wolvic_user_agent">Emplear el valor de \"User-Agent\" específico de Wolvic</string>
<!-- This string is the title of a dialog explaining the risk of changing the default User-Agent. -->
<string name="developer_options_wolvic_user_agent_warning_title">Atención</string>
<!-- This string is the body of a dialog explaining the risk of changing the default User-Agent. -->
<string name="developer_options_wolvic_user_agent_warning_body">Cambiar el valor por defecto de \"User-Agent\" es una functionalidad experimental que podría hacer que algunas páginas Web no funcionasen correctamente.</string>
<!-- This string is used to label a set of radio buttons that allow the user to change the
User-Agent (UA) string of the browser. -->
<string name="developer_options_ua_mode">Modo de agente de usuario</string>
<string name="developer_options_ua_mode">Modo de \"User-Agent\" por defecto</string>
<!-- This string is used to label a set of radio buttons that allow the user to change the
default downloads storage. -->
<string name="security_options_downloads_storage">Almacenamiento predeterminado para descargas</string>
Expand Down
Loading