Commit 361a3c45 authored by erikchen's avatar erikchen Committed by Commit Bot

Update HeadlessWebContentsImpl to use strong ownership semantics for WebContents.

This CL is a refactor with no intended behavior change.

Bug: 832879
Change-Id: Ic90818e6ce6fff15b323aefbe7e0150cf9bb9f8c
Reviewed-on: https://chromium-review.googlesource.com/1040474Reviewed-by: default avatarAlexander Timin <altimin@chromium.org>
Commit-Queue: Erik Chen <erikchen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#555585}
parent f20e454c
...@@ -70,25 +70,6 @@ class HeadlessWebContentsImpl::Delegate : public content::WebContentsDelegate { ...@@ -70,25 +70,6 @@ class HeadlessWebContentsImpl::Delegate : public content::WebContentsDelegate {
explicit Delegate(HeadlessWebContentsImpl* headless_web_contents) explicit Delegate(HeadlessWebContentsImpl* headless_web_contents)
: headless_web_contents_(headless_web_contents) {} : headless_web_contents_(headless_web_contents) {}
void WebContentsCreated(content::WebContents* source_contents,
int opener_render_process_id,
int opener_render_frame_id,
const std::string& frame_name,
const GURL& target_url,
content::WebContents* new_contents) override {
DCHECK(new_contents->GetBrowserContext() ==
headless_web_contents_->browser_context());
std::unique_ptr<HeadlessWebContentsImpl> child_contents =
HeadlessWebContentsImpl::CreateForChildContents(headless_web_contents_,
new_contents);
HeadlessWebContentsImpl* raw_child_contents = child_contents.get();
headless_web_contents_->browser_context()->RegisterWebContents(
std::move(child_contents));
headless_web_contents_->browser_context()->NotifyChildContentsCreated(
headless_web_contents_, raw_child_contents);
}
#if !defined(CHROME_MULTIPLE_DLL_CHILD) #if !defined(CHROME_MULTIPLE_DLL_CHILD)
// Return the security style of the given |web_contents|, populating // Return the security style of the given |web_contents|, populating
// |security_style_explanations| to explain why the SecurityStyle was chosen. // |security_style_explanations| to explain why the SecurityStyle was chosen.
...@@ -123,15 +104,22 @@ class HeadlessWebContentsImpl::Delegate : public content::WebContentsDelegate { ...@@ -123,15 +104,22 @@ class HeadlessWebContentsImpl::Delegate : public content::WebContentsDelegate {
const gfx::Rect& initial_rect, const gfx::Rect& initial_rect,
bool user_gesture, bool user_gesture,
bool* was_blocked) override { bool* was_blocked) override {
DCHECK(new_contents->GetBrowserContext() ==
headless_web_contents_->browser_context());
std::unique_ptr<HeadlessWebContentsImpl> child_contents =
HeadlessWebContentsImpl::CreateForChildContents(
headless_web_contents_, std::move(new_contents));
HeadlessWebContentsImpl* raw_child_contents = child_contents.get();
headless_web_contents_->browser_context()->RegisterWebContents(
std::move(child_contents));
headless_web_contents_->browser_context()->NotifyChildContentsCreated(
headless_web_contents_, raw_child_contents);
const gfx::Rect default_rect( const gfx::Rect default_rect(
headless_web_contents_->browser()->options()->window_size); headless_web_contents_->browser()->options()->window_size);
const gfx::Rect rect = initial_rect.IsEmpty() ? default_rect : initial_rect; const gfx::Rect rect = initial_rect.IsEmpty() ? default_rect : initial_rect;
// TODO(erikchen): Refactor this class to use strong ownership semantics. raw_child_contents->SetBounds(rect);
// https://crbug.com/832879.
auto* const headless_contents =
HeadlessWebContentsImpl::From(browser(), new_contents.release());
DCHECK(headless_contents);
headless_contents->SetBounds(rect);
} }
content::WebContents* OpenURLFromTab( content::WebContents* OpenURLFromTab(
...@@ -257,11 +245,9 @@ std::unique_ptr<HeadlessWebContentsImpl> HeadlessWebContentsImpl::Create( ...@@ -257,11 +245,9 @@ std::unique_ptr<HeadlessWebContentsImpl> HeadlessWebContentsImpl::Create(
nullptr); nullptr);
create_params.initial_size = builder->window_size_; create_params.initial_size = builder->window_size_;
// TODO(erikchen): Refactor this class to use strong ownership semantics.
// https://crbug.com/832879.
std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents =
base::WrapUnique(new HeadlessWebContentsImpl( base::WrapUnique(new HeadlessWebContentsImpl(
content::WebContents::Create(create_params).release(), content::WebContents::Create(create_params),
builder->browser_context_)); builder->browser_context_));
if (builder->tab_sockets_allowed_) { if (builder->tab_sockets_allowed_) {
...@@ -288,26 +274,27 @@ std::unique_ptr<HeadlessWebContentsImpl> HeadlessWebContentsImpl::Create( ...@@ -288,26 +274,27 @@ std::unique_ptr<HeadlessWebContentsImpl> HeadlessWebContentsImpl::Create(
std::unique_ptr<HeadlessWebContentsImpl> std::unique_ptr<HeadlessWebContentsImpl>
HeadlessWebContentsImpl::CreateForChildContents( HeadlessWebContentsImpl::CreateForChildContents(
HeadlessWebContentsImpl* parent, HeadlessWebContentsImpl* parent,
content::WebContents* child_contents) { std::unique_ptr<content::WebContents> child_contents) {
auto child = base::WrapUnique( auto child = base::WrapUnique(new HeadlessWebContentsImpl(
new HeadlessWebContentsImpl(child_contents, parent->browser_context())); std::move(child_contents), parent->browser_context()));
// Child contents have their own root window and inherit the BeginFrameControl // Child contents have their own root window and inherit the BeginFrameControl
// setting. // setting.
child->begin_frame_control_enabled_ = parent->begin_frame_control_enabled_; child->begin_frame_control_enabled_ = parent->begin_frame_control_enabled_;
child->InitializeWindow(child_contents->GetContainerBounds()); child->InitializeWindow(child->web_contents_->GetContainerBounds());
// Copy mojo services and tab socket settings from parent. // Copy mojo services and tab socket settings from parent.
child->mojo_services_ = parent->mojo_services_; child->mojo_services_ = parent->mojo_services_;
if (parent->headless_tab_socket_) { if (parent->headless_tab_socket_) {
child->headless_tab_socket_ = child->headless_tab_socket_ =
std::make_unique<HeadlessTabSocketImpl>(child_contents); std::make_unique<HeadlessTabSocketImpl>(child->web_contents_.get());
child->inject_mojo_services_into_isolated_world_ = child->inject_mojo_services_into_isolated_world_ =
parent->inject_mojo_services_into_isolated_world_; parent->inject_mojo_services_into_isolated_world_;
} }
// There may already be frames, so make sure they also have our services. // There may already be frames, so make sure they also have our services.
for (content::RenderFrameHost* frame_host : child_contents->GetAllFrames()) for (content::RenderFrameHost* frame_host :
child->web_contents_->GetAllFrames())
child->RenderFrameCreated(frame_host); child->RenderFrameCreated(frame_host);
return child; return child;
...@@ -334,23 +321,24 @@ void HeadlessWebContentsImpl::SetBounds(const gfx::Rect& bounds) { ...@@ -334,23 +321,24 @@ void HeadlessWebContentsImpl::SetBounds(const gfx::Rect& bounds) {
} }
HeadlessWebContentsImpl::HeadlessWebContentsImpl( HeadlessWebContentsImpl::HeadlessWebContentsImpl(
content::WebContents* web_contents, std::unique_ptr<content::WebContents> web_contents,
HeadlessBrowserContextImpl* browser_context) HeadlessBrowserContextImpl* browser_context)
: content::WebContentsObserver(web_contents), : content::WebContentsObserver(web_contents.get()),
web_contents_delegate_(new HeadlessWebContentsImpl::Delegate(this)), web_contents_delegate_(new HeadlessWebContentsImpl::Delegate(this)),
web_contents_(web_contents), web_contents_(std::move(web_contents)),
agent_host_(content::DevToolsAgentHost::GetOrCreateFor(web_contents)), agent_host_(
content::DevToolsAgentHost::GetOrCreateFor(web_contents_.get())),
inject_mojo_services_into_isolated_world_(false), inject_mojo_services_into_isolated_world_(false),
browser_context_(browser_context), browser_context_(browser_context),
render_process_host_(web_contents->GetMainFrame()->GetProcess()), render_process_host_(web_contents_->GetMainFrame()->GetProcess()),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
#if BUILDFLAG(ENABLE_PRINTING) && !defined(CHROME_MULTIPLE_DLL_CHILD) #if BUILDFLAG(ENABLE_PRINTING) && !defined(CHROME_MULTIPLE_DLL_CHILD)
HeadlessPrintManager::CreateForWebContents(web_contents); HeadlessPrintManager::CreateForWebContents(web_contents_.get());
// TODO(weili): Add support for printing OOPIFs. // TODO(weili): Add support for printing OOPIFs.
#endif #endif
web_contents->GetMutableRendererPrefs()->accept_languages = web_contents_->GetMutableRendererPrefs()->accept_languages =
browser_context->options()->accept_language(); browser_context->options()->accept_language();
web_contents->GetMutableRendererPrefs()->hinting = web_contents_->GetMutableRendererPrefs()->hinting =
browser_context->options()->font_render_hinting(); browser_context->options()->font_render_hinting();
web_contents_->SetDelegate(web_contents_delegate_.get()); web_contents_->SetDelegate(web_contents_delegate_.get());
render_process_host_->AddObserver(this); render_process_host_->AddObserver(this);
......
...@@ -59,7 +59,7 @@ class HEADLESS_EXPORT HeadlessWebContentsImpl ...@@ -59,7 +59,7 @@ class HEADLESS_EXPORT HeadlessWebContentsImpl
// Takes ownership of |child_contents|. // Takes ownership of |child_contents|.
static std::unique_ptr<HeadlessWebContentsImpl> CreateForChildContents( static std::unique_ptr<HeadlessWebContentsImpl> CreateForChildContents(
HeadlessWebContentsImpl* parent, HeadlessWebContentsImpl* parent,
content::WebContents* child_contents); std::unique_ptr<content::WebContents> child_contents);
// HeadlessWebContents implementation: // HeadlessWebContents implementation:
void AddObserver(Observer* observer) override; void AddObserver(Observer* observer) override;
...@@ -155,7 +155,7 @@ class HEADLESS_EXPORT HeadlessWebContentsImpl ...@@ -155,7 +155,7 @@ class HEADLESS_EXPORT HeadlessWebContentsImpl
struct PendingFrame; struct PendingFrame;
// Takes ownership of |web_contents|. // Takes ownership of |web_contents|.
HeadlessWebContentsImpl(content::WebContents* web_contents, HeadlessWebContentsImpl(std::unique_ptr<content::WebContents> web_contents,
HeadlessBrowserContextImpl* browser_context); HeadlessBrowserContextImpl* browser_context);
void InitializeWindow(const gfx::Rect& initial_bounds); void InitializeWindow(const gfx::Rect& initial_bounds);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment