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/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/env.h" |
| 21 #include "ui/aura/window.h" |
| 22 #include "ui/aura/window_tree_host.h" |
| 23 |
| 24 namespace headless { |
| 25 |
| 26 class HeadlessWebContentsImpl::Delegate : public content::WebContentsDelegate { |
| 27 }; |
| 28 |
| 29 HeadlessWebContentsImpl::HeadlessWebContentsImpl( |
| 30 content::BrowserContext* browser_context, |
| 31 const gfx::Size& initial_size) |
| 32 : web_contents_delegate_(new HeadlessWebContentsImpl::Delegate()) { |
| 33 content::WebContents::CreateParams create_params(browser_context, nullptr); |
| 34 create_params.initial_size = initial_size; |
| 35 |
| 36 web_contents_.reset(content::WebContents::Create(create_params)); |
| 37 web_contents_->SetDelegate(web_contents_delegate_.get()); |
| 38 |
| 39 DCHECK(aura::Env::GetInstance()); |
| 40 window_tree_host_.reset( |
| 41 aura::WindowTreeHost::Create(gfx::Rect(initial_size))); |
| 42 window_tree_host_->InitHost(); |
| 43 |
| 44 aura::Window* contents = web_contents_->GetNativeView(); |
| 45 aura::Window* parent = window_tree_host_->window(); |
| 46 |
| 47 if (!parent->Contains(contents)) { |
| 48 parent->AddChild(contents); |
| 49 contents->Show(); |
| 50 } |
| 51 |
| 52 contents->SetBounds(gfx::Rect(initial_size)); |
| 53 content::RenderWidgetHostView* host_view = |
| 54 web_contents_->GetRenderWidgetHostView(); |
| 55 if (host_view) |
| 56 host_view->SetSize(initial_size); |
| 57 } |
| 58 |
| 59 HeadlessWebContentsImpl::~HeadlessWebContentsImpl() { |
| 60 web_contents_->Close(); |
| 61 } |
| 62 |
| 63 void HeadlessWebContentsImpl::OpenURL(const GURL& url) { |
| 64 content::NavigationController::LoadURLParams params(url); |
| 65 params.transition_type = ui::PageTransitionFromInt( |
| 66 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); |
| 67 web_contents_->GetController().LoadURLWithParams(params); |
| 68 web_contents_->Focus(); |
| 69 } |
| 70 |
| 71 content::WebContents* HeadlessWebContentsImpl::web_contents() { |
| 72 return web_contents_.get(); |
| 73 } |
| 74 |
| 75 class HeadlessWebContents::Observer::ObserverImpl |
| 76 : public content::WebContentsObserver { |
| 77 public: |
| 78 ObserverImpl(content::WebContents* web_contents, |
| 79 HeadlessWebContents::Observer* observer) |
| 80 : content::WebContentsObserver(web_contents), observer_(observer) {} |
| 81 ObserverImpl(HeadlessWebContents::Observer* observer) : observer_(observer) {} |
| 82 |
| 83 ~ObserverImpl() override {} |
| 84 |
| 85 void ObserveWebContents(content::WebContents* web_contents) { |
| 86 DCHECK(!this->web_contents()); |
| 87 Observe(web_contents); |
| 88 } |
| 89 |
| 90 void DocumentOnLoadCompletedInMainFrame() override { |
| 91 observer_->DocumentOnLoadCompletedInMainFrame(); |
| 92 } |
| 93 |
| 94 private: |
| 95 HeadlessWebContents::Observer* observer_; |
| 96 }; |
| 97 |
| 98 HeadlessWebContents::Observer::Observer(HeadlessWebContents* web_contents) |
| 99 : observer_(new HeadlessWebContents::Observer::ObserverImpl( |
| 100 static_cast<HeadlessWebContentsImpl*>(web_contents)->web_contents(), |
| 101 this)) {} |
| 102 |
| 103 HeadlessWebContents::Observer::Observer() |
| 104 : observer_(new HeadlessWebContents::Observer::ObserverImpl(this)) {} |
| 105 |
| 106 HeadlessWebContents::Observer::~Observer() {} |
| 107 |
| 108 void HeadlessWebContents::Observer::Observe(HeadlessWebContents* web_contents) { |
| 109 observer_->ObserveWebContents( |
| 110 static_cast<HeadlessWebContentsImpl*>(web_contents)->web_contents()); |
| 111 } |
| 112 |
| 113 } // namespace headless |
OLD | NEW |