Chromium Code Reviews| 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..43915a523f9117f29926ea294be633c51248b3b6 |
| --- /dev/null |
| +++ b/chrome/browser/android/vr_shell/vr_shell_delegate.cc |
| @@ -0,0 +1,85 @@ |
| +// 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); |
| +} |
| + |
| +VrShellDelegate* VrShellDelegate::getNativePointer( |
| + JNIEnv* env, jobject jdelegate) { |
| + long native_delegate = Java_VrShellDelegate_getNativePointer(env, jdelegate); |
| + return reinterpret_cast<VrShellDelegate*>(native_delegate); |
| +} |
| + |
| +// Returns true if the GvrDeviceProvider needs to handle shutdown first. |
|
Ted C
2016/09/21 17:59:25
would this fit better in the header?
|
| +bool VrShellDelegate::ExitWebVRIfNecessary(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_enterVRIfNecessary(env, |
| + j_vr_shell_delegate_.obj(), |
| + true); |
| +} |
| + |
| +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_exitWebVR(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 |