From a8184a2465aee946723dc28f7c252472c5043577 Mon Sep 17 00:00:00 2001 From: Kaido Kert Date: Tue, 7 Jan 2025 12:09:45 -0800 Subject: [PATCH] Make shell overridden --- cobalt/cobalt_shell.cc | 53 ++++++++++++++++++++++++++++++++++ cobalt/cobalt_shell.h | 22 +++++++++++++- content/shell/browser/shell.cc | 8 +++++ content/shell/browser/shell.h | 16 ++++++++++ 4 files changed, 98 insertions(+), 1 deletion(-) diff --git a/cobalt/cobalt_shell.cc b/cobalt/cobalt_shell.cc index d17e75be861d..365d241c47f4 100644 --- a/cobalt/cobalt_shell.cc +++ b/cobalt/cobalt_shell.cc @@ -1,7 +1,60 @@ #include "cobalt/cobalt_shell.h" +#include "base/command_line.h" +#include "content/public/browser/presentation_receiver_flags.h" +#include "content/public/common/content_switches.h" + +// This file defines a Cobalt specific shell entry point, that delegates most +// of its functionality to content::Shell. This allows overrides to +// WebContentsDelegate and WebContentsObserver interfaces. +// +// We expect to fully migrate away from depending on content::Shell in the +// future. namespace cobalt { +CobaltShell::CobaltShell(std::unique_ptr web_contents, + bool should_set_delegate) + : content::Shell(std::move(web_contents), should_set_delegate) {} + +// static +content::Shell* CobaltShell::CreateShell( + std::unique_ptr web_contents, + const gfx::Size& initial_size, + bool should_set_delegate) { + content::WebContents* raw_web_contents = web_contents.get(); + // Create a Cobalt specific shell instance + CobaltShell* shell = + new CobaltShell(std::move(web_contents), should_set_delegate); + // Delegate the rest of Shell setup to content::Shell + return content::Shell::CreateShellFromPointer( + shell, raw_web_contents, initial_size, should_set_delegate); +} + +// static +content::Shell* CobaltShell::CreateNewWindow( + content::BrowserContext* browser_context, + const GURL& url, + const scoped_refptr& site_instance, + const gfx::Size& initial_size) { + content::WebContents::CreateParams create_params(browser_context, + site_instance); + if (base::CommandLine::ForCurrentProcess()->HasSwitch( + switches::kForcePresentationReceiverForTesting)) { + create_params.starting_sandbox_flags = + content::kPresentationReceiverSandboxFlags; + } + std::unique_ptr web_contents = + content::WebContents::Create(create_params); + content::Shell* shell = + CreateShell(std::move(web_contents), AdjustWindowSize(initial_size), + true /* should_set_delegate */); + + if (!url.is_empty()) { + shell->LoadURL(url); + } + return shell; +} + // Placeholder for a WebContentsObserver override void CobaltShell::PrimaryMainDocumentElementAvailable() { LOG(INFO) << "Cobalt::PrimaryMainDocumentElementAvailable"; diff --git a/cobalt/cobalt_shell.h b/cobalt/cobalt_shell.h index 217f7fab6076..6fc4e0675c92 100644 --- a/cobalt/cobalt_shell.h +++ b/cobalt/cobalt_shell.h @@ -1,12 +1,32 @@ #ifndef COBALT_COBALT_SHELL_H #define COBALT_COBALT_SHELL_H +#include "content/public/browser/browser_context.h" +#include "content/public/browser/web_contents.h" #include "content/shell/browser/shell.h" namespace cobalt { class CobaltShell : public content::Shell { - // WebContentsObserver + public: + // Override content::Shell entry point + static content::Shell* CreateNewWindow( + content::BrowserContext* browser_context, + const GURL& url, + const scoped_refptr& site_instance, + const gfx::Size& initial_size); + + private: + CobaltShell(std::unique_ptr web_contents, + bool should_set_delegate); + + // Overridden from content::Shell + static content::Shell* CreateShell( + std::unique_ptr web_contents, + const gfx::Size& initial_size, + bool should_set_delegate); + + // WebContentsObserver interface void PrimaryMainDocumentElementAvailable() override; }; diff --git a/content/shell/browser/shell.cc b/content/shell/browser/shell.cc index 40835b205377..830fb41e9210 100644 --- a/content/shell/browser/shell.cc +++ b/content/shell/browser/shell.cc @@ -108,6 +108,14 @@ Shell* Shell::CreateShell(std::unique_ptr web_contents, bool should_set_delegate) { WebContents* raw_web_contents = web_contents.get(); Shell* shell = new Shell(std::move(web_contents), should_set_delegate); +#if BUILDFLAG(IS_COBALT) + return CreateShellFromPointer(shell, raw_web_contents, initial_size, should_set_delegate); +} + +Shell* Shell::CreateShellFromPointer(Shell* shell, WebContents* raw_web_contents, + const gfx::Size& initial_size, + bool should_set_delegate) { +#endif g_platform->CreatePlatformWindow(shell, initial_size); // Note: Do not make RenderFrameHost or RenderViewHost specific state changes diff --git a/content/shell/browser/shell.h b/content/shell/browser/shell.h index bbe8635423e8..fd0579ea0cc8 100644 --- a/content/shell/browser/shell.h +++ b/content/shell/browser/shell.h @@ -195,17 +195,33 @@ class Shell : public WebContentsDelegate, public WebContentsObserver { private: class DevToolsWebContentsObserver; +#if BUILDFLAG(IS_COBALT) + // Cobalt Hack: make following members protected to be able to partially + // customize Shell in a derived class. +protected: +#endif Shell(std::unique_ptr web_contents, bool should_set_delegate); // Helper to create a new Shell given a newly created WebContents. static Shell* CreateShell(std::unique_ptr web_contents, const gfx::Size& initial_size, bool should_set_delegate); +#if BUILDFLAG(IS_COBALT) + // Cobalt Hack: Splits CreateShell to two parts, so a derived class instance can be + // passed to factory function. + static Shell* CreateShellFromPointer(Shell * shell, WebContents* raw_web_contents, + const gfx::Size& initial_size, + bool should_set_delegate); +#endif // Adjust the size when Blink sends 0 for width and/or height. // This happens when Blink requests a default-sized window. static gfx::Size AdjustWindowSize(const gfx::Size& initial_size); +#if BUILDFLAG(IS_COBALT) +private: +#endif + // Helper method for the two public LoadData methods. void LoadDataWithBaseURLInternal(const GURL& url, const std::string& data,