Chromium Code Reviews| Index: chrome/browser/android/vr_shell/vr_shell.cc |
| diff --git a/chrome/browser/android/vr_shell/vr_shell.cc b/chrome/browser/android/vr_shell/vr_shell.cc |
| index d77ea0bc040bed3e4a4c0d53920e6f198d74578f..d5c53fe76c009b98c99b780de6b5f2b86854af24 100644 |
| --- a/chrome/browser/android/vr_shell/vr_shell.cc |
| +++ b/chrome/browser/android/vr_shell/vr_shell.cc |
| @@ -55,7 +55,7 @@ vr_shell::VrShell* g_instance; |
| static const char kVrShellUIURL[] = "chrome://vr-shell-ui"; |
| void SetIsInVR(content::WebContents* contents, bool is_in_vr) { |
| - if (contents->GetRenderWidgetHostView()) |
| + if (contents && contents->GetRenderWidgetHostView()) |
| contents->GetRenderWidgetHostView()->SetIsInVR(is_in_vr); |
| } |
| @@ -63,7 +63,6 @@ void SetIsInVR(content::WebContents* contents, bool is_in_vr) { |
| VrShell::VrShell(JNIEnv* env, |
| jobject obj, |
| - content::WebContents* main_contents, |
| ui::WindowAndroid* content_window, |
| content::WebContents* ui_contents, |
| ui::WindowAndroid* ui_window, |
| @@ -72,13 +71,12 @@ VrShell::VrShell(JNIEnv* env, |
| gvr_context* gvr_api, |
| bool reprojected_rendering) |
| : WebContentsObserver(ui_contents), |
| - main_contents_(main_contents), |
| + content_window_(content_window), |
| content_compositor_( |
| - base::MakeUnique<VrCompositor>(content_window, false)), |
| + base::MakeUnique<VrCompositor>(content_window_, false)), |
| ui_contents_(ui_contents), |
| ui_compositor_(base::MakeUnique<VrCompositor>(ui_window, true)), |
| delegate_provider_(delegate), |
| - metrics_helper_(base::MakeUnique<VrMetricsHelper>(main_contents_)), |
| main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| reprojected_rendering_(reprojected_rendering), |
| gvr_api_(gvr_api), |
| @@ -87,10 +85,7 @@ VrShell::VrShell(JNIEnv* env, |
| g_instance = this; |
| j_vr_shell_.Reset(env, obj); |
| - content_input_manager_ = base::MakeUnique<VrInputManager>(main_contents_); |
| ui_input_manager_ = base::MakeUnique<VrInputManager>(ui_contents_); |
| - |
| - content_compositor_->SetLayer(main_contents_); |
| ui_compositor_->SetLayer(ui_contents_); |
| gl_thread_ = base::MakeUnique<VrGLThread>( |
| @@ -101,15 +96,8 @@ VrShell::VrShell(JNIEnv* env, |
| options.priority = base::ThreadPriority::DISPLAY; |
| gl_thread_->StartWithOptions(options); |
| - if (for_web_vr) |
| - metrics_helper_->SetWebVREnabled(true); |
| html_interface_ = base::MakeUnique<UiInterface>( |
| - for_web_vr ? UiInterface::Mode::WEB_VR : UiInterface::Mode::STANDARD, |
| - main_contents_->IsFullscreen()); |
| - vr_web_contents_observer_ = base::MakeUnique<VrWebContentsObserver>( |
| - main_contents_, html_interface_.get(), this); |
| - |
| - SetIsInVR(main_contents_, true); |
| + for_web_vr ? UiInterface::Mode::WEB_VR : UiInterface::Mode::STANDARD); |
| } |
| void VrShell::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| @@ -118,15 +106,34 @@ void VrShell::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| void VrShell::SwapContents(JNIEnv* env, const JavaParamRef<jobject>& obj, |
| const JavaParamRef<jobject>& web_contents) { |
| - SetIsInVR(main_contents_, false); |
| content::WebContents* contents = |
| content::WebContents::FromJavaWebContents(web_contents); |
| + if (contents == main_contents_) |
| + return; |
| + |
| + SetIsInVR(main_contents_, false); |
| + |
| main_contents_ = contents; |
| - content_input_manager_ = base::MakeUnique<VrInputManager>(main_contents_); |
| content_compositor_->SetLayer(main_contents_); |
| + SetIsInVR(main_contents_, true); |
| + ContentFrameWasResized(false /* unused */); |
|
cjgrant
2017/02/10 15:21:47
Is "unused" necessary?
mthiesse
2017/02/10 16:13:39
Technically no, but I'm pointing out that this val
|
| + |
| + if (!main_contents_) { |
| + content_input_manager_ = nullptr; |
| + vr_web_contents_observer_ = nullptr; |
| + html_interface_->SetFullscreen(false); |
| + metrics_helper_ = nullptr; |
| + // TODO(mthiesse): Properly handle native page URLs. |
| + html_interface_->SetURL(GURL()); |
| + html_interface_->SetLoading(false); |
| + return; |
| + } |
| + content_input_manager_ = base::MakeUnique<VrInputManager>(main_contents_); |
| vr_web_contents_observer_ = base::MakeUnique<VrWebContentsObserver>( |
| main_contents_, html_interface_.get(), this); |
| - SetIsInVR(main_contents_, true); |
| + html_interface_->SetFullscreen(main_contents_->IsFullscreen()); |
|
cjgrant
2017/02/10 15:21:47
Should probably create a SetUiState() method that
mthiesse
2017/02/10 16:13:39
Done.
|
| + html_interface_->SetURL(main_contents_->GetVisibleURL()); |
| + html_interface_->SetLoading(main_contents_->IsLoading()); |
| // TODO(billorr): Make VrMetricsHelper tab-aware and able to track multiple |
| // tabs. crbug.com/684661 |
| @@ -202,7 +209,8 @@ void VrShell::OnPause(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| FROM_HERE, base::Bind(&VrShellGl::OnPause, gl_thread_->GetVrShellGl())); |
| // exit vr session |
| - metrics_helper_->SetVRActive(false); |
| + if (metrics_helper_) |
| + metrics_helper_->SetVRActive(false); |
| SetIsInVR(main_contents_, false); |
| } |
| @@ -210,7 +218,8 @@ void VrShell::OnResume(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| gl_thread_->task_runner()->PostTask( |
| FROM_HERE, base::Bind(&VrShellGl::OnResume, gl_thread_->GetVrShellGl())); |
| - metrics_helper_->SetVRActive(true); |
| + if (metrics_helper_) |
| + metrics_helper_->SetVRActive(true); |
| SetIsInVR(main_contents_, true); |
| } |
| @@ -235,8 +244,10 @@ base::WeakPtr<VrShell> VrShell::GetWeakPtr( |
| } |
| void VrShell::OnDomContentsLoaded() { |
| - html_interface_->SetURL(main_contents_->GetVisibleURL()); |
| - html_interface_->SetLoading(main_contents_->IsLoading()); |
| + if (main_contents_) { |
| + html_interface_->SetURL(main_contents_->GetVisibleURL()); |
| + html_interface_->SetLoading(main_contents_->IsLoading()); |
| + } |
| html_interface_->OnDomContentsLoaded(); |
| } |
| @@ -244,7 +255,8 @@ void VrShell::SetWebVrMode(JNIEnv* env, |
| const base::android::JavaParamRef<jobject>& obj, |
| bool enabled) { |
| webvr_mode_ = enabled; |
| - metrics_helper_->SetWebVREnabled(enabled); |
| + if (metrics_helper_) |
| + metrics_helper_->SetWebVREnabled(enabled); |
| PostToGlThreadWhenReady(base::Bind(&VrShellGl::SetWebVrMode, |
| gl_thread_->GetVrShellGl(), enabled)); |
| html_interface_->SetMode( |
| @@ -324,9 +336,30 @@ void VrShell::CreateVRDisplayInfo( |
| callback, device_id)); |
| } |
| -void VrShell::SurfacesChanged(jobject content_surface, jobject ui_surface) { |
| - content_compositor_->SurfaceChanged(content_surface); |
| - ui_compositor_->SurfaceChanged(ui_surface); |
| +base::android::ScopedJavaGlobalRef<jobject> VrShell::TakeContentSurface( |
| + JNIEnv* env, |
| + const JavaParamRef<jobject>& obj) { |
| + content_compositor_->SurfaceChanged(nullptr); |
| + base::android::ScopedJavaGlobalRef<jobject> surface(env, content_surface_); |
| + content_surface_ = nullptr; |
| + return surface; |
| +} |
| + |
| +void VrShell::RestoreContentSurface(JNIEnv* env, |
| + const JavaParamRef<jobject>& obj) { |
| + PostToGlThreadWhenReady( |
| + base::Bind(&VrShellGl::CreateContentSurface, gl_thread_->GetVrShellGl())); |
| +} |
| + |
| +void VrShell::UiSurfaceChanged(jobject surface) { |
| + ui_compositor_->SurfaceChanged(surface); |
| +} |
| + |
| +void VrShell::ContentSurfaceChanged(jobject surface) { |
| + content_surface_ = surface; |
| + content_compositor_->SurfaceChanged(surface); |
| + JNIEnv* env = base::android::AttachCurrentThread(); |
| + Java_VrShellImpl_contentSurfaceCreated(env, j_vr_shell_.obj()); |
| } |
| void VrShell::GvrDelegateReady() { |
| @@ -371,31 +404,37 @@ void VrShell::UpdateScene(const base::ListValue* args) { |
| void VrShell::DoUiAction(const UiAction action, |
| const base::DictionaryValue* arguments) { |
| - content::NavigationController& controller = main_contents_->GetController(); |
|
cjgrant
2017/02/10 15:21:47
For readability, it's odd to have several differen
mthiesse
2017/02/10 16:13:39
Done.
|
| + content::NavigationController* controller = |
| + main_contents_ ? &main_contents_->GetController() : nullptr; |
| switch (action) { |
| case HISTORY_BACK: |
| - if (main_contents_->IsFullscreen()) { |
| + if (main_contents_ && main_contents_->IsFullscreen()) { |
| main_contents_->ExitFullscreen(false); |
| - } else if (controller.CanGoBack()) { |
| - controller.GoBack(); |
| + } else if (controller && controller->CanGoBack()) { |
| + controller->GoBack(); |
| } |
| break; |
| case HISTORY_FORWARD: |
| - if (controller.CanGoForward()) |
| - controller.GoForward(); |
| + if (controller && controller->CanGoForward()) |
| + controller->GoForward(); |
| break; |
| case RELOAD: |
| - controller.Reload(content::ReloadType::NORMAL, false); |
| + if (controller) |
| + controller->Reload(content::ReloadType::NORMAL, false); |
| break; |
| case LOAD_URL: { |
| + // TODO(mthiesse): Pass the URL up to the Tab in java and navigate there |
| + // to support navigating from native pages. |
| + if (!controller) |
| + break; |
| std::string url_string; |
| CHECK(arguments->GetString("url", &url_string)); |
| GURL url(url_string); |
| // TODO(crbug.com/683344): Sanitize the URL and prefix, and pass the |
| // proper transition type down from the UI. |
| - controller.LoadURL(url, content::Referrer(), |
| - ui::PageTransition::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| - std::string("")); |
| + controller->LoadURL(url, content::Referrer(), |
| + ui::PageTransition::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| + std::string("")); |
| break; |
| } |
| case OMNIBOX_CONTENT: |
| @@ -409,6 +448,8 @@ void VrShell::DoUiAction(const UiAction action, |
| } |
| #if defined(ENABLE_VR_SHELL_UI_DEV) |
| case RELOAD_UI: |
| + if (!main_contents_) |
| + return; |
| ui_contents_->GetController().Reload(content::ReloadType::NORMAL, false); |
| html_interface_.reset(new UiInterface(UiInterface::Mode::STANDARD, |
| main_contents_->IsFullscreen())); |
| @@ -437,8 +478,8 @@ void VrShell::MainFrameWasResized(bool width_changed) { |
| } |
| void VrShell::ContentFrameWasResized(bool width_changed) { |
| - display::Display display = display::Screen::GetScreen() |
| - ->GetDisplayNearestWindow(main_contents_->GetNativeView()); |
| + display::Display display = |
| + display::Screen::GetScreen()->GetDisplayNearestWindow(content_window_); |
| PostToGlThreadWhenReady( |
| base::Bind(&VrShellGl::ContentBoundsChanged, gl_thread_->GetVrShellGl(), |
| display.size().width(), display.size().height())); |
| @@ -464,7 +505,8 @@ void VrShell::ContentWasHidden() { |
| } |
| void VrShell::ContentWasShown() { |
| - content_input_manager_ = base::MakeUnique<VrInputManager>(main_contents_); |
| + if (main_contents_) |
| + content_input_manager_ = base::MakeUnique<VrInputManager>(main_contents_); |
| } |
| void VrShell::ForceExitVr() { |
| @@ -595,19 +637,20 @@ device::mojom::VRDisplayInfoPtr VrShell::CreateVRDisplayInfo( |
| // Native JNI methods |
| // ---------------------------------------------------------------------------- |
| -jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj, |
| - const JavaParamRef<jobject>& content_web_contents, |
| - jlong content_window_android, |
| +jlong Init(JNIEnv* env, |
| + const JavaParamRef<jobject>& obj, |
| const JavaParamRef<jobject>& ui_web_contents, |
| - jlong ui_window_android, jboolean for_web_vr, |
| + jlong content_window_android, |
| + jlong ui_window_android, |
| + jboolean for_web_vr, |
| const base::android::JavaParamRef<jobject>& delegate, |
| - jlong gvr_api, jboolean reprojected_rendering) { |
| + jlong gvr_api, |
| + jboolean reprojected_rendering) { |
| return reinterpret_cast<intptr_t>(new VrShell( |
| - env, obj, content::WebContents::FromJavaWebContents(content_web_contents), |
| - reinterpret_cast<ui::WindowAndroid*>(content_window_android), |
| + env, obj, reinterpret_cast<ui::WindowAndroid*>(content_window_android), |
| content::WebContents::FromJavaWebContents(ui_web_contents), |
| - reinterpret_cast<ui::WindowAndroid*>(ui_window_android), |
| - for_web_vr, VrShellDelegate::GetNativeVrShellDelegate(env, delegate), |
| + reinterpret_cast<ui::WindowAndroid*>(ui_window_android), for_web_vr, |
| + VrShellDelegate::GetNativeVrShellDelegate(env, delegate), |
| reinterpret_cast<gvr_context*>(gvr_api), reprojected_rendering)); |
| } |