Commit b0b19809 authored by Marcin Wiacek's avatar Marcin Wiacek Committed by Commit Bot

Migrate WebappScopePolicy to @IntDef

@IntDef/@StringDef annotation are preferred way for declaring
set of String/int values.

1. they need less space in APK than enum, see
https://developer.android.com/topic/performance/reduce-apk-size#remove-enums
2. they give more control over allowed values than "static final" values

Main goal of patch is writing "static final" values, enum
and some classes in one common @IntDef/@StringDef form:

1. with @IntDef/@StringDef first, @Retention second
   and related @interface third
2. with values inside @interface
3. with NUM_ENTRIES declaring number of entries if necessary
4. with comment about numbering from 0 without gaps when necessary
5. with @Retention(RetentionPolicy.SOURCE)
6. without "static final" in the @interface

Change-Id: Ibfce629a890785c6b367d38ab45b05ecac893934
Reviewed-on: https://chromium-review.googlesource.com/1183488Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Reviewed-by: default avatarMounir Lamouri <mlamouri@chromium.org>
Commit-Queue: Marcin Wiącek <marcin@mwiacek.com>
Cr-Commit-Position: refs/heads/master@{#585549}
parent 9537e32b
...@@ -260,8 +260,8 @@ public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat ...@@ -260,8 +260,8 @@ public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat
Context context = getAvailableContext(); Context context = getAvailableContext();
if (context instanceof WebappActivity) { if (context instanceof WebappActivity) {
WebappActivity webappActivity = (WebappActivity) context; WebappActivity webappActivity = (WebappActivity) context;
return webappActivity.scopePolicy().applyPolicyForNavigationToUrl( return WebappScopePolicy.applyPolicyForNavigationToUrl(
webappActivity.getWebappInfo(), url); webappActivity.scopePolicy(), webappActivity.getWebappInfo(), url);
} }
return WebappScopePolicy.NavigationDirective.NORMAL_BEHAVIOR; return WebappScopePolicy.NavigationDirective.NORMAL_BEHAVIOR;
} }
......
...@@ -723,7 +723,7 @@ public class WebappActivity extends SingleTabActivity { ...@@ -723,7 +723,7 @@ public class WebappActivity extends SingleTabActivity {
updateToolbarCloseButtonVisibility(); updateToolbarCloseButtonVisibility();
if (!scopePolicy().isUrlInScope(mWebappInfo, url)) { if (!WebappScopePolicy.isUrlInScope(scopePolicy(), mWebappInfo, url)) {
// Briefly show the toolbar for off-scope navigations. // Briefly show the toolbar for off-scope navigations.
getFullscreenManager() getFullscreenManager()
.getBrowserVisibilityDelegate() .getBrowserVisibilityDelegate()
...@@ -785,8 +785,8 @@ public class WebappActivity extends SingleTabActivity { ...@@ -785,8 +785,8 @@ public class WebappActivity extends SingleTabActivity {
}; };
} }
public WebappScopePolicy scopePolicy() { public @WebappScopePolicy.Type int scopePolicy() {
return isVerified() ? WebappScopePolicy.STRICT : WebappScopePolicy.LEGACY; return isVerified() ? WebappScopePolicy.Type.STRICT : WebappScopePolicy.Type.LEGACY;
} }
/** /**
...@@ -850,8 +850,8 @@ public class WebappActivity extends SingleTabActivity { ...@@ -850,8 +850,8 @@ public class WebappActivity extends SingleTabActivity {
final int lastIndex = nc.getLastCommittedEntryIndex(); final int lastIndex = nc.getLastCommittedEntryIndex();
int index = lastIndex; int index = lastIndex;
while (index > 0 while (index > 0
&& !scopePolicy().isUrlInScope( && !WebappScopePolicy.isUrlInScope(
getWebappInfo(), nc.getEntryAtIndex(index).getUrl())) { scopePolicy(), getWebappInfo(), nc.getEntryAtIndex(index).getUrl())) {
index--; index--;
} }
......
...@@ -47,8 +47,8 @@ class WebappBrowserControlsDelegate extends TabStateBrowserControlsVisibilityDel ...@@ -47,8 +47,8 @@ class WebappBrowserControlsDelegate extends TabStateBrowserControlsVisibilityDel
* @param twaVerificationFailed Whether a verification attempt for TWA to client has failed. * @param twaVerificationFailed Whether a verification attempt for TWA to client has failed.
* @return Whether the browser controls should be shown for {@code url}. * @return Whether the browser controls should be shown for {@code url}.
*/ */
static boolean shouldShowBrowserControls(WebappScopePolicy scopePolicy, WebappInfo info, static boolean shouldShowBrowserControls(@WebappScopePolicy.Type int scopePolicy,
String url, int securityLevel, boolean twaVerificationFailed) { WebappInfo info, String url, int securityLevel, boolean twaVerificationFailed) {
// Do not show browser controls when URL is not ready yet. // Do not show browser controls when URL is not ready yet.
if (TextUtils.isEmpty(url)) return false; if (TextUtils.isEmpty(url)) return false;
...@@ -82,8 +82,8 @@ class WebappBrowserControlsDelegate extends TabStateBrowserControlsVisibilityDel ...@@ -82,8 +82,8 @@ class WebappBrowserControlsDelegate extends TabStateBrowserControlsVisibilityDel
* {@code url}. * {@code url}.
*/ */
private static boolean shouldShowBrowserControlsForUrl( private static boolean shouldShowBrowserControlsForUrl(
WebappScopePolicy scopePolicy, WebappInfo webappInfo, String url) { @WebappScopePolicy.Type int scopePolicy, WebappInfo webappInfo, String url) {
return !scopePolicy.isUrlInScope(webappInfo, url); return !WebappScopePolicy.isUrlInScope(scopePolicy, webappInfo, url);
} }
private static boolean shouldShowBrowserControlsForSecurityLevel(int securityLevel) { private static boolean shouldShowBrowserControlsForSecurityLevel(int securityLevel) {
......
...@@ -13,23 +13,19 @@ import java.lang.annotation.RetentionPolicy; ...@@ -13,23 +13,19 @@ import java.lang.annotation.RetentionPolicy;
/** /**
* Defines which URLs are inside a web app scope as well as what to do when user navigates to them. * Defines which URLs are inside a web app scope as well as what to do when user navigates to them.
*/ */
public enum WebappScopePolicy { public class WebappScopePolicy {
LEGACY { @IntDef({Type.LEGACY, Type.STRICT})
@Override
public boolean isUrlInScope(WebappInfo info, String url) {
return UrlUtilities.sameDomainOrHost(info.uri().toString(), url, true);
}
},
STRICT {
@Override
public boolean isUrlInScope(WebappInfo info, String url) {
return UrlUtilities.isUrlWithinScope(url, info.scopeUri().toString());
}
};
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
public @interface Type {
// Values should be numerated from 0 and can't have gaps.
int LEGACY = 0;
int STRICT = 1;
int NUM_ENTRIES = 2;
}
@IntDef({NavigationDirective.NORMAL_BEHAVIOR, @IntDef({NavigationDirective.NORMAL_BEHAVIOR,
NavigationDirective.IGNORE_EXTERNAL_INTENT_REQUESTS}) NavigationDirective.IGNORE_EXTERNAL_INTENT_REQUESTS})
@Retention(RetentionPolicy.SOURCE)
public @interface NavigationDirective { public @interface NavigationDirective {
// No special handling. // No special handling.
int NORMAL_BEHAVIOR = 0; int NORMAL_BEHAVIOR = 0;
...@@ -41,11 +37,22 @@ public enum WebappScopePolicy { ...@@ -41,11 +37,22 @@ public enum WebappScopePolicy {
* @return {@code true} if given {@code url} is in scope of a web app as defined by its * @return {@code true} if given {@code url} is in scope of a web app as defined by its
* {@code WebappInfo}, {@code false} otherwise. * {@code WebappInfo}, {@code false} otherwise.
*/ */
abstract boolean isUrlInScope(WebappInfo info, String url); public static boolean isUrlInScope(@Type int type, WebappInfo info, String url) {
switch (type) {
case Type.LEGACY:
return UrlUtilities.sameDomainOrHost(info.uri().toString(), url, true);
case Type.STRICT:
return UrlUtilities.isUrlWithinScope(url, info.scopeUri().toString());
default:
assert false;
return false;
}
}
/** Applies the scope policy for navigation to {@link url}. */ /** Applies the scope policy for navigation to {@link url}. */
public @NavigationDirective int applyPolicyForNavigationToUrl(WebappInfo info, String url) { public static @NavigationDirective int applyPolicyForNavigationToUrl(
if (isUrlInScope(info, url)) return NavigationDirective.IGNORE_EXTERNAL_INTENT_REQUESTS; @Type int type, WebappInfo info, String url) {
return NavigationDirective.NORMAL_BEHAVIOR; return isUrlInScope(type, info, url) ? NavigationDirective.IGNORE_EXTERNAL_INTENT_REQUESTS
: NavigationDirective.NORMAL_BEHAVIOR;
} }
} }
...@@ -1285,7 +1285,7 @@ public class ExternalNavigationHandlerTest { ...@@ -1285,7 +1285,7 @@ public class ExternalNavigationHandlerTest {
final String twaScope = "https://my_twa.org"; final String twaScope = "https://my_twa.org";
final String twaPackageName = "org.my_twa"; final String twaPackageName = "org.my_twa";
mDelegate.add(new IntentActivity(twaScope, twaPackageName) mDelegate.add(new IntentActivity(twaScope, twaPackageName)
.withWebappScopePolicy(WebappScopePolicy.STRICT)); .withWebappScopePolicy(WebappScopePolicy.Type.STRICT));
mDelegate.setReferrerWebappPackageName(twaPackageName); mDelegate.setReferrerWebappPackageName(twaPackageName);
checkUrl(twaScope + "/new.html").expecting(OverrideUrlLoadingResult.NO_OVERRIDE, IGNORE); checkUrl(twaScope + "/new.html").expecting(OverrideUrlLoadingResult.NO_OVERRIDE, IGNORE);
...@@ -1301,7 +1301,7 @@ public class ExternalNavigationHandlerTest { ...@@ -1301,7 +1301,7 @@ public class ExternalNavigationHandlerTest {
final String twaScope = "https://my_twa.org"; final String twaScope = "https://my_twa.org";
final String twaPackageName = "org.my_twa"; final String twaPackageName = "org.my_twa";
mDelegate.add(new IntentActivity(twaScope, twaPackageName) mDelegate.add(new IntentActivity(twaScope, twaPackageName)
.withWebappScopePolicy(WebappScopePolicy.STRICT)); .withWebappScopePolicy(WebappScopePolicy.Type.STRICT));
mDelegate.setReferrerWebappPackageName(twaPackageName); mDelegate.setReferrerWebappPackageName(twaPackageName);
checkUrl(SEARCH_RESULT_URL_FOR_TOM_HANKS) checkUrl(SEARCH_RESULT_URL_FOR_TOM_HANKS)
...@@ -1319,7 +1319,7 @@ public class ExternalNavigationHandlerTest { ...@@ -1319,7 +1319,7 @@ public class ExternalNavigationHandlerTest {
final String twaScope = "https://my_twa.org"; final String twaScope = "https://my_twa.org";
final String twaPackageName = "org.my_twa"; final String twaPackageName = "org.my_twa";
mDelegate.add(new IntentActivity(twaScope, twaPackageName) mDelegate.add(new IntentActivity(twaScope, twaPackageName)
.withWebappScopePolicy(WebappScopePolicy.STRICT)); .withWebappScopePolicy(WebappScopePolicy.Type.STRICT));
mDelegate.setReferrerWebappPackageName(twaPackageName); mDelegate.setReferrerWebappPackageName(twaPackageName);
checkUrl(SEARCH_RESULT_URL_FOR_TOM_HANKS) checkUrl(SEARCH_RESULT_URL_FOR_TOM_HANKS)
...@@ -1337,7 +1337,7 @@ public class ExternalNavigationHandlerTest { ...@@ -1337,7 +1337,7 @@ public class ExternalNavigationHandlerTest {
final String twaScope = "https://my_twa.org"; final String twaScope = "https://my_twa.org";
final String twaPackageName = "org.my_twa"; final String twaPackageName = "org.my_twa";
mDelegate.add(new IntentActivity(twaScope, twaPackageName) mDelegate.add(new IntentActivity(twaScope, twaPackageName)
.withWebappScopePolicy(WebappScopePolicy.LEGACY)); .withWebappScopePolicy(WebappScopePolicy.Type.LEGACY));
mDelegate.setReferrerWebappPackageName(twaPackageName); mDelegate.setReferrerWebappPackageName(twaPackageName);
checkUrl(SEARCH_RESULT_URL_FOR_TOM_HANKS) checkUrl(SEARCH_RESULT_URL_FOR_TOM_HANKS)
...@@ -1468,21 +1468,21 @@ public class ExternalNavigationHandlerTest { ...@@ -1468,21 +1468,21 @@ public class ExternalNavigationHandlerTest {
private String mUrlPrefix; private String mUrlPrefix;
private String mPackageName; private String mPackageName;
private boolean mIsWebApk; private boolean mIsWebApk;
private WebappScopePolicy mWebappScopePolicy; private @WebappScopePolicy.Type int mWebappScopePolicy;
public IntentActivity(String urlPrefix, String packageName) { public IntentActivity(String urlPrefix, String packageName) {
mUrlPrefix = urlPrefix; mUrlPrefix = urlPrefix;
mPackageName = packageName; mPackageName = packageName;
mWebappScopePolicy = WebappScopePolicy.LEGACY; mWebappScopePolicy = WebappScopePolicy.Type.LEGACY;
} }
public IntentActivity withIsWebApk(boolean isWebApk) { public IntentActivity withIsWebApk(boolean isWebApk) {
mIsWebApk = isWebApk; mIsWebApk = isWebApk;
mWebappScopePolicy = WebappScopePolicy.STRICT; mWebappScopePolicy = WebappScopePolicy.Type.STRICT;
return this; return this;
} }
public IntentActivity withWebappScopePolicy(WebappScopePolicy policy) { public IntentActivity withWebappScopePolicy(@WebappScopePolicy.Type int policy) {
mWebappScopePolicy = policy; mWebappScopePolicy = policy;
return this; return this;
} }
...@@ -1499,7 +1499,7 @@ public class ExternalNavigationHandlerTest { ...@@ -1499,7 +1499,7 @@ public class ExternalNavigationHandlerTest {
return mIsWebApk; return mIsWebApk;
} }
public WebappScopePolicy webappScopePolicy() { public @WebappScopePolicy.Type int webappScopePolicy() {
return mWebappScopePolicy; return mWebappScopePolicy;
} }
...@@ -1556,8 +1556,8 @@ public class ExternalNavigationHandlerTest { ...@@ -1556,8 +1556,8 @@ public class ExternalNavigationHandlerTest {
for (IntentActivity intentActivity : mIntentActivities) { for (IntentActivity intentActivity : mIntentActivities) {
if (intentActivity.packageName().equals(mReferrerWebappPackageName)) { if (intentActivity.packageName().equals(mReferrerWebappPackageName)) {
WebappInfo info = newWebappInfoFromScope(intentActivity.urlPrefix()); WebappInfo info = newWebappInfoFromScope(intentActivity.urlPrefix());
return intentActivity.webappScopePolicy().applyPolicyForNavigationToUrl( return WebappScopePolicy.applyPolicyForNavigationToUrl(
info, url); intentActivity.webappScopePolicy(), info, url);
} }
} }
return WebappScopePolicy.NavigationDirective.NORMAL_BEHAVIOR; return WebappScopePolicy.NavigationDirective.NORMAL_BEHAVIOR;
......
...@@ -45,7 +45,8 @@ public class WebappVisibilityTest { ...@@ -45,7 +45,8 @@ public class WebappVisibilityTest {
@Override @Override
public void run() { public void run() {
testCanAutoHideBrowserControls(); testCanAutoHideBrowserControls();
for (WebappScopePolicy scopePolicy : WebappScopePolicy.values()) { for (@WebappScopePolicy.Type int scopePolicy = WebappScopePolicy.Type.LEGACY;
scopePolicy < WebappScopePolicy.Type.NUM_ENTRIES; scopePolicy++) {
for (@WebDisplayMode int displayMode : new int[] {WebDisplayMode.STANDALONE, for (@WebDisplayMode int displayMode : new int[] {WebDisplayMode.STANDALONE,
WebDisplayMode.FULLSCREEN, WebDisplayMode.MINIMAL_UI}) { WebDisplayMode.FULLSCREEN, WebDisplayMode.MINIMAL_UI}) {
testShouldShowBrowserControls(scopePolicy, displayMode); testShouldShowBrowserControls(scopePolicy, displayMode);
...@@ -65,7 +66,7 @@ public class WebappVisibilityTest { ...@@ -65,7 +66,7 @@ public class WebappVisibilityTest {
} }
private static void testShouldShowBrowserControls( private static void testShouldShowBrowserControls(
WebappScopePolicy scopePolicy, @WebDisplayMode int displayMode) { @WebappScopePolicy.Type int scopePolicy, @WebDisplayMode int displayMode) {
// Show browser controls for out-of-domain URLs. // Show browser controls for out-of-domain URLs.
Assert.assertTrue(shouldShowBrowserControls(WEBAPP_URL, "http://notoriginalwebsite.com", Assert.assertTrue(shouldShowBrowserControls(WEBAPP_URL, "http://notoriginalwebsite.com",
ConnectionSecurityLevel.NONE, scopePolicy, displayMode)); ConnectionSecurityLevel.NONE, scopePolicy, displayMode));
...@@ -85,12 +86,12 @@ public class WebappVisibilityTest { ...@@ -85,12 +86,12 @@ public class WebappVisibilityTest {
// For WebAPKs but not Webapps show browser controls for subdomains and private // For WebAPKs but not Webapps show browser controls for subdomains and private
// registries that are secure. // registries that are secure.
Assert.assertEquals( Assert.assertEquals(scopePolicy == WebappScopePolicy.Type.STRICT
scopePolicy == WebappScopePolicy.STRICT || displayMode == WebDisplayMode.MINIMAL_UI, || displayMode == WebDisplayMode.MINIMAL_UI,
shouldShowBrowserControls(WEBAPP_URL, "http://sub.originalwebsite.com", shouldShowBrowserControls(WEBAPP_URL, "http://sub.originalwebsite.com",
ConnectionSecurityLevel.NONE, scopePolicy, displayMode)); ConnectionSecurityLevel.NONE, scopePolicy, displayMode));
Assert.assertEquals( Assert.assertEquals(scopePolicy == WebappScopePolicy.Type.STRICT
scopePolicy == WebappScopePolicy.STRICT || displayMode == WebDisplayMode.MINIMAL_UI, || displayMode == WebDisplayMode.MINIMAL_UI,
shouldShowBrowserControls(WEBAPP_URL, "http://thing.originalwebsite.com", shouldShowBrowserControls(WEBAPP_URL, "http://thing.originalwebsite.com",
ConnectionSecurityLevel.NONE, scopePolicy, displayMode)); ConnectionSecurityLevel.NONE, scopePolicy, displayMode));
...@@ -114,7 +115,8 @@ public class WebappVisibilityTest { ...@@ -114,7 +115,8 @@ public class WebappVisibilityTest {
} }
private static boolean shouldShowBrowserControls(String webappStartUrlOrScopeUrl, String url, private static boolean shouldShowBrowserControls(String webappStartUrlOrScopeUrl, String url,
int securityLevel, WebappScopePolicy scopePolicy, @WebDisplayMode int displayMode) { int securityLevel, @WebappScopePolicy.Type int scopePolicy,
@WebDisplayMode int displayMode) {
return WebappBrowserControlsDelegate.shouldShowBrowserControls(scopePolicy, return WebappBrowserControlsDelegate.shouldShowBrowserControls(scopePolicy,
createWebappInfo(webappStartUrlOrScopeUrl, scopePolicy, displayMode), url, createWebappInfo(webappStartUrlOrScopeUrl, scopePolicy, displayMode), url,
securityLevel, false); securityLevel, false);
...@@ -125,8 +127,8 @@ public class WebappVisibilityTest { ...@@ -125,8 +127,8 @@ public class WebappVisibilityTest {
} }
private static WebappInfo createWebappInfo(String webappStartUrlOrScopeUrl, private static WebappInfo createWebappInfo(String webappStartUrlOrScopeUrl,
WebappScopePolicy scopePolicy, @WebDisplayMode int displayMode) { @WebappScopePolicy.Type int scopePolicy, @WebDisplayMode int displayMode) {
return scopePolicy == WebappScopePolicy.LEGACY return scopePolicy == WebappScopePolicy.Type.LEGACY
? WebappInfo.create("", webappStartUrlOrScopeUrl, null, null, null, null, ? WebappInfo.create("", webappStartUrlOrScopeUrl, null, null, null, null,
displayMode, 0, 0, 0, 0, null, false /* isIconGenerated */, displayMode, 0, 0, 0, 0, null, false /* isIconGenerated */,
false /* forceNavigation */) false /* forceNavigation */)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment