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 authored and keianhzo committed Aug 19, 2019
1 parent 06bb0a8 commit b41a82c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
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.isMultiprocessEnabled;
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 @@ -36,6 +36,10 @@
import org.mozilla.vrbrowser.utils.InternalPages;

import java.util.ArrayList;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -743,13 +747,55 @@ public int getUaMode() {
return mCurrentSession.getSettings().getUserAgentMode();
}

private static final String M_PREFIX = "m.";
private static final String MOBILE_PREFIX = "mobile.";

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();
String foundPrefix = null;
if (authority.startsWith(M_PREFIX)) {
foundPrefix= M_PREFIX;
} else if (authority.startsWith(MOBILE_PREFIX)) {
foundPrefix = MOBILE_PREFIX;
}
if (foundPrefix != null) {
try {
uri = new URI(uri.getScheme(), authority.substring(foundPrefix.length()), uri.getPath(), uri.getQuery(), uri.getFragment());
result = uri.toString();
} catch (URISyntaxException e) {
Log.d(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());
mCurrentSession.loadUri(overrideUri != null ? overrideUri : state.mUri, GeckoSession.LOAD_FLAGS_BYPASS_CACHE | GeckoSession.LOAD_FLAGS_REPLACE_HISTORY);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/cpp/WidgetPlacement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ WidgetPlacement::FromJava(JNIEnv* aEnv, jobject& aObject) {

jclass clazz = aEnv->GetObjectClass(aObject);

std::shared_ptr<WidgetPlacement> result(new WidgetPlacement());;
std::shared_ptr<WidgetPlacement> result(new WidgetPlacement());

#define GET_INT_FIELD(name) { \
jfieldID f = aEnv->GetFieldID(clazz, #name, "I"); \
Expand Down Expand Up @@ -77,4 +77,4 @@ WidgetPlacement::GetTextureHeight() const {
return (int32_t)ceilf(height * density * textureScale);
}

}
}

0 comments on commit b41a82c

Please sign in to comment.