location_api_adapter_android.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) 2012 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. #ifndef SERVICES_DEVICE_GEOLOCATION_LOCATION_API_ADAPTER_ANDROID_H_
  5. #define SERVICES_DEVICE_GEOLOCATION_LOCATION_API_ADAPTER_ANDROID_H_
  6. #include "base/android/jni_weak_ref.h"
  7. #include "base/android/scoped_java_ref.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/memory/singleton.h"
  10. #include "base/synchronization/lock.h"
  11. #include "base/threading/thread_checker.h"
  12. #include "services/device/public/mojom/geoposition.mojom.h"
  13. namespace base {
  14. class SingleThreadTaskRunner;
  15. }
  16. namespace device {
  17. // Interacts with JNI and reports back to |on_geoposition_callback_|. This class
  18. // creates a LocationProvider java object and listens for updates.
  19. // The simplified flow is:
  20. // - GeolocationProvider runs in a Geolocation |task_runner_| and fetches
  21. // geolocation data from a LocationProvider.
  22. // - LocationApiAdapterAndroid calls via JNI and uses the main thread Looper
  23. // in the java side to listen for location updates. We then bounce these
  24. // updates to the Geolocation |task_runner_|.
  25. //
  26. // Note that LocationApiAdapterAndroid is a singleton and there's at most only
  27. // one call to Start().
  28. class LocationApiAdapterAndroid {
  29. public:
  30. using OnGeopositionCB =
  31. base::RepeatingCallback<void(const mojom::Geoposition&)>;
  32. // Starts the underlying location provider.
  33. // Called on |task_runner_|.
  34. void Start(OnGeopositionCB on_geoposition_callback, bool high_accuracy);
  35. // Stops the underlying location provider. Called on |task_runner_|.
  36. void Stop();
  37. // Called by JNI on its thread looper.
  38. static void OnNewLocationAvailable(double latitude,
  39. double longitude,
  40. double time_stamp,
  41. bool has_altitude,
  42. double altitude,
  43. bool has_accuracy,
  44. double accuracy,
  45. bool has_heading,
  46. double heading,
  47. bool has_speed,
  48. double speed);
  49. static void OnNewErrorAvailable(JNIEnv* env, jstring message);
  50. // Returns our singleton.
  51. static LocationApiAdapterAndroid* GetInstance();
  52. private:
  53. friend struct base::DefaultSingletonTraits<LocationApiAdapterAndroid>;
  54. LocationApiAdapterAndroid();
  55. ~LocationApiAdapterAndroid();
  56. // Calls |on_geoposition_callback_| with the new location.
  57. void NotifyNewGeoposition(const mojom::Geoposition& geoposition);
  58. base::android::ScopedJavaGlobalRef<jobject> java_location_provider_adapter_;
  59. // Valid between Start() and Stop().
  60. OnGeopositionCB on_geoposition_callback_;
  61. const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  62. base::ThreadChecker thread_checker_;
  63. };
  64. } // namespace device
  65. #endif // SERVICES_DEVICE_GEOLOCATION_LOCATION_API_ADAPTER_ANDROID_H_