1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/permissions/android/android_permission_util.h"
#include "base/android/jni_array.h"
#include "components/location/android/location_settings_impl.h"
#include "components/permissions/permission_uma_util.h"
#include "content/public/browser/web_contents.h"
#include "ui/android/window_android.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "components/permissions/android/core_jni/PermissionUtil_jni.h"
#include "components/permissions/android/jni_headers/AndroidPermissionRequester_jni.h"
namespace permissions {
namespace {
// Returns whether the Android location setting is enabled/disabled.
bool IsSystemLocationSettingEnabled() {
LocationSettingsImpl location_settings;
return location_settings.IsSystemLocationSettingEnabled();
}
} // namespace
void AppendRequiredAndroidPermissionsForContentSetting(
ContentSettingsType content_settings_type,
std::vector<std::string>* out) {
JNIEnv* env = base::android::AttachCurrentThread();
base::android::AppendJavaStringArrayToStringVector(
env,
Java_PermissionUtil_getRequiredAndroidPermissionsForContentSetting(
env, static_cast<int>(content_settings_type)),
out);
}
void AppendOptionalAndroidPermissionsForContentSetting(
ContentSettingsType content_settings_type,
std::vector<std::string>* out) {
JNIEnv* env = base::android::AttachCurrentThread();
base::android::AppendJavaStringArrayToStringVector(
env,
Java_PermissionUtil_getOptionalAndroidPermissionsForContentSetting(
env, static_cast<int>(content_settings_type)),
out);
}
bool HasRequiredAndroidPermissionsForContentSetting(
ui::WindowAndroid* window_android,
ContentSettingsType content_settings_type) {
JNIEnv* env = base::android::AttachCurrentThread();
return Java_AndroidPermissionRequester_hasRequiredAndroidPermissionsForContentSetting(
env, window_android->GetJavaObject(),
static_cast<int>(content_settings_type));
}
PermissionRepromptState ShouldRepromptUserForPermissions(
content::WebContents* web_contents,
const std::vector<ContentSettingsType>& content_settings_types) {
if (!web_contents)
return PermissionRepromptState::kCannotShow;
auto* window_android = web_contents->GetNativeView()->GetWindowAndroid();
if (!window_android)
return PermissionRepromptState::kCannotShow;
for (ContentSettingsType content_settings_type : content_settings_types) {
if (!HasRequiredAndroidPermissionsForContentSetting(
window_android, content_settings_type)) {
PermissionUmaUtil::RecordMissingPermissionInfobarShouldShow(
true, content_settings_types);
return PermissionRepromptState::kShow;
}
}
permissions::PermissionUmaUtil::RecordMissingPermissionInfobarShouldShow(
false, content_settings_types);
return PermissionRepromptState::kNoNeed;
}
std::vector<ContentSettingsType>
GetContentSettingsWithMissingRequiredAndroidPermissions(
const std::vector<ContentSettingsType>& content_settings_types,
content::WebContents* web_contents) {
auto* window_android = web_contents->GetNativeView()->GetWindowAndroid();
DCHECK(window_android);
std::vector<ContentSettingsType> filtered_types;
for (ContentSettingsType content_settings_type : content_settings_types) {
if (HasRequiredAndroidPermissionsForContentSetting(window_android,
content_settings_type)) {
continue;
}
filtered_types.push_back(content_settings_type);
}
return filtered_types;
}
void AppendRequiredAndOptionalAndroidPermissionsForContentSettings(
const std::vector<ContentSettingsType>& content_settings_types,
std::vector<std::string>& out_required_permissions,
std::vector<std::string>& out_optional_permissions) {
for (ContentSettingsType content_settings_type : content_settings_types) {
permissions::AppendRequiredAndroidPermissionsForContentSetting(
content_settings_type, &out_required_permissions);
permissions::AppendOptionalAndroidPermissionsForContentSetting(
content_settings_type, &out_optional_permissions);
}
}
bool DoesAppLevelSettingsAllowSiteNotifications() {
JNIEnv* env = base::android::AttachCurrentThread();
return Java_PermissionUtil_doesAppLevelSettingsAllowSiteNotifications(env);
}
bool NeedsLocationPermissionForBluetooth(content::WebContents* web_contents) {
JNIEnv* env = base::android::AttachCurrentThread();
auto* window_android = web_contents->GetNativeView()->GetWindowAndroid();
DCHECK(window_android);
return Java_PermissionUtil_needsLocationPermissionForBluetooth(
env, window_android->GetJavaObject());
}
bool NeedsNearbyDevicesPermissionForBluetooth(
content::WebContents* web_contents) {
JNIEnv* env = base::android::AttachCurrentThread();
auto* window_android = web_contents->GetNativeView()->GetWindowAndroid();
DCHECK(window_android);
return Java_PermissionUtil_needsNearbyDevicesPermissionForBluetooth(
env, window_android->GetJavaObject());
}
bool NeedsLocationServicesForBluetooth() {
JNIEnv* env = base::android::AttachCurrentThread();
return Java_PermissionUtil_needsLocationServicesForBluetooth(env);
}
bool CanRequestSystemPermissionsForBluetooth(
content::WebContents* web_contents) {
JNIEnv* env = base::android::AttachCurrentThread();
auto* window_android = web_contents->GetNativeView()->GetWindowAndroid();
DCHECK(window_android);
return Java_PermissionUtil_canRequestSystemPermissionsForBluetooth(
env, window_android->GetJavaObject());
}
bool HasSystemPermission(ContentSettingsType type,
content::WebContents* web_contents) {
if (!web_contents || !web_contents->GetNativeView()) {
return false;
}
if (type == ContentSettingsType::GEOLOCATION &&
!IsSystemLocationSettingEnabled()) {
return false;
}
auto* window_android = web_contents->GetNativeView()->GetWindowAndroid();
DCHECK(window_android);
return HasRequiredAndroidPermissionsForContentSetting(window_android, type);
}
bool CanRequestSystemPermission(ContentSettingsType type,
content::WebContents* web_contents) {
if (!web_contents || !web_contents->GetNativeView()) {
return false;
}
if (type == ContentSettingsType::GEOLOCATION &&
!IsSystemLocationSettingEnabled()) {
return false;
}
JNIEnv* env = base::android::AttachCurrentThread();
auto* window_android = web_contents->GetNativeView()->GetWindowAndroid();
DCHECK(window_android);
return Java_PermissionUtil_canRequestSystemPermission(
env, static_cast<int>(type), window_android->GetJavaObject());
}
void RequestSystemPermissionsForBluetooth(content::WebContents* web_contents) {
JNIEnv* env = base::android::AttachCurrentThread();
auto* window_android = web_contents->GetNativeView()->GetWindowAndroid();
DCHECK(window_android);
// TODO(crbug.com/40255210): Pass the callback from native layer.
return Java_PermissionUtil_requestSystemPermissionsForBluetooth(
env, window_android->GetJavaObject(), nullptr);
}
void RequestLocationServices(content::WebContents* web_contents) {
JNIEnv* env = base::android::AttachCurrentThread();
auto* window_android = web_contents->GetNativeView()->GetWindowAndroid();
DCHECK(window_android);
return Java_PermissionUtil_requestLocationServices(
env, window_android->GetJavaObject());
}
} // namespace permissions
|