Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Commit

Permalink
Adding <keygen> Content Setting (Android UI)
Browse files Browse the repository at this point in the history
This adds Android UI to allow the user to change the Content Setting to block/allow keygen usage on a per-site basis.

To trigger the exception, we also create a Content Exception on any site that uses a <keygen> element.

BUG=514767

Review URL: https://codereview.chromium.org/1432573002

Cr-Commit-Position: refs/heads/master@{#370416}
  • Loading branch information
svaldez authored and Commit bot committed Jan 20, 2016
1 parent a72c69a commit 34a7682
Show file tree
Hide file tree
Showing 11 changed files with 257 additions and 2 deletions.
2 changes: 2 additions & 0 deletions chrome/android/java/res/xml/single_website_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
android:key="push_notifications_list" />
<org.chromium.chrome.browser.preferences.ChromeBaseListPreference
android:key="protected_media_identifier_permission_list" />
<org.chromium.chrome.browser.preferences.ChromeBaseListPreference
android:key="keygen_permission_list" />
<org.chromium.chrome.browser.preferences.ButtonPreference
android:key="reset_site_button"
android:title="@string/website_reset" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,8 @@ public void onClick(View view) {
public void run() {
Bundle fragmentArguments =
SingleWebsitePreferences.createFragmentArgsForSite(mFullUrl);
fragmentArguments.putParcelable(SingleWebsitePreferences.EXTRA_WEB_CONTENTS,
mWebContents);
Intent preferencesIntent = PreferencesLauncher.createIntentForSettingsPage(
mContext, SingleWebsitePreferences.class.getName());
preferencesIntent.putExtra(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ private static Map<Integer, ResourceItem> getResourceInfo() {
new ResourceItem(R.drawable.permission_protected_media,
org.chromium.chrome.R.string.protected_content, 0,
ContentSetting.ASK, ContentSetting.BLOCK, 0, 0));
localMap.put(ContentSettingsType.CONTENT_SETTINGS_TYPE_KEYGEN,
new ResourceItem(R.drawable.permission_keygen,
R.string.keygen_permission_title,
R.string.keygen_permission_title, ContentSetting.ALLOW,
ContentSetting.BLOCK,
0, R.string.website_settings_category_blocked_recommended));
sResourceInfo = localMap;
}
return sResourceInfo;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.chrome.browser.preferences.website;

/**
* Keygen information for a given origin.
*/
public class KeygenInfo extends PermissionInfo {
public KeygenInfo(String origin, String embedder, boolean isIncognito) {
super(origin, embedder, isIncognito);
}

protected int getNativePreferenceValue(String origin, String embedder, boolean isIncognito) {
return WebsitePreferenceBridge.nativeGetKeygenSettingForOrigin(
origin, embedder, isIncognito);
}

protected void setNativePreferenceValue(
String origin, String embedder, ContentSetting value, boolean isIncognito) {
WebsitePreferenceBridge.nativeSetKeygenSettingForOrigin(
origin, embedder, value.toInt(), isIncognito);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.chromium.chrome.browser.omnibox.geo.GeolocationHeader;
import org.chromium.chrome.browser.search_engines.TemplateUrlService;
import org.chromium.chrome.browser.util.UrlUtilities;
import org.chromium.content_public.browser.WebContents;

import java.net.URI;
import java.util.ArrayList;
Expand All @@ -52,6 +53,8 @@ public class SingleWebsitePreferences extends PreferenceFragment
public static final String EXTRA_ORIGIN = "org.chromium.chrome.preferences.origin";
public static final String EXTRA_LOCATION = "org.chromium.chrome.preferences.location";

public static final String EXTRA_WEB_CONTENTS = "org.chromium.chrome.preferences.web_contents";

// Preference keys, see single_website_preferences.xml
// Headings:
public static final String PREF_SITE_TITLE = "site_title";
Expand All @@ -70,6 +73,7 @@ public class SingleWebsitePreferences extends PreferenceFragment
public static final String PREF_COOKIES_PERMISSION = "cookies_permission_list";
public static final String PREF_FULLSCREEN_PERMISSION = "fullscreen_permission_list";
public static final String PREF_JAVASCRIPT_PERMISSION = "javascript_permission_list";
public static final String PREF_KEYGEN_PERMISSION = "keygen_permission_list";
public static final String PREF_LOCATION_ACCESS = "location_access_list";
public static final String PREF_MIC_CAPTURE_PERMISSION = "microphone_permission_list";
public static final String PREF_MIDI_SYSEX_PERMISSION = "midi_sysex_permission_list";
Expand All @@ -86,6 +90,7 @@ public class SingleWebsitePreferences extends PreferenceFragment
PREF_COOKIES_PERMISSION,
PREF_FULLSCREEN_PERMISSION,
PREF_JAVASCRIPT_PERMISSION,
PREF_KEYGEN_PERMISSION,
PREF_LOCATION_ACCESS,
PREF_MIC_CAPTURE_PERMISSION,
PREF_MIDI_SYSEX_PERMISSION,
Expand All @@ -102,6 +107,12 @@ public class SingleWebsitePreferences extends PreferenceFragment

private class SingleWebsitePermissionsPopulator
implements WebsitePermissionsFetcher.WebsitePermissionsCallback {
private final WebContents mWebContents;

public SingleWebsitePermissionsPopulator(WebContents webContents) {
mWebContents = webContents;
}

@Override
public void onWebsitePermissionsAvailable(
Map<String, Set<Website>> sitesByOrigin, Map<String, Set<Website>> sitesByHost) {
Expand All @@ -115,6 +126,14 @@ public void onWebsitePermissionsAvailable(
allSites.addAll(sitesByHost.values());
// TODO(mvanouwerkerk): Avoid modifying the outer class from this inner class.
mSite = mergePermissionInfoForTopLevelOrigin(mSiteAddress, allSites);

// Display Keygen Content Setting if Keygen is blocked.
if (mSite.getKeygenInfo() == null && mWebContents != null
&& WebsitePreferenceBridge.getKeygenBlocked(mWebContents)) {
String origin = mSiteAddress.getOrigin();
mSite.setKeygenInfo(new KeygenInfo(origin, origin, false));
}

displaySitePermissions();
}
}
Expand Down Expand Up @@ -145,14 +164,17 @@ public void onActivityCreated(Bundle savedInstanceState) {

Object extraSite = getArguments().getSerializable(EXTRA_SITE);
Object extraOrigin = getArguments().getSerializable(EXTRA_ORIGIN);
getArguments().setClassLoader(WebContents.class.getClassLoader());
Object webContents = getArguments().get(EXTRA_WEB_CONTENTS);

if (extraSite != null && extraOrigin == null) {
mSite = (Website) extraSite;
displaySitePermissions();
} else if (extraOrigin != null && extraSite == null) {
mSiteAddress = WebsiteAddress.create((String) extraOrigin);
WebsitePermissionsFetcher fetcher =
new WebsitePermissionsFetcher(new SingleWebsitePermissionsPopulator());
WebsitePermissionsFetcher fetcher;
fetcher = new WebsitePermissionsFetcher(
new SingleWebsitePermissionsPopulator((WebContents) webContents));
fetcher.fetchAllPreferences();
} else {
assert false : "Exactly one of EXTRA_SITE or EXTRA_SITE_ADDRESS must be provided.";
Expand Down Expand Up @@ -193,6 +215,10 @@ && permissionInfoIsForTopLevelOrigin(other.getFullscreenInfo(), origin)) {
&& permissionInfoIsForTopLevelOrigin(other.getGeolocationInfo(), origin)) {
merged.setGeolocationInfo(other.getGeolocationInfo());
}
if (merged.getKeygenInfo() == null && other.getKeygenInfo() != null
&& permissionInfoIsForTopLevelOrigin(other.getKeygenInfo(), origin)) {
merged.setKeygenInfo(other.getKeygenInfo());
}
if (merged.getMidiInfo() == null && other.getMidiInfo() != null
&& permissionInfoIsForTopLevelOrigin(other.getMidiInfo(), origin)) {
merged.setMidiInfo(other.getMidiInfo());
Expand Down Expand Up @@ -284,6 +310,8 @@ private void displaySitePermissions() {
setUpListPreference(preference, mSite.getFullscreenPermission());
} else if (PREF_JAVASCRIPT_PERMISSION.equals(preference.getKey())) {
setUpListPreference(preference, mSite.getJavaScriptPermission());
} else if (PREF_KEYGEN_PERMISSION.equals(preference.getKey())) {
setUpListPreference(preference, mSite.getKeygenPermission());
} else if (PREF_LOCATION_ACCESS.equals(preference.getKey())) {
setUpLocationPreference(preference);
} else if (PREF_MIC_CAPTURE_PERMISSION.equals(preference.getKey())) {
Expand Down Expand Up @@ -508,6 +536,8 @@ private int getContentSettingsTypeFromPreferenceKey(String preferenceKey) {
return ContentSettingsType.CONTENT_SETTINGS_TYPE_FULLSCREEN;
case PREF_JAVASCRIPT_PERMISSION:
return ContentSettingsType.CONTENT_SETTINGS_TYPE_JAVASCRIPT;
case PREF_KEYGEN_PERMISSION:
return ContentSettingsType.CONTENT_SETTINGS_TYPE_KEYGEN;
case PREF_LOCATION_ACCESS:
return ContentSettingsType.CONTENT_SETTINGS_TYPE_GEOLOCATION;
case PREF_MIC_CAPTURE_PERMISSION:
Expand Down Expand Up @@ -566,6 +596,8 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
mSite.setFullscreenPermission(permission);
} else if (PREF_JAVASCRIPT_PERMISSION.equals(preference.getKey())) {
mSite.setJavaScriptPermission(permission);
} else if (PREF_KEYGEN_PERMISSION.equals(preference.getKey())) {
mSite.setKeygenPermission(permission);
} else if (PREF_LOCATION_ACCESS.equals(preference.getKey())) {
mSite.setGeolocationPermission(permission);
} else if (PREF_MIC_CAPTURE_PERMISSION.equals(preference.getKey())) {
Expand Down Expand Up @@ -618,6 +650,7 @@ private void resetSite() {
mSite.setFullscreenPermission(ContentSetting.DEFAULT);
mSite.setGeolocationPermission(ContentSetting.DEFAULT);
mSite.setJavaScriptPermission(ContentSetting.DEFAULT);
mSite.setKeygenPermission(ContentSetting.DEFAULT);
mSite.setMicrophonePermission(ContentSetting.DEFAULT);
mSite.setMidiPermission(ContentSetting.DEFAULT);
mSite.setPopupPermission(ContentSetting.DEFAULT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class Website implements Serializable {
private CameraInfo mCameraInfo;
private CookieInfo mCookieInfo;
private GeolocationInfo mGeolocationInfo;
private KeygenInfo mKeygenInfo;
private MicrophoneInfo mMicrophoneInfo;
private MidiInfo mMidiInfo;
private ContentSettingException mJavaScriptException;
Expand Down Expand Up @@ -132,6 +133,37 @@ public void setGeolocationPermission(ContentSetting value) {
}
}

/**
* Sets the KeygenInfo object for this Website.
*/
public void setKeygenInfo(KeygenInfo info) {
mKeygenInfo = info;
WebsiteAddress embedder = WebsiteAddress.create(info.getEmbedder());
if (embedder != null) {
mSummary = embedder.getTitle();
}
}

public KeygenInfo getKeygenInfo() {
return mKeygenInfo;
}

/**
* Returns what permission governs keygen access.
*/
public ContentSetting getKeygenPermission() {
return mKeygenInfo != null ? mKeygenInfo.getContentSetting() : null;
}

/**
* Configure keygen access setting for this site.
*/
public void setKeygenPermission(ContentSetting value) {
if (mKeygenInfo != null) {
mKeygenInfo.setContentSetting(value);
}
}

/**
* Sets the MidiInfo object for this Website.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public void fetchAllPreferences() {
queue.add(new CookieInfoFetcher());
// Fullscreen are stored per-origin.
queue.add(new FullscreenInfoFetcher());
// Keygen permissions are per-origin.
queue.add(new KeygenInfoFetcher());
// Local storage info is per-origin.
queue.add(new LocalStorageInfoFetcher());
// Website storage is per-host.
Expand Down Expand Up @@ -248,6 +250,17 @@ public void run() {
}
}

private class KeygenInfoFetcher extends Task {
@Override
public void run() {
for (KeygenInfo info : WebsitePreferenceBridge.getKeygenInfo()) {
WebsiteAddress address = WebsiteAddress.create(info.getOrigin());
if (address == null) continue;
createSiteByOriginAndHost(address).setKeygenInfo(info);
}
}
}

private class CookieInfoFetcher extends Task {
@Override
public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.chromium.base.annotations.CalledByNative;
import org.chromium.chrome.browser.preferences.PrefServiceBridge;
import org.chromium.content_public.browser.WebContents;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -41,6 +42,30 @@ public interface StorageInfoClearedCallback {
public void onStorageInfoCleared();
}

/**
* @return the list of all origins that have keygen permissions in non-incognito mode.
*/
@SuppressWarnings("unchecked")
public static List<KeygenInfo> getKeygenInfo() {
ArrayList<KeygenInfo> list = new ArrayList<KeygenInfo>();
nativeGetKeygenOrigins(list);
return list;
}

@CalledByNative
private static void insertKeygenInfoIntoList(
ArrayList<KeygenInfo> list, String origin, String embedder) {
list.add(new KeygenInfo(origin, embedder, false));
}

/**
* @return whether we've blocked key generation in the current tab.
*/
@SuppressWarnings("unchecked")
public static boolean getKeygenBlocked(WebContents webContents) {
return nativeGetKeygenBlocked(webContents);
}

/**
* @return the list of all origins that have geolocation permissions in non-incognito mode.
*/
Expand Down Expand Up @@ -247,6 +272,12 @@ static native int nativeGetGeolocationSettingForOrigin(
String origin, String embedder, boolean isIncognito);
public static native void nativeSetGeolocationSettingForOrigin(
String origin, String embedder, int value, boolean isIncognito);
private static native void nativeGetKeygenOrigins(Object list);
static native int nativeGetKeygenSettingForOrigin(
String origin, String embedder, boolean isIncognito);
static native void nativeSetKeygenSettingForOrigin(
String origin, String embedder, int value, boolean isIncognito);
private static native boolean nativeGetKeygenBlocked(Object webContents);
private static native void nativeGetMidiOrigins(Object list);
static native int nativeGetMidiSettingForOrigin(
String origin, String embedder, boolean isIncognito);
Expand Down
Loading

0 comments on commit 34a7682

Please sign in to comment.