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

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

Issue 2727873002: Implement lazy initialization for VrShellDelegate (Closed)
Patch Set: Fix FindBugs errors - neat! 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::OpenNewTab(bool incognito) { 134 void VrShellDelegate::OpenNewTab(bool incognito) {
124 JNIEnv* env = AttachCurrentThread(); 135 JNIEnv* env = AttachCurrentThread();
125 Java_VrShellDelegate_openNewTab(env, j_vr_shell_delegate_.obj(), incognito); 136 Java_VrShellDelegate_openNewTab(env, j_vr_shell_delegate_.obj(), incognito);
126 } 137 }
127 138
128 void VrShellDelegate::SetDeviceProvider( 139 void VrShellDelegate::SetDeviceProvider(
129 device::GvrDeviceProvider* device_provider) { 140 device::GvrDeviceProvider* device_provider) {
130 CHECK(!device_provider_); 141 CHECK(!device_provider_);
131 device_provider_ = device_provider; 142 device_provider_ = device_provider;
132 if (!delegate_) 143 if (!delegate_)
133 CreateNonPresentingDelegate(); 144 CreateNonPresentingDelegate();
134 device_provider_->Device()->OnDelegateChanged(); 145 device_provider_->Device()->OnDelegateChanged();
135 } 146 }
136 147
137 void VrShellDelegate::ClearDeviceProvider() { 148 void VrShellDelegate::ClearDeviceProvider() {
138 non_presenting_delegate_ = nullptr; 149 non_presenting_delegate_ = nullptr;
139 JNIEnv* env = AttachCurrentThread(); 150 JNIEnv* env = AttachCurrentThread();
140 Java_VrShellDelegate_shutdownNonPresentingNativeContext( 151 Java_VrShellDelegate_shutdownNonPresentingNativeContext(
141 env, j_vr_shell_delegate_.obj()); 152 env, j_vr_shell_delegate_.obj());
142 device_provider_->Device()->OnDelegateChanged();
143 device_provider_ = nullptr; 153 device_provider_ = nullptr;
144 } 154 }
145 155
146 void VrShellDelegate::RequestWebVRPresent( 156 void VrShellDelegate::RequestWebVRPresent(
147 const base::Callback<void(bool)>& callback) { 157 const base::Callback<void(bool)>& callback) {
148 if (!present_callback_.is_null()) { 158 if (!present_callback_.is_null()) {
149 // Can only handle one request at a time. This is also extremely unlikely to 159 // Can only handle one request at a time. This is also extremely unlikely to
150 // happen in practice. 160 // happen in practice.
151 callback.Run(false); 161 callback.Run(false);
152 return; 162 return;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 // ---------------------------------------------------------------------------- 216 // ----------------------------------------------------------------------------
207 217
208 bool RegisterVrShellDelegate(JNIEnv* env) { 218 bool RegisterVrShellDelegate(JNIEnv* env) {
209 return RegisterNativesImpl(env); 219 return RegisterNativesImpl(env);
210 } 220 }
211 221
212 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { 222 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) {
213 return reinterpret_cast<intptr_t>(new VrShellDelegate(env, obj)); 223 return reinterpret_cast<intptr_t>(new VrShellDelegate(env, obj));
214 } 224 }
215 225
226 static void OnLibraryAvailable(JNIEnv* env, const JavaParamRef<jclass>& clazz) {
227 device::GvrDelegateProvider::SetInstance(
228 base::Bind(&VrShellDelegate::CreateVrShellDelegate));
229 }
230
216 } // namespace vr_shell 231 } // namespace vr_shell
OLDNEW
« no previous file with comments | « chrome/browser/android/vr_shell/vr_shell_delegate.h ('k') | chrome/browser/android/vr_shell/vr_shell_gl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698