OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/vr_shell/vr_shell_delegate.h" |
| 6 |
| 7 #include <jni.h> |
| 8 |
| 9 #include "base/android/jni_android.h" |
| 10 #include "chrome/browser/android/vr_shell/vr_shell.h" |
| 11 #include "jni/VrShellDelegate_jni.h" |
| 12 |
| 13 using base::android::JavaParamRef; |
| 14 using base::android::AttachCurrentThread; |
| 15 |
| 16 namespace vr_shell { |
| 17 |
| 18 VrShellDelegate::VrShellDelegate(JNIEnv* env, jobject obj) |
| 19 : device_provider_(nullptr) { |
| 20 j_vr_shell_delegate_.Reset(env, obj); |
| 21 GvrDelegateProvider::SetInstance(this); |
| 22 } |
| 23 |
| 24 VrShellDelegate::~VrShellDelegate() { |
| 25 GvrDelegateProvider::SetInstance(nullptr); |
| 26 } |
| 27 |
| 28 VrShellDelegate* VrShellDelegate::getNativeDelegate( |
| 29 JNIEnv* env, jobject jdelegate) { |
| 30 long native_delegate = Java_VrShellDelegate_getNativePointer(env, jdelegate); |
| 31 return reinterpret_cast<VrShellDelegate*>(native_delegate); |
| 32 } |
| 33 |
| 34 bool VrShellDelegate::ExitWebVRIfNecessary(JNIEnv* env, jobject obj) { |
| 35 if (!device_provider_) |
| 36 return false; |
| 37 |
| 38 device_provider_->OnGvrDelegateRemoved(); |
| 39 return true; |
| 40 } |
| 41 |
| 42 bool VrShellDelegate::RequestWebVRPresent( |
| 43 device::GvrDeviceProvider* device_provider) { |
| 44 // Only set one device provider at a time |
| 45 DCHECK(!device_provider_); |
| 46 device_provider_ = device_provider; |
| 47 |
| 48 // If/When VRShell is ready for use it will call OnVrShellReady. |
| 49 JNIEnv* env = AttachCurrentThread(); |
| 50 return Java_VrShellDelegate_enterVRIfNecessary(env, |
| 51 j_vr_shell_delegate_.obj(), |
| 52 true); |
| 53 } |
| 54 |
| 55 void VrShellDelegate::ExitWebVRPresent() { |
| 56 if (!device_provider_) |
| 57 return; |
| 58 |
| 59 device_provider_ = nullptr; |
| 60 |
| 61 // VRShell is no longer needed by WebVR, allow it to shut down if it's not |
| 62 // being used elsewhere. |
| 63 JNIEnv* env = AttachCurrentThread(); |
| 64 Java_VrShellDelegate_exitWebVR(env, j_vr_shell_delegate_.obj()); |
| 65 } |
| 66 |
| 67 void VrShellDelegate::OnVrShellReady(VrShell* vr_shell) { |
| 68 if (device_provider_) |
| 69 device_provider_->OnGvrDelegateReady(vr_shell); |
| 70 } |
| 71 |
| 72 // ---------------------------------------------------------------------------- |
| 73 // Native JNI methods |
| 74 // ---------------------------------------------------------------------------- |
| 75 |
| 76 bool RegisterVrShellDelegate(JNIEnv* env) { |
| 77 return RegisterNativesImpl(env); |
| 78 } |
| 79 |
| 80 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 81 return reinterpret_cast<intptr_t>(new VrShellDelegate(env, obj)); |
| 82 } |
| 83 |
| 84 } // namespace vr_shell |
OLD | NEW |