blob: 41cb546714328d20f6189901578d9cbf47744097 [file] [log] [blame]
newte587c6d2015-01-21 20:45:121// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/android/location_settings_impl.h"
6
7#include "base/android/jni_android.h"
tedchocc3461062015-06-25 17:28:008#include "content/public/browser/web_contents.h"
newte587c6d2015-01-21 20:45:129#include "jni/LocationSettings_jni.h"
10
11using base::android::AttachCurrentThread;
12
qfiard6b1ebf72017-03-02 23:41:1813using LocationSettingsDialogOutcomeCallback =
14 LocationSettings::LocationSettingsDialogOutcomeCallback;
15
newte587c6d2015-01-21 20:45:1216LocationSettingsImpl::LocationSettingsImpl() {}
17
18LocationSettingsImpl::~LocationSettingsImpl() {}
19
benwellse962df52017-03-03 23:26:5820bool LocationSettingsImpl::HasAndroidLocationPermission() {
21 JNIEnv* env = AttachCurrentThread();
22 return Java_LocationSettings_hasAndroidLocationPermission(env);
23}
24
25bool LocationSettingsImpl::CanPromptForAndroidLocationPermission(
tedchocc3461062015-06-25 17:28:0026 content::WebContents* web_contents) {
newte587c6d2015-01-21 20:45:1227 JNIEnv* env = AttachCurrentThread();
benwellse962df52017-03-03 23:26:5828 return Java_LocationSettings_canPromptForAndroidLocationPermission(
torne948f3662016-08-16 15:10:4429 env, web_contents->GetJavaWebContents());
newte587c6d2015-01-21 20:45:1230}
qfiard6b1ebf72017-03-02 23:41:1831
benwellse962df52017-03-03 23:26:5832bool LocationSettingsImpl::IsSystemLocationSettingEnabled() {
33 JNIEnv* env = AttachCurrentThread();
34 return Java_LocationSettings_isSystemLocationSettingEnabled(env);
35}
36
qfiard6b1ebf72017-03-02 23:41:1837bool LocationSettingsImpl::CanPromptToEnableSystemLocationSetting() {
38 JNIEnv* env = AttachCurrentThread();
39 return Java_LocationSettings_canPromptToEnableSystemLocationSetting(env);
40}
41
42void LocationSettingsImpl::PromptToEnableSystemLocationSetting(
43 const LocationSettingsDialogContext prompt_context,
44 content::WebContents* web_contents,
45 LocationSettingsDialogOutcomeCallback callback) {
46 JNIEnv* env = AttachCurrentThread();
47 // Transfers the ownership of the callback to the Java callback. The Java
48 // callback is guaranteed to be called unless the user never replies to the
49 // dialog, and the callback pointer will be destroyed in
50 // OnLocationSettingsDialogOutcome.
51 auto* callback_ptr =
52 new LocationSettingsDialogOutcomeCallback(std::move(callback));
53 Java_LocationSettings_promptToEnableSystemLocationSetting(
54 env, prompt_context, web_contents->GetJavaWebContents(),
55 reinterpret_cast<jlong>(callback_ptr));
56}
57
Daniel Bratell7aacf952017-11-21 17:51:2558static void JNI_LocationSettings_OnLocationSettingsDialogOutcome(
qfiard6b1ebf72017-03-02 23:41:1859 JNIEnv* env,
qfiard6b1ebf72017-03-02 23:41:1860 jlong callback_ptr,
61 int result) {
62 auto* callback =
63 reinterpret_cast<LocationSettingsDialogOutcomeCallback*>(callback_ptr);
64 std::move(*callback).Run(static_cast<LocationSettingsDialogOutcome>(result));
65 // Destroy the callback whose ownership was transferred in
66 // PromptToEnableSystemLocationSetting.
67 delete callback;
68}