Commit e5c80add authored by Mugdha Lakhani's avatar Mugdha Lakhani Committed by Commit Bot

[WebApk] Fix logic that checks if a PWA is installed.

We were checking that the url of the installed PWA is a prefix of its
scope. However, the url we have available from the PermissionContext
is only the requesting origin, and hence this check fails if the scope
is longer than the origin.

This CL reverses the check to verify that the url is a prefix of the
scope. A unit test has also been updated to test for this case.

Bug: 1062177
Change-Id: Ic81f9b5b10fcb378cc3d65dadee947f53e8755b1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2107599
Commit-Queue: Mugdha Lakhani <nator@chromium.org>
Reviewed-by: default avatarPeter Conn <peconn@chromium.org>
Reviewed-by: default avatarBalazs Engedy <engedy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#753220}
parent 08331e59
......@@ -18,12 +18,20 @@ import java.util.Locale;
*/
public class BackgroundSyncPwaDetector {
/**
* Returns true if there's a PWA installed for the given origin, false otherwise.
* Returns true if there's at least one PWA installed for the given origin, false otherwise.
* Installed PWAs are associated with a scope, while permissions are attributed to an origin.
* Because we're only passed the requesting origin from the permissions infrastructure, we can't
* match the scope of installed PWAs to the exact URL of the permission request. We instead
* look for any installed PWA for the requesting origin. With this logic, if there's already a
* PWA installed for travel.google.com, and a request to register Periodic Background Sync comes
* in from maps.google.com, this method will return true and registration will succeed, provided
* other required conditions are met.
*
* @param origin The origin for which to look for a PWA.
*/
@CalledByNative
public static boolean isPwaInstalled(String origin) {
return WebappRegistry.getInstance().hasWebApkForUrl(
return WebappRegistry.getInstance().hasAtLeastOneWebApkForOrigin(
origin.toLowerCase(Locale.getDefault()));
}
......
......@@ -196,10 +196,10 @@ public class WebappRegistry {
}
/**
* Returns true if a WebAPK is found whose scope matches the provided URL.
* @param url The URL to search a WebAPK for.
* Returns true if a WebAPK is found whose scope matches |origin|.
* @param origin The origin to search a WebAPK for.
*/
public boolean hasWebApkForUrl(String url) {
public boolean hasAtLeastOneWebApkForOrigin(String origin) {
for (HashMap.Entry<String, WebappDataStorage> entry : mStorages.entrySet()) {
WebappDataStorage storage = entry.getValue();
if (!storage.getId().startsWith(WebApkConstants.WEBAPK_ID_PREFIX)) continue;
......@@ -209,7 +209,7 @@ public class WebappRegistry {
// Scope shouldn't be empty.
assert (!scope.isEmpty());
if (url.startsWith(scope)) return true;
if (scope.startsWith(origin)) return true;
}
return false;
}
......
......@@ -666,19 +666,19 @@ public class WebappRegistryTest {
@Test
@Feature({"WebApk"})
public void testHasWebApkForUrl() throws Exception {
final String startUrl = START_URL;
final String testUrl = START_URL + "/index.html";
public void testHasWebApkForOrigin() throws Exception {
final String startUrl = START_URL + "/test_page.html";
final String testOrigin = START_URL;
assertFalse(WebappRegistry.getInstance().hasWebApkForUrl(testUrl));
assertFalse(WebappRegistry.getInstance().hasAtLeastOneWebApkForOrigin(testOrigin));
String webappId = "webapp";
registerWebapp(webappId, createShortcutWebappInfo(startUrl));
assertFalse(WebappRegistry.getInstance().hasWebApkForUrl(testUrl));
assertFalse(WebappRegistry.getInstance().hasAtLeastOneWebApkForOrigin(testOrigin));
WebApkInfo webApkInfo = new WebApkInfoBuilder("org.chromium.webapk", startUrl).build();
registerWebapp(webApkInfo.id(), webApkInfo);
assertTrue(WebappRegistry.getInstance().hasWebApkForUrl(testUrl));
assertTrue(WebappRegistry.getInstance().hasAtLeastOneWebApkForOrigin(testOrigin));
}
private Set<String> addWebappsToRegistry(String... webapps) {
......
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