Index: headless/lib/headless_web_contents_impl.cc |
diff --git a/headless/lib/headless_web_contents_impl.cc b/headless/lib/headless_web_contents_impl.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b56df6f2e9f43e38c2e2e80b84da7ea35d700f98 |
--- /dev/null |
+++ b/headless/lib/headless_web_contents_impl.cc |
@@ -0,0 +1,120 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "headless/lib/headless_web_contents_impl.h" |
+ |
+#include "base/bind.h" |
+#include "base/memory/weak_ptr.h" |
+#include "base/trace_event/trace_event.h" |
+#include "content/public/browser/render_frame_host.h" |
+#include "content/public/browser/render_process_host.h" |
+#include "content/public/browser/render_view_host.h" |
+#include "content/public/browser/render_widget_host_view.h" |
+#include "content/public/browser/web_contents.h" |
+#include "content/public/browser/web_contents_delegate.h" |
+#include "content/public/browser/web_contents_observer.h" |
+#include "content/public/common/bindings_policy.h" |
+#include "content/public/common/service_registry.h" |
+#include "content/public/renderer/render_frame.h" |
+#include "ui/aura/env.h" |
+#include "ui/aura/window.h" |
+#include "ui/aura/window_tree_host.h" |
+ |
+namespace headless { |
+ |
+class HeadlessWebContentsImpl::Delegate : public content::WebContentsDelegate { |
+ public: |
+ Delegate() {} |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(Delegate); |
+}; |
+ |
+HeadlessWebContentsImpl::HeadlessWebContentsImpl( |
+ content::BrowserContext* browser_context, |
+ const gfx::Size& initial_size) |
+ : web_contents_delegate_(new HeadlessWebContentsImpl::Delegate()) { |
+ content::WebContents::CreateParams create_params(browser_context, nullptr); |
+ create_params.initial_size = initial_size; |
+ |
+ web_contents_.reset(content::WebContents::Create(create_params)); |
+ web_contents_->SetDelegate(web_contents_delegate_.get()); |
+ |
+ DCHECK(aura::Env::GetInstance()); |
+ window_tree_host_.reset( |
+ aura::WindowTreeHost::Create(gfx::Rect(initial_size))); |
+ window_tree_host_->InitHost(); |
+ |
+ aura::Window* contents = web_contents_->GetNativeView(); |
+ aura::Window* parent = window_tree_host_->window(); |
+ |
+ if (!parent->Contains(contents)) { |
+ parent->AddChild(contents); |
+ contents->Show(); |
+ } |
+ |
+ contents->SetBounds(gfx::Rect(initial_size)); |
+ content::RenderWidgetHostView* host_view = |
+ web_contents_->GetRenderWidgetHostView(); |
+ if (host_view) |
+ host_view->SetSize(initial_size); |
+} |
+ |
+HeadlessWebContentsImpl::~HeadlessWebContentsImpl() { |
+ web_contents_->Close(); |
+} |
+ |
+void HeadlessWebContentsImpl::OpenURL(const GURL& url) { |
+ content::NavigationController::LoadURLParams params(url); |
+ params.transition_type = ui::PageTransitionFromInt( |
+ ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); |
+ web_contents_->GetController().LoadURLWithParams(params); |
+ web_contents_->Focus(); |
+} |
+ |
+content::WebContents* HeadlessWebContentsImpl::web_contents() { |
+ return web_contents_.get(); |
+} |
+ |
+class HeadlessWebContents::Observer::ObserverImpl |
+ : public content::WebContentsObserver { |
+ public: |
+ ObserverImpl(content::WebContents* web_contents, |
+ HeadlessWebContents::Observer* observer) |
+ : content::WebContentsObserver(web_contents), observer_(observer) {} |
+ ObserverImpl(HeadlessWebContents::Observer* observer) : observer_(observer) {} |
+ |
+ ~ObserverImpl() override {} |
+ |
+ void ObserveWebContents(content::WebContents* web_contents) { |
+ DCHECK(!this->web_contents()); |
+ Observe(web_contents); |
+ } |
+ |
+ void DocumentOnLoadCompletedInMainFrame() override { |
+ observer_->DocumentOnLoadCompletedInMainFrame(); |
+ } |
+ |
+ private: |
+ HeadlessWebContents::Observer* observer_; |
alex clarke (OOO till 29th)
2016/02/10 17:49:22
I guess this isn't owned?
Sami
2016/02/10 18:47:38
Yep, the Observer owns an ObserverImpl and this is
|
+ |
+ DISALLOW_COPY_AND_ASSIGN(ObserverImpl); |
+}; |
+ |
+HeadlessWebContents::Observer::Observer(HeadlessWebContents* web_contents) |
+ : observer_(new HeadlessWebContents::Observer::ObserverImpl( |
+ static_cast<HeadlessWebContentsImpl*>(web_contents)->web_contents(), |
alex clarke (OOO till 29th)
2016/02/10 17:49:22
I hope these casts are safe :)
Sami
2016/02/10 18:47:38
I've now made the base class only implementable by
|
+ this)) {} |
+ |
+HeadlessWebContents::Observer::Observer() |
+ : observer_(new HeadlessWebContents::Observer::ObserverImpl(this)) {} |
+ |
+HeadlessWebContents::Observer::~Observer() {} |
+ |
+void HeadlessWebContents::Observer::Observe(HeadlessWebContents* web_contents) { |
+ observer_->ObserveWebContents( |
+ static_cast<HeadlessWebContentsImpl*>(web_contents)->web_contents()); |
+} |
+ |
+} // namespace headless |