Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / content / browser / device_orientation / data_fetcher_impl_android.h
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 CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_
6 #define CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_
7
8 #include "base/android/scoped_java_ref.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/synchronization/lock.h"
11 #include "content/common/content_export.h"
12 #include "content/common/device_orientation/device_motion_hardware_buffer.h"
13 #include "content/common/device_orientation/device_orientation_hardware_buffer.h"
14
15 template<typename T> struct DefaultSingletonTraits;
16
17 namespace content {
18
19 // Android implementation of Device Orientation API.
20 //
21 // Android's SensorManager has a push API, so when Got*() methods are called
22 // by the system the browser process puts the received data into a shared
23 // memory buffer, which is read by the renderer processes.
24 //
25
26 // TODO(timvolodine): rename this class to SensorManagerAndroid.
27 class CONTENT_EXPORT DataFetcherImplAndroid {
28  public:
29   // Must be called at startup, before GetInstance().
30   static bool Register(JNIEnv* env);
31
32   // Needs to be thread-safe, because accessed from different threads.
33   static DataFetcherImplAndroid* GetInstance();
34
35   // Called from Java via JNI.
36   void GotOrientation(JNIEnv*, jobject,
37                       double alpha, double beta, double gamma);
38   void GotAcceleration(JNIEnv*, jobject,
39                        double x, double y, double z);
40   void GotAccelerationIncludingGravity(JNIEnv*, jobject,
41                                        double x, double y, double z);
42   void GotRotationRate(JNIEnv*, jobject,
43                        double alpha, double beta, double gamma);
44
45   // Shared memory related methods.
46   bool StartFetchingDeviceMotionData(DeviceMotionHardwareBuffer* buffer);
47   void StopFetchingDeviceMotionData();
48
49   bool StartFetchingDeviceOrientationData(
50       DeviceOrientationHardwareBuffer* buffer);
51   void StopFetchingDeviceOrientationData();
52
53  protected:
54   enum EventType {
55     // These constants should match DEVICE_ORIENTATION and DEVICE_MOTION
56     // constants in content/public/android/java/src/org/chromium/content/
57     // browser/DeviceMotionAndOrientation.java
58     kTypeOrientation = 0,
59     kTypeMotion = 1
60   };
61
62   DataFetcherImplAndroid();
63   virtual ~DataFetcherImplAndroid();
64
65   virtual bool Start(EventType event_type);
66   virtual void Stop(EventType event_type);
67   virtual int GetNumberActiveDeviceMotionSensors();
68
69  private:
70   friend struct DefaultSingletonTraits<DataFetcherImplAndroid>;
71
72   enum {
73     RECEIVED_MOTION_DATA_ACCELERATION = 0,
74     RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY = 1,
75     RECEIVED_MOTION_DATA_ROTATION_RATE = 2,
76     RECEIVED_MOTION_DATA_MAX = 3,
77   };
78
79   void CheckMotionBufferReadyToRead();
80   void SetMotionBufferReadyStatus(bool ready);
81   void ClearInternalMotionBuffers();
82
83   void SetOrientationBufferReadyStatus(bool ready);
84
85   // The Java provider of orientation info.
86   base::android::ScopedJavaGlobalRef<jobject> device_orientation_;
87   int number_active_device_motion_sensors_;
88   int received_motion_data_[RECEIVED_MOTION_DATA_MAX];
89   DeviceMotionHardwareBuffer* device_motion_buffer_;
90   DeviceOrientationHardwareBuffer* device_orientation_buffer_;
91   bool is_motion_buffer_ready_;
92   bool is_orientation_buffer_ready_;
93
94   base::Lock motion_buffer_lock_;
95   base::Lock orientation_buffer_lock_;
96
97   DISALLOW_COPY_AND_ASSIGN(DataFetcherImplAndroid);
98 };
99
100 }  // namespace content
101
102 #endif  // CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_