Commit e11e868e authored by reillyg's avatar reillyg Committed by Commit bot

Add a section to Site Settings listing USB devices.

The new section lists USB devices that the user has granted a website
permission to access. Tapping on a particular device brings you to the
site settings view for that site.

BUG=601627

Review-Url: https://codereview.chromium.org/2159533002
Cr-Commit-Position: refs/heads/master@{#412722}
parent e65bebf6
......@@ -74,4 +74,10 @@
android:key="use_storage"
android:title="@string/website_settings_storage"
android:icon="@drawable/settings_storage" />
<!-- USB -->
<org.chromium.chrome.browser.preferences.website.SiteSettingsPreference
android:fragment="org.chromium.chrome.browser.preferences.website.UsbChooserPreferences"
android:key="usb"
android:title="@string/website_settings_usb"
android:icon="@drawable/settings_usb" />
</PreferenceScreen>
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2016 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. -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" />
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2016 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. -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:key="object_name"
android:widgetLayout="@layout/usb_permission" />
<Preference
android:key="divider"
android:layout="@layout/divider_preference" />
</PreferenceScreen>
......@@ -45,6 +45,7 @@ public class SiteSettingsCategory {
public static final String CATEGORY_POPUPS = "popups";
public static final String CATEGORY_PROTECTED_MEDIA = "protected_content";
public static final String CATEGORY_USE_STORAGE = "use_storage";
public static final String CATEGORY_USB = "usb";
// The id of this category.
private String mCategory;
......@@ -131,6 +132,10 @@ public class SiteSettingsCategory {
if (CATEGORY_USE_STORAGE.equals(category)) {
return new SiteSettingsCategory(CATEGORY_USE_STORAGE, "", -1);
}
if (CATEGORY_USB.equals(category)) {
return new SiteSettingsCategory(
CATEGORY_USB, "", ContentSettingsType.CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA);
}
return null;
}
......@@ -175,6 +180,9 @@ public class SiteSettingsCategory {
== ContentSettingsType.CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER) {
return fromString(CATEGORY_PROTECTED_MEDIA);
}
if (contentSettingsType == ContentSettingsType.CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA) {
return fromString(CATEGORY_USB);
}
return null;
}
......@@ -280,6 +288,13 @@ public class SiteSettingsCategory {
return CATEGORY_USE_STORAGE.equals(mCategory);
}
/**
* Returns whether this category is the USB category.
*/
public boolean showUsbDevices() {
return mContentSettingsType == ContentSettingsType.CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA;
}
/**
* Returns whether the current category is managed either by enterprise policy or by the
* custodian of a supervised account.
......
......@@ -48,8 +48,9 @@ public class SiteSettingsPreferences extends PreferenceFragment
static final String NOTIFICATIONS_KEY = "notifications";
static final String POPUPS_KEY = "popups";
static final String PROTECTED_CONTENT_KEY = "protected_content";
static final String TRANSLATE_KEY = "translate";
static final String STORAGE_KEY = "use_storage";
static final String TRANSLATE_KEY = "translate";
static final String USB_KEY = "usb";
static final String AUTOPLAY_MUTED_VIDEOS = "AutoplayMutedVideos";
......@@ -132,6 +133,7 @@ public class SiteSettingsPreferences extends PreferenceFragment
getPreferenceScreen().removePreference(findPreference(POPUPS_KEY));
getPreferenceScreen().removePreference(findPreference(STORAGE_KEY));
getPreferenceScreen().removePreference(findPreference(TRANSLATE_KEY));
getPreferenceScreen().removePreference(findPreference(USB_KEY));
} else {
// If both Autoplay and Protected Content menus are available, they'll be tucked under
// the Media key. Otherwise, we can remove the Media menu entry.
......@@ -246,6 +248,8 @@ public class SiteSettingsPreferences extends PreferenceFragment
// TODO(finnur): Re-move this for Storage once it can be moved to the 'Usage' menu.
p = findPreference(STORAGE_KEY);
if (p != null) p.setOnPreferenceClickListener(this);
p = findPreference(USB_KEY);
if (p != null) p.setOnPreferenceClickListener(this);
}
@Override
......
// Copyright 2016 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;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.util.Pair;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.inputmethod.EditorInfo;
import android.widget.ListView;
import android.widget.TextView;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.help.HelpAndFeedback;
import org.chromium.chrome.browser.profiles.Profile;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* Shows a list of USB devices that the user has granted websites permission to access.
*
* When the user selects an item UsbDevicePreferences is launched to show which sites have access
* to the device.
*/
public class UsbChooserPreferences extends PreferenceFragment {
// The site settings category we are showing.
private SiteSettingsCategory mCategory;
// Multiple sites may have access to the same device. A canonical UsbInfo for each device is
// therefore arbitrarily chosen to represent it.
private Map<String, Pair<ArrayList<UsbInfo>, ArrayList<Website>>> mPermissionsByObject =
new HashMap<>();
// The view to show when the list is empty.
private TextView mEmptyView;
// The view for searching the list of items.
private SearchView mSearchView;
// If not blank, represents a substring to use to search for site names.
private String mSearch = "";
@Override
public void onActivityCreated(Bundle savedInstanceState) {
addPreferencesFromResource(R.xml.usb_chooser_preferences);
ListView listView = (ListView) getView().findViewById(android.R.id.list);
mEmptyView = (TextView) getView().findViewById(android.R.id.empty);
listView.setEmptyView(mEmptyView);
listView.setDivider(null);
String category = getArguments().getString(SingleCategoryPreferences.EXTRA_CATEGORY);
mCategory = SiteSettingsCategory.fromString(category);
String title = getArguments().getString(SingleCategoryPreferences.EXTRA_TITLE);
getActivity().setTitle(title);
setHasOptionsMenu(true);
super.onActivityCreated(savedInstanceState);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.website_preferences_menu, menu);
MenuItem searchItem = menu.findItem(R.id.search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
mSearchView.setImeOptions(EditorInfo.IME_FLAG_NO_FULLSCREEN);
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String query) {
// Make search case-insensitive.
query = query.toLowerCase();
if (query.equals(mSearch)) return true;
mSearch = query;
getInfo();
return true;
}
};
mSearchView.setOnQueryTextListener(queryTextListener);
MenuItem help =
menu.add(Menu.NONE, R.id.menu_id_targeted_help, Menu.NONE, R.string.menu_help);
help.setIcon(R.drawable.ic_help_and_feedback);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_id_targeted_help) {
HelpAndFeedback.getInstance(getActivity())
.show(getActivity(), getString(R.string.help_context_settings),
Profile.getLastUsedProfile(), null);
return true;
}
return false;
}
@Override
public void onResume() {
super.onResume();
getInfo();
}
private class ResultsPopulator implements WebsitePermissionsFetcher.WebsitePermissionsCallback {
@Override
public void onWebsitePermissionsAvailable(Collection<Website> sites) {
// This method may be called after the activity has been destroyed.
// In that case, bail out.
if (getActivity() == null) return;
mPermissionsByObject.clear();
for (Website site : sites) {
for (UsbInfo info : site.getUsbInfo()) {
if (mSearch.isEmpty() || info.getName().toLowerCase().contains(mSearch)) {
Pair<ArrayList<UsbInfo>, ArrayList<Website>> entry =
mPermissionsByObject.get(info.getObject());
if (entry == null) {
entry = Pair.create(new ArrayList<UsbInfo>(), new ArrayList<Website>());
mPermissionsByObject.put(info.getObject(), entry);
}
entry.first.add(info);
entry.second.add(site);
}
}
}
resetList();
}
}
/**
* Refreshes |mPermissionsByObject| with new data from native.
*
* resetList() is called to refresh the view when the data is ready.
*/
private void getInfo() {
WebsitePermissionsFetcher fetcher = new WebsitePermissionsFetcher(new ResultsPopulator());
fetcher.fetchPreferencesForCategory(mCategory);
}
private void resetList() {
getPreferenceScreen().removeAll();
addPreferencesFromResource(R.xml.usb_chooser_preferences);
if (mPermissionsByObject.isEmpty() && mSearch.isEmpty() && mEmptyView != null) {
mEmptyView.setText(R.string.website_settings_usb_no_devices);
}
for (Pair<ArrayList<UsbInfo>, ArrayList<Website>> entry : mPermissionsByObject.values()) {
Preference preference = new Preference(getActivity());
Bundle extras = preference.getExtras();
extras.putInt(UsbDevicePreferences.EXTRA_CATEGORY, mCategory.toContentSettingsType());
extras.putString(
SingleCategoryPreferences.EXTRA_TITLE, getActivity().getTitle().toString());
extras.putSerializable(UsbDevicePreferences.EXTRA_USB_INFOS, entry.first);
extras.putSerializable(UsbDevicePreferences.EXTRA_SITES, entry.second);
preference.setIcon(R.drawable.settings_usb);
preference.setTitle(entry.first.get(0).getName());
preference.setFragment(UsbDevicePreferences.class.getCanonicalName());
getPreferenceScreen().addPreference(preference);
}
}
}
// Copyright 2016 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;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.inputmethod.EditorInfo;
import android.widget.ListView;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.help.HelpAndFeedback;
import org.chromium.chrome.browser.profiles.Profile;
import java.util.ArrayList;
import java.util.Collection;
/**
* Shows the list of sites that the user has granted access to a particular USB device.
*/
public class UsbDevicePreferences
extends PreferenceFragment implements Preference.OnPreferenceClickListener {
public static final String EXTRA_USB_INFOS = "org.chromium.chrome.preferences.usb_infos";
public static final String EXTRA_SITES = "org.chromium.chrome.preferences.site_set";
public static final String EXTRA_CATEGORY =
"org.chromium.chrome.preferences.content_settings_type";
public static final String PREF_OBJECT_NAME = "object_name";
// The site settings category we are showing.
private SiteSettingsCategory mCategory;
// Canonical example of the USB device being examined.
private UsbInfo mUsbInfo;
// All of the USB device permission entries matching the canonical device.
private ArrayList<UsbInfo> mUsbInfos;
// The set of sites to display.
private ArrayList<Website> mSites;
// The view for searching the list of items.
private SearchView mSearchView;
// If not blank, represents a substring to use to search for site names.
private String mSearch = "";
@Override
@SuppressWarnings("unchecked")
public void onActivityCreated(Bundle savedInstanceState) {
addPreferencesFromResource(R.xml.usb_device_preferences);
ListView listView = (ListView) getView().findViewById(android.R.id.list);
listView.setDivider(null);
int contentSettingsType = getArguments().getInt(EXTRA_CATEGORY);
mCategory = SiteSettingsCategory.fromContentSettingsType(contentSettingsType);
mUsbInfos = (ArrayList<UsbInfo>) getArguments().getSerializable(EXTRA_USB_INFOS);
mUsbInfo = mUsbInfos.get(0);
mSites = (ArrayList<Website>) getArguments().getSerializable(EXTRA_SITES);
String title = getArguments().getString(SingleCategoryPreferences.EXTRA_TITLE);
if (title != null) getActivity().setTitle(title);
setHasOptionsMenu(true);
super.onActivityCreated(savedInstanceState);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.website_preferences_menu, menu);
MenuItem searchItem = menu.findItem(R.id.search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
mSearchView.setImeOptions(EditorInfo.IME_FLAG_NO_FULLSCREEN);
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String query) {
// Make search case-insensitive.
query = query.toLowerCase();
if (query.equals(mSearch)) return true;
mSearch = query;
getInfo();
return true;
}
};
mSearchView.setOnQueryTextListener(queryTextListener);
MenuItem help =
menu.add(Menu.NONE, R.id.menu_id_targeted_help, Menu.NONE, R.string.menu_help);
help.setIcon(R.drawable.ic_help_and_feedback);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_id_targeted_help) {
HelpAndFeedback.getInstance(getActivity())
.show(getActivity(), getString(R.string.help_context_settings),
Profile.getLastUsedProfile(), null);
return true;
}
return false;
}
@Override
public void onResume() {
super.onResume();
if (mSites == null) {
getInfo();
} else {
resetList();
}
}
@Override
public boolean onPreferenceClick(Preference preference) {
if (PREF_OBJECT_NAME.equals(preference.getKey())) {
for (int i = 0; i < mUsbInfos.size(); ++i) mUsbInfos.get(i).revoke();
getActivity().finish();
return true;
}
return false;
}
private class ResultsPopulator implements WebsitePermissionsFetcher.WebsitePermissionsCallback {
@Override
public void onWebsitePermissionsAvailable(Collection<Website> sites) {
// This method may be called after the activity has been destroyed.
// In that case, bail out.
if (getActivity() == null) return;
mUsbInfos.clear();
mSites = new ArrayList<Website>();
for (Website site : sites) {
for (UsbInfo info : site.getUsbInfo()) {
if (info.getObject().equals(mUsbInfo.getObject())) {
mUsbInfos.add(info);
if (mSearch.isEmpty() || site.getTitle().toLowerCase().contains(mSearch)) {
mSites.add(site);
}
}
}
}
// After revoking a site's permission to access a device the user may end up back at
// this activity. It is awkward to display this empty list because there's no action
// that can be taken from it. In this case we dismiss this activity as well, taking
// them back to UsbChooserPreferences which will now no longer offer the option to
// examine the permissions for this device.
if (mUsbInfos.isEmpty()) {
getActivity().finish();
} else {
resetList();
}
}
}
/**
* Refreshes the list of sites with access to the object being examined.
*
* resetList() is called to refresh the view when the data is ready.
*/
private void getInfo() {
WebsitePermissionsFetcher fetcher = new WebsitePermissionsFetcher(new ResultsPopulator());
fetcher.fetchPreferencesForCategory(mCategory);
}
private void resetList() {
getPreferenceScreen().removeAll();
addPreferencesFromResource(R.xml.usb_device_preferences);
PreferenceScreen preferenceScreen = getPreferenceScreen();
Preference header = preferenceScreen.findPreference(PREF_OBJECT_NAME);
header.setTitle(mUsbInfo.getName());
header.setOnPreferenceClickListener(this);
for (int i = 0; i < mSites.size(); ++i) {
Website site = mSites.get(i);
Preference preference = new WebsitePreference(getActivity(), site, mCategory);
preference.getExtras().putSerializable(SingleWebsitePreferences.EXTRA_SITE, site);
preference.setFragment(SingleWebsitePreferences.class.getCanonicalName());
preferenceScreen.addPreference(preference);
}
// Force this list to be reloaded if the activity is resumed.
mSites = null;
}
}
......@@ -8,6 +8,9 @@ import java.io.Serializable;
/**
* USB device information for a given origin.
*
* These objects are compared only by the identity of the device, not by which site has permission
* to access it.
*/
public class UsbInfo implements Serializable {
private final String mOrigin;
......@@ -43,6 +46,13 @@ public class UsbInfo implements Serializable {
return mName;
}
/**
* Returns the opaque object string that represents the device.
*/
public String getObject() {
return mObject;
}
/**
* Revokes permission for the origin to access the USB device.
*/
......
......@@ -140,6 +140,9 @@ public class WebsitePermissionsFetcher {
} else if (category.showAutoplaySites()) {
// Autoplay permission is per-origin.
queue.add(new AutoplayExceptionInfoFetcher());
} else if (category.showUsbDevices()) {
// USB device permission is per-origin.
queue.add(new UsbInfoFetcher());
}
queue.add(new PermissionsAvailableCallbackRunner());
queue.next();
......
......@@ -840,9 +840,15 @@ To obtain new licenses, connect to the internet and play your downloaded content
</message>
<!-- USB preferences -->
<message name="IDS_WEBSITE_SETTINGS_USB" desc="Title for USB settings, which control which of the user's USB devices can be accessed from websites.">
USB
</message>
<message name="IDS_WEBSITE_SETTINGS_REVOKE_DEVICE_PERMISSION" desc="Content description for revoking a website's permission to access a device.">
Revoke device permission
</message>
<message name="IDS_WEBSITE_SETTINGS_USB_NO_DEVICES" desc="Empty list text for a list of USB devices that sites have been granted access to.">
No USB devices here
</message>
<!-- Data Saver -->
<message name="IDS_DATA_REDUCTION_LO_FI_SNACKBAR_MESSAGE" desc="Message shown when a page is opened in low fidelity mode when there is a slow unreliable connection.">
......
......@@ -780,6 +780,8 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/preferences/website/SiteSettingsPreferences.java",
"java/src/org/chromium/chrome/browser/preferences/website/StorageInfo.java",
"java/src/org/chromium/chrome/browser/preferences/website/TranslatePreferences.java",
"java/src/org/chromium/chrome/browser/preferences/website/UsbChooserPreferences.java",
"java/src/org/chromium/chrome/browser/preferences/website/UsbDevicePreferences.java",
"java/src/org/chromium/chrome/browser/preferences/website/UsbInfo.java",
"java/src/org/chromium/chrome/browser/preferences/website/Website.java",
"java/src/org/chromium/chrome/browser/preferences/website/WebsiteAddress.java",
......
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