• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #ifndef CONTENT_BROWSER_GEOLOCATION_LOCATION_API_ADAPTER_ANDROID_H_
6 #define CONTENT_BROWSER_GEOLOCATION_LOCATION_API_ADAPTER_ANDROID_H_
7 
8 #include "base/android/jni_weak_ref.h"
9 #include "base/android/scoped_java_ref.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/singleton.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "base/synchronization/lock.h"
14 
15 namespace content {
16 class LocationProviderAndroid;
17 struct Geoposition;
18 
19 // Interacts with JNI and reports back to AndroidLocationProvider.
20 // This class creates a LocationProvider java object and listens for
21 // updates.
22 // The simplified flow is:
23 // GeolocationProvider runs in a Geolocation Thread and fetches geolocation data
24 // from a LocationProvider.
25 // AndroidLocationProvider access a singleton AndroidLocationApiAdapter
26 // AndroidLocationApiAdapter calls via JNI and uses the main thread Looper
27 // in the java side to listen for location updates. We then bounce these updates
28 // to the Geolocation thread.
29 // Note that AndroidLocationApiAdapter is a singleton and there's at most only
30 // one AndroidLocationProvider that has called Start().
31 class AndroidLocationApiAdapter {
32  public:
33   // Starts the underlying location provider, returns true if successful.
34   // Called on the Geolocation thread.
35   bool Start(LocationProviderAndroid* location_provider, bool high_accuracy);
36   // Stops the underlying location provider.
37   // Called on the Geolocation thread.
38   void Stop();
39 
40   // Returns our singleton.
41   static AndroidLocationApiAdapter* GetInstance();
42 
43   // Called when initializing chrome_view to obtain a pointer to the java class.
44   static bool RegisterGeolocationService(JNIEnv* env);
45 
46   // Called by JNI on main thread looper.
47   static void OnNewLocationAvailable(double latitude,
48                                      double longitude,
49                                      double time_stamp,
50                                      bool has_altitude, double altitude,
51                                      bool has_accuracy, double accuracy,
52                                      bool has_heading, double heading,
53                                      bool has_speed, double speed);
54   static void OnNewErrorAvailable(JNIEnv* env, jstring message);
55 
56  private:
57   friend struct DefaultSingletonTraits<AndroidLocationApiAdapter>;
58   AndroidLocationApiAdapter();
59   ~AndroidLocationApiAdapter();
60 
61   void CreateJavaObject(JNIEnv* env);
62 
63   // Called on the JNI main thread looper.
64   void OnNewGeopositionInternal(const Geoposition& geoposition);
65 
66   /// Called on the Geolocation thread.
67   static void NotifyProviderNewGeoposition(const Geoposition& geoposition);
68 
69   base::android::ScopedJavaGlobalRef<jobject>
70       java_location_provider_android_object_;
71   LocationProviderAndroid* location_provider_;
72 
73   // Guards against the following member which is accessed on Geolocation
74   // thread and the JNI main thread looper.
75   base::Lock lock_;
76   scoped_refptr<base::MessageLoopProxy> message_loop_;
77 };
78 
79 }  // namespace content
80 
81 #endif  // CONTENT_BROWSER_GEOLOCATION_LOCATION_API_ADAPTER_ANDROID_H_
82