blob: 5cf79486d6131bbb2c597fe7e18be2359d68c4c8 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2015 The Chromium Authors
newte587c6d2015-01-21 20:45:122// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Clark DuVall18d5be82020-03-16 23:40:345#include "components/location/android/location_settings_impl.h"
newte587c6d2015-01-21 20:45:126
7#include "base/android/jni_android.h"
Clark DuVall18d5be82020-03-16 23:40:348#include "components/location/android/location_settings_dialog_outcome.h"
9#include "ui/android/window_android.h"
newte587c6d2015-01-21 20:45:1210
Andrew Grieveecb885bb2024-05-29 18:14:1911// Must come after all headers that specialize FromJniType() / ToJniType().
12#include "components/location/android/jni_headers/LocationSettings_jni.h"
13
newte587c6d2015-01-21 20:45:1214using base::android::AttachCurrentThread;
15
qfiard6b1ebf72017-03-02 23:41:1816using LocationSettingsDialogOutcomeCallback =
17 LocationSettings::LocationSettingsDialogOutcomeCallback;
18
Sorin Jianu6fbeca02024-10-09 17:31:5219LocationSettingsImpl::LocationSettingsImpl() = default;
newte587c6d2015-01-21 20:45:1220
Sorin Jianu6fbeca02024-10-09 17:31:5221LocationSettingsImpl::~LocationSettingsImpl() = default;
newte587c6d2015-01-21 20:45:1222
benwellse962df52017-03-03 23:26:5823bool LocationSettingsImpl::HasAndroidLocationPermission() {
24 JNIEnv* env = AttachCurrentThread();
25 return Java_LocationSettings_hasAndroidLocationPermission(env);
26}
27
Matt Reynoldsa8ed6872023-07-06 19:21:2628bool LocationSettingsImpl::HasAndroidFineLocationPermission() {
29 JNIEnv* env = AttachCurrentThread();
30 return Java_LocationSettings_hasAndroidFineLocationPermission(env);
31}
32
benwellse962df52017-03-03 23:26:5833bool LocationSettingsImpl::CanPromptForAndroidLocationPermission(
Clark DuVall18d5be82020-03-16 23:40:3434 ui::WindowAndroid* window) {
35 if (window == nullptr)
36 return false;
newte587c6d2015-01-21 20:45:1237 JNIEnv* env = AttachCurrentThread();
benwellse962df52017-03-03 23:26:5838 return Java_LocationSettings_canPromptForAndroidLocationPermission(
Clark DuVall18d5be82020-03-16 23:40:3439 env, window->GetJavaObject());
newte587c6d2015-01-21 20:45:1240}
qfiard6b1ebf72017-03-02 23:41:1841
benwellse962df52017-03-03 23:26:5842bool LocationSettingsImpl::IsSystemLocationSettingEnabled() {
43 JNIEnv* env = AttachCurrentThread();
44 return Java_LocationSettings_isSystemLocationSettingEnabled(env);
45}
46
qfiard6b1ebf72017-03-02 23:41:1847bool LocationSettingsImpl::CanPromptToEnableSystemLocationSetting() {
48 JNIEnv* env = AttachCurrentThread();
49 return Java_LocationSettings_canPromptToEnableSystemLocationSetting(env);
50}
51
52void LocationSettingsImpl::PromptToEnableSystemLocationSetting(
53 const LocationSettingsDialogContext prompt_context,
Clark DuVall18d5be82020-03-16 23:40:3454 ui::WindowAndroid* window,
qfiard6b1ebf72017-03-02 23:41:1855 LocationSettingsDialogOutcomeCallback callback) {
Clark DuVall18d5be82020-03-16 23:40:3456 if (window == nullptr) {
57 std::move(callback).Run(LocationSettingsDialogOutcome::NO_PROMPT);
58 return;
59 }
qfiard6b1ebf72017-03-02 23:41:1860 JNIEnv* env = AttachCurrentThread();
61 // Transfers the ownership of the callback to the Java callback. The Java
62 // callback is guaranteed to be called unless the user never replies to the
63 // dialog, and the callback pointer will be destroyed in
64 // OnLocationSettingsDialogOutcome.
65 auto* callback_ptr =
66 new LocationSettingsDialogOutcomeCallback(std::move(callback));
67 Java_LocationSettings_promptToEnableSystemLocationSetting(
Clark DuVall18d5be82020-03-16 23:40:3468 env, prompt_context, window->GetJavaObject(),
qfiard6b1ebf72017-03-02 23:41:1869 reinterpret_cast<jlong>(callback_ptr));
70}
71
Daniel Bratell7aacf952017-11-21 17:51:2572static void JNI_LocationSettings_OnLocationSettingsDialogOutcome(
qfiard6b1ebf72017-03-02 23:41:1873 JNIEnv* env,
qfiard6b1ebf72017-03-02 23:41:1874 jlong callback_ptr,
75 int result) {
76 auto* callback =
77 reinterpret_cast<LocationSettingsDialogOutcomeCallback*>(callback_ptr);
78 std::move(*callback).Run(static_cast<LocationSettingsDialogOutcome>(result));
79 // Destroy the callback whose ownership was transferred in
80 // PromptToEnableSystemLocationSetting.
81 delete callback;
82}