| OLD | NEW |
| 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 #include "content/browser/device_orientation/data_fetcher_impl_android.h" | 5 #include "content/browser/device_orientation/data_fetcher_impl_android.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/scoped_java_ref.h" | 8 #include "base/android/scoped_java_ref.h" |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 return fetcher.release(); | 47 return fetcher.release(); |
| 48 | 48 |
| 49 LOG(ERROR) << "DataFetcherImplAndroid::Start failed!"; | 49 LOG(ERROR) << "DataFetcherImplAndroid::Start failed!"; |
| 50 return NULL; | 50 return NULL; |
| 51 } | 51 } |
| 52 | 52 |
| 53 DataFetcherImplAndroid::~DataFetcherImplAndroid() { | 53 DataFetcherImplAndroid::~DataFetcherImplAndroid() { |
| 54 Stop(); | 54 Stop(); |
| 55 } | 55 } |
| 56 | 56 |
| 57 bool DataFetcherImplAndroid::GetOrientation(Orientation* orientation) { | 57 const DeviceData* DataFetcherImplAndroid::GetDeviceData( |
| 58 DeviceData::Type type) { |
| 59 if (type != DeviceData::kTypeOrientation) |
| 60 return NULL; |
| 61 return GetOrientation(); |
| 62 } |
| 63 |
| 64 const Orientation* DataFetcherImplAndroid::GetOrientation() { |
| 58 // Do we have a new orientation value? (It's safe to do this outside the lock | 65 // Do we have a new orientation value? (It's safe to do this outside the lock |
| 59 // because we only skip the lock if the value is null. We always enter the | 66 // because we only skip the lock if the value is null. We always enter the |
| 60 // lock if we're going to make use of the new value.) | 67 // lock if we're going to make use of the new value.) |
| 61 if (next_orientation_.get()) { | 68 if (next_orientation_.get()) { |
| 62 base::AutoLock autolock(next_orientation_lock_); | 69 base::AutoLock autolock(next_orientation_lock_); |
| 63 next_orientation_.swap(current_orientation_); | 70 next_orientation_.swap(current_orientation_); |
| 64 } | 71 } |
| 65 if (current_orientation_.get()) | 72 if (!current_orientation_.get()) |
| 66 *orientation = *current_orientation_; | 73 return new Orientation(); |
| 67 return true; | 74 return current_orientation_.get(); |
| 68 } | 75 } |
| 69 | 76 |
| 70 void DataFetcherImplAndroid::GotOrientation( | 77 void DataFetcherImplAndroid::GotOrientation( |
| 71 JNIEnv*, jobject, double alpha, double beta, double gamma) { | 78 JNIEnv*, jobject, double alpha, double beta, double gamma) { |
| 72 base::AutoLock autolock(next_orientation_lock_); | 79 base::AutoLock autolock(next_orientation_lock_); |
| 73 | 80 |
| 74 Orientation* orientation = new Orientation(); | 81 Orientation* orientation = new Orientation(); |
| 75 orientation->set_alpha(alpha); | 82 orientation->set_alpha(alpha); |
| 76 orientation->set_beta(beta); | 83 orientation->set_beta(beta); |
| 77 orientation->set_gamma(gamma); | 84 orientation->set_gamma(gamma); |
| 78 orientation->set_absolute(true); | 85 orientation->set_absolute(true); |
| 79 next_orientation_.reset(orientation); | 86 next_orientation_ = orientation; |
| 80 } | 87 } |
| 81 | 88 |
| 82 bool DataFetcherImplAndroid::Start(int rate_in_milliseconds) { | 89 bool DataFetcherImplAndroid::Start(int rate_in_milliseconds) { |
| 83 DCHECK(!g_jni_obj.Get().is_null()); | 90 DCHECK(!g_jni_obj.Get().is_null()); |
| 84 return Java_DeviceOrientation_start(AttachCurrentThread(), | 91 return Java_DeviceOrientation_start(AttachCurrentThread(), |
| 85 g_jni_obj.Get().obj(), | 92 g_jni_obj.Get().obj(), |
| 86 reinterpret_cast<jint>(this), | 93 reinterpret_cast<jint>(this), |
| 87 rate_in_milliseconds); | 94 rate_in_milliseconds); |
| 88 } | 95 } |
| 89 | 96 |
| 90 void DataFetcherImplAndroid::Stop() { | 97 void DataFetcherImplAndroid::Stop() { |
| 91 DCHECK(!g_jni_obj.Get().is_null()); | 98 DCHECK(!g_jni_obj.Get().is_null()); |
| 92 Java_DeviceOrientation_stop(AttachCurrentThread(), g_jni_obj.Get().obj()); | 99 Java_DeviceOrientation_stop(AttachCurrentThread(), g_jni_obj.Get().obj()); |
| 93 } | 100 } |
| 94 | 101 |
| 95 } // namespace device_orientation | 102 } // namespace device_orientation |
| OLD | NEW |