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.
5 #ifndef CONTENT_BROWSER_GEOLOCATION_LOCATION_API_ADAPTER_ANDROID_H_
6 #define CONTENT_BROWSER_GEOLOCATION_LOCATION_API_ADAPTER_ANDROID_H_
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/synchronization/lock.h"
15 class SingleThreadTaskRunner
;
19 class LocationProviderAndroid
;
22 // Interacts with JNI and reports back to AndroidLocationProvider.
23 // This class creates a LocationProvider java object and listens for
25 // The simplified flow is:
26 // GeolocationProvider runs in a Geolocation Thread and fetches geolocation data
27 // from a LocationProvider.
28 // AndroidLocationProvider access a singleton AndroidLocationApiAdapter
29 // AndroidLocationApiAdapter calls via JNI and uses the main thread Looper
30 // in the java side to listen for location updates. We then bounce these updates
31 // to the Geolocation thread.
32 // Note that AndroidLocationApiAdapter is a singleton and there's at most only
33 // one AndroidLocationProvider that has called Start().
34 class AndroidLocationApiAdapter
{
36 // Starts the underlying location provider, returns true if successful.
37 // Called on the Geolocation thread.
38 bool Start(LocationProviderAndroid
* location_provider
, bool high_accuracy
);
39 // Stops the underlying location provider.
40 // Called on the Geolocation thread.
43 // Returns our singleton.
44 static AndroidLocationApiAdapter
* GetInstance();
46 // Called when initializing chrome_view to obtain a pointer to the java class.
47 static bool RegisterGeolocationService(JNIEnv
* env
);
49 // Called by JNI on main thread looper.
50 static void OnNewLocationAvailable(double latitude
,
53 bool has_altitude
, double altitude
,
54 bool has_accuracy
, double accuracy
,
55 bool has_heading
, double heading
,
56 bool has_speed
, double speed
);
57 static void OnNewErrorAvailable(JNIEnv
* env
, jstring message
);
60 friend struct base::DefaultSingletonTraits
<AndroidLocationApiAdapter
>;
61 AndroidLocationApiAdapter();
62 ~AndroidLocationApiAdapter();
64 void CreateJavaObject(JNIEnv
* env
);
66 // Called on the JNI main thread looper.
67 void OnNewGeopositionInternal(const Geoposition
& geoposition
);
69 /// Called on the Geolocation thread.
70 static void NotifyProviderNewGeoposition(const Geoposition
& geoposition
);
72 base::android::ScopedJavaGlobalRef
<jobject
>
73 java_location_provider_android_object_
;
74 LocationProviderAndroid
* location_provider_
;
76 // Guards against the following member which is accessed on Geolocation
77 // thread and the JNI main thread looper.
79 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
82 } // namespace content
84 #endif // CONTENT_BROWSER_GEOLOCATION_LOCATION_API_ADAPTER_ANDROID_H_