newt | e587c6d | 2015-01-21 20:45:12 | [diff] [blame] | 1 | // 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" |
tedchoc | c346106 | 2015-06-25 17:28:00 | [diff] [blame] | 8 | #include "content/public/browser/web_contents.h" |
newt | e587c6d | 2015-01-21 20:45:12 | [diff] [blame] | 9 | #include "jni/LocationSettings_jni.h" |
| 10 | |
| 11 | using base::android::AttachCurrentThread; |
| 12 | |
qfiard | 6b1ebf7 | 2017-03-02 23:41:18 | [diff] [blame^] | 13 | using LocationSettingsDialogOutcomeCallback = |
| 14 | LocationSettings::LocationSettingsDialogOutcomeCallback; |
| 15 | |
newt | e587c6d | 2015-01-21 20:45:12 | [diff] [blame] | 16 | LocationSettingsImpl::LocationSettingsImpl() {} |
| 17 | |
| 18 | LocationSettingsImpl::~LocationSettingsImpl() {} |
| 19 | |
tedchoc | c346106 | 2015-06-25 17:28:00 | [diff] [blame] | 20 | bool LocationSettingsImpl::CanSitesRequestLocationPermission( |
| 21 | content::WebContents* web_contents) { |
newt | e587c6d | 2015-01-21 20:45:12 | [diff] [blame] | 22 | JNIEnv* env = AttachCurrentThread(); |
tedchoc | c346106 | 2015-06-25 17:28:00 | [diff] [blame] | 23 | return Java_LocationSettings_canSitesRequestLocationPermission( |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 24 | env, web_contents->GetJavaWebContents()); |
newt | e587c6d | 2015-01-21 20:45:12 | [diff] [blame] | 25 | } |
qfiard | 6b1ebf7 | 2017-03-02 23:41:18 | [diff] [blame^] | 26 | |
| 27 | bool LocationSettingsImpl::CanPromptToEnableSystemLocationSetting() { |
| 28 | JNIEnv* env = AttachCurrentThread(); |
| 29 | return Java_LocationSettings_canPromptToEnableSystemLocationSetting(env); |
| 30 | } |
| 31 | |
| 32 | void 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 | |
| 48 | static 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 | |
| 61 | bool LocationSettingsImpl::Register(JNIEnv* env) { |
| 62 | return RegisterNativesImpl(env); |
| 63 | } |