Index: chrome/browser/android/vr_shell/vr_shell_delegate.cc |
diff --git a/chrome/browser/android/vr_shell/vr_shell_delegate.cc b/chrome/browser/android/vr_shell/vr_shell_delegate.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..57088626926067a321d8210a2af6dc1cc5a6e843 |
--- /dev/null |
+++ b/chrome/browser/android/vr_shell/vr_shell_delegate.cc |
@@ -0,0 +1,78 @@ |
+// Copyright 2016 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 "chrome/browser/android/vr_shell/vr_shell_delegate.h" |
+ |
+#include <jni.h> |
+ |
+#include "base/android/jni_android.h" |
+#include "chrome/browser/android/vr_shell/vr_shell.h" |
+#include "jni/VrShellDelegate_jni.h" |
+ |
+using base::android::JavaParamRef; |
+using base::android::AttachCurrentThread; |
+ |
+namespace vr_shell { |
+ |
+VrShellDelegate::VrShellDelegate(JNIEnv* env, jobject obj) |
+ : device_provider_(nullptr) { |
+ j_vr_shell_delegate_.Reset(env, obj); |
+ GvrDelegateProvider::SetInstance(this); |
+} |
+ |
+VrShellDelegate::~VrShellDelegate() { |
+ GvrDelegateProvider::SetInstance(nullptr); |
+} |
+ |
+// Returns true if the GvrDeviceProvider needs to handle shutdown first. |
+bool VrShellDelegate::ExitVRIfNecessary(JNIEnv* env, jobject obj) { |
+ if (!device_provider_) |
+ return false; |
+ |
+ device_provider_->OnGvrDelegateRemoved(); |
+ return true; |
+} |
+ |
+bool VrShellDelegate::RequestWebVRPresent( |
+ device::GvrDeviceProvider* device_provider) { |
+ // Only set one device provider at a time |
+ DCHECK(!device_provider_); |
+ device_provider_ = device_provider; |
+ |
+ // If/When VRShell is ready for use it will call OnVrShellReady. |
+ JNIEnv* env = AttachCurrentThread(); |
+ return Java_VrShellDelegate_enterWebVRIfNecessary(env, |
+ j_vr_shell_delegate_.obj()); |
+} |
+ |
+void VrShellDelegate::ExitWebVRPresent() { |
+ if (!device_provider_) |
+ return; |
+ |
+ device_provider_ = nullptr; |
+ |
+ // VRShell is no longer needed by WebVR, allow it to shut down if it's not |
+ // being used elsewhere. |
+ JNIEnv* env = AttachCurrentThread(); |
+ Java_VrShellDelegate_exitWebVRIfNecessary(env, j_vr_shell_delegate_.obj()); |
+} |
+ |
+void VrShellDelegate::OnVrShellReady(VrShell* vr_shell) { |
+ if (device_provider_) |
+ device_provider_->OnGvrDelegateReady(vr_shell); |
+} |
+ |
+// ---------------------------------------------------------------------------- |
+// Native JNI methods |
+// ---------------------------------------------------------------------------- |
+ |
+bool RegisterVrShellDelegate(JNIEnv* env) { |
+ return RegisterNativesImpl(env); |
+} |
+ |
+jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
+ return reinterpret_cast<intptr_t>(new VrShellDelegate(env, obj)); |
+} |
+ |
+} // namespace vr_shell |