Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(360)

Side by Side Diff: chrome/browser/android/vr_shell/vr_shell_delegate.cc

Issue 2727873002: Implement lazy initialization for VrShellDelegate (Closed)
Patch Set: Address comments + Fix shutdown crash Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 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 "chrome/browser/android/vr_shell/vr_shell_delegate.h" 5 #include "chrome/browser/android/vr_shell/vr_shell_delegate.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/android/jni_android.h" 9 #include "base/android/jni_android.h"
10 #include "chrome/browser/android/vr_shell/non_presenting_gvr_delegate.h" 10 #include "chrome/browser/android/vr_shell/non_presenting_gvr_delegate.h"
11 #include "device/vr/android/gvr/gvr_device.h" 11 #include "device/vr/android/gvr/gvr_device.h"
12 #include "device/vr/android/gvr/gvr_device_provider.h" 12 #include "device/vr/android/gvr/gvr_device_provider.h"
13 #include "device/vr/android/gvr/gvr_gamepad_data_fetcher.h" 13 #include "device/vr/android/gvr/gvr_gamepad_data_fetcher.h"
14 #include "jni/VrShellDelegate_jni.h" 14 #include "jni/VrShellDelegate_jni.h"
15 15
16 using base::android::JavaParamRef; 16 using base::android::JavaParamRef;
17 using base::android::AttachCurrentThread; 17 using base::android::AttachCurrentThread;
18 18
19 namespace vr_shell { 19 namespace vr_shell {
20 20
21 VrShellDelegate::VrShellDelegate(JNIEnv* env, jobject obj) 21 VrShellDelegate::VrShellDelegate(JNIEnv* env, jobject obj)
22 : weak_ptr_factory_(this) { 22 : weak_ptr_factory_(this) {
23 j_vr_shell_delegate_.Reset(env, obj); 23 j_vr_shell_delegate_.Reset(env, obj);
24 GvrDelegateProvider::SetInstance(this);
25 } 24 }
26 25
27 VrShellDelegate::~VrShellDelegate() { 26 VrShellDelegate::~VrShellDelegate() {
28 GvrDelegateProvider::SetInstance(nullptr);
29 if (device_provider_) { 27 if (device_provider_) {
30 device_provider_->Device()->OnDelegateChanged(); 28 device_provider_->Device()->OnDelegateChanged();
31 } 29 }
32 } 30 }
33 31
32 device::GvrDelegateProvider* VrShellDelegate::CreateVrShellDelegate() {
33 JNIEnv* env = AttachCurrentThread();
34 base::android::ScopedJavaLocalRef<jobject> jdelegate =
35 Java_VrShellDelegate_getInstance(env);
36 if (!jdelegate.is_null())
37 return GetNativeVrShellDelegate(env, jdelegate.obj());
38 return nullptr;
39 }
40
34 VrShellDelegate* VrShellDelegate::GetNativeVrShellDelegate(JNIEnv* env, 41 VrShellDelegate* VrShellDelegate::GetNativeVrShellDelegate(JNIEnv* env,
35 jobject jdelegate) { 42 jobject jdelegate) {
36 return reinterpret_cast<VrShellDelegate*>( 43 return reinterpret_cast<VrShellDelegate*>(
37 Java_VrShellDelegate_getNativePointer(env, jdelegate)); 44 Java_VrShellDelegate_getNativePointer(env, jdelegate));
38 } 45 }
39 46
40 void VrShellDelegate::SetDelegate(device::GvrDelegate* delegate, 47 void VrShellDelegate::SetDelegate(device::GvrDelegate* delegate,
41 gvr_context* context) { 48 gvr_context* context) {
42 context_ = context; 49 context_ = context;
43 delegate_ = delegate; 50 delegate_ = delegate;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 non_presenting_delegate_->Pause(); 115 non_presenting_delegate_->Pause();
109 } 116 }
110 } 117 }
111 118
112 void VrShellDelegate::OnResume(JNIEnv* env, const JavaParamRef<jobject>& obj) { 119 void VrShellDelegate::OnResume(JNIEnv* env, const JavaParamRef<jobject>& obj) {
113 if (non_presenting_delegate_) { 120 if (non_presenting_delegate_) {
114 non_presenting_delegate_->Resume(); 121 non_presenting_delegate_->Resume();
115 } 122 }
116 } 123 }
117 124
125 void VrShellDelegate::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) {
126 delete this;
127 }
128
118 void VrShellDelegate::ShowTab(int id) { 129 void VrShellDelegate::ShowTab(int id) {
119 JNIEnv* env = AttachCurrentThread(); 130 JNIEnv* env = AttachCurrentThread();
120 Java_VrShellDelegate_showTab(env, j_vr_shell_delegate_.obj(), id); 131 Java_VrShellDelegate_showTab(env, j_vr_shell_delegate_.obj(), id);
121 } 132 }
122 133
123 void VrShellDelegate::SetDeviceProvider( 134 void VrShellDelegate::SetDeviceProvider(
124 device::GvrDeviceProvider* device_provider) { 135 device::GvrDeviceProvider* device_provider) {
125 CHECK(!device_provider_); 136 CHECK(!device_provider_);
126 device_provider_ = device_provider; 137 device_provider_ = device_provider;
127 if (!delegate_) 138 if (!delegate_)
128 CreateNonPresentingDelegate(); 139 CreateNonPresentingDelegate();
129 device_provider_->Device()->OnDelegateChanged(); 140 device_provider_->Device()->OnDelegateChanged();
130 } 141 }
131 142
132 void VrShellDelegate::ClearDeviceProvider() { 143 void VrShellDelegate::ClearDeviceProvider() {
133 non_presenting_delegate_ = nullptr; 144 non_presenting_delegate_ = nullptr;
134 JNIEnv* env = AttachCurrentThread(); 145 JNIEnv* env = AttachCurrentThread();
135 Java_VrShellDelegate_shutdownNonPresentingNativeContext( 146 Java_VrShellDelegate_shutdownNonPresentingNativeContext(
136 env, j_vr_shell_delegate_.obj()); 147 env, j_vr_shell_delegate_.obj());
137 device_provider_->Device()->OnDelegateChanged();
138 device_provider_ = nullptr; 148 device_provider_ = nullptr;
139 } 149 }
140 150
141 void VrShellDelegate::RequestWebVRPresent( 151 void VrShellDelegate::RequestWebVRPresent(
142 const base::Callback<void(bool)>& callback) { 152 const base::Callback<void(bool)>& callback) {
143 if (!present_callback_.is_null()) { 153 if (!present_callback_.is_null()) {
144 // Can only handle one request at a time. This is also extremely unlikely to 154 // Can only handle one request at a time. This is also extremely unlikely to
145 // happen in practice. 155 // happen in practice.
146 callback.Run(false); 156 callback.Run(false);
147 return; 157 return;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 // ---------------------------------------------------------------------------- 211 // ----------------------------------------------------------------------------
202 212
203 bool RegisterVrShellDelegate(JNIEnv* env) { 213 bool RegisterVrShellDelegate(JNIEnv* env) {
204 return RegisterNativesImpl(env); 214 return RegisterNativesImpl(env);
205 } 215 }
206 216
207 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { 217 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) {
208 return reinterpret_cast<intptr_t>(new VrShellDelegate(env, obj)); 218 return reinterpret_cast<intptr_t>(new VrShellDelegate(env, obj));
209 } 219 }
210 220
221 static void OnLibraryAvailable(JNIEnv* env, const JavaParamRef<jclass>& clazz) {
222 device::GvrDelegateProvider::SetInstance(
223 base::Bind(&VrShellDelegate::CreateVrShellDelegate));
224 }
225
211 } // namespace vr_shell 226 } // namespace vr_shell
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698