From 8f9a109fa67356e6d1e9329ab573b6c6d3a5b052 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 5 Jun 2015 17:27:24 +0800 Subject: [PATCH] Implement HTML5 fullscreen in CommonWebContentsDelegate --- atom/browser/api/atom_api_web_contents.cc | 27 ++------- atom/browser/api/atom_api_web_contents.h | 2 - atom/browser/common_web_contents_delegate.cc | 64 +++++++++++++++++--- atom/browser/common_web_contents_delegate.h | 14 +++++ atom/browser/native_window.cc | 35 ----------- atom/browser/native_window.h | 16 ----- 6 files changed, 72 insertions(+), 86 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 49b0a89d..18bfc7a5 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -223,32 +223,13 @@ void WebContents::HandleKeyboardEvent( void WebContents::EnterFullscreenModeForTab(content::WebContents* source, const GURL& origin) { - auto window = GetWindowFromGuest(source); - if (window) { - window->SetHtmlApiFullscreen(true); - window->NotifyWindowEnterHtmlFullScreen(); - source->GetRenderViewHost()->WasResized(); - Emit("enter-html-full-screen"); - } + CommonWebContentsDelegate::EnterFullscreenModeForTab(source, origin); + Emit("enter-html-full-screen"); } void WebContents::ExitFullscreenModeForTab(content::WebContents* source) { - auto window = GetWindowFromGuest(source); - if (window) { - window->SetHtmlApiFullscreen(false); - window->NotifyWindowLeaveHtmlFullScreen(); - source->GetRenderViewHost()->WasResized(); - Emit("leave-html-full-screen"); - } -} - -bool WebContents::IsFullscreenForTabOrPending( - const content::WebContents* source) const { - auto window = GetWindowFromGuest(source); - if (window) - return window->is_html_api_fullscreen(); - else - return false; + CommonWebContentsDelegate::ExitFullscreenModeForTab(source); + Emit("leave-html-full-screen"); } void WebContents::RenderViewDeleted(content::RenderViewHost* render_view_host) { diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index f16680b5..58059d9e 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -158,8 +158,6 @@ class WebContents : public mate::EventEmitter, void EnterFullscreenModeForTab(content::WebContents* source, const GURL& origin) override; void ExitFullscreenModeForTab(content::WebContents* source) override; - bool IsFullscreenForTabOrPending( - const content::WebContents* source) const override; // content::WebContentsObserver: void RenderViewDeleted(content::RenderViewHost*) override; diff --git a/atom/browser/common_web_contents_delegate.cc b/atom/browser/common_web_contents_delegate.cc index 1a64f249..564efe7e 100644 --- a/atom/browser/common_web_contents_delegate.cc +++ b/atom/browser/common_web_contents_delegate.cc @@ -87,7 +87,9 @@ base::DictionaryValue* CreateFileSystemValue(const FileSystem& file_system) { CommonWebContentsDelegate::CommonWebContentsDelegate(bool is_guest) : is_guest_(is_guest), - owner_window_(nullptr) { + owner_window_(nullptr), + html_fullscreen_(false), + native_fullscreen_(false) { } CommonWebContentsDelegate::~CommonWebContentsDelegate() { @@ -108,6 +110,19 @@ void CommonWebContentsDelegate::DestroyWebContents() { web_contents_.reset(); } +content::WebContents* CommonWebContentsDelegate::GetWebContents() const { + if (!web_contents_) + return nullptr; + return web_contents_->GetWebContents(); +} + +content::WebContents* +CommonWebContentsDelegate::GetDevToolsWebContents() const { + if (!web_contents_) + return nullptr; + return web_contents_->GetDevToolsWebContents(); +} + void CommonWebContentsDelegate::RequestToLockMouse( content::WebContents* web_contents, bool user_gesture, @@ -156,17 +171,27 @@ void CommonWebContentsDelegate::EnumerateDirectory(content::WebContents* guest, web_dialog_helper_->EnumerateDirectory(guest, request_id, path); } -content::WebContents* CommonWebContentsDelegate::GetWebContents() const { - if (!web_contents_) - return nullptr; - return web_contents_->GetWebContents(); +void CommonWebContentsDelegate::EnterFullscreenModeForTab( + content::WebContents* source, const GURL& origin) { + if (!owner_window_) + return; + SetHtmlApiFullscreen(true); + owner_window_->NotifyWindowEnterHtmlFullScreen(); + source->GetRenderViewHost()->WasResized(); } -content::WebContents* -CommonWebContentsDelegate::GetDevToolsWebContents() const { - if (!web_contents_) - return nullptr; - return web_contents_->GetDevToolsWebContents(); +void CommonWebContentsDelegate::ExitFullscreenModeForTab( + content::WebContents* source) { + if (!owner_window_) + return; + SetHtmlApiFullscreen(false); + owner_window_->NotifyWindowLeaveHtmlFullScreen(); + source->GetRenderViewHost()->WasResized(); +} + +bool CommonWebContentsDelegate::IsFullscreenForTabOrPending( + const content::WebContents* source) const { + return html_fullscreen_; } void CommonWebContentsDelegate::DevToolsSaveToFile( @@ -269,4 +294,23 @@ void CommonWebContentsDelegate::DevToolsRemoveFileSystem( nullptr); } +void CommonWebContentsDelegate::SetHtmlApiFullscreen(bool enter_fullscreen) { + // Window is already in fullscreen mode, save the state. + if (enter_fullscreen && owner_window_->IsFullscreen()) { + native_fullscreen_ = true; + html_fullscreen_ = true; + return; + } + + // Exit html fullscreen state but not window's fullscreen mode. + if (!enter_fullscreen && native_fullscreen_) { + html_fullscreen_ = false; + return; + } + + owner_window_->SetFullScreen(enter_fullscreen); + html_fullscreen_ = enter_fullscreen; + native_fullscreen_ = false; +} + } // namespace atom diff --git a/atom/browser/common_web_contents_delegate.h b/atom/browser/common_web_contents_delegate.h index 8909d193..30545e2e 100644 --- a/atom/browser/common_web_contents_delegate.h +++ b/atom/browser/common_web_contents_delegate.h @@ -64,6 +64,11 @@ class CommonWebContentsDelegate void EnumerateDirectory(content::WebContents* web_contents, int request_id, const base::FilePath& path) override; + void EnterFullscreenModeForTab(content::WebContents* source, + const GURL& origin) override; + void ExitFullscreenModeForTab(content::WebContents* source) override; + bool IsFullscreenForTabOrPending( + const content::WebContents* source) const override; // brightray::InspectableWebContentsDelegate: void DevToolsSaveToFile(const std::string& url, @@ -75,12 +80,21 @@ class CommonWebContentsDelegate void DevToolsRemoveFileSystem(const std::string& file_system_path) override; private: + // Set fullscreen mode triggered by html api. + void SetHtmlApiFullscreen(bool enter_fullscreen); + // Whether this is guest WebContents or NativeWindow. const bool is_guest_; // The window that this WebContents belongs to. NativeWindow* owner_window_; + // Whether window is fullscreened by HTML5 api. + bool html_fullscreen_; + + // Whether window is fullscreened by window api. + bool native_fullscreen_; + scoped_ptr web_dialog_helper_; scoped_ptr dialog_manager_; diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index 07ad83f1..60a9eb27 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -94,8 +94,6 @@ NativeWindow::NativeWindow(content::WebContents* web_contents, is_closed_(false), node_integration_(true), has_dialog_attached_(false), - html_fullscreen_(false), - native_fullscreen_(false), zoom_factor_(1.0), weak_factory_(this) { printing::PrintViewManagerBasic::CreateForWebContents(web_contents); @@ -431,25 +429,6 @@ void NativeWindow::OverrideWebkitPrefs(content::WebPreferences* prefs) { } } -void NativeWindow::SetHtmlApiFullscreen(bool enter_fullscreen) { - // Window is already in fullscreen mode, save the state. - if (enter_fullscreen && IsFullscreen()) { - native_fullscreen_ = true; - html_fullscreen_ = true; - return; - } - - // Exit html fullscreen state but not window's fullscreen mode. - if (!enter_fullscreen && native_fullscreen_) { - html_fullscreen_ = false; - return; - } - - SetFullScreen(enter_fullscreen); - html_fullscreen_ = enter_fullscreen; - native_fullscreen_ = false; -} - void NativeWindow::NotifyWindowClosed() { if (is_closed_) return; @@ -647,20 +626,6 @@ void NativeWindow::RendererResponsive(content::WebContents* source) { FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnRendererResponsive()); } -void NativeWindow::EnterFullscreenModeForTab(content::WebContents* source, - const GURL& origin) { - SetHtmlApiFullscreen(true); -} - -void NativeWindow::ExitFullscreenModeForTab(content::WebContents* source) { - SetHtmlApiFullscreen(false); -} - -bool NativeWindow::IsFullscreenForTabOrPending( - const content::WebContents* source) const { - return is_html_api_fullscreen(); -} - void NativeWindow::BeforeUnloadFired(const base::TimeTicks& proceed_time) { // Do nothing, we override this method just to avoid compilation error since // there are two virtual functions named BeforeUnloadFired. diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index 191e326d..2038775f 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -179,9 +179,6 @@ class NativeWindow : public CommonWebContentsDelegate, void AppendExtraCommandLineSwitches(base::CommandLine* command_line); void OverrideWebkitPrefs(content::WebPreferences* prefs); - // Set fullscreen mode triggered by html api. - void SetHtmlApiFullscreen(bool enter_fullscreen); - // Public API used by platform-dependent delegates and observers to send UI // related notifications. void NotifyWindowClosed(); @@ -213,8 +210,6 @@ class NativeWindow : public CommonWebContentsDelegate, bool has_frame() const { return has_frame_; } - bool is_html_api_fullscreen() const { return html_fullscreen_; } - void set_has_dialog_attached(bool has_dialog_attached) { has_dialog_attached_ = has_dialog_attached; } @@ -250,11 +245,6 @@ class NativeWindow : public CommonWebContentsDelegate, void CloseContents(content::WebContents* source) override; void RendererUnresponsive(content::WebContents* source) override; void RendererResponsive(content::WebContents* source) override; - void EnterFullscreenModeForTab(content::WebContents* source, - const GURL& origin) override; - void ExitFullscreenModeForTab(content::WebContents* source) override; - bool IsFullscreenForTabOrPending( - const content::WebContents* source) const override; // Implementations of content::WebContentsObserver. void RenderViewCreated(content::RenderViewHost* render_view_host) override; @@ -310,12 +300,6 @@ class NativeWindow : public CommonWebContentsDelegate, // There is a dialog that has been attached to window. bool has_dialog_attached_; - // Whether window is fullscreened by HTML5 api. - bool html_fullscreen_; - - // Whether window is fullscreened by window api. - bool native_fullscreen_; - // Closure that would be called when window is unresponsive when closing, // it should be cancelled when we can prove that the window is responsive. base::CancelableClosure window_unresposive_closure_; -- 2.43.0