Commit 632104a8 authored by Ehimare Okoyomon's avatar Ehimare Okoyomon Committed by Commit Bot

[Android] Refactor WebsitePermissionsFetcher methods.

Add utility function and enum to get a WebsitePermissionsType from a
ContentSettingsType because they can take on different objects. Then use
these additions to simplify WebsitePermissionsFetcher logic.

Bug: 1077766
Change-Id: Ifd69d3a32df1193e295835d7c0fd2ce292e31570
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2421498
Commit-Queue: Ehimare Okoyomon <eokoyomon@chromium.org>
Reviewed-by: default avatarChristian Dullweber <dullweber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#812060}
parent 5fe8fd2f
...@@ -569,9 +569,9 @@ public class WebsitePermissionsFetcherTest { ...@@ -569,9 +569,9 @@ public class WebsitePermissionsFetcherTest {
ArrayList<ChosenObjectInfo> chosenObjectInfos = ArrayList<ChosenObjectInfo> chosenObjectInfos =
new ArrayList<>(site.getChosenObjectInfo()); new ArrayList<>(site.getChosenObjectInfo());
Assert.assertEquals(2, chosenObjectInfos.size()); Assert.assertEquals(2, chosenObjectInfos.size());
Assert.assertEquals(ContentSettingsType.USB_CHOOSER_DATA,
chosenObjectInfos.get(0).getContentSettingsType());
Assert.assertEquals(ContentSettingsType.BLUETOOTH_CHOOSER_DATA, Assert.assertEquals(ContentSettingsType.BLUETOOTH_CHOOSER_DATA,
chosenObjectInfos.get(0).getContentSettingsType());
Assert.assertEquals(ContentSettingsType.USB_CHOOSER_DATA,
chosenObjectInfos.get(1).getContentSettingsType()); chosenObjectInfos.get(1).getContentSettingsType());
}); });
} }
......
...@@ -46,10 +46,9 @@ public class SiteSettingsCategory { ...@@ -46,10 +46,9 @@ public class SiteSettingsCategory {
Type.USE_STORAGE}) Type.USE_STORAGE})
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
public @interface Type { public @interface Type {
// Values used to address array index - should be enumerated from 0 and can't have gaps.
// All updates here must also be reflected in {@link #preferenceKey(int) // All updates here must also be reflected in {@link #preferenceKey(int)
// preferenceKey} and {@link #contentSettingsType(int) contentSettingsType}. // preferenceKey} and {@link #contentSettingsType(int) contentSettingsType}.
int ALL_SITES = 0; // Always first as it should appear in the UI at the top. int ALL_SITES = 0;
int ADS = 1; int ADS = 1;
int AUGMENTED_REALITY = 2; int AUGMENTED_REALITY = 2;
int AUTOMATIC_DOWNLOADS = 3; int AUTOMATIC_DOWNLOADS = 3;
...@@ -71,7 +70,7 @@ public class SiteSettingsCategory { ...@@ -71,7 +70,7 @@ public class SiteSettingsCategory {
int USB = 19; int USB = 19;
int BLUETOOTH = 20; int BLUETOOTH = 20;
int VIRTUAL_REALITY = 21; int VIRTUAL_REALITY = 21;
int USE_STORAGE = 22; // Always last as it should appear in the UI at the bottom. int USE_STORAGE = 22;
/** /**
* Number of handled categories used for calculating array sizes. * Number of handled categories used for calculating array sizes.
*/ */
......
...@@ -28,6 +28,15 @@ import java.util.Map; ...@@ -28,6 +28,15 @@ import java.util.Map;
* that the user has set for them. * that the user has set for them.
*/ */
public class WebsitePermissionsFetcher { public class WebsitePermissionsFetcher {
/**
* An enum describing the types of permissions that exist in website settings.
*/
public enum WebsitePermissionsType {
CONTENT_SETTING_EXCEPTION,
PERMISSION_INFO,
CHOSEN_OBJECT_INFO
}
private BrowserContextHandle mBrowserContextHandle; private BrowserContextHandle mBrowserContextHandle;
private WebsitePreferenceBridge mWebsitePreferenceBridge; private WebsitePreferenceBridge mWebsitePreferenceBridge;
...@@ -39,6 +48,44 @@ public class WebsitePermissionsFetcher { ...@@ -39,6 +48,44 @@ public class WebsitePermissionsFetcher {
void onWebsitePermissionsAvailable(Collection<Website> sites); void onWebsitePermissionsAvailable(Collection<Website> sites);
} }
/**
* A helper function to get the associated WebsitePermissionsType of a particular
* ContentSettingsType
* @param contentSettingsType The ContentSettingsType int of the permission.
*/
public static WebsitePermissionsType getPermissionsType(
@ContentSettingsType int contentSettingsType) {
switch (contentSettingsType) {
case ContentSettingsType.ADS:
case ContentSettingsType.AUTOMATIC_DOWNLOADS:
case ContentSettingsType.BACKGROUND_SYNC:
case ContentSettingsType.BLUETOOTH_SCANNING:
case ContentSettingsType.COOKIES:
case ContentSettingsType.JAVASCRIPT:
case ContentSettingsType.POPUPS:
case ContentSettingsType.SOUND:
return WebsitePermissionsType.CONTENT_SETTING_EXCEPTION;
case ContentSettingsType.AR:
case ContentSettingsType.CLIPBOARD_READ_WRITE:
case ContentSettingsType.GEOLOCATION:
case ContentSettingsType.IDLE_DETECTION:
case ContentSettingsType.MEDIASTREAM_CAMERA:
case ContentSettingsType.MEDIASTREAM_MIC:
case ContentSettingsType.MIDI_SYSEX:
case ContentSettingsType.NFC:
case ContentSettingsType.NOTIFICATIONS:
case ContentSettingsType.PROTECTED_MEDIA_IDENTIFIER:
case ContentSettingsType.SENSORS:
case ContentSettingsType.VR:
return WebsitePermissionsType.PERMISSION_INFO;
case ContentSettingsType.BLUETOOTH_GUARD:
case ContentSettingsType.USB_GUARD:
return WebsitePermissionsType.CHOSEN_OBJECT_INFO;
default:
return null;
}
}
/** /**
* A specialization of Pair to hold an (origin, embedder) tuple. This overrides * A specialization of Pair to hold an (origin, embedder) tuple. This overrides
* android.util.Pair#hashCode, which simply XORs the hashCodes of the pair of values together. * android.util.Pair#hashCode, which simply XORs the hashCodes of the pair of values together.
...@@ -93,80 +140,11 @@ public class WebsitePermissionsFetcher { ...@@ -93,80 +140,11 @@ public class WebsitePermissionsFetcher {
*/ */
public void fetchAllPreferences(WebsitePermissionsCallback callback) { public void fetchAllPreferences(WebsitePermissionsCallback callback) {
TaskQueue queue = new TaskQueue(); TaskQueue queue = new TaskQueue();
// Populate features from more specific to less specific. addFetcherForStorage(queue);
// Geolocation lookup permission is per-origin and per-embedder. for (@ContentSettingsType int type = 0; type < ContentSettingsType.NUM_TYPES; type++) {
queue.add(new PermissionInfoFetcher(ContentSettingsType.GEOLOCATION)); addFetcherForContentSettingsType(queue, type);
// Idle detection permission is per-origin and per-embedder.
queue.add(new PermissionInfoFetcher(ContentSettingsType.IDLE_DETECTION));
// Midi sysex access permission is per-origin and per-embedder.
queue.add(new PermissionInfoFetcher(ContentSettingsType.MIDI_SYSEX));
// Cookies are stored per-host.
queue.add(new ExceptionInfoFetcher(ContentSettingsType.COOKIES));
// Local storage info is per-origin.
queue.add(new LocalStorageInfoFetcher());
// Website storage is per-host.
queue.add(new WebStorageInfoFetcher());
// Popup exceptions are host-based patterns (unless we start
// synchronizing popup exceptions with desktop Chrome).
queue.add(new ExceptionInfoFetcher(ContentSettingsType.POPUPS));
// Ads exceptions are host-based.
queue.add(new ExceptionInfoFetcher(ContentSettingsType.ADS));
// JavaScript exceptions are host-based patterns.
queue.add(new ExceptionInfoFetcher(ContentSettingsType.JAVASCRIPT));
// Sound exceptions are host-based patterns.
queue.add(new ExceptionInfoFetcher(ContentSettingsType.SOUND));
// Protected media identifier permission is per-origin and per-embedder.
queue.add(new PermissionInfoFetcher(ContentSettingsType.PROTECTED_MEDIA_IDENTIFIER));
// Notification permission is per-origin.
queue.add(new PermissionInfoFetcher(ContentSettingsType.NOTIFICATIONS));
// Camera capture permission is per-origin and per-embedder.
queue.add(new PermissionInfoFetcher(ContentSettingsType.MEDIASTREAM_CAMERA));
// Micropohone capture permission is per-origin and per-embedder.
queue.add(new PermissionInfoFetcher(ContentSettingsType.MEDIASTREAM_MIC));
// Background sync permission is per-origin.
queue.add(new ExceptionInfoFetcher(ContentSettingsType.BACKGROUND_SYNC));
// Automatic Downloads permission is per-origin.
queue.add(new ExceptionInfoFetcher(ContentSettingsType.AUTOMATIC_DOWNLOADS));
// USB device permission is per-origin and per-embedder.
queue.add(new ChooserExceptionInfoFetcher(ContentSettingsType.USB_GUARD));
// Clipboard info is per-origin.
queue.add(new PermissionInfoFetcher(ContentSettingsType.CLIPBOARD_READ_WRITE));
// Sensors permission is per-origin.
queue.add(new PermissionInfoFetcher(ContentSettingsType.SENSORS));
// There are two Bluetooth related permissions: Bluetooth scanning and
// Bluetooth guard.
//
// The Bluetooth Scanning permission controls access to the Web Bluetooth
// Scanning API, which enables sites to scan for and receive events for
// advertisement packets received from nearby Bluetooth devices.
CommandLine commandLine = CommandLine.getInstance();
if (commandLine.hasSwitch(ContentSwitches.ENABLE_EXPERIMENTAL_WEB_PLATFORM_FEATURES)) {
// Bluetooth scanning permission is per-origin.
queue.add(new ExceptionInfoFetcher(ContentSettingsType.BLUETOOTH_SCANNING));
}
// The Bluetooth guard permission controls access to the Web Bluetooth
// API, which enables sites to request access to connect to specific
// Bluetooth devices. Users are presented with a chooser prompt in which
// they must select the Bluetooth device that they would like to allow
// the site to connect to. Therefore, this permission also displays a
// list of permitted Bluetooth devices that each site can connect to.
if (ContentFeatureList.isEnabled(
ContentFeatureList.WEB_BLUETOOTH_NEW_PERMISSIONS_BACKEND)) {
// Bluetooth device permission is per-origin and per-embedder.
queue.add(new ChooserExceptionInfoFetcher(ContentSettingsType.BLUETOOTH_GUARD));
} }
if (ContentFeatureList.isEnabled(ContentFeatureList.WEB_NFC)) {
// NFC permission is per-origin and per-embedder.
queue.add(new PermissionInfoFetcher(ContentSettingsType.NFC));
}
// VIRTUAL_REALITY permission is per-origin and per-embedder.
queue.add(new PermissionInfoFetcher(ContentSettingsType.VR));
// AR permission is per-origin and per-embedder.
queue.add(new PermissionInfoFetcher(ContentSettingsType.AR));
queue.add(new PermissionsAvailableCallbackRunner(callback)); queue.add(new PermissionsAvailableCallbackRunner(callback));
queue.next(); queue.next();
} }
...@@ -187,86 +165,73 @@ public class WebsitePermissionsFetcher { ...@@ -187,86 +165,73 @@ public class WebsitePermissionsFetcher {
} }
TaskQueue queue = new TaskQueue(); TaskQueue queue = new TaskQueue();
// Populate features from more specific to less specific. if (category.showSites(SiteSettingsCategory.Type.USE_STORAGE)) {
if (category.showSites(SiteSettingsCategory.Type.DEVICE_LOCATION)) { addFetcherForStorage(queue);
// Geolocation lookup permission is per-origin and per-embedder. } else {
queue.add(new PermissionInfoFetcher(ContentSettingsType.GEOLOCATION)); assert getPermissionsType(category.getContentSettingsType()) != null;
} else if (category.showSites(SiteSettingsCategory.Type.IDLE_DETECTION)) { addFetcherForContentSettingsType(queue, category.getContentSettingsType());
// Idle detection permission is per-origin and per-embedder.
queue.add(new PermissionInfoFetcher(ContentSettingsType.IDLE_DETECTION));
} else if (category.showSites(SiteSettingsCategory.Type.COOKIES)) {
// Cookies exceptions are patterns.
queue.add(new ExceptionInfoFetcher(ContentSettingsType.COOKIES));
} else if (category.showSites(SiteSettingsCategory.Type.USE_STORAGE)) {
// Local storage info is per-origin.
queue.add(new LocalStorageInfoFetcher());
// Website storage is per-host.
queue.add(new WebStorageInfoFetcher());
} else if (category.showSites(SiteSettingsCategory.Type.CAMERA)) {
// Camera capture permission is per-origin and per-embedder.
queue.add(new PermissionInfoFetcher(ContentSettingsType.MEDIASTREAM_CAMERA));
} else if (category.showSites(SiteSettingsCategory.Type.MICROPHONE)) {
// Micropohone capture permission is per-origin and per-embedder.
queue.add(new PermissionInfoFetcher(ContentSettingsType.MEDIASTREAM_MIC));
} else if (category.showSites(SiteSettingsCategory.Type.POPUPS)) {
// Popup exceptions are host-based patterns (unless we start
// synchronizing popup exceptions with desktop Chrome.)
queue.add(new ExceptionInfoFetcher(ContentSettingsType.POPUPS));
} else if (category.showSites(SiteSettingsCategory.Type.ADS)) {
// Ads exceptions are host-based.
queue.add(new ExceptionInfoFetcher(ContentSettingsType.ADS));
} else if (category.showSites(SiteSettingsCategory.Type.JAVASCRIPT)) {
// JavaScript exceptions are host-based patterns.
queue.add(new ExceptionInfoFetcher(ContentSettingsType.JAVASCRIPT));
} else if (category.showSites(SiteSettingsCategory.Type.SOUND)) {
// Sound exceptions are host-based patterns.
queue.add(new ExceptionInfoFetcher(ContentSettingsType.SOUND));
} else if (category.showSites(SiteSettingsCategory.Type.NOTIFICATIONS)) {
// Push notification permission is per-origin.
queue.add(new PermissionInfoFetcher(ContentSettingsType.NOTIFICATIONS));
} else if (category.showSites(SiteSettingsCategory.Type.BACKGROUND_SYNC)) {
// Background sync info is per-origin.
queue.add(new ExceptionInfoFetcher(ContentSettingsType.BACKGROUND_SYNC));
} else if (category.showSites(SiteSettingsCategory.Type.AUTOMATIC_DOWNLOADS)) {
// Automatic downloads info is per-origin.
queue.add(new ExceptionInfoFetcher(ContentSettingsType.AUTOMATIC_DOWNLOADS));
} else if (category.showSites(SiteSettingsCategory.Type.PROTECTED_MEDIA)) {
// Protected media identifier permission is per-origin and per-embedder.
queue.add(new PermissionInfoFetcher(ContentSettingsType.PROTECTED_MEDIA_IDENTIFIER));
} else if (category.showSites(SiteSettingsCategory.Type.USB)) {
// USB device permission is per-origin.
queue.add(new ChooserExceptionInfoFetcher(ContentSettingsType.USB_GUARD));
} else if (category.showSites(SiteSettingsCategory.Type.BLUETOOTH)) {
// Bluetooth device permission is per-origin.
queue.add(new ChooserExceptionInfoFetcher(ContentSettingsType.BLUETOOTH_GUARD));
} else if (category.showSites(SiteSettingsCategory.Type.CLIPBOARD)) {
// Clipboard permission is per-origin.
queue.add(new PermissionInfoFetcher(ContentSettingsType.CLIPBOARD_READ_WRITE));
} else if (category.showSites(SiteSettingsCategory.Type.SENSORS)) {
// Sensors permission is per-origin.
queue.add(new PermissionInfoFetcher(ContentSettingsType.SENSORS));
} else if (category.showSites(SiteSettingsCategory.Type.BLUETOOTH_SCANNING)) {
CommandLine commandLine = CommandLine.getInstance();
if (commandLine.hasSwitch(ContentSwitches.ENABLE_EXPERIMENTAL_WEB_PLATFORM_FEATURES)) {
// Bluetooth scanning permission is per-origin.
queue.add(new ExceptionInfoFetcher(ContentSettingsType.BLUETOOTH_SCANNING));
}
} else if (category.showSites(SiteSettingsCategory.Type.NFC)) {
if (ContentFeatureList.isEnabled(ContentFeatureList.WEB_NFC)) {
// NFC permission is per-origin and per-embedder.
queue.add(new PermissionInfoFetcher(ContentSettingsType.NFC));
}
} else if (category.showSites(SiteSettingsCategory.Type.VIRTUAL_REALITY)) {
// VIRTUAL_REALITY permission is per-origin and per-embedder.
queue.add(new PermissionInfoFetcher(ContentSettingsType.VR));
} else if (category.showSites(SiteSettingsCategory.Type.AUGMENTED_REALITY)) {
// AUGMENTED_REALITY permission is per-origin and per-embedder.
queue.add(new PermissionInfoFetcher(ContentSettingsType.AR));
} }
queue.add(new PermissionsAvailableCallbackRunner(callback)); queue.add(new PermissionsAvailableCallbackRunner(callback));
queue.next(); queue.next();
} }
private void addFetcherForStorage(TaskQueue queue) {
// Local storage info is per-origin.
queue.add(new LocalStorageInfoFetcher());
// Website storage is per-host.
queue.add(new WebStorageInfoFetcher());
}
private void addFetcherForContentSettingsType(
TaskQueue queue, @ContentSettingsType int contentSettingsType) {
WebsitePermissionsType websitePermissionsType = getPermissionsType(contentSettingsType);
if (websitePermissionsType == null) {
return;
}
// Remove this check after the flag is removed.
// The Bluetooth Scanning permission controls access to the Web Bluetooth
// Scanning API, which enables sites to scan for and receive events for
// advertisement packets received from nearby Bluetooth devices.
if (contentSettingsType == ContentSettingsType.BLUETOOTH_SCANNING) {
CommandLine commandLine = CommandLine.getInstance();
if (!commandLine.hasSwitch(ContentSwitches.ENABLE_EXPERIMENTAL_WEB_PLATFORM_FEATURES)) {
return;
}
}
// Remove this check after the flag is removed.
if (contentSettingsType == ContentSettingsType.NFC
&& !ContentFeatureList.isEnabled(ContentFeatureList.WEB_NFC)) {
return;
}
// The Bluetooth guard permission controls access to the Web Bluetooth
// API, which enables sites to request access to connect to specific
// Bluetooth devices. Users are presented with a chooser prompt in which
// they must select the Bluetooth device that they would like to allow
// the site to connect to. Therefore, this permission also displays a
// list of permitted Bluetooth devices that each site can connect to.
// Remove this check after the flag is removed.
if (contentSettingsType == ContentSettingsType.BLUETOOTH_GUARD
&& !ContentFeatureList.isEnabled(
ContentFeatureList.WEB_BLUETOOTH_NEW_PERMISSIONS_BACKEND)) {
return;
}
switch (websitePermissionsType) {
case CONTENT_SETTING_EXCEPTION:
queue.add(new ExceptionInfoFetcher(contentSettingsType));
return;
case PERMISSION_INFO:
queue.add(new PermissionInfoFetcher(contentSettingsType));
return;
case CHOSEN_OBJECT_INFO:
queue.add(new ChooserExceptionInfoFetcher(contentSettingsType));
return;
}
}
private Website findOrCreateSite(String origin, String embedder) { private Website findOrCreateSite(String origin, String embedder) {
// This allows us to show multiple entries in "All sites" for the same origin, based on // This allows us to show multiple entries in "All sites" for the same origin, based on
// the (origin, embedder) combination. For example, "cnn.com", "cnn.com all cookies on this // the (origin, embedder) combination. For example, "cnn.com", "cnn.com all cookies on this
......
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