Skip to content

Commit

Permalink
Make shell overridden
Browse files Browse the repository at this point in the history
  • Loading branch information
kaidokert committed Jan 14, 2025
1 parent be84c65 commit a8184a2
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
53 changes: 53 additions & 0 deletions cobalt/cobalt_shell.cc
Original file line number Diff line number Diff line change
@@ -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<content::WebContents> web_contents,
bool should_set_delegate)
: content::Shell(std::move(web_contents), should_set_delegate) {}

// static
content::Shell* CobaltShell::CreateShell(
std::unique_ptr<content::WebContents> 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<content::SiteInstance>& 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<content::WebContents> 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";
Expand Down
22 changes: 21 additions & 1 deletion cobalt/cobalt_shell.h
Original file line number Diff line number Diff line change
@@ -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<content::SiteInstance>& site_instance,
const gfx::Size& initial_size);

private:
CobaltShell(std::unique_ptr<content::WebContents> web_contents,
bool should_set_delegate);

// Overridden from content::Shell
static content::Shell* CreateShell(
std::unique_ptr<content::WebContents> web_contents,
const gfx::Size& initial_size,
bool should_set_delegate);

// WebContentsObserver interface
void PrimaryMainDocumentElementAvailable() override;
};

Expand Down
8 changes: 8 additions & 0 deletions content/shell/browser/shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ Shell* Shell::CreateShell(std::unique_ptr<WebContents> 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
Expand Down
16 changes: 16 additions & 0 deletions content/shell/browser/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<WebContents> web_contents, bool should_set_delegate);

// Helper to create a new Shell given a newly created WebContents.
static Shell* CreateShell(std::unique_ptr<WebContents> 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,
Expand Down

0 comments on commit a8184a2

Please sign in to comment.