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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
|
// Copyright 2012 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/contexts/geolocation_permission_context_android.h"
#include <memory>
#include <utility>
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "components/location/android/location_settings.h"
#include "components/location/android/location_settings_impl.h"
#include "components/permissions/android/android_permission_util.h"
#include "components/permissions/android/permissions_reprompt_controller_android.h"
#include "components/permissions/permission_decision.h"
#include "components/permissions/permission_request_data.h"
#include "components/permissions/permission_request_id.h"
#include "components/permissions/permissions_client.h"
#include "components/permissions/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/permission_descriptor_util.h"
#include "content/public/browser/permission_request_description.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "url/gurl.h"
namespace permissions {
namespace {
using PermissionStatus = blink::mojom::PermissionStatus;
int g_day_offset_for_testing = 0;
base::Time GetTimeNow() {
return base::Time::Now() + base::Days(g_day_offset_for_testing);
}
// These values are recorded in histograms. Entries should not be renumbered and
// numeric values should never be reused.
enum class AndroidLocationPermissionState {
kNoAccess = 0,
kAccessCoarse = 1,
kAccessFine = 2,
kMaxValue = kAccessFine,
};
void RecordUmaPermissionState(AndroidLocationPermissionState state) {
base::UmaHistogramEnumeration("Geolocation.Android.LocationPermissionState",
state);
}
} // namespace
// static
void GeolocationPermissionContextAndroid::RegisterProfilePrefs(
PrefRegistrySimple* registry) {
registry->RegisterIntegerPref(prefs::kLocationSettingsBackoffLevelDSE, 0);
registry->RegisterIntegerPref(prefs::kLocationSettingsBackoffLevelDefault, 0);
registry->RegisterInt64Pref(prefs::kLocationSettingsNextShowDSE, 0);
registry->RegisterInt64Pref(prefs::kLocationSettingsNextShowDefault, 0);
}
GeolocationPermissionContextAndroid::GeolocationPermissionContextAndroid(
content::BrowserContext* browser_context,
std::unique_ptr<Delegate> delegate,
bool is_regular_profile,
std::unique_ptr<LocationSettings> settings_override_for_test)
: GeolocationPermissionContext(browser_context, std::move(delegate)),
location_settings_(std::move(settings_override_for_test)),
location_settings_dialog_request_id_(
content::GlobalRenderFrameHostId(0, 0),
PermissionRequestID::RequestLocalId()) {
if (!location_settings_) {
location_settings_ = std::make_unique<LocationSettingsImpl>();
}
if (is_regular_profile) {
// Record the initial system permission state.
if (location_settings_->HasAndroidFineLocationPermission()) {
RecordUmaPermissionState(AndroidLocationPermissionState::kAccessFine);
} else if (location_settings_->HasAndroidLocationPermission()) {
RecordUmaPermissionState(AndroidLocationPermissionState::kAccessCoarse);
} else {
RecordUmaPermissionState(AndroidLocationPermissionState::kNoAccess);
}
}
}
GeolocationPermissionContextAndroid::~GeolocationPermissionContextAndroid() =
default;
void GeolocationPermissionContextAndroid::OnRequestsFinalized() {
std::vector<std::pair<std::unique_ptr<PermissionRequestData>,
BrowserPermissionCallback>>
pending_reprompt_requests_local;
pending_reprompt_requests_.swap(pending_reprompt_requests_local);
for (auto& [request_data, callback] : pending_reprompt_requests_local) {
GeolocationPermissionContext::RequestPermission(std::move(request_data),
std::move(callback));
}
}
// static
void GeolocationPermissionContextAndroid::AddDayOffsetForTesting(int days) {
g_day_offset_for_testing += days;
}
void GeolocationPermissionContextAndroid::RequestPermission(
std::unique_ptr<PermissionRequestData> request_data,
BrowserPermissionCallback callback) {
content::RenderFrameHost* const render_frame_host =
content::RenderFrameHost::FromID(
request_data->id.global_render_frame_host_id());
content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(render_frame_host);
request_data->embedding_origin = PermissionUtil::GetLastCommittedOriginAsURL(
render_frame_host->GetMainFrame());
// Relax the location access check if the request comes from a permission
// element and still keep the whole permission process going. We'll check the
// status later when we show a prompt and help the user fix it if they haven't
// given us location access yet.
if (!request_data->embedded_permission_element_initiated &&
!IsLocationAccessPossible(web_contents, request_data->requesting_origin,
request_data->user_gesture)) {
NotifyPermissionSet(*request_data, std::move(callback),
/*persist=*/false, PermissionDecision::kDeny,
/*is_final_decision=*/true);
return;
}
DCHECK(render_frame_host);
PermissionStatus status =
GeolocationPermissionContext::GetPermissionStatus(
*request_data->resolver, render_frame_host,
request_data->requesting_origin, request_data->embedding_origin)
.status;
if (!request_data->embedded_permission_element_initiated &&
status == PermissionStatus::GRANTED &&
ShouldRepromptUserForPermissions(web_contents,
{ContentSettingsType::GEOLOCATION}) ==
PermissionRepromptState::kShow) {
if (auto* manager =
PermissionRequestManager::FromWebContents(web_contents)) {
if (manager->IsCurrentRequestEmbeddedPermissionElementInitiated() &&
manager->Requests()[0]->request_type() == RequestType::kGeolocation) {
manager->AddObserver(this);
pending_reprompt_requests_.push_back(
std::make_pair(std::move(request_data), std::move(callback)));
return;
}
}
permissions::PermissionsRepromptControllerAndroid::CreateForWebContents(
web_contents);
permissions::PermissionsRepromptControllerAndroid::FromWebContents(
web_contents)
->RepromptPermissionRequest(
{ContentSettingsType::GEOLOCATION}, content_settings_type(),
base::BindOnce(&GeolocationPermissionContextAndroid::
HandleUpdateAndroidPermissions,
weak_factory_.GetWeakPtr(), request_data->id,
request_data->requesting_origin,
request_data->embedding_origin,
std::move(callback)));
return;
}
GeolocationPermissionContext::RequestPermission(std::move(request_data),
std::move(callback));
}
void GeolocationPermissionContextAndroid::UserMadePermissionDecision(
const PermissionRequestID& id,
const GURL& requesting_origin,
const GURL& embedding_origin,
PermissionDecision decision) {
// If the user has accepted geolocation, reset the location settings dialog
// backoff.
if (decision == PermissionDecision::kAllow) {
ResetLocationSettingsBackOff(IsRequestingOriginDSE(requesting_origin));
}
}
void GeolocationPermissionContextAndroid::NotifyPermissionSet(
const PermissionRequestData& request_data,
BrowserPermissionCallback callback,
bool persist,
PermissionDecision decision,
bool is_final_decision) {
DCHECK(is_final_decision);
bool is_default_search =
IsRequestingOriginDSE(request_data.requesting_origin);
if (decision == PermissionDecision::kAllow &&
!location_settings_->IsSystemLocationSettingEnabled()) {
// There is no need to check CanShowLocationSettingsDialog here again, as it
// must have been checked to get this far. But, the backoff will not have
// been checked, so check that. Backoff isn't checked earlier because if the
// content setting is ASK the backoff should be ignored. However if we get
// here and the content setting was ASK, the user must have accepted which
// would reset the backoff.
if (IsInLocationSettingsBackOff(is_default_search)) {
FinishNotifyPermissionSet(request_data.id, request_data.requesting_origin,
request_data.embedding_origin,
std::move(callback), false /* persist */,
PermissionDecision::kDeny);
return;
}
content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(
content::RenderFrameHost::FromID(
request_data.id.global_render_frame_host_id()));
if (!web_contents)
return;
// Only show the location settings dialog if the tab for |web_contents| is
// user-interactable (i.e. is the current tab, and Chrome is active and not
// in tab-switching mode) and we're not already showing the LSD. The latter
// case can occur in split-screen multi-window.
if (!delegate_->IsInteractable(web_contents) ||
!location_settings_dialog_callback_.is_null()) {
FinishNotifyPermissionSet(request_data.id, request_data.requesting_origin,
request_data.embedding_origin,
std::move(callback), false /* persist */,
PermissionDecision::kDeny);
return;
}
location_settings_dialog_request_id_ = request_data.id;
location_settings_dialog_callback_ = std::move(callback);
location_settings_->PromptToEnableSystemLocationSetting(
is_default_search ? SEARCH : DEFAULT,
web_contents->GetTopLevelNativeWindow(),
base::BindOnce(
&GeolocationPermissionContextAndroid::OnLocationSettingsDialogShown,
weak_factory_.GetWeakPtr(), request_data.requesting_origin,
request_data.embedding_origin, persist, decision));
return;
}
FinishNotifyPermissionSet(request_data.id, request_data.requesting_origin,
request_data.embedding_origin, std::move(callback),
persist, decision);
}
content::PermissionResult
GeolocationPermissionContextAndroid::UpdatePermissionStatusWithDeviceStatus(
content::WebContents* web_contents,
content::PermissionResult result,
const GURL& requesting_origin,
const GURL& embedding_origin) {
MaybeUpdateCachedHasDevicePermission(web_contents);
if (result.status != PermissionStatus::DENIED) {
if (!location_settings_->IsSystemLocationSettingEnabled()) {
// As this is returning the status for possible future permission
// requests, whose gesture status is unknown, pretend there is a user
// gesture here. If there is a possibility of PROMPT (i.e. if there is a
// user gesture attached to the later request) that should be returned,
// not BLOCK.
// If the permission is in the ASK state, backoff is ignored. Permission
// prompts are shown regardless of backoff, if the location is off and the
// LSD can be shown, as permission prompts are less annoying than the
// modal LSD, and if the user accepts the permission prompt the LSD
// backoff will be reset.
if (CanShowLocationSettingsDialog(
requesting_origin, true /* user_gesture */,
result.status == PermissionStatus::ASK /* ignore_backoff */)) {
result.status = PermissionStatus::ASK;
} else {
result.status = PermissionStatus::DENIED;
}
result.source = content::PermissionStatusSource::UNSPECIFIED;
}
if (result.status != PermissionStatus::DENIED &&
!location_settings_->HasAndroidLocationPermission()) {
// TODO(benwells): plumb through the RFH and use the associated
// WebContents to check that the android location can be prompted for.
result.status = PermissionStatus::ASK;
result.source = content::PermissionStatusSource::UNSPECIFIED;
}
}
return result;
}
bool GeolocationPermissionContextAndroid::AlwaysIncludeDeviceStatus() const {
return true;
}
std::string
GeolocationPermissionContextAndroid::GetLocationSettingsBackOffLevelPref(
bool is_default_search) const {
return is_default_search ? prefs::kLocationSettingsBackoffLevelDSE
: prefs::kLocationSettingsBackoffLevelDefault;
}
std::string
GeolocationPermissionContextAndroid::GetLocationSettingsNextShowPref(
bool is_default_search) const {
return is_default_search ? prefs::kLocationSettingsNextShowDSE
: prefs::kLocationSettingsNextShowDefault;
}
bool GeolocationPermissionContextAndroid::IsInLocationSettingsBackOff(
bool is_default_search) const {
base::Time next_show = base::Time::FromInternalValue(
delegate_->GetPrefs(browser_context())
->GetInt64(GetLocationSettingsNextShowPref(is_default_search)));
return GetTimeNow() < next_show;
}
void GeolocationPermissionContextAndroid::ResetLocationSettingsBackOff(
bool is_default_search) {
PrefService* prefs = delegate_->GetPrefs(browser_context());
prefs->ClearPref(GetLocationSettingsNextShowPref(is_default_search));
prefs->ClearPref(GetLocationSettingsBackOffLevelPref(is_default_search));
}
void GeolocationPermissionContextAndroid::UpdateLocationSettingsBackOff(
bool is_default_search) {
LocationSettingsDialogBackOff backoff_level =
LocationSettingsBackOffLevel(is_default_search);
base::Time next_show = GetTimeNow();
switch (backoff_level) {
case LocationSettingsDialogBackOff::kNoBackOff:
backoff_level = LocationSettingsDialogBackOff::kOneWeek;
next_show += base::Days(7);
break;
case LocationSettingsDialogBackOff::kOneWeek:
backoff_level = LocationSettingsDialogBackOff::kOneMonth;
next_show += base::Days(30);
break;
case LocationSettingsDialogBackOff::kOneMonth:
backoff_level = LocationSettingsDialogBackOff::kThreeMonths;
[[fallthrough]];
case LocationSettingsDialogBackOff::kThreeMonths:
next_show += base::Days(90);
break;
default:
NOTREACHED();
}
PrefService* prefs = delegate_->GetPrefs(browser_context());
prefs->SetInteger(GetLocationSettingsBackOffLevelPref(is_default_search),
static_cast<int>(backoff_level));
prefs->SetInt64(GetLocationSettingsNextShowPref(is_default_search),
next_show.ToInternalValue());
}
GeolocationPermissionContextAndroid::LocationSettingsDialogBackOff
GeolocationPermissionContextAndroid::LocationSettingsBackOffLevel(
bool is_default_search) const {
PrefService* prefs = delegate_->GetPrefs(browser_context());
int int_backoff =
prefs->GetInteger(GetLocationSettingsBackOffLevelPref(is_default_search));
return static_cast<LocationSettingsDialogBackOff>(int_backoff);
}
bool GeolocationPermissionContextAndroid::IsLocationAccessPossible(
content::WebContents* web_contents,
const GURL& requesting_origin,
bool user_gesture) {
return (location_settings_->HasAndroidLocationPermission() ||
location_settings_->CanPromptForAndroidLocationPermission(
web_contents->GetTopLevelNativeWindow())) &&
(location_settings_->IsSystemLocationSettingEnabled() ||
CanShowLocationSettingsDialog(requesting_origin, user_gesture,
true /* ignore_backoff */));
}
bool GeolocationPermissionContextAndroid::IsRequestingOriginDSE(
const GURL& requesting_origin) const {
return delegate_->IsRequestingOriginDSE(browser_context(), requesting_origin);
}
void GeolocationPermissionContextAndroid::HandleUpdateAndroidPermissions(
const PermissionRequestID& id,
const GURL& requesting_frame_origin,
const GURL& embedding_origin,
BrowserPermissionCallback callback,
bool permissions_updated) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
PermissionDecision decision = permissions_updated ? PermissionDecision::kAllow
: PermissionDecision::kDeny;
NotifyPermissionSet(
PermissionRequestData(this, id,
content::PermissionRequestDescription(
content::PermissionDescriptorUtil::
CreatePermissionDescriptorForPermissionType(
blink::PermissionType::GEOLOCATION)),
requesting_frame_origin, embedding_origin),
std::move(callback), false /* persist */, decision,
/*is_final_decision=*/true);
}
bool GeolocationPermissionContextAndroid::CanShowLocationSettingsDialog(
const GURL& requesting_origin,
bool user_gesture,
bool ignore_backoff) const {
bool is_default_search = IsRequestingOriginDSE(requesting_origin);
// If this isn't the default search engine, a gesture is needed.
if (!is_default_search && !user_gesture) {
return false;
}
if (!ignore_backoff && IsInLocationSettingsBackOff(is_default_search))
return false;
return location_settings_->CanPromptToEnableSystemLocationSetting();
}
void GeolocationPermissionContextAndroid::OnLocationSettingsDialogShown(
const GURL& requesting_origin,
const GURL& embedding_origin,
bool persist,
PermissionDecision decision,
LocationSettingsDialogOutcome prompt_outcome) {
bool is_default_search = IsRequestingOriginDSE(requesting_origin);
if (prompt_outcome == GRANTED) {
ResetLocationSettingsBackOff(is_default_search);
} else {
UpdateLocationSettingsBackOff(is_default_search);
decision = PermissionDecision::kDeny;
persist = false;
}
// If the permission was cancelled while the LSD was up, the callback has
// already been dropped.
if (!location_settings_dialog_callback_)
return;
FinishNotifyPermissionSet(
location_settings_dialog_request_id_, requesting_origin, embedding_origin,
std::move(location_settings_dialog_callback_), persist, decision);
location_settings_dialog_request_id_ =
PermissionRequestID(content::GlobalRenderFrameHostId(0, 0),
PermissionRequestID::RequestLocalId());
}
void GeolocationPermissionContextAndroid::FinishNotifyPermissionSet(
const PermissionRequestID& id,
const GURL& requesting_origin,
const GURL& embedding_origin,
BrowserPermissionCallback callback,
bool persist,
PermissionDecision decision) {
GeolocationPermissionContext::NotifyPermissionSet(
PermissionRequestData(this, id,
content::PermissionRequestDescription(
content::PermissionDescriptorUtil::
CreatePermissionDescriptorForPermissionType(
blink::PermissionType::GEOLOCATION)),
requesting_origin),
std::move(callback), persist, decision, /*is_final_decision=*/true);
}
void GeolocationPermissionContextAndroid::SetLocationSettingsForTesting(
std::unique_ptr<LocationSettings> settings) {
location_settings_ = std::move(settings);
}
} // namespace permissions
|