OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "headless/lib/browser/headless_web_contents_impl.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/weak_ptr.h" |
| 9 #include "base/trace_event/trace_event.h" |
| 10 #include "content/public/browser/render_frame_host.h" |
| 11 #include "content/public/browser/render_process_host.h" |
| 12 #include "content/public/browser/render_view_host.h" |
| 13 #include "content/public/browser/render_widget_host_view.h" |
| 14 #include "content/public/browser/web_contents.h" |
| 15 #include "content/public/browser/web_contents_delegate.h" |
| 16 #include "content/public/browser/web_contents_observer.h" |
| 17 #include "content/public/common/bindings_policy.h" |
| 18 #include "content/public/common/service_registry.h" |
| 19 #include "content/public/renderer/render_frame.h" |
| 20 #include "ui/aura/window.h" |
| 21 |
| 22 namespace headless { |
| 23 |
| 24 class HeadlessWebContentsImpl::Delegate : public content::WebContentsDelegate { |
| 25 public: |
| 26 Delegate() {} |
| 27 |
| 28 private: |
| 29 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 30 }; |
| 31 |
| 32 HeadlessWebContentsImpl::HeadlessWebContentsImpl( |
| 33 content::BrowserContext* browser_context, |
| 34 aura::Window* parent_window, |
| 35 const gfx::Size& initial_size) |
| 36 : web_contents_delegate_(new HeadlessWebContentsImpl::Delegate()) { |
| 37 content::WebContents::CreateParams create_params(browser_context, nullptr); |
| 38 create_params.initial_size = initial_size; |
| 39 |
| 40 web_contents_.reset(content::WebContents::Create(create_params)); |
| 41 web_contents_->SetDelegate(web_contents_delegate_.get()); |
| 42 |
| 43 aura::Window* contents = web_contents_->GetNativeView(); |
| 44 DCHECK(!parent_window->Contains(contents)); |
| 45 parent_window->AddChild(contents); |
| 46 contents->Show(); |
| 47 |
| 48 contents->SetBounds(gfx::Rect(initial_size)); |
| 49 content::RenderWidgetHostView* host_view = |
| 50 web_contents_->GetRenderWidgetHostView(); |
| 51 if (host_view) |
| 52 host_view->SetSize(initial_size); |
| 53 } |
| 54 |
| 55 HeadlessWebContentsImpl::~HeadlessWebContentsImpl() { |
| 56 web_contents_->Close(); |
| 57 } |
| 58 |
| 59 void HeadlessWebContentsImpl::OpenURL(const GURL& url) { |
| 60 content::NavigationController::LoadURLParams params(url); |
| 61 params.transition_type = ui::PageTransitionFromInt( |
| 62 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); |
| 63 web_contents_->GetController().LoadURLWithParams(params); |
| 64 web_contents_->Focus(); |
| 65 } |
| 66 |
| 67 content::WebContents* HeadlessWebContentsImpl::web_contents() const { |
| 68 return web_contents_.get(); |
| 69 } |
| 70 |
| 71 class HeadlessWebContents::Observer::ObserverImpl |
| 72 : public content::WebContentsObserver { |
| 73 public: |
| 74 ObserverImpl(content::WebContents* web_contents, |
| 75 HeadlessWebContents::Observer* observer) |
| 76 : content::WebContentsObserver(web_contents), observer_(observer) {} |
| 77 ObserverImpl(HeadlessWebContents::Observer* observer) : observer_(observer) {} |
| 78 |
| 79 ~ObserverImpl() override {} |
| 80 |
| 81 void ObserveWebContents(content::WebContents* web_contents) { |
| 82 DCHECK(!this->web_contents()); |
| 83 Observe(web_contents); |
| 84 } |
| 85 |
| 86 void DocumentOnLoadCompletedInMainFrame() override { |
| 87 observer_->DocumentOnLoadCompletedInMainFrame(); |
| 88 } |
| 89 |
| 90 private: |
| 91 HeadlessWebContents::Observer* observer_; // Not owned. |
| 92 |
| 93 DISALLOW_COPY_AND_ASSIGN(ObserverImpl); |
| 94 }; |
| 95 |
| 96 HeadlessWebContents::Observer::Observer(HeadlessWebContents* web_contents) |
| 97 : observer_impl_(new HeadlessWebContents::Observer::ObserverImpl( |
| 98 static_cast<HeadlessWebContentsImpl*>(web_contents)->web_contents(), |
| 99 this)) {} |
| 100 |
| 101 HeadlessWebContents::Observer::Observer() |
| 102 : observer_impl_(new HeadlessWebContents::Observer::ObserverImpl(this)) {} |
| 103 |
| 104 HeadlessWebContents::Observer::~Observer() {} |
| 105 |
| 106 void HeadlessWebContents::Observer::Observe(HeadlessWebContents* web_contents) { |
| 107 observer_impl_->ObserveWebContents( |
| 108 static_cast<HeadlessWebContentsImpl*>(web_contents)->web_contents()); |
| 109 } |
| 110 |
| 111 } // namespace headless |
OLD | NEW |