location_settings_impl.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #include "components/location/android/location_settings_impl.h"
  5. #include "base/android/jni_android.h"
  6. #include "components/location/android/jni_headers/LocationSettings_jni.h"
  7. #include "components/location/android/location_settings_dialog_outcome.h"
  8. #include "ui/android/window_android.h"
  9. using base::android::AttachCurrentThread;
  10. using LocationSettingsDialogOutcomeCallback =
  11. LocationSettings::LocationSettingsDialogOutcomeCallback;
  12. LocationSettingsImpl::LocationSettingsImpl() {}
  13. LocationSettingsImpl::~LocationSettingsImpl() {}
  14. bool LocationSettingsImpl::HasAndroidLocationPermission() {
  15. JNIEnv* env = AttachCurrentThread();
  16. return Java_LocationSettings_hasAndroidLocationPermission(env);
  17. }
  18. bool LocationSettingsImpl::CanPromptForAndroidLocationPermission(
  19. ui::WindowAndroid* window) {
  20. if (window == nullptr)
  21. return false;
  22. JNIEnv* env = AttachCurrentThread();
  23. return Java_LocationSettings_canPromptForAndroidLocationPermission(
  24. env, window->GetJavaObject());
  25. }
  26. bool LocationSettingsImpl::IsSystemLocationSettingEnabled() {
  27. JNIEnv* env = AttachCurrentThread();
  28. return Java_LocationSettings_isSystemLocationSettingEnabled(env);
  29. }
  30. bool LocationSettingsImpl::CanPromptToEnableSystemLocationSetting() {
  31. JNIEnv* env = AttachCurrentThread();
  32. return Java_LocationSettings_canPromptToEnableSystemLocationSetting(env);
  33. }
  34. void LocationSettingsImpl::PromptToEnableSystemLocationSetting(
  35. const LocationSettingsDialogContext prompt_context,
  36. ui::WindowAndroid* window,
  37. LocationSettingsDialogOutcomeCallback callback) {
  38. if (window == nullptr) {
  39. std::move(callback).Run(LocationSettingsDialogOutcome::NO_PROMPT);
  40. return;
  41. }
  42. JNIEnv* env = AttachCurrentThread();
  43. // Transfers the ownership of the callback to the Java callback. The Java
  44. // callback is guaranteed to be called unless the user never replies to the
  45. // dialog, and the callback pointer will be destroyed in
  46. // OnLocationSettingsDialogOutcome.
  47. auto* callback_ptr =
  48. new LocationSettingsDialogOutcomeCallback(std::move(callback));
  49. Java_LocationSettings_promptToEnableSystemLocationSetting(
  50. env, prompt_context, window->GetJavaObject(),
  51. reinterpret_cast<jlong>(callback_ptr));
  52. }
  53. static void JNI_LocationSettings_OnLocationSettingsDialogOutcome(
  54. JNIEnv* env,
  55. jlong callback_ptr,
  56. int result) {
  57. auto* callback =
  58. reinterpret_cast<LocationSettingsDialogOutcomeCallback*>(callback_ptr);
  59. std::move(*callback).Run(static_cast<LocationSettingsDialogOutcome>(result));
  60. // Destroy the callback whose ownership was transferred in
  61. // PromptToEnableSystemLocationSetting.
  62. delete callback;
  63. }