- add sources.
[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/browser/device_orientation/device_data.h"
12 #include "content/common/content_export.h"
13 #include "content/common/device_orientation/device_motion_hardware_buffer.h"
14 #include "content/common/device_orientation/device_orientation_hardware_buffer.h"
15
16 template<typename T> struct DefaultSingletonTraits;
17
18 namespace content {
19
20 // Android implementation of Device Orientation API.
21 //
22 // Android's SensorManager has a push API, so when Got*() methods are called
23 // by the system the browser process puts the received data into a shared
24 // memory buffer, which is read by the renderer processes.
25 //
26
27 // TODO(timvolodine): rename this class to SensorManagerAndroid.
28 class CONTENT_EXPORT DataFetcherImplAndroid {
29  public:
30   // Must be called at startup, before GetInstance().
31   static bool Register(JNIEnv* env);
32
33   // Needs to be thread-safe, because accessed from different threads.
34   static DataFetcherImplAndroid* GetInstance();
35
36   // Called from Java via JNI.
37   void GotOrientation(JNIEnv*, jobject,
38                       double alpha, double beta, double gamma);
39   void GotAcceleration(JNIEnv*, jobject,
40                        double x, double y, double z);
41   void GotAccelerationIncludingGravity(JNIEnv*, jobject,
42                                        double x, double y, double z);
43   void GotRotationRate(JNIEnv*, jobject,
44                        double alpha, double beta, double gamma);
45
46   virtual bool Start(DeviceData::Type event_type);
47   virtual void Stop(DeviceData::Type event_type);
48
49   // Shared memory related methods.
50   bool StartFetchingDeviceMotionData(DeviceMotionHardwareBuffer* buffer);
51   void StopFetchingDeviceMotionData();
52
53   bool StartFetchingDeviceOrientationData(
54       DeviceOrientationHardwareBuffer* buffer);
55   void StopFetchingDeviceOrientationData();
56
57  protected:
58   DataFetcherImplAndroid();
59   virtual ~DataFetcherImplAndroid();
60
61   virtual int GetNumberActiveDeviceMotionSensors();
62
63  private:
64   friend struct DefaultSingletonTraits<DataFetcherImplAndroid>;
65
66   void CheckMotionBufferReadyToRead();
67   void SetMotionBufferReadyStatus(bool ready);
68   void ClearInternalMotionBuffers();
69
70   void SetOrientationBufferReadyStatus(bool ready);
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   // The Java provider of orientation info.
80   base::android::ScopedJavaGlobalRef<jobject> device_orientation_;
81   int number_active_device_motion_sensors_;
82   int received_motion_data_[RECEIVED_MOTION_DATA_MAX];
83   DeviceMotionHardwareBuffer* device_motion_buffer_;
84   DeviceOrientationHardwareBuffer* device_orientation_buffer_;
85   bool is_motion_buffer_ready_;
86   bool is_orientation_buffer_ready_;
87
88   base::Lock motion_buffer_lock_;
89   base::Lock orientation_buffer_lock_;
90
91   DISALLOW_COPY_AND_ASSIGN(DataFetcherImplAndroid);
92 };
93
94 }  // namespace content
95
96 #endif  // CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_