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

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

Issue 10755002: Refactors DeviceOrientation to make it more extensible (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Adds a unit test, responds to other comments Created 8 years, 4 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 <jni.h> 8 #include <jni.h>
9 9
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/synchronization/lock.h" 11 #include "base/synchronization/lock.h"
12 #include "content/browser/device_orientation/data_fetcher.h" 12 #include "content/browser/device_orientation/data_fetcher.h"
13 #include "content/browser/device_orientation/orientation.h" 13 #include "content/browser/device_orientation/device_data.h"
14 14
15 namespace device_orientation { 15 namespace device_orientation {
16 16
17 class Orientation;
18
17 // Android implementation of DeviceOrientation API. 19 // Android implementation of DeviceOrientation API.
18 20
19 // Android's SensorManager has a push API, whereas Chrome wants to pull data. 21 // Android's SensorManager has a push API, whereas Chrome wants to pull data.
20 // To fit them together, we store incoming sensor events in a 1-element buffer. 22 // To fit them together, we store incoming sensor events in a 1-element buffer.
21 // SensorManager calls SetOrientation() which pushes a new value (discarding the 23 // SensorManager calls SetOrientation() which pushes a new value (discarding the
22 // previous value if any). Chrome calls GetOrientation() which reads the most 24 // previous value if any). Chrome calls GetDeviceData() which reads the most
23 // recent value. Repeated calls to GetOrientation() will return the same value. 25 // recent value. Repeated calls to GetDeviceData() will return the same value.
24 26
25 class DataFetcherImplAndroid : public DataFetcher { 27 class DataFetcherImplAndroid : public DataFetcher {
26 public: 28 public:
27 // Must be called at startup, before Create(). 29 // Must be called at startup, before Create().
28 static void Init(JNIEnv* env); 30 static void Init(JNIEnv* env);
29 31
30 // Factory function. We'll listen for events for the lifetime of this object. 32 // Factory function. We'll listen for events for the lifetime of this object.
31 // Returns NULL on error. 33 // Returns NULL on error.
32 static DataFetcher* Create(); 34 static DataFetcher* Create();
33 35
34 virtual ~DataFetcherImplAndroid(); 36 virtual ~DataFetcherImplAndroid();
35 37
36 // Called from Java via JNI. 38 // Called from Java via JNI.
37 void GotOrientation(JNIEnv*, jobject, 39 void GotOrientation(JNIEnv*, jobject,
38 double alpha, double beta, double gamma); 40 double alpha, double beta, double gamma);
39 41
40 // Implementation of DataFetcher. 42 // Implementation of DataFetcher.
41 virtual bool GetOrientation(Orientation* orientation) OVERRIDE; 43 virtual const DeviceData* GetDeviceData(DeviceData::Type type) OVERRIDE;
42 44
43 private: 45 private:
44 DataFetcherImplAndroid(); 46 DataFetcherImplAndroid();
47 const Orientation* GetOrientation();
45 48
46 // Wrappers for JNI methods. 49 // Wrappers for JNI methods.
47 bool Start(int rate_in_milliseconds); 50 bool Start(int rate_in_milliseconds);
48 void Stop(); 51 void Stop();
49 52
50 // Value returned by GetOrientation. 53 // Value returned by GetDeviceData.
51 scoped_ptr<Orientation> current_orientation_; 54 scoped_ptr<Orientation> current_orientation_;
hans 2012/08/03 13:05:41 should this be a scoped_refptr?
aousterh 2012/08/03 13:42:18 Done.
52 55
53 // 1-element buffer, written by GotOrientation, read by GetOrientation. 56 // 1-element buffer, written by GotOrientation, read by GetDeviceData.
54 base::Lock next_orientation_lock_; 57 base::Lock next_orientation_lock_;
55 scoped_ptr<Orientation> next_orientation_; 58 scoped_ptr<Orientation> next_orientation_;
56 59
57 DISALLOW_COPY_AND_ASSIGN(DataFetcherImplAndroid); 60 DISALLOW_COPY_AND_ASSIGN(DataFetcherImplAndroid);
58 }; 61 };
59 62
60 } // namespace device_orientation 63 } // namespace device_orientation
61 64
62 #endif // CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_ 65 #endif // CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698