Commit 724976dd authored by Francois Beaufort's avatar Francois Beaufort Committed by Commit Bot

[WebNFC] Warn user in site settings when NFC setting is disabled.

This CL adds a warning message in "NFC devices" site settings category
so that user can turn on NFC if needed.

Screenshots: https://photos.app.goo.gl/ZAsjS97LyRqQq5tH9

Change-Id: I439d0c68414e29b765408d979850ea6eca0b97c4
Bug: 520391
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1954004
Commit-Queue: François Beaufort <beaufort.francois@gmail.com>
Reviewed-by: default avatarBoris Sazonov <bsazonov@chromium.org>
Reviewed-by: default avatarFinnur Thorarinsson <finnur@chromium.org>
Cr-Commit-Position: refs/heads/master@{#725647}
parent b583aadf
......@@ -1465,6 +1465,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/settings/website/LocalStorageInfo.java",
"java/src/org/chromium/chrome/browser/settings/website/LocationCategory.java",
"java/src/org/chromium/chrome/browser/settings/website/ManageSpaceActivity.java",
"java/src/org/chromium/chrome/browser/settings/website/NfcCategory.java",
"java/src/org/chromium/chrome/browser/settings/website/NotificationCategory.java",
"java/src/org/chromium/chrome/browser/settings/website/PermissionInfo.java",
"java/src/org/chromium/chrome/browser/settings/website/SettingsNavigationSource.java",
......
......@@ -10,6 +10,8 @@ import android.content.pm.PackageManager;
import android.nfc.NfcAdapter;
import android.os.Process;
import androidx.annotation.VisibleForTesting;
import org.chromium.base.ContextUtils;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.NativeMethods;
......@@ -25,6 +27,8 @@ import org.chromium.ui.base.WindowAndroid;
* This class should be used only on the UI thread.
*/
public class NfcSystemLevelSetting {
private static Boolean sSystemNfcSettingForTesting;
@CalledByNative
private static boolean isNfcAccessPossible() {
Context context = ContextUtils.getApplicationContext();
......@@ -39,7 +43,11 @@ public class NfcSystemLevelSetting {
}
@CalledByNative
private static boolean isNfcSystemLevelSettingEnabled() {
public static boolean isNfcSystemLevelSettingEnabled() {
if (sSystemNfcSettingForTesting != null) {
return sSystemNfcSettingForTesting;
}
if (!isNfcAccessPossible()) return false;
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(ContextUtils.getApplicationContext());
......@@ -66,6 +74,12 @@ public class NfcSystemLevelSetting {
nativeCallback));
}
/** Disable/enable Android NFC setting for testing use only. */
@VisibleForTesting
public static void setNfcSettingForTesting(Boolean enabled) {
sSystemNfcSettingForTesting = enabled;
}
@NativeMethods
interface Natives {
void onNfcSystemLevelPromptCompleted(long callback);
......
// Copyright 2019 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.settings.website;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.settings.NfcSystemLevelSetting;
/**
* A class for dealing with the NFC category.
*/
public class NfcCategory extends SiteSettingsCategory {
public NfcCategory() {
// As NFC is not a per-app permission, passing an empty string means the NFC permission is
// always enabled for Chrome.
super(Type.NFC, "" /* androidPermission*/);
}
@Override
protected boolean enabledGlobally() {
return NfcSystemLevelSetting.isNfcSystemLevelSettingEnabled();
}
@Override
protected Intent getIntentToEnableOsGlobalPermission(Context context) {
return new Intent(Settings.ACTION_NFC_SETTINGS);
}
@Override
protected String getMessageForEnablingOsGlobalPermission(Activity activity) {
return activity.getResources().getString(R.string.android_nfc_off_globally);
}
}
......@@ -941,9 +941,11 @@ public class SingleCategoryPreferences extends PreferenceFragmentCompat
mCategory.configurePermissionIsOffPreferences(
osWarning, osWarningExtra, getActivity(), true);
if (osWarning.getTitle() != null) {
osWarning.setKey(SingleWebsitePreferences.PREF_OS_PERMISSIONS_WARNING);
screen.addPreference(osWarning);
}
if (osWarningExtra.getTitle() != null) {
osWarningExtra.setKey(SingleWebsitePreferences.PREF_OS_PERMISSIONS_WARNING_EXTRA);
screen.addPreference(osWarningExtra);
}
}
......
......@@ -95,6 +95,7 @@ public class SiteSettingsCategory {
*/
public static SiteSettingsCategory createFromType(@Type int type) {
if (type == Type.DEVICE_LOCATION) return new LocationCategory();
if (type == Type.NFC) return new NfcCategory();
if (type == Type.NOTIFICATIONS) return new NotificationCategory();
final String permission;
......
......@@ -35,6 +35,7 @@ import org.chromium.chrome.browser.preferences.PrefServiceBridge;
import org.chromium.chrome.browser.settings.ChromeBaseCheckBoxPreference;
import org.chromium.chrome.browser.settings.ChromeSwitchPreference;
import org.chromium.chrome.browser.settings.LocationSettings;
import org.chromium.chrome.browser.settings.NfcSystemLevelSetting;
import org.chromium.chrome.browser.settings.SettingsActivity;
import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
......@@ -521,6 +522,9 @@ public class SiteSettingsPreferencesTest {
@EnableFeatures("QuietNotificationPrompts")
@DisabledTest(message = "Flaky. crbug.com/1030218")
public void testOnlyExpectedPreferencesShown() {
LocationSettingsTestUtil.setSystemLocationSettingEnabled(true);
NfcSystemLevelSetting.setNfcSettingForTesting(true);
// If you add a category in the SiteSettings UI, please add a test for it below.
Assert.assertEquals(19, SiteSettingsCategory.Type.NUM_ENTRIES);
......@@ -528,6 +532,8 @@ public class SiteSettingsPreferencesTest {
String[] binaryToggle = new String[] {"binary_toggle"};
String[] binaryToggleWithException = new String[] {"binary_toggle", "add_exception"};
String[] binaryToggleWithAllowed = new String[] {"binary_toggle", "allowed_group"};
String[] binaryToggleWithOsWarningExtra =
new String[] {"binary_toggle", "os_permissions_warning_extra"};
String[] cookie = new String[] {"binary_toggle", "third_party_cookies", "add_exception"};
String[] protectedMedia = new String[] {"tri_state_toggle", "protected_content_learn_more"};
String[] notifications_enabled;
......@@ -603,12 +609,16 @@ public class SiteSettingsPreferencesTest {
checkPreferencesForCategory(key, values.second);
}
// Location is not the only system-managed permission, but having one test for a
// system-managed permission has been shown to catch stray permissons appearing where they
// should not.
// Disable system location setting and check for the right preferences.
LocationSettingsTestUtil.setSystemLocationSettingEnabled(false);
checkPreferencesForCategory(SiteSettingsCategory.Type.DEVICE_LOCATION, binaryToggle);
checkPreferencesForCategory(
SiteSettingsCategory.Type.DEVICE_LOCATION, binaryToggleWithOsWarningExtra);
LocationSettingsTestUtil.setSystemLocationSettingEnabled(true);
// Disable system nfc setting and check for the right preferences.
NfcSystemLevelSetting.setNfcSettingForTesting(false);
checkPreferencesForCategory(SiteSettingsCategory.Type.NFC, binaryToggleWithOsWarningExtra);
NfcSystemLevelSetting.setNfcSettingForTesting(null);
}
/**
......
......@@ -4100,6 +4100,9 @@ The site does NOT gain access to the camera. The camera images are only visible
<message name="IDS_NFC_PROMPT_TURN_ON" desc="Text on the positive button of the nfc prompt">
Turn on
</message>
<message name="IDS_ANDROID_NFC_OFF_GLOBALLY" desc="The message to show when NFC has been turned off globally in Android. Contains a link to the settings menu to enable NFC.">
NFC is off for this device. Turn it on NFC in <ph name="BEGIN_LINK">&lt;link&gt;</ph>Android Settings<ph name="END_LINK">&lt;/link&gt;</ph>.
</message>
</messages>
</release>
</grit>
56681f3701c16d54f915744074bddd043a2ecc33
\ No newline at end of file
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