Change AttachToOuterWebContents to AttachInnerWebContents instead.
This CL changes the the semantic of attaching an inner WebContents from
being called on the inner WebContents to being called on the outer
WebContents instead.
Since the attachment process takes ownership of the WebContents, this
CL cleans up the API, so instead of the inner WebContents getting a
unique_ptr<> of itself and passing it to the outer WebContents, it
makes it explicit that the outer WebContents is the one taking ownership
of the inner WebContents.
Change-Id: Ieb31a3cb03e372d36edc8af46924f47d82fc19e0
Reviewed-on: https://chromium-review.googlesource.com/c/1492653
Commit-Queue: Lucas Gadani <[email protected]>
Reviewed-by: James MacLean <[email protected]>
Reviewed-by: Charlie Reis <[email protected]>
Cr-Commit-Position: refs/heads/master@{#637085}
diff --git a/content/browser/portal/portal.cc b/content/browser/portal/portal.cc
index 093cfc2..c2f18623 100644
--- a/content/browser/portal/portal.cc
+++ b/content/browser/portal/portal.cc
@@ -82,8 +82,8 @@
portal_contents_impl_ = static_cast<WebContentsImpl*>(portal_contents.get());
portal_contents_impl_->set_portal(this);
- portal_contents_impl_->AttachToOuterWebContentsFrame(
- std::move(portal_contents), outer_node->current_frame_host());
+ outer_contents_impl->AttachInnerWebContents(std::move(portal_contents),
+ outer_node->current_frame_host());
FrameTreeNode* frame_tree_node =
portal_contents_impl_->GetMainFrame()->frame_tree_node();
diff --git a/content/browser/web_contents/web_contents_impl.cc b/content/browser/web_contents/web_contents_impl.cc
index 3f5c4fc..09c35a2b 100644
--- a/content/browser/web_contents/web_contents_impl.cc
+++ b/content/browser/web_contents/web_contents_impl.cc
@@ -451,23 +451,6 @@
WebContentsImpl::WebContentsTreeNode::~WebContentsTreeNode() {}
-void WebContentsImpl::WebContentsTreeNode::ConnectToOuterWebContents(
- std::unique_ptr<WebContents> current_web_contents,
- RenderFrameHostImpl* outer_contents_frame) {
- DCHECK_EQ(current_web_contents.get(), current_web_contents_);
- auto* outer_web_contents =
- static_cast<WebContentsImpl*>(FromRenderFrameHost(outer_contents_frame));
-
- focused_web_contents_ = nullptr;
- outer_web_contents_ = outer_web_contents;
- outer_contents_frame_tree_node_id_ =
- outer_contents_frame->frame_tree_node()->frame_tree_node_id();
-
- outer_web_contents_->node_.AttachInnerWebContents(
- std::move(current_web_contents));
- outer_contents_frame->frame_tree_node()->AddObserver(this);
-}
-
std::unique_ptr<WebContents>
WebContentsImpl::WebContentsTreeNode::DisconnectFromOuterWebContents() {
std::unique_ptr<WebContents> inner_contents =
@@ -479,8 +462,20 @@
}
void WebContentsImpl::WebContentsTreeNode::AttachInnerWebContents(
- std::unique_ptr<WebContents> inner_web_contents) {
+ std::unique_ptr<WebContents> inner_web_contents,
+ RenderFrameHostImpl* render_frame_host) {
+ WebContentsImpl* inner_web_contents_impl =
+ static_cast<WebContentsImpl*>(inner_web_contents.get());
+ WebContentsTreeNode& inner_web_contents_node = inner_web_contents_impl->node_;
+
+ inner_web_contents_node.focused_web_contents_ = nullptr;
+ inner_web_contents_node.outer_web_contents_ = current_web_contents_;
+ inner_web_contents_node.outer_contents_frame_tree_node_id_ =
+ render_frame_host->frame_tree_node()->frame_tree_node_id();
+
inner_web_contents_.push_back(std::move(inner_web_contents));
+
+ render_frame_host->frame_tree_node()->AddObserver(&inner_web_contents_node);
}
std::unique_ptr<WebContents>
@@ -1808,23 +1803,33 @@
GetMainFrame()->DispatchBeforeUnload(before_unload_type, false);
}
-void WebContentsImpl::AttachToOuterWebContentsFrame(
- std::unique_ptr<WebContents> current_web_contents,
- RenderFrameHost* outer_contents_frame) {
- DCHECK(!node_.outer_web_contents());
- DCHECK_EQ(current_web_contents.get(), this);
- auto* outer_contents_frame_impl =
- static_cast<RenderFrameHostImpl*>(outer_contents_frame);
+void WebContentsImpl::AttachInnerWebContents(
+ std::unique_ptr<WebContents> inner_web_contents,
+ RenderFrameHost* render_frame_host) {
+ WebContentsImpl* inner_web_contents_impl =
+ static_cast<WebContentsImpl*>(inner_web_contents.get());
+ DCHECK(!inner_web_contents_impl->node_.outer_web_contents());
+ auto* render_frame_host_impl =
+ static_cast<RenderFrameHostImpl*>(render_frame_host);
+ DCHECK_EQ(&frame_tree_,
+ render_frame_host_impl->frame_tree_node()->frame_tree());
- RenderFrameHostManager* render_manager = GetRenderManager();
- auto* outer_contnets_render_manager =
- outer_contents_frame_impl->frame_tree_node()->render_manager();
+ RenderFrameHostManager* inner_render_manager =
+ inner_web_contents_impl->GetRenderManager();
+ RenderFrameHostImpl* inner_main_frame =
+ inner_render_manager->current_frame_host();
+ RenderViewHostImpl* inner_render_view_host =
+ inner_render_manager->current_host();
+ auto* outer_render_manager =
+ render_frame_host_impl->frame_tree_node()->render_manager();
// When attaching a WebContents as an inner WebContents, we need to replace
// the Webcontents' view with a WebContentsViewChildFrame.
- view_.reset(new WebContentsViewChildFrame(
- this, GetContentClient()->browser()->GetWebContentsViewDelegate(this),
- &render_view_host_delegate_view_));
+ inner_web_contents_impl->view_.reset(new WebContentsViewChildFrame(
+ inner_web_contents_impl,
+ GetContentClient()->browser()->GetWebContentsViewDelegate(
+ inner_web_contents_impl),
+ &inner_web_contents_impl->render_view_host_delegate_view_));
// When the WebContents being initialized has an opener, the browser side
// Render{View,Frame}Host must be initialized and the RenderWidgetHostView
@@ -1832,36 +1837,39 @@
// the first navigation, but when attaching a new window we don't navigate
// before attaching. If the browser side is already initialized, the calls
// below will just early return.
- render_manager->InitRenderView(GetRenderViewHost(), nullptr);
- GetMainFrame()->Init();
- if (!render_manager->GetRenderWidgetHostView())
- CreateRenderWidgetHostViewForRenderManager(GetRenderViewHost());
+ inner_render_manager->InitRenderView(inner_render_view_host, nullptr);
+ inner_main_frame->Init();
+ if (!inner_render_manager->GetRenderWidgetHostView()) {
+ inner_web_contents_impl->CreateRenderWidgetHostViewForRenderManager(
+ inner_render_view_host);
+ }
// Create a link to our outer WebContents.
- node_.ConnectToOuterWebContents(std::move(current_web_contents),
- outer_contents_frame_impl);
+ node_.AttachInnerWebContents(std::move(inner_web_contents),
+ render_frame_host_impl);
// Create a proxy in top-level RenderFrameHostManager, pointing to the
// SiteInstance of the outer WebContents. The proxy will be used to send
// postMessage to the inner WebContents.
- auto* proxy = render_manager->CreateOuterDelegateProxy(
- outer_contents_frame->GetSiteInstance());
+ auto* proxy = inner_render_manager->CreateOuterDelegateProxy(
+ render_frame_host_impl->GetSiteInstance());
// When attaching a GuestView as an inner WebContents, there should already be
// a live RenderFrame, which has to be swapped. When attaching a portal, there
// will not be a live RenderFrame before creating the proxy.
- if (outer_contents_frame->IsRenderFrameLive()) {
- render_manager->SwapOuterDelegateFrame(outer_contents_frame_impl, proxy);
+ if (render_frame_host_impl->IsRenderFrameLive()) {
+ inner_render_manager->SwapOuterDelegateFrame(render_frame_host_impl, proxy);
- ReattachToOuterWebContentsFrame();
+ inner_web_contents_impl->ReattachToOuterWebContentsFrame();
}
- if (node_.outer_web_contents()->frame_tree_.GetFocusedFrame() ==
- outer_contents_frame_impl->frame_tree_node()) {
- SetFocusedFrame(frame_tree_.root(),
- outer_contents_frame->GetSiteInstance());
+ if (frame_tree_.GetFocusedFrame() ==
+ render_frame_host_impl->frame_tree_node()) {
+ inner_web_contents_impl->SetFocusedFrame(
+ inner_web_contents_impl->frame_tree_.root(),
+ render_frame_host_impl->GetSiteInstance());
}
- outer_contnets_render_manager->set_attach_complete();
+ outer_render_manager->set_attach_complete();
}
std::unique_ptr<WebContents> WebContentsImpl::DetachFromOuterWebContents() {
diff --git a/content/browser/web_contents/web_contents_impl.h b/content/browser/web_contents/web_contents_impl.h
index 8d85f56..568aa2e 100644
--- a/content/browser/web_contents/web_contents_impl.h
+++ b/content/browser/web_contents/web_contents_impl.h
@@ -359,9 +359,8 @@
Visibility GetVisibility() override;
bool NeedToFireBeforeUnload() override;
void DispatchBeforeUnload(bool auto_cancel) override;
- void AttachToOuterWebContentsFrame(
- std::unique_ptr<WebContents> current_web_contents,
- RenderFrameHost* outer_contents_frame) override;
+ void AttachInnerWebContents(std::unique_ptr<WebContents> inner_web_contents,
+ RenderFrameHost* render_frame_host) override;
RenderFrameHostImpl* GetOuterWebContentsFrame() override;
WebContentsImpl* GetOuterWebContents() override;
WebContentsImpl* GetOutermostWebContents() override;
@@ -1110,11 +1109,10 @@
explicit WebContentsTreeNode(WebContentsImpl* current_web_contents);
~WebContentsTreeNode() final;
- // Connects |current_web_contents| to the outer WebContents that owns
- // |outer_contents_frame|.
- void ConnectToOuterWebContents(
- std::unique_ptr<WebContents> current_web_contents,
- RenderFrameHostImpl* outer_contents_frame);
+ // Attaches |inner_web_contents| to the |render_frame_host| within this
+ // WebContents.
+ void AttachInnerWebContents(std::unique_ptr<WebContents> inner_web_contents,
+ RenderFrameHostImpl* render_frame_host);
// Disconnects the current WebContents from its outer WebContents, and
// returns ownership to the caller. This is used when activating a portal,
@@ -1141,8 +1139,6 @@
std::vector<WebContentsImpl*> GetInnerWebContents() const;
private:
- void AttachInnerWebContents(
- std::unique_ptr<WebContents> inner_web_contents);
std::unique_ptr<WebContents> DetachInnerWebContents(
WebContentsImpl* inner_web_contents);
diff --git a/content/public/browser/web_contents.h b/content/public/browser/web_contents.h
index 8bf04ec..2fe0a25a 100644
--- a/content/public/browser/web_contents.h
+++ b/content/public/browser/web_contents.h
@@ -529,20 +529,17 @@
// potential discard without causing the dialog to appear.
virtual void DispatchBeforeUnload(bool auto_cancel) = 0;
- // Attaches |current_web_contents|, which should be the same as |this|
- // WebContents, to its container frame |outer_contents_frame|, which should be
- // in the outer WebContents. The |current_web_contents| is pointer is needed
- // for passing ownership of the inner WebContents to the outer WebContents.
- // Note: |outer_contents_frame| will be swapped out and destroyed during the
+ // Attaches |inner_web_contents| to the container frame |render_frame_host|,
+ // which should be in this WebContents' FrameTree. This outer WebContents
+ // takes ownership of |inner_web_contents|.
+ // Note: |render_frame_host| will be swapped out and destroyed during the
// process. Generally a frame same-process with its parent is the right choice
// but ideally it should be "about:blank" to avoid problems with beforeunload.
// To ensure sane usage of this API users first should call the async API
// RenderFrameHost::PrepareForInnerWebContentsAttach first.
- // TODO(lfg): This API should be moved so that it is called on the outer
- // WebContents.
- virtual void AttachToOuterWebContentsFrame(
- std::unique_ptr<WebContents> current_web_contents,
- RenderFrameHost* outer_contents_frame) = 0;
+ virtual void AttachInnerWebContents(
+ std::unique_ptr<WebContents> inner_web_contents,
+ RenderFrameHost* render_frame_host) = 0;
// Returns the outer WebContents frame, the same frame that this WebContents
// was attached in AttachToOuterWebContentsFrame().
diff --git a/content/public/test/test_utils.cc b/content/public/test/test_utils.cc
index 01ac477a..151f062 100644
--- a/content/public/test/test_utils.cc
+++ b/content/public/test/test_utils.cc
@@ -285,8 +285,7 @@
// Attach. |inner_contents| becomes owned by |outer_contents|.
WebContents* inner_contents = inner_contents_ptr.get();
- inner_contents->AttachToOuterWebContentsFrame(std::move(inner_contents_ptr),
- rfh);
+ outer_contents->AttachInnerWebContents(std::move(inner_contents_ptr), rfh);
// |guest_delegate| becomes owned by |inner_contents|.
guest_delegate.release()->SetInnerWebContents(inner_contents);