Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.infobar; | 5 package org.chromium.chrome.browser.infobar; |
| 6 | 6 |
| 7 import android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.content.DialogInterface; | 8 import android.content.DialogInterface; |
| 9 import android.content.pm.PackageManager; | 9 import android.content.pm.PackageManager; |
| 10 import android.graphics.Bitmap; | 10 import android.graphics.Bitmap; |
| 11 import android.support.v7.app.AlertDialog; | 11 import android.support.v7.app.AlertDialog; |
| 12 import android.support.v7.widget.SwitchCompat; | 12 import android.support.v7.widget.SwitchCompat; |
| 13 import android.util.SparseArray; | 13 import android.util.SparseArray; |
| 14 import android.view.View; | 14 import android.view.View; |
| 15 import android.widget.TextView; | 15 import android.widget.TextView; |
| 16 | 16 |
| 17 import org.chromium.base.annotations.CalledByNative; | 17 import org.chromium.base.annotations.CalledByNative; |
| 18 import org.chromium.chrome.R; | 18 import org.chromium.chrome.R; |
| 19 import org.chromium.chrome.browser.ContentSettingsType; | 19 import org.chromium.chrome.browser.PermissionType; |
| 20 import org.chromium.chrome.browser.ResourceId; | 20 import org.chromium.chrome.browser.ResourceId; |
| 21 import org.chromium.chrome.browser.preferences.PrefServiceBridge; | 21 import org.chromium.chrome.browser.preferences.PrefServiceBridge; |
| 22 import org.chromium.chrome.browser.tab.Tab; | 22 import org.chromium.chrome.browser.tab.Tab; |
| 23 import org.chromium.ui.base.WindowAndroid; | 23 import org.chromium.ui.base.WindowAndroid; |
| 24 import org.chromium.ui.base.WindowAndroid.PermissionCallback; | 24 import org.chromium.ui.base.WindowAndroid.PermissionCallback; |
| 25 | 25 |
| 26 /** | 26 /** |
| 27 * An infobar used for prompting the user to grant a web API permission. | 27 * An infobar used for prompting the user to grant a web API permission. |
| 28 * | 28 * |
| 29 */ | 29 */ |
| 30 public class PermissionInfoBar extends ConfirmInfoBar { | 30 public class PermissionInfoBar extends ConfirmInfoBar { |
| 31 | 31 |
| 32 /** Whether or not to show a toggle for opting out of persisting the decisio n. */ | 32 /** Whether or not to show a toggle for opting out of persisting the decisio n. */ |
| 33 private boolean mShowPersistenceToggle; | 33 private boolean mShowPersistenceToggle; |
| 34 | 34 |
| 35 private WindowAndroid mWindowAndroid; | 35 private WindowAndroid mWindowAndroid; |
| 36 | 36 |
| 37 /** | 37 /** |
| 38 * Mapping between the required {@link ContentSettingsType}s and their assoc iated Android | 38 * Mapping between the required {@link PermissionType}s and their associated Android |
| 39 * runtime permissions. Only {@link ContentSettingsType}s that are associat ed with runtime | 39 * runtime permissions. Only {@link PermissionType}s that are associated wi th runtime |
| 40 * permissions will be included in this list while all others will be exclud ed. | 40 * permissions will be included in this list while all others will be exclud ed. |
| 41 */ | 41 */ |
| 42 private SparseArray<String> mContentSettingsToPermissionsMap; | 42 private SparseArray<String> mPermissionTypeToPermissionsMap; |
| 43 | 43 |
| 44 protected PermissionInfoBar(int iconDrawableId, Bitmap iconBitmap, String me ssage, | 44 protected PermissionInfoBar(int iconDrawableId, Bitmap iconBitmap, String me ssage, |
| 45 String linkText, String primaryButtonText, String secondaryButtonTex t, | 45 String linkText, String primaryButtonText, String secondaryButtonTex t, |
| 46 boolean showPersistenceToggle) { | 46 boolean showPersistenceToggle) { |
| 47 super(iconDrawableId, iconBitmap, message, linkText, primaryButtonText, | 47 super(iconDrawableId, iconBitmap, message, linkText, primaryButtonText, |
| 48 secondaryButtonText); | 48 secondaryButtonText); |
| 49 mShowPersistenceToggle = showPersistenceToggle; | 49 mShowPersistenceToggle = showPersistenceToggle; |
| 50 } | 50 } |
| 51 | 51 |
| 52 @Override | 52 @Override |
| 53 public void createContent(InfoBarLayout layout) { | 53 public void createContent(InfoBarLayout layout) { |
| 54 super.createContent(layout); | 54 super.createContent(layout); |
| 55 | 55 |
| 56 if (mShowPersistenceToggle) { | 56 if (mShowPersistenceToggle) { |
| 57 InfoBarControlLayout controlLayout = layout.addControlLayout(); | 57 InfoBarControlLayout controlLayout = layout.addControlLayout(); |
| 58 String description = | 58 String description = |
| 59 layout.getContext().getString(R.string.permission_infobar_pe rsist_text); | 59 layout.getContext().getString(R.string.permission_infobar_pe rsist_text); |
| 60 controlLayout.addSwitch( | 60 controlLayout.addSwitch( |
| 61 0, 0, description, R.id.permission_infobar_persist_toggle, t rue); | 61 0, 0, description, R.id.permission_infobar_persist_toggle, t rue); |
| 62 } | 62 } |
| 63 } | 63 } |
| 64 | 64 |
| 65 @Override | 65 @Override |
| 66 public void onTabReparented(Tab tab) { | 66 public void onTabReparented(Tab tab) { |
| 67 mWindowAndroid = tab.getWindowAndroid(); | 67 mWindowAndroid = tab.getWindowAndroid(); |
| 68 } | 68 } |
| 69 | 69 |
| 70 /** | 70 /** |
| 71 * Specifies the {@link ContentSettingsType}s that are controlled by this In foBar. | 71 * Specifies the {@link PermissionType}s that are controlled by this InfoBar . |
| 72 * | 72 * |
| 73 * @param windowAndroid The WindowAndroid that will be used to check for the necessary | 73 * @param windowAndroid The WindowAndroid that will be used to check for the necessary |
| 74 * permissions. | 74 * permissions. |
| 75 * @param contentSettings The list of {@link ContentSettingsType}s whose acc ess is guarded | 75 * @param permissions The list of {@link PermissionType}s whose access is gu arded |
|
raymes
2016/10/17 01:11:12
nit: permissionTypes
| |
| 76 * by this InfoBar. | 76 * by this InfoBar. |
| 77 */ | 77 */ |
| 78 protected void setContentSettings( | 78 protected void setPermissions(WindowAndroid windowAndroid, int[] permissions ) { |
| 79 WindowAndroid windowAndroid, int[] contentSettings) { | |
| 80 mWindowAndroid = windowAndroid; | 79 mWindowAndroid = windowAndroid; |
| 81 assert windowAndroid != null | 80 assert windowAndroid != null |
| 82 : "A WindowAndroid must be specified to request access to conten t settings"; | 81 : "A WindowAndroid must be specified to request access to permis sions"; |
| 83 | 82 |
| 84 mContentSettingsToPermissionsMap = generatePermissionsMapping(contentSet tings); | 83 mPermissionTypeToPermissionsMap = generatePermissionsMapping(permissions ); |
| 85 } | 84 } |
| 86 | 85 |
| 87 private SparseArray<String> generatePermissionsMapping(int[] contentSettings ) { | 86 private SparseArray<String> generatePermissionsMapping(int[] permissions) { |
| 88 SparseArray<String> permissionsToRequest = new SparseArray<String>(); | 87 SparseArray<String> permissionsToRequest = new SparseArray<String>(); |
| 89 for (int i = 0; i < contentSettings.length; i++) { | 88 for (int i = 0; i < permissions.length; i++) { |
| 90 String permission = PrefServiceBridge.getAndroidPermissionForContent Setting( | 89 String permission = |
| 91 contentSettings[i]); | 90 PrefServiceBridge.getAndroidPermissionForPermissionType(perm issions[i]); |
| 92 if (permission != null && !mWindowAndroid.hasPermission(permission)) { | 91 if (permission != null && !mWindowAndroid.hasPermission(permission)) { |
| 93 permissionsToRequest.append(contentSettings[i], permission); | 92 permissionsToRequest.append(permissions[i], permission); |
| 94 } | 93 } |
| 95 } | 94 } |
| 96 return permissionsToRequest; | 95 return permissionsToRequest; |
| 97 } | 96 } |
| 98 | 97 |
| 99 private int getDeniedPermissionResourceId(String permission) { | 98 private int getDeniedPermissionResourceId(String permission) { |
| 100 int contentSettingsType = 0; | 99 int permissionType = 0; |
| 101 // SparseArray#indexOfValue uses == instead of .equals, so we need to ma nually iterate | 100 // SparseArray#indexOfValue uses == instead of .equals, so we need to ma nually iterate |
| 102 // over the list. | 101 // over the list. |
| 103 for (int i = 0; i < mContentSettingsToPermissionsMap.size(); i++) { | 102 for (int i = 0; i < mPermissionTypeToPermissionsMap.size(); i++) { |
| 104 if (permission.equals(mContentSettingsToPermissionsMap.valueAt(i))) { | 103 if (permission.equals(mPermissionTypeToPermissionsMap.valueAt(i))) { |
| 105 contentSettingsType = mContentSettingsToPermissionsMap.keyAt(i); | 104 permissionType = mPermissionTypeToPermissionsMap.keyAt(i); |
| 106 } | 105 } |
| 107 } | 106 } |
| 108 | 107 |
| 109 if (contentSettingsType == ContentSettingsType.CONTENT_SETTINGS_TYPE_GEO LOCATION) { | 108 if (permissionType == PermissionType.GEOLOCATION) { |
| 110 return R.string.infobar_missing_location_permission_text; | 109 return R.string.infobar_missing_location_permission_text; |
| 111 } | 110 } |
| 112 if (contentSettingsType == ContentSettingsType.CONTENT_SETTINGS_TYPE_MED IASTREAM_MIC) { | 111 if (permissionType == PermissionType.AUDIO_CAPTURE) { |
| 113 return R.string.infobar_missing_microphone_permission_text; | 112 return R.string.infobar_missing_microphone_permission_text; |
| 114 } | 113 } |
| 115 if (contentSettingsType == ContentSettingsType.CONTENT_SETTINGS_TYPE_MED IASTREAM_CAMERA) { | 114 if (permissionType == PermissionType.VIDEO_CAPTURE) { |
| 116 return R.string.infobar_missing_camera_permission_text; | 115 return R.string.infobar_missing_camera_permission_text; |
| 117 } | 116 } |
| 118 assert false : "Unexpected content setting type received: " + contentSet tingsType; | 117 assert false : "Unexpected permission type received: " + permissionType; |
| 119 return R.string.infobar_missing_multiple_permissions_text; | 118 return R.string.infobar_missing_multiple_permissions_text; |
| 120 } | 119 } |
| 121 | 120 |
| 122 @Override | 121 @Override |
| 123 public void onButtonClicked(final boolean isPrimaryButton) { | 122 public void onButtonClicked(final boolean isPrimaryButton) { |
| 124 if (mWindowAndroid == null || !isPrimaryButton || getContext() == null | 123 if (mWindowAndroid == null || !isPrimaryButton || getContext() == null |
| 125 || mContentSettingsToPermissionsMap == null | 124 || mPermissionTypeToPermissionsMap == null |
| 126 || mContentSettingsToPermissionsMap.size() == 0) { | 125 || mPermissionTypeToPermissionsMap.size() == 0) { |
| 127 onButtonClickedInternal(isPrimaryButton); | 126 onButtonClickedInternal(isPrimaryButton); |
| 128 return; | 127 return; |
| 129 } | 128 } |
| 130 | 129 |
| 131 requestAndroidPermissions(); | 130 requestAndroidPermissions(); |
| 132 } | 131 } |
| 133 | 132 |
| 134 private void requestAndroidPermissions() { | 133 private void requestAndroidPermissions() { |
| 135 PermissionCallback callback = new PermissionCallback() { | 134 PermissionCallback callback = new PermissionCallback() { |
| 136 @Override | 135 @Override |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 }); | 178 }); |
| 180 builder.create().show(); | 179 builder.create().show(); |
| 181 } else if (deniedCount > 0) { | 180 } else if (deniedCount > 0) { |
| 182 onCloseButtonClicked(); | 181 onCloseButtonClicked(); |
| 183 } else { | 182 } else { |
| 184 onButtonClickedInternal(true); | 183 onButtonClickedInternal(true); |
| 185 } | 184 } |
| 186 } | 185 } |
| 187 }; | 186 }; |
| 188 | 187 |
| 189 String[] permissionsToRequest = new String[mContentSettingsToPermissions Map.size()]; | 188 String[] permissionsToRequest = new String[mPermissionTypeToPermissionsM ap.size()]; |
| 190 for (int i = 0; i < mContentSettingsToPermissionsMap.size(); i++) { | 189 for (int i = 0; i < mPermissionTypeToPermissionsMap.size(); i++) { |
| 191 permissionsToRequest[i] = mContentSettingsToPermissionsMap.valueAt(i ); | 190 permissionsToRequest[i] = mPermissionTypeToPermissionsMap.valueAt(i) ; |
| 192 } | 191 } |
| 193 mWindowAndroid.requestPermissions(permissionsToRequest, callback); | 192 mWindowAndroid.requestPermissions(permissionsToRequest, callback); |
| 194 } | 193 } |
| 195 | 194 |
| 196 private void onButtonClickedInternal(boolean isPrimaryButton) { | 195 private void onButtonClickedInternal(boolean isPrimaryButton) { |
| 197 super.onButtonClicked(isPrimaryButton); | 196 super.onButtonClicked(isPrimaryButton); |
| 198 } | 197 } |
| 199 | 198 |
| 200 /** | 199 /** |
| 201 * Returns true if the persist switch exists and is toggled on. | 200 * Returns true if the persist switch exists and is toggled on. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 215 * @param windowAndroid The owning window for the infobar. | 214 * @param windowAndroid The owning window for the infobar. |
| 216 * @param enumeratedIconId ID corresponding to the icon that will be shown f or the infobar. | 215 * @param enumeratedIconId ID corresponding to the icon that will be shown f or the infobar. |
| 217 * The ID must have been mapped using the ResourceMa pper class before | 216 * The ID must have been mapped using the ResourceMa pper class before |
| 218 * passing it to this function. | 217 * passing it to this function. |
| 219 * @param iconBitmap Bitmap to use if there is no equivalent Java resource f or | 218 * @param iconBitmap Bitmap to use if there is no equivalent Java resource f or |
| 220 * enumeratedIconId. | 219 * enumeratedIconId. |
| 221 * @param message Message to display to the user indicating what the infobar is for. | 220 * @param message Message to display to the user indicating what the infobar is for. |
| 222 * @param linkText Link text to display in addition to the message. | 221 * @param linkText Link text to display in addition to the message. |
| 223 * @param buttonOk String to display on the OK button. | 222 * @param buttonOk String to display on the OK button. |
| 224 * @param buttonCancel String to display on the Cancel button. | 223 * @param buttonCancel String to display on the Cancel button. |
| 225 * @param contentSettings The list of ContentSettingTypes being requested by this infobar. | 224 * @param permissions The list of PermissionTypes being requested by this in fobar. |
|
raymes
2016/10/17 01:11:12
nit: permissionTypes
| |
| 226 * @param showPersistenceToggle Whether or not a toggle to opt-out of persis ting a decision | 225 * @param showPersistenceToggle Whether or not a toggle to opt-out of persis ting a decision |
| 227 * should be displayed. | 226 * should be displayed. |
| 228 */ | 227 */ |
| 229 @CalledByNative | 228 @CalledByNative |
| 230 private static PermissionInfoBar create(WindowAndroid windowAndroid, int enu meratedIconId, | 229 private static PermissionInfoBar create(WindowAndroid windowAndroid, int enu meratedIconId, |
| 231 Bitmap iconBitmap, String message, String linkText, String buttonOk, | 230 Bitmap iconBitmap, String message, String linkText, String buttonOk, |
| 232 String buttonCancel, int[] contentSettings, boolean showPersistenceT oggle) { | 231 String buttonCancel, int[] permissions, boolean showPersistenceToggl e) { |
| 233 int drawableId = ResourceId.mapToDrawableId(enumeratedIconId); | 232 int drawableId = ResourceId.mapToDrawableId(enumeratedIconId); |
| 234 | 233 |
| 235 PermissionInfoBar infoBar = new PermissionInfoBar(drawableId, iconBitmap , message, linkText, | 234 PermissionInfoBar infoBar = new PermissionInfoBar(drawableId, iconBitmap , message, linkText, |
| 236 buttonOk, buttonCancel, showPersistenceToggle); | 235 buttonOk, buttonCancel, showPersistenceToggle); |
| 237 infoBar.setContentSettings(windowAndroid, contentSettings); | 236 infoBar.setPermissions(windowAndroid, permissions); |
| 238 | 237 |
| 239 return infoBar; | 238 return infoBar; |
| 240 } | 239 } |
| 241 } | 240 } |
| OLD | NEW |