| 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
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..1547a7e8829ce92ff8ade4286f1a61fc0f35c9c1
|
| --- /dev/null
|
| +++ b/headless/lib/browser/headless_web_contents_impl.cc
|
| @@ -0,0 +1,111 @@
|
| +// 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/browser/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/window.h"
|
| +
|
| +namespace headless {
|
| +
|
| +class HeadlessWebContentsImpl::Delegate : public content::WebContentsDelegate {
|
| + public:
|
| + Delegate() {}
|
| +
|
| + private:
|
| + DISALLOW_COPY_AND_ASSIGN(Delegate);
|
| +};
|
| +
|
| +HeadlessWebContentsImpl::HeadlessWebContentsImpl(
|
| + content::BrowserContext* browser_context,
|
| + aura::Window* parent_window,
|
| + 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());
|
| +
|
| + aura::Window* contents = web_contents_->GetNativeView();
|
| + DCHECK(!parent_window->Contains(contents));
|
| + parent_window->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() const {
|
| + 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_; // Not owned.
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(ObserverImpl);
|
| +};
|
| +
|
| +HeadlessWebContents::Observer::Observer(HeadlessWebContents* web_contents)
|
| + : observer_impl_(new HeadlessWebContents::Observer::ObserverImpl(
|
| + static_cast<HeadlessWebContentsImpl*>(web_contents)->web_contents(),
|
| + this)) {}
|
| +
|
| +HeadlessWebContents::Observer::Observer()
|
| + : observer_impl_(new HeadlessWebContents::Observer::ObserverImpl(this)) {}
|
| +
|
| +HeadlessWebContents::Observer::~Observer() {}
|
| +
|
| +void HeadlessWebContents::Observer::Observe(HeadlessWebContents* web_contents) {
|
| + observer_impl_->ObserveWebContents(
|
| + static_cast<HeadlessWebContentsImpl*>(web_contents)->web_contents());
|
| +}
|
| +
|
| +} // namespace headless
|
|
|