Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(307)

Unified Diff: headless/lib/browser/headless_web_contents_impl.cc

Issue 1891843004: [headless] AddWindowTreeClient (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added tests and changed ownership model Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: headless/lib/browser/headless_web_contents_impl.cc
diff --git a/headless/lib/browser/headless_web_contents_impl.cc b/headless/lib/browser/headless_web_contents_impl.cc
index f58dfe0671c2b627794fc9b2540eb9624d134809..7902dd943941ef67a3f3d940b348ee9f06a48f63 100644
--- a/headless/lib/browser/headless_web_contents_impl.cc
+++ b/headless/lib/browser/headless_web_contents_impl.cc
@@ -19,6 +19,7 @@
#include "content/public/common/bindings_policy.h"
#include "content/public/common/service_registry.h"
#include "content/public/renderer/render_frame.h"
+#include "headless/lib/browser/headless_browser_impl.h"
#include "ui/aura/window.h"
namespace headless {
@@ -51,23 +52,55 @@ class WebContentsObserverAdapter : public content::WebContentsObserver {
class HeadlessWebContentsImpl::Delegate : public content::WebContentsDelegate {
public:
- Delegate() {}
+ explicit Delegate(HeadlessBrowserImpl* browser) : browser_(browser) {}
+
+ void WebContentsCreated(content::WebContents* source_contents,
+ int opener_render_frame_id,
+ const std::string& frame_name,
+ const GURL& target_url,
+ content::WebContents* new_contents) override {
+ browser_->RegisterWebContents(
+ HeadlessWebContentsImpl::FromWebContents(new_contents, browser_));
+ }
private:
+ HeadlessBrowserImpl* browser_; // Not owned.
DISALLOW_COPY_AND_ASSIGN(Delegate);
};
-HeadlessWebContentsImpl::HeadlessWebContentsImpl(
- content::BrowserContext* browser_context,
+// static
+std::unique_ptr<HeadlessWebContentsImpl> HeadlessWebContentsImpl::Create(
+ content::BrowserContext* context,
aura::Window* parent_window,
- const gfx::Size& initial_size)
- : web_contents_delegate_(new HeadlessWebContentsImpl::Delegate()) {
- content::WebContents::CreateParams create_params(browser_context, nullptr);
+ const gfx::Size& initial_size,
+ HeadlessBrowserImpl* browser) {
+ content::WebContents::CreateParams create_params(context, nullptr);
create_params.initial_size = initial_size;
- web_contents_.reset(content::WebContents::Create(create_params));
- web_contents_->SetDelegate(web_contents_delegate_.get());
+ std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents =
+ base::WrapUnique(new HeadlessWebContentsImpl(
+ content::WebContents::Create(create_params), browser));
+ headless_web_contents->is_directly_created_ = true;
+
+ headless_web_contents->InitializeScreen(parent_window, initial_size);
+
+ return headless_web_contents;
+}
+
+// static
+std::unique_ptr<HeadlessWebContentsImpl>
+HeadlessWebContentsImpl::FromWebContents(content::WebContents* web_contents,
+ HeadlessBrowserImpl* browser) {
+ std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents =
+ base::WrapUnique(new HeadlessWebContentsImpl(web_contents, browser));
+
+ headless_web_contents->is_directly_created_ = false;
+
+ return headless_web_contents;
+}
+void HeadlessWebContentsImpl::InitializeScreen(aura::Window* parent_window,
+ const gfx::Size& initial_size) {
aura::Window* contents = web_contents_->GetNativeView();
DCHECK(!parent_window->Contains(contents));
parent_window->AddChild(contents);
@@ -80,6 +113,16 @@ HeadlessWebContentsImpl::HeadlessWebContentsImpl(
host_view->SetSize(initial_size);
}
+HeadlessWebContentsImpl::HeadlessWebContentsImpl(
+ content::WebContents* web_contents,
+ HeadlessBrowserImpl* browser)
+ : web_contents_delegate_(new HeadlessWebContentsImpl::Delegate(browser)),
+ web_contents_(web_contents),
+ is_directly_created_(false),
+ browser_(browser) {
+ web_contents_->SetDelegate(web_contents_delegate_.get());
+}
+
HeadlessWebContentsImpl::~HeadlessWebContentsImpl() {
web_contents_->Close();
}
@@ -95,6 +138,14 @@ bool HeadlessWebContentsImpl::OpenURL(const GURL& url) {
return true;
}
+bool HeadlessWebContentsImpl::IsDirectlyCreated() const {
+ return is_directly_created_;
+}
+
+void HeadlessWebContentsImpl::Close() {
+ browser_->DestroyWebContents(this);
+}
+
void HeadlessWebContentsImpl::AddObserver(Observer* observer) {
DCHECK(observer_map_.find(observer) == observer_map_.end());
observer_map_[observer] = base::WrapUnique(

Powered by Google App Engine
This is Rietveld 408576698