summaryrefslogtreecommitdiff
path: root/chromium/services/device/screen_orientation/screen_orientation_listener_android.cc
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/services/device/screen_orientation/screen_orientation_listener_android.cc')
-rw-r--r--chromium/services/device/screen_orientation/screen_orientation_listener_android.cc58
1 files changed, 58 insertions, 0 deletions
diff --git a/chromium/services/device/screen_orientation/screen_orientation_listener_android.cc b/chromium/services/device/screen_orientation/screen_orientation_listener_android.cc
new file mode 100644
index 00000000000..0c863d8b699
--- /dev/null
+++ b/chromium/services/device/screen_orientation/screen_orientation_listener_android.cc
@@ -0,0 +1,58 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "services/device/screen_orientation/screen_orientation_listener_android.h"
+
+#include "base/android/jni_android.h"
+#include "base/message_loop/message_loop.h"
+#include "jni/ScreenOrientationListener_jni.h"
+#include "mojo/public/cpp/bindings/strong_binding.h"
+
+namespace device {
+
+// static
+void ScreenOrientationListenerAndroid::Create(
+ mojom::ScreenOrientationListenerRequest request) {
+ mojo::MakeStrongBinding(
+ base::WrapUnique(new ScreenOrientationListenerAndroid()),
+ std::move(request));
+}
+
+ScreenOrientationListenerAndroid::ScreenOrientationListenerAndroid()
+ : listeners_count_(0) {}
+
+ScreenOrientationListenerAndroid::~ScreenOrientationListenerAndroid() {
+ DCHECK(base::MessageLoopForIO::IsCurrent());
+ if (listeners_count_ > 0) {
+ Java_ScreenOrientationListener_startAccurateListening(
+ base::android::AttachCurrentThread());
+ }
+}
+
+void ScreenOrientationListenerAndroid::Start() {
+ DCHECK(base::MessageLoopForIO::IsCurrent());
+ ++listeners_count_;
+ if (listeners_count_ == 1) {
+ // Ask the ScreenOrientationListener (Java) to start accurately listening to
+ // the screen orientation. It keep track of the number of start request if
+ // it is already running an accurate listening.
+ Java_ScreenOrientationListener_startAccurateListening(
+ base::android::AttachCurrentThread());
+ }
+}
+
+void ScreenOrientationListenerAndroid::Stop() {
+ DCHECK(base::MessageLoopForIO::IsCurrent());
+ DCHECK(listeners_count_ > 0);
+ --listeners_count_;
+ if (listeners_count_ == 0) {
+ // Ask the ScreenOrientationListener (Java) to stop accurately listening to
+ // the screen orientation. It will actually stop only if the number of stop
+ // requests matches the number of start requests.
+ Java_ScreenOrientationListener_stopAccurateListening(
+ base::android::AttachCurrentThread());
+ }
+}
+
+} // namespace device