Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: content/browser/device_orientation/data_fetcher_impl_android.h

Issue 18572014: Implement Android shared memory data fetcher for Device Motion. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@renderer-sync-12June-tryASYNC-2-bis-tryRebase-6
Patch Set: fixed some includes and removed instance() from data_fetcher_shared_memory_default Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_ 5 #ifndef CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_
6 #define CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_ 6 #define CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_
7 7
8 #include "base/android/scoped_java_ref.h" 8 #include "base/android/scoped_java_ref.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/synchronization/lock.h" 10 #include "base/synchronization/lock.h"
11 #include "content/browser/device_orientation/data_fetcher.h" 11 #include "content/browser/device_orientation/data_fetcher.h"
12 #include "content/browser/device_orientation/device_data.h" 12 #include "content/browser/device_orientation/device_data.h"
13 #include "content/common/device_motion_hardware_buffer.h"
14
15 template<typename T> struct DefaultSingletonTraits;
bulach 2013/07/12 14:00:41 I don't quite understand why the Leaky version wou
timvolodine 2013/07/12 15:22:18 If I change this to Leaky the below friend declara
13 16
14 namespace content { 17 namespace content {
15
16 class Orientation; 18 class Orientation;
17 19
18 // Android implementation of DeviceOrientation API. 20 // Android implementation of DeviceOrientation API.
19 21
20 // Android's SensorManager has a push API, whereas Chrome wants to pull data. 22 // Android's SensorManager has a push API, whereas Chrome wants to pull data.
21 // To fit them together, we store incoming sensor events in a 1-element buffer. 23 // To fit them together, we store incoming sensor events in a 1-element buffer.
22 // SensorManager calls SetOrientation() which pushes a new value (discarding the 24 // SensorManager calls SetOrientation() which pushes a new value (discarding the
23 // previous value if any). Chrome calls GetDeviceData() which reads the most 25 // previous value if any). Chrome calls GetDeviceData() which reads the most
24 // recent value. Repeated calls to GetDeviceData() will return the same value. 26 // recent value. Repeated calls to GetDeviceData() will return the same value.
25 27
26 class DataFetcherImplAndroid : public DataFetcher { 28 // TODO(timvolodine): Move this class implementation to
29 // data_fetcher_shared_memory_android.cc, once Device Orientation switches to
30 // shared memory implementation.
bulach 2013/07/12 14:00:41 would this be the case? I think this class will re
timvolodine 2013/07/12 15:22:18 yes, but the class can be simplified once device o
31 class DataFetcherImplAndroid {
27 public: 32 public:
28 // Must be called at startup, before Create(). 33 // Must be called at startup, before GetInstance().
29 static void Init(JNIEnv* env); 34 static void Init(JNIEnv* env);
30 35
31 // Factory function. We'll listen for events for the lifetime of this object. 36 // Needs to be thread-safe, because accessed from different threads.
32 // Returns NULL on error. 37 static DataFetcherImplAndroid* GetInstance();
33 static DataFetcher* Create();
34
35 virtual ~DataFetcherImplAndroid();
36 38
37 // Called from Java via JNI. 39 // Called from Java via JNI.
38 void GotOrientation(JNIEnv*, jobject, 40 void GotOrientation(JNIEnv*, jobject,
39 double alpha, double beta, double gamma); 41 double alpha, double beta, double gamma);
40 void GotAcceleration(JNIEnv*, jobject, 42 void GotAcceleration(JNIEnv*, jobject,
41 double x, double y, double z); 43 double x, double y, double z);
42 void GotAccelerationIncludingGravity(JNIEnv*, jobject, 44 void GotAccelerationIncludingGravity(JNIEnv*, jobject,
43 double x, double y, double z); 45 double x, double y, double z);
44 void GotRotationRate(JNIEnv*, jobject, 46 void GotRotationRate(JNIEnv*, jobject,
45 double alpha, double beta, double gamma); 47 double alpha, double beta, double gamma);
46 48
47 // Implementation of DataFetcher. 49 // Implementation of DataFetcher.
48 virtual const DeviceData* GetDeviceData(DeviceData::Type type) OVERRIDE; 50 virtual const DeviceData* GetDeviceData(DeviceData::Type type) OVERRIDE;
49 51
50 private:
51 DataFetcherImplAndroid();
52 const Orientation* GetOrientation();
53
54 // Wrappers for JNI methods. 52 // Wrappers for JNI methods.
55 // TODO(timvolodine): move the DeviceData::Type enum to the service class 53 // TODO(timvolodine): move the DeviceData::Type enum to the service class
56 // once it is implemented. 54 // once it is implemented.
bulach 2013/07/12 14:00:41 I think 52-54 can be dropped? :)
timvolodine 2013/07/12 15:22:18 Done.
57 bool Start(DeviceData::Type event_type, int rate_in_milliseconds); 55 virtual bool Start(DeviceData::Type event_type);
58 void Stop(DeviceData::Type event_type); 56 virtual void Stop(DeviceData::Type event_type);
57
58 // Shared memory related methods.
59 virtual bool NeedsPolling();
60 virtual bool FetchDeviceMotionDataIntoBuffer();
61 virtual bool StartFetchingDeviceMotionData(
62 DeviceMotionHardwareBuffer* buffer);
63 virtual void StopFetchingDeviceMotionData();
bulach 2013/07/12 14:00:41 hmm... is "NeedsPolling()" necessary, since this c
timvolodine 2013/07/12 15:22:18 Done.
64
65 protected:
66 DataFetcherImplAndroid();
67 virtual ~DataFetcherImplAndroid();
68
69 const Orientation* GetOrientation();
59 70
60 virtual int GetNumberActiveDeviceMotionSensors(); 71 virtual int GetNumberActiveDeviceMotionSensors();
72 void CheckBufferReadyToRead();
73 void SetBufferReadyStatus(bool ready);
74 void ClearInternalBuffers();
61 75
76 private:
77 friend struct DefaultSingletonTraits<DataFetcherImplAndroid>;
78
79 enum {
80 RECEIVED_MOTION_DATA_ACCELERATION = 0,
81 RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY = 1,
82 RECEIVED_MOTION_DATA_ROTATION_RATE = 2,
83 RECEIVED_MOTION_DATA_MAX = 3,
84 };
62 // Value returned by GetDeviceData. 85 // Value returned by GetDeviceData.
63 scoped_refptr<Orientation> current_orientation_; 86 scoped_refptr<Orientation> current_orientation_;
64 87
65 // 1-element buffer, written by GotOrientation, read by GetDeviceData. 88 // 1-element buffer, written by GotOrientation, read by GetDeviceData.
66 base::Lock next_orientation_lock_; 89 base::Lock next_orientation_lock_;
67 scoped_refptr<Orientation> next_orientation_; 90 scoped_refptr<Orientation> next_orientation_;
68 91
69 // The Java provider of orientation info. 92 // The Java provider of orientation info.
70 base::android::ScopedJavaGlobalRef<jobject> device_orientation_; 93 base::android::ScopedJavaGlobalRef<jobject> device_orientation_;
94 int number_active_device_motion_sensors_;
95 int received_motion_data_[RECEIVED_MOTION_DATA_MAX];
96 DeviceMotionHardwareBuffer* device_motion_buffer_;
97 bool is_buffer_ready_;
71 98
72 DISALLOW_COPY_AND_ASSIGN(DataFetcherImplAndroid); 99 DISALLOW_COPY_AND_ASSIGN(DataFetcherImplAndroid);
73 }; 100 };
74 101
75 } // namespace content 102 } // namespace content
76 103
77 #endif // CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_ 104 #endif // CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698