Skip to content

Commit

Permalink
Drop m. prefix from URI when switching to desktop mode. Fixes #1473
Browse files Browse the repository at this point in the history
  • Loading branch information
bluemarvin committed Aug 14, 2019
1 parent dbab195 commit 830fc6c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ class SessionSettings {
private boolean isTrackingProtectionEnabled;
private boolean isSuspendMediaWhenInactiveEnabled;
private int userAgentMode;
private int viewportMode;
private boolean isServoEnabled;

private SessionSettings(@NotNull Builder builder) {
this.isMultiprocessEnabled = builder.isMltiprocessEnabled;
this.isTrackingProtectionEnabled = builder.isTrackingProtectionEnabled;
this.isSuspendMediaWhenInactiveEnabled = builder.isSuspendMediaWhenInactiveEnabled;
this.userAgentMode = builder.userAgentMode;
this.viewportMode = builder.viewportMode;
this.isServoEnabled = builder.isServoEnabled;
}

Expand Down Expand Up @@ -50,6 +52,10 @@ public void setUserAgentMode(int mode) {
userAgentMode = mode;
}

public int getViewportMode() { return viewportMode; }

public void setViewportMode(final int mode) { viewportMode = mode; }

public boolean isServoEnabled() {
return isServoEnabled;
}
Expand All @@ -64,6 +70,7 @@ public static class Builder {
private boolean isTrackingProtectionEnabled;
private boolean isSuspendMediaWhenInactiveEnabled;
private int userAgentMode;
private int viewportMode;
private boolean isServoEnabled;

public Builder() {
Expand All @@ -89,6 +96,11 @@ public Builder withUserAgent(int userAgent){
return this;
}

public Builder withViewport(int viewPort) {
this.viewportMode = viewPort;
return this;
}

public Builder withServo(boolean isServoEnabled){
this.isServoEnabled= isServoEnabled;
return this;
Expand All @@ -100,6 +112,7 @@ public Builder withDefaultSettings(Context context) {
.withTrackingProteccion(SettingsStore.getInstance(context).isTrackingProtectionEnabled())
.withSuspendMediaWhenInactive(true)
.withUserAgent(GeckoSessionSettings.USER_AGENT_MODE_VR)
.withViewport(GeckoSessionSettings.VIEWPORT_MODE_MOBILE)
.withServo(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.mozilla.vrbrowser.telemetry.TelemetryWrapper;
import org.mozilla.vrbrowser.utils.InternalPages;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
Expand Down Expand Up @@ -777,13 +779,51 @@ public int getUaMode() {
return mCurrentSession.getSettings().getUserAgentMode();
}

private static final String MOBILE_PREFIX = "m.";
private String checkForMobileSite(String aUri) {
String result = null;
URI uri;
try {
uri = new URI(aUri);
} catch (URISyntaxException e) {
Log.d(LOGTAG, "Error parsing URL: " + aUri + " " + e.getMessage());
return null;
}
String authority = uri.getAuthority();
if (authority == null) {
return null;
}
authority = authority.toLowerCase();
if (authority.startsWith(MOBILE_PREFIX)) {
try {
uri = new URI(uri.getScheme(), authority.substring(MOBILE_PREFIX.length()), uri.getPath(), uri.getQuery(), uri.getFragment());
result = uri.toString();
} catch (URISyntaxException e) {
Log.e(LOGTAG, "Error dropping mobile prefix from: " + aUri + " " + e.getMessage());
}
}
return result;
}

public void setUaMode(int mode) {
if (mCurrentSession != null) {
SessionState state = mSessions.get(mCurrentSession.hashCode());
if (state != null && state.mSettings.getUserAgentMode() != mode) {
state.mSettings.setUserAgentMode(mode);
mCurrentSession.getSettings().setUserAgentMode(mode);
mCurrentSession.reload();
String overrideUri = null;
if (mode == GeckoSessionSettings.USER_AGENT_MODE_DESKTOP) {
state.mSettings.setViewportMode(GeckoSessionSettings.VIEWPORT_MODE_DESKTOP);
overrideUri = checkForMobileSite(state.mUri);
} else {
state.mSettings.setViewportMode(GeckoSessionSettings.VIEWPORT_MODE_MOBILE);
}
mCurrentSession.getSettings().setViewportMode(state.mSettings.getViewportMode());
if (overrideUri != null) {
mCurrentSession.loadUri(overrideUri, GeckoSession.LOAD_FLAGS_BYPASS_CACHE);
} else if (state.mUri != null){
mCurrentSession.loadUri(state.mUri, GeckoSession.LOAD_FLAGS_BYPASS_CACHE);;
}
}
}
}
Expand Down

0 comments on commit 830fc6c

Please sign in to comment.