blob: 5ddf275d05e1972ca58c1b96e026bcc6dc6fce81 [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
tedchocc3461062015-06-25 17:28:0020bool LocationSettingsImpl::CanSitesRequestLocationPermission(
21 content::WebContents* web_contents) {
newte587c6d2015-01-21 20:45:1222 JNIEnv* env = AttachCurrentThread();
tedchocc3461062015-06-25 17:28:0023 return Java_LocationSettings_canSitesRequestLocationPermission(
torne948f3662016-08-16 15:10:4424 env, web_contents->GetJavaWebContents());
newte587c6d2015-01-21 20:45:1225}
qfiard6b1ebf72017-03-02 23:41:1826
27bool LocationSettingsImpl::CanPromptToEnableSystemLocationSetting() {
28 JNIEnv* env = AttachCurrentThread();
29 return Java_LocationSettings_canPromptToEnableSystemLocationSetting(env);
30}
31
32void LocationSettingsImpl::PromptToEnableSystemLocationSetting(
33 const LocationSettingsDialogContext prompt_context,
34 content::WebContents* web_contents,
35 LocationSettingsDialogOutcomeCallback callback) {
36 JNIEnv* env = AttachCurrentThread();
37 // Transfers the ownership of the callback to the Java callback. The Java
38 // callback is guaranteed to be called unless the user never replies to the
39 // dialog, and the callback pointer will be destroyed in
40 // OnLocationSettingsDialogOutcome.
41 auto* callback_ptr =
42 new LocationSettingsDialogOutcomeCallback(std::move(callback));
43 Java_LocationSettings_promptToEnableSystemLocationSetting(
44 env, prompt_context, web_contents->GetJavaWebContents(),
45 reinterpret_cast<jlong>(callback_ptr));
46}
47
48static void OnLocationSettingsDialogOutcome(
49 JNIEnv* env,
50 const base::android::JavaParamRef<jclass>& jcaller,
51 jlong callback_ptr,
52 int result) {
53 auto* callback =
54 reinterpret_cast<LocationSettingsDialogOutcomeCallback*>(callback_ptr);
55 std::move(*callback).Run(static_cast<LocationSettingsDialogOutcome>(result));
56 // Destroy the callback whose ownership was transferred in
57 // PromptToEnableSystemLocationSetting.
58 delete callback;
59}
60
61bool LocationSettingsImpl::Register(JNIEnv* env) {
62 return RegisterNativesImpl(env);
63}