Commit 0b108ac1 authored by Christian Dullweber's avatar Christian Dullweber Committed by Commit Bot

Remove ContentSettingExceptionType enum

This enum contains a subset of the values of ContentSettingsType.
As WebsitePermissionFetcher defines which ContentSettingsType are
fetched as ContentSettingsException and which as PermissionInfo, we do
not need this duplicate and the code to convert between types.

Bug: 1103597
Change-Id: I17667e76c5091ed6417795cdba807c78b35a85a3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2346425Reviewed-by: default avatarEhimare Okoyomon <eokoyomon@chromium.org>
Reviewed-by: default avatarAndy Paicu <andypaicu@chromium.org>
Commit-Queue: Christian Dullweber <dullweber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#798580}
parent 2ecce21d
......@@ -536,20 +536,19 @@ public class WebsitePermissionsFetcherTest {
// Check content setting exception types.
Assert.assertEquals(Integer.valueOf(ContentSettingValues.DEFAULT),
site.getContentSettingPermission(ContentSettingException.Type.COOKIE));
site.getContentSettingPermission(ContentSettingsType.COOKIES));
Assert.assertEquals(Integer.valueOf(ContentSettingValues.DEFAULT),
site.getContentSettingPermission(ContentSettingException.Type.POPUP));
site.getContentSettingPermission(ContentSettingsType.POPUPS));
Assert.assertEquals(Integer.valueOf(ContentSettingValues.DEFAULT),
site.getContentSettingPermission(ContentSettingException.Type.ADS));
site.getContentSettingPermission(ContentSettingsType.ADS));
Assert.assertEquals(Integer.valueOf(ContentSettingValues.DEFAULT),
site.getContentSettingPermission(ContentSettingException.Type.JAVASCRIPT));
site.getContentSettingPermission(ContentSettingsType.JAVASCRIPT));
Assert.assertEquals(Integer.valueOf(ContentSettingValues.DEFAULT),
site.getContentSettingPermission(ContentSettingException.Type.SOUND));
site.getContentSettingPermission(ContentSettingsType.SOUND));
Assert.assertEquals(Integer.valueOf(ContentSettingValues.DEFAULT),
site.getContentSettingPermission(ContentSettingException.Type.BACKGROUND_SYNC));
site.getContentSettingPermission(ContentSettingsType.BACKGROUND_SYNC));
Assert.assertEquals(Integer.valueOf(ContentSettingValues.DEFAULT),
site.getContentSettingPermission(
ContentSettingException.Type.AUTOMATIC_DOWNLOADS));
site.getContentSettingPermission(ContentSettingsType.AUTOMATIC_DOWNLOADS));
// Check storage info.
ArrayList<StorageInfo> storageInfos = new ArrayList<>(site.getStorageInfo());
......@@ -705,26 +704,21 @@ public class WebsitePermissionsFetcherTest {
String googleOrigin = "https://google.com";
String preferenceSource = "preference";
ArrayList<Integer> contentSettingExceptionTypes = new ArrayList<>(Arrays.asList(
ContentSettingException.Type.ADS, ContentSettingException.Type.AUTOMATIC_DOWNLOADS,
ContentSettingException.Type.BACKGROUND_SYNC,
ContentSettingException.Type.BLUETOOTH_SCANNING,
ContentSettingException.Type.COOKIE, ContentSettingException.Type.JAVASCRIPT,
ContentSettingException.Type.POPUP, ContentSettingException.Type.SOUND));
Assert.assertEquals(8, ContentSettingException.Type.NUM_ENTRIES);
ArrayList<Integer> contentSettingExceptionTypes = new ArrayList<>(
Arrays.asList(ContentSettingsType.ADS, ContentSettingsType.AUTOMATIC_DOWNLOADS,
ContentSettingsType.BACKGROUND_SYNC, ContentSettingsType.BLUETOOTH_SCANNING,
ContentSettingsType.COOKIES, ContentSettingsType.JAVASCRIPT,
ContentSettingsType.POPUPS, ContentSettingsType.SOUND));
for (@ContentSettingsType int type : contentSettingExceptionTypes) {
@ContentSettingsType
int contentSettingsType = ContentSettingException.getContentSettingsType(type);
{
ContentSettingException fakeContentSettingException =
new ContentSettingException(contentSettingsType, googleOrigin,
ContentSettingValues.DEFAULT, preferenceSource);
ContentSettingException fakeContentSettingException = new ContentSettingException(
type, googleOrigin, ContentSettingValues.DEFAULT, preferenceSource);
websitePreferenceBridge.addContentSettingException(fakeContentSettingException);
fetcher.fetchPreferencesForCategory(
SiteSettingsCategory.createFromContentSettingsType(
UNUSED_BROWSER_CONTEXT_HANDLE, contentSettingsType),
UNUSED_BROWSER_CONTEXT_HANDLE, type),
(sites) -> {
Assert.assertEquals(1, sites.size());
......@@ -736,14 +730,13 @@ public class WebsitePermissionsFetcherTest {
// Make sure that the content setting value is updated.
{
ContentSettingException fakeContentSettingException =
new ContentSettingException(contentSettingsType, googleOrigin,
ContentSettingValues.BLOCK, preferenceSource);
ContentSettingException fakeContentSettingException = new ContentSettingException(
type, googleOrigin, ContentSettingValues.BLOCK, preferenceSource);
websitePreferenceBridge.addContentSettingException(fakeContentSettingException);
fetcher.fetchPreferencesForCategory(
SiteSettingsCategory.createFromContentSettingsType(
UNUSED_BROWSER_CONTEXT_HANDLE, contentSettingsType),
UNUSED_BROWSER_CONTEXT_HANDLE, type),
(sites) -> {
Assert.assertEquals(1, sites.size());
......@@ -766,11 +759,8 @@ public class WebsitePermissionsFetcherTest {
String mainSite = "https://a.com";
String thirdPartySite = "https://b.com";
String preferenceSource = "preference";
@ContentSettingException.Type
int contentSettingExceptionType = ContentSettingException.Type.COOKIE;
@ContentSettingsType
int contentSettingsType =
ContentSettingException.getContentSettingsType(contentSettingExceptionType);
int contentSettingsType = ContentSettingsType.COOKIES;
// Test the advanced exception combinations of:
// b.com on a.com
......@@ -797,7 +787,7 @@ public class WebsitePermissionsFetcherTest {
Website site = sites.iterator().next();
assertContentSettingExceptionEquals(fakeContentSettingException,
site.getContentSettingException(contentSettingExceptionType));
site.getContentSettingException(contentSettingsType));
});
}
......@@ -816,7 +806,7 @@ public class WebsitePermissionsFetcherTest {
Website site = sites.iterator().next();
assertContentSettingExceptionEquals(fakeContentSettingException,
site.getContentSettingException(contentSettingExceptionType));
site.getContentSettingException(contentSettingsType));
});
}
}
......
......@@ -73,8 +73,9 @@ public class SingleWebsiteSettingsTest {
@Test
@SmallTest
public void testCorrectMapOfPreferenceKeyToContentSettingsType() {
SingleWebsiteSettings settings = new SingleWebsiteSettings();
for (String key : SingleWebsiteSettings.PERMISSION_PREFERENCE_KEYS) {
Assert.assertEquals(SingleWebsiteSettings.getContentSettingsTypeFromPreferenceKey(key),
Assert.assertEquals(settings.getContentSettingsTypeFromPreferenceKey(key),
getCorrectContentSettingsTypeForPreferenceKey(key));
}
}
......
......@@ -6,42 +6,17 @@ package org.chromium.components.browser_ui.site_settings;
import static org.chromium.components.browser_ui.site_settings.WebsitePreferenceBridge.SITE_WILDCARD;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
import org.chromium.components.content_settings.ContentSettingValues;
import org.chromium.components.content_settings.ContentSettingsType;
import org.chromium.components.embedder_support.browser_context.BrowserContextHandle;
import java.io.Serializable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Exception information for a given origin.
*/
public class ContentSettingException implements Serializable {
@IntDef({Type.ADS, Type.AUTOMATIC_DOWNLOADS, Type.BACKGROUND_SYNC, Type.BLUETOOTH_SCANNING,
Type.COOKIE, Type.JAVASCRIPT, Type.POPUP, Type.SOUND})
@Retention(RetentionPolicy.SOURCE)
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 #getContentSettingsType(int)
// getContentSettingsType} and {@link SingleWebsiteSettings.PERMISSION_PREFERENCE_KEYS}.
int ADS = 0;
int AUTOMATIC_DOWNLOADS = 1;
int BACKGROUND_SYNC = 2;
int BLUETOOTH_SCANNING = 3;
int COOKIE = 4;
int JAVASCRIPT = 5;
int POPUP = 6;
int SOUND = 7;
/**
* Number of handled exceptions used for calculating array sizes.
*/
int NUM_ENTRIES = 8;
}
private final int mContentSettingType;
private final String mPrimaryPattern;
private final String mSecondaryPattern;
......@@ -97,33 +72,9 @@ public class ContentSettingException implements Serializable {
/**
* Sets the content setting value for this exception.
*/
public void setContentSetting(BrowserContextHandle browserContextHandle,
@ContentSettingValues @Nullable Integer value) {
public void setContentSetting(
BrowserContextHandle browserContextHandle, @ContentSettingValues int value) {
WebsitePreferenceBridge.setContentSettingForPattern(browserContextHandle,
mContentSettingType, mPrimaryPattern, mSecondaryPattern, value);
}
public static @ContentSettingsType int getContentSettingsType(@Type int type) {
switch (type) {
case Type.ADS:
return ContentSettingsType.ADS;
case Type.AUTOMATIC_DOWNLOADS:
return ContentSettingsType.AUTOMATIC_DOWNLOADS;
case Type.BACKGROUND_SYNC:
return ContentSettingsType.BACKGROUND_SYNC;
case Type.BLUETOOTH_SCANNING:
return ContentSettingsType.BLUETOOTH_SCANNING;
case Type.COOKIE:
return ContentSettingsType.COOKIES;
case Type.JAVASCRIPT:
return ContentSettingsType.JAVASCRIPT;
case Type.POPUP:
return ContentSettingsType.POPUPS;
case Type.SOUND:
return ContentSettingsType.SOUND;
default:
assert false;
return ContentSettingsType.DEFAULT;
}
}
}
......@@ -210,13 +210,11 @@ public class SingleCategorySettings extends SiteSettingsPreferenceFragment
getSiteSettingsClient().getBrowserContextHandle();
for (@SiteSettingsCategory.Type int i = 0; i < SiteSettingsCategory.Type.NUM_ENTRIES; i++) {
if (!mCategory.showSites(i)) continue;
for (@ContentSettingException.Type int j = 0;
j < ContentSettingException.Type.NUM_ENTRIES; j++) {
if (ContentSettingException.getContentSettingsType(j)
== SiteSettingsCategory.contentSettingsType(i)) {
return ContentSettingValues.BLOCK
== website.site().getContentSettingPermission(j);
}
@ContentSettingValues
Integer contentSettingPermission = website.site().getContentSettingPermission(
SiteSettingsCategory.contentSettingsType(i));
if (contentSettingPermission != null) {
return ContentSettingValues.BLOCK == contentSettingPermission;
}
for (@PermissionInfo.Type int j = 0; j < PermissionInfo.Type.NUM_ENTRIES; j++) {
if (PermissionInfo.getContentSettingsType(j)
......
......@@ -35,10 +35,9 @@ import org.chromium.components.content_settings.ContentSettingsType;
import org.chromium.components.embedder_support.browser_context.BrowserContextHandle;
import org.chromium.components.embedder_support.util.Origin;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.HashMap;
import java.util.Map;
/**
* Shows the permissions and other settings for a particular website.
......@@ -71,19 +70,38 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
// Buttons:
public static final String PREF_RESET_SITE = "reset_site_button";
/**
* Currently only implemented for ContentSettingsType that have a ContentSettingsException.
* TODO(crbug.com/1103597): Replace PermissionInfo.Type enum with ContentSettingsType.
* @param type ContentSettingsType
* @return The preference key of this type
*/
private static @Nullable String getPreferenceKey(@ContentSettingsType int type) {
switch (type) {
case ContentSettingsType.ADS:
return "ads_permission_list";
case ContentSettingsType.AUTOMATIC_DOWNLOADS:
return "automatic_downloads_permission_list";
case ContentSettingsType.BACKGROUND_SYNC:
return "background_sync_permission_list";
case ContentSettingsType.BLUETOOTH_SCANNING:
return "bluetooth_scanning_permission_list";
case ContentSettingsType.COOKIES:
return "cookies_permission_list";
case ContentSettingsType.JAVASCRIPT:
return "javascript_permission_list";
case ContentSettingsType.POPUPS:
return "popup_permission_list";
case ContentSettingsType.SOUND:
return "sound_permission_list";
default:
return null;
}
}
// Website permissions (if adding new, see hasPermissionsPreferences and resetSite below)
// All permissions from the permissions preference category must be listed here.
public static final String[] PERMISSION_PREFERENCE_KEYS = {
// Permission keys mapped for next {@link ContentSettingException.Type} values.
"ads_permission_list", // ContentSettingException.Type.ADS
"automatic_downloads_permission_list",
// ContentSettingException.Type.AUTOMATIC_DOWNLOADS
"background_sync_permission_list", // ContentSettingException.Type.BACKGROUND_SYNC
"bluetooth_scanning_permission_list", // ContentSettingException.Type.BLUETOOTH_SCANNING
"cookies_permission_list", // ContentSettingException.Type.COOKIE
"javascript_permission_list", // ContentSettingException.Type.JAVASCRIPT
"popup_permission_list", // ContentSettingException.Type.POPUP
"sound_permission_list", // ContentSettingException.Type.SOUND
// Permission keys mapped for next {@link PermissionInfo.Type} values.
"ar_permission_list", // PermissionInfo.Type.AUGMENTED_REALITY
"camera_permission_list", // PermissionInfo.Type.CAMERA
......@@ -123,6 +141,8 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
// The Preference key for chooser object permissions.
private static final String CHOOSER_PERMISSION_PREFERENCE_KEY = "chooser_permission_list";
// The maximum order of a permission preference.
private int mMaxPermissionOrder;
// The number of user and policy chosen object permissions displayed.
private int mObjectUserPermissionCount;
private int mObjectPolicyPermissionCount;
......@@ -131,6 +151,9 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
// revocation within the Android system permission activity.
private @ContentSettingValues @Nullable Integer mPreviousNotificationPermission;
// Map from preference key to ContentSettingsType.
private Map<String, Integer> mPreferenceMap;
private class SingleWebsitePermissionsPopulator
implements WebsitePermissionsFetcher.WebsitePermissionsCallback {
private final WebsiteAddress mSiteAddress;
......@@ -260,11 +283,11 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
// This loop looks expensive, but the amount of data is likely to be relatively small
// because most sites have very few permissions.
for (Website other : websites) {
if (merged.getContentSettingException(ContentSettingException.Type.ADS) == null
&& other.getContentSettingException(ContentSettingException.Type.ADS) != null
if (merged.getContentSettingException(ContentSettingsType.ADS) == null
&& other.getContentSettingException(ContentSettingsType.ADS) != null
&& other.compareByAddressTo(merged) == 0) {
merged.setContentSettingException(ContentSettingException.Type.ADS,
other.getContentSettingException(ContentSettingException.Type.ADS));
merged.setContentSettingException(ContentSettingsType.ADS,
other.getContentSettingException(ContentSettingsType.ADS));
}
for (@PermissionInfo.Type int type = 0; type < PermissionInfo.Type.NUM_ENTRIES;
type++) {
......@@ -291,15 +314,13 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
}
}
if (host.equals(other.getAddress().getHost())) {
for (@ContentSettingException.Type int type = 0;
type < ContentSettingException.Type.NUM_ENTRIES; type++) {
if (type == ContentSettingException.Type.ADS) {
for (ContentSettingException exception : other.getContentSettingExceptions()) {
int type = exception.getContentSettingType();
if (type == ContentSettingsType.ADS) {
continue;
}
if (merged.getContentSettingException(type) == null
&& other.getContentSettingException(type) != null) {
merged.setContentSettingException(
type, other.getContentSettingException(type));
if (merged.getContentSettingException(type) == null) {
merged.setContentSettingException(type, exception);
}
}
}
......@@ -329,22 +350,19 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
}
SettingsUtils.addPreferencesFromResource(this, R.xml.single_website_preferences);
Set<String> permissionPreferenceKeys =
new HashSet<>(Arrays.asList(PERMISSION_PREFERENCE_KEYS));
int maxPermissionOrder = 0;
mMaxPermissionOrder = 0;
PreferenceScreen preferenceScreen = getPreferenceScreen();
// Iterate over preferences in reverse order because some preferences will be removed during
// this setup, causing indices of later preferences to change.
for (int i = preferenceScreen.getPreferenceCount() - 1; i >= 0; i--) {
Preference preference = preferenceScreen.getPreference(i);
setUpPreference(preference);
// Keep track of the maximum 'order' value of permission preferences, to allow correct
// positioning of subsequent permission preferences.
if (permissionPreferenceKeys.contains(preference.getKey())) {
maxPermissionOrder = Math.max(maxPermissionOrder, preference.getOrder());
if (getContentSettingsTypeFromPreferenceKey(preference.getKey())
!= ContentSettingsType.DEFAULT) {
mMaxPermissionOrder = Math.max(mMaxPermissionOrder, preference.getOrder());
}
}
setUpChosenObjectPreferences(maxPermissionOrder);
setUpChosenObjectPreferences(mMaxPermissionOrder);
setUpOsWarningPreferences();
setUpAdsInformationalBanner();
......@@ -375,29 +393,9 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
} else if (PREF_RESET_SITE.equals(preference.getKey())) {
setupResetSitePreference(preference);
} else {
assert PERMISSION_PREFERENCE_KEYS.length
== ContentSettingException.Type.NUM_ENTRIES + PermissionInfo.Type.NUM_ENTRIES;
for (@ContentSettingException.Type int i = 0;
i < ContentSettingException.Type.NUM_ENTRIES; i++) {
if (!PERMISSION_PREFERENCE_KEYS[i].equals(preference.getKey())) {
continue;
}
if (i == ContentSettingException.Type.ADS) {
setUpAdsPreference(preference);
} else if (i == ContentSettingException.Type.SOUND) {
setUpSoundPreference(preference);
} else if (i == ContentSettingException.Type.JAVASCRIPT) {
setUpJavascriptPreference(preference);
} else {
// ContentSettingException can not be embargoed.
setUpListPreference(preference, mSite.getContentSettingPermission(i),
false /* isEmbargoed */);
}
return;
}
assert PERMISSION_PREFERENCE_KEYS.length == PermissionInfo.Type.NUM_ENTRIES;
for (@PermissionInfo.Type int i = 0; i < PermissionInfo.Type.NUM_ENTRIES; i++) {
if (!PERMISSION_PREFERENCE_KEYS[i + ContentSettingException.Type.NUM_ENTRIES]
.equals(preference.getKey())) {
if (!PERMISSION_PREFERENCE_KEYS[i].equals(preference.getKey())) {
continue;
}
if (i == PermissionInfo.Type.GEOLOCATION) {
......@@ -413,6 +411,22 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
}
return;
}
@ContentSettingsType
int type = getContentSettingsTypeFromPreferenceKey(preference.getKey());
if (type != ContentSettingsType.DEFAULT) {
if (type == ContentSettingsType.ADS) {
setUpAdsPreference(preference);
} else if (type == ContentSettingsType.SOUND) {
setUpSoundPreference(preference);
} else if (type == ContentSettingsType.JAVASCRIPT) {
setUpJavascriptPreference(preference);
} else {
// ContentSettingException can not be embargoed.
setUpListPreference(preference, mSite.getContentSettingPermission(type),
false /* isEmbargoed */);
}
return;
}
}
}
......@@ -613,8 +627,7 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
// User has navigated back from system channel settings on O+. Ensure notification
// preference is up to date, since they might have toggled it from channel settings.
Preference notificationsPreference =
findPreference(PERMISSION_PREFERENCE_KEYS[PermissionInfo.Type.NOTIFICATION
+ ContentSettingException.Type.NUM_ENTRIES]);
findPreference(PERMISSION_PREFERENCE_KEYS[PermissionInfo.Type.NOTIFICATION]);
if (notificationsPreference != null) {
setUpNotificationsPreference(notificationsPreference, false /* isEmbargoed */);
}
......@@ -722,8 +735,7 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
&& WebsitePreferenceBridge.getAdBlockingActivated(
getSiteSettingsClient().getBrowserContextHandle(),
mSite.getAddress().getOrigin())
&& findPreference(PERMISSION_PREFERENCE_KEYS[ContentSettingException.Type.ADS])
!= null;
&& findPreference(getPreferenceKey(ContentSettingsType.ADS)) != null;
if (!adBlockingActivated) {
removePreferenceSafely(PREF_INTRUSIVE_ADS_INFO);
......@@ -767,9 +779,8 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
for (int i = 0; i < PermissionInfo.Type.NUM_ENTRIES; i++) {
if (PermissionInfo.getContentSettingsType(i)
== SiteSettingsCategory.contentSettingsType(type)) {
return mSite.getPermission(browserContextHandle, i) == null
? false
: SiteSettingsCategory.createFromType(browserContextHandle, type)
return mSite.getPermission(browserContextHandle, i) != null
&& SiteSettingsCategory.createFromType(browserContextHandle, type)
.showPermissionBlockedMessage(getActivity());
}
}
......@@ -784,9 +795,7 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
private boolean hasPermissionsPreferences() {
if (mObjectUserPermissionCount > 0 || mObjectPolicyPermissionCount > 0) return true;
for (String key : PERMISSION_PREFERENCE_KEYS) {
if (findPreference(key) != null) return true;
}
if (mMaxPermissionOrder > 0) return true;
return false;
}
......@@ -868,8 +877,7 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
private void setUpSoundPreference(Preference preference) {
@ContentSettingValues
@Nullable
Integer currentValue =
mSite.getContentSettingPermission(ContentSettingException.Type.SOUND);
Integer currentValue = mSite.getContentSettingPermission(ContentSettingsType.SOUND);
// In order to always show the sound permission, set it up with the default value if it
// doesn't have a current value.
if (currentValue == null) {
......@@ -886,8 +894,7 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
private void setUpJavascriptPreference(Preference preference) {
@ContentSettingValues
@Nullable
Integer currentValue =
mSite.getContentSettingPermission(ContentSettingException.Type.JAVASCRIPT);
Integer currentValue = mSite.getContentSettingPermission(ContentSettingsType.JAVASCRIPT);
// If Javascript is blocked by default, then always show a Javascript permission.
// To do this, set it to the default value (blocked).
if ((currentValue == null)
......@@ -919,7 +926,7 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
getSiteSettingsClient().getBrowserContextHandle(), mSite.getAddress().getOrigin());
@ContentSettingValues
@Nullable
Integer permission = mSite.getContentSettingPermission(ContentSettingException.Type.ADS);
Integer permission = mSite.getContentSettingPermission(ContentSettingsType.ADS);
// If |permission| is null, there is no explicit (non-default) permission set for this site.
// If the site is not considered a candidate for blocking, do the standard thing and remove
......@@ -972,16 +979,23 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
});
}
public static int getContentSettingsTypeFromPreferenceKey(String preferenceKey) {
public @ContentSettingsType int getContentSettingsTypeFromPreferenceKey(String preferenceKey) {
if (mPreferenceMap == null) {
mPreferenceMap = new HashMap<>();
for (int i = 0; i < PERMISSION_PREFERENCE_KEYS.length; i++) {
if (PERMISSION_PREFERENCE_KEYS[i].equals(preferenceKey)) {
return i < ContentSettingException.Type.NUM_ENTRIES
? ContentSettingException.getContentSettingsType(i)
: PermissionInfo.getContentSettingsType(
i - ContentSettingException.Type.NUM_ENTRIES);
mPreferenceMap.put(
PERMISSION_PREFERENCE_KEYS[i], PermissionInfo.getContentSettingsType(i));
}
for (@ContentSettingsType int type = 0; type < ContentSettingsType.NUM_TYPES; type++) {
String key = getPreferenceKey(type);
if (key != null) {
mPreferenceMap.put(key, type);
}
}
return 0;
}
Integer type = mPreferenceMap.get(preferenceKey);
if (type != null) return type;
return ContentSettingsType.DEFAULT;
}
private void popBackIfNoSettings() {
......@@ -1001,16 +1015,16 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
getSiteSettingsClient().getBrowserContextHandle();
for (int i = 0; i < PERMISSION_PREFERENCE_KEYS.length; i++) {
if (PERMISSION_PREFERENCE_KEYS[i].equals(preference.getKey())) {
if (i < ContentSettingException.Type.NUM_ENTRIES) {
mSite.setContentSettingPermission(
getSiteSettingsClient().getBrowserContextHandle(), i, permission);
} else {
mSite.setPermission(browserContextHandle,
i - ContentSettingException.Type.NUM_ENTRIES, permission);
}
mSite.setPermission(browserContextHandle, i, permission);
return true;
}
}
int type = getContentSettingsTypeFromPreferenceKey(preference.getKey());
if (type != ContentSettingsType.DEFAULT) {
mSite.setContentSettingPermission(
getSiteSettingsClient().getBrowserContextHandle(), type, permission);
}
return true;
}
......@@ -1045,6 +1059,12 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
for (String key : PERMISSION_PREFERENCE_KEYS) {
removePreferenceSafely(key);
}
for (@ContentSettingsType int type = 0; type < ContentSettingsType.NUM_TYPES; type++) {
String key = getPreferenceKey(type);
if (key != null) {
removePreferenceSafely(key);
}
}
// Clearing stored data implies popping back to parent menu if there is nothing left to
// show. Therefore, we only need to explicitly close the activity if there's no stored data
......
......@@ -24,10 +24,9 @@ public class SiteDataCleaner {
WebsitePreferenceBridgeJni.get().clearMediaLicenses(browserContextHandle, origin);
// Clear the permissions.
for (@ContentSettingException.Type int type = 0;
type < ContentSettingException.Type.NUM_ENTRIES; type++) {
site.setContentSettingPermission(
browserContextHandle, type, ContentSettingValues.DEFAULT);
for (ContentSettingException exception : site.getContentSettingExceptions()) {
site.setContentSettingPermission(browserContextHandle,
exception.getContentSettingType(), ContentSettingValues.DEFAULT);
}
for (@PermissionInfo.Type int type = 0; type < PermissionInfo.Type.NUM_ENTRIES; type++) {
site.setPermission(browserContextHandle, type, ContentSettingValues.DEFAULT);
......
......@@ -17,34 +17,30 @@ import org.chromium.components.embedder_support.browser_context.BrowserContextHa
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Website is a class for storing information about a website and its associated permissions.
*/
public class Website implements Serializable {
static final int INVALID_CAMERA_OR_MICROPHONE_ACCESS = 0;
static final int CAMERA_ACCESS_ALLOWED = 1;
static final int MICROPHONE_AND_CAMERA_ACCESS_ALLOWED = 2;
static final int MICROPHONE_ACCESS_ALLOWED = 3;
static final int CAMERA_ACCESS_DENIED = 4;
static final int MICROPHONE_AND_CAMERA_ACCESS_DENIED = 5;
static final int MICROPHONE_ACCESS_DENIED = 6;
public final class Website implements Serializable {
private final WebsiteAddress mOrigin;
private final WebsiteAddress mEmbedder;
/**
* Indexed by ContentSettingException.Type.
* Indexed by ContentSettingsType.
*/
private ContentSettingException mContentSettingException[];
private Map<Integer, ContentSettingException> mContentSettingExceptions = new HashMap<>();
/**
* Indexed by PermissionInfo.Type.
*/
private PermissionInfo[] mPermissionInfo;
private LocalStorageInfo mLocalStorageInfo;
private final List<StorageInfo> mStorageInfo = new ArrayList<StorageInfo>();
private final List<StorageInfo> mStorageInfo = new ArrayList<>();
private int mStorageInfoCallbacksLeft;
// The collection of chooser-based permissions (e.g. USB device access) granted to this site.
......@@ -56,8 +52,6 @@ public class Website implements Serializable {
mOrigin = origin;
mEmbedder = embedder;
mPermissionInfo = new PermissionInfo[PermissionInfo.Type.NUM_ENTRIES];
mContentSettingException =
new ContentSettingException[ContentSettingException.Type.NUM_ENTRIES];
}
public WebsiteAddress getAddress() {
......@@ -164,29 +158,34 @@ public class Website implements Serializable {
}
}
public Collection<ContentSettingException> getContentSettingExceptions() {
return mContentSettingExceptions.values();
}
/**
* Returns the exception info for this Website for specified type.
*/
public ContentSettingException getContentSettingException(
@ContentSettingException.Type int type) {
return mContentSettingException[type];
public ContentSettingException getContentSettingException(@ContentSettingsType int type) {
return mContentSettingExceptions.get(type);
}
/**
* Sets the exception info for this Website for specified type.
*/
public void setContentSettingException(
@ContentSettingException.Type int type, ContentSettingException exception) {
mContentSettingException[type] = exception;
@ContentSettingsType int type, ContentSettingException exception) {
mContentSettingExceptions.put(type, exception);
}
/**
* Returns what ContentSettingException governs the setting of specified type.
*/
public @ContentSettingValues @Nullable Integer getContentSettingPermission(
@ContentSettingException.Type int type) {
return mContentSettingException[type] != null
? mContentSettingException[type].getContentSetting()
@ContentSettingsType int type) {
// TODO(crbug.com/1103597): Merge with getPermission() when both are keyed by
// ContentSettingsType.
return getContentSettingException(type) != null
? getContentSettingException(type).getContentSetting()
: null;
}
......@@ -194,24 +193,28 @@ public class Website implements Serializable {
* Sets the permission.
*/
public void setContentSettingPermission(BrowserContextHandle browserContextHandle,
@ContentSettingException.Type int type, @ContentSettingValues int value) {
if (type == ContentSettingException.Type.ADS) {
@ContentSettingsType int type, @ContentSettingValues int value) {
// TODO(crbug.com/1103597): Merge with setPermission() when both are keyed by
// ContentSettingsType.
ContentSettingException exception = getContentSettingException(type);
if (type == ContentSettingsType.ADS) {
// It is possible to set the permission without having an existing exception,
// because we can show the BLOCK state even when this permission is set to the
// default. In that case, just set an exception now to BLOCK to enable changing the
// permission.
if (mContentSettingException[type] == null) {
mContentSettingException[type] =
new ContentSettingException(ContentSettingsType.ADS,
if (exception == null) {
exception = new ContentSettingException(ContentSettingsType.ADS,
getAddress().getOrigin(), ContentSettingValues.BLOCK, "");
setContentSettingException(type, exception);
}
} else if (type == ContentSettingException.Type.JAVASCRIPT) {
} else if (type == ContentSettingsType.JAVASCRIPT) {
// It is possible to set the permission without having an existing exception,
// because we show the javascript permission in Site Settings if javascript
// is blocked by default.
if (mContentSettingException[type] == null) {
mContentSettingException[type] = new ContentSettingException(
if (exception == null) {
exception = new ContentSettingException(
ContentSettingsType.JAVASCRIPT, getAddress().getHost(), value, "");
setContentSettingException(type, exception);
}
// It's possible for either action to be emitted. This code path is hit
// regardless of whether there was an existing permission or not.
......@@ -220,12 +223,13 @@ public class Website implements Serializable {
} else {
RecordUserAction.record("JavascriptContentSetting.DisableBy.SiteSettings");
}
} else if (type == ContentSettingException.Type.SOUND) {
} else if (type == ContentSettingsType.SOUND) {
// It is possible to set the permission without having an existing exception,
// because we always show the sound permission in Site Settings.
if (mContentSettingException[type] == null) {
mContentSettingException[type] = new ContentSettingException(
if (exception == null) {
exception = new ContentSettingException(
ContentSettingsType.SOUND, getAddress().getHost(), value, "");
setContentSettingException(type, exception);
}
if (value == ContentSettingValues.BLOCK) {
RecordUserAction.record("SoundContentSetting.MuteBy.SiteSettings");
......@@ -236,8 +240,8 @@ public class Website implements Serializable {
// We want to call setContentSetting even after explicitly setting
// mContentSettingException above because this will trigger the actual change
// on the PrefServiceBridge.
if (mContentSettingException[type] != null) {
mContentSettingException[type].setContentSetting(browserContextHandle, value);
if (exception != null) {
exception.setContentSetting(browserContextHandle, value);
}
}
......
......@@ -285,20 +285,6 @@ public class WebsitePermissionsFetcher {
}
private void setException(int contentSettingsType) {
@ContentSettingException.Type
int exceptionType;
for (exceptionType = 0; exceptionType < ContentSettingException.Type.NUM_ENTRIES;
exceptionType++) {
if (contentSettingsType
== ContentSettingException.getContentSettingsType(exceptionType)) {
break;
}
}
assert contentSettingsType
== ContentSettingException.getContentSettingsType(exceptionType)
: "Unexpected content setting type received: "
+ contentSettingsType;
for (ContentSettingException exception :
mWebsitePreferenceBridge.getContentSettingsExceptions(
mBrowserContextHandle, contentSettingsType)) {
......@@ -309,7 +295,7 @@ public class WebsitePermissionsFetcher {
continue;
}
Website site = findOrCreateSite(address, embedder);
site.setContentSettingException(exceptionType, exception);
site.setContentSettingException(contentSettingsType, exception);
}
}
......
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