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/web_contents_impl.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "cc/output/copy_output_request.h" |
| 9 #include "cc/output/copy_output_result.h" |
| 10 #include "content/public/browser/render_frame_host.h" |
| 11 #include "content/public/browser/render_widget_host_view.h" |
| 12 #include "content/public/browser/web_contents.h" |
| 13 #include "content/public/browser/web_contents_delegate.h" |
| 14 #include "content/public/browser/web_contents_observer.h" |
| 15 #include "content/public/renderer/render_frame.h" |
| 16 #include "headless/lib/web_frame_impl.h" |
| 17 #include "headless/public/web_frame.h" |
| 18 #include "ui/aura/env.h" |
| 19 #include "ui/aura/window.h" |
| 20 #include "ui/aura/window_tree_host.h" |
| 21 |
| 22 namespace headless { |
| 23 |
| 24 class WebContentsImpl::Delegate : public content::WebContentsDelegate {}; |
| 25 |
| 26 WebContentsImpl::WebContentsImpl(content::BrowserContext* browser_context, |
| 27 const gfx::Size& initial_size) |
| 28 : web_contents_delegate_(new WebContentsImpl::Delegate()) { |
| 29 content::WebContents::CreateParams create_params(browser_context, NULL); |
| 30 create_params.initial_size = initial_size; |
| 31 |
| 32 web_contents_.reset(content::WebContents::Create(create_params)); |
| 33 web_contents_->SetDelegate(web_contents_delegate_.get()); |
| 34 |
| 35 DCHECK(aura::Env::GetInstance()); |
| 36 window_tree_host_.reset( |
| 37 aura::WindowTreeHost::Create(gfx::Rect(initial_size))); |
| 38 window_tree_host_->InitHost(); |
| 39 |
| 40 aura::Window* contents = web_contents_->GetNativeView(); |
| 41 aura::Window* parent = window_tree_host_->window(); |
| 42 |
| 43 if (!parent->Contains(contents)) { |
| 44 parent->AddChild(contents); |
| 45 contents->Show(); |
| 46 } |
| 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 WebContentsImpl::~WebContentsImpl() {} |
| 56 |
| 57 scoped_ptr<WebFrame> WebContentsImpl::main_frame() { |
| 58 auto routing_id = web_contents_->GetMainFrame()->GetRoutingID(); |
| 59 // We do hope that renderer and browser live in the same process. |
| 60 content::RenderFrame* render_frame = |
| 61 content::RenderFrame::FromRoutingID(routing_id); |
| 62 |
| 63 return scoped_ptr<WebFrame>(new WebFrameImpl(render_frame->GetWebFrame())); |
| 64 } |
| 65 |
| 66 void WebContentsImpl::OpenURL(const GURL& url) { |
| 67 content::NavigationController::LoadURLParams params(url); |
| 68 params.transition_type = ui::PageTransitionFromInt( |
| 69 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); |
| 70 web_contents_->GetController().LoadURLWithParams(params); |
| 71 web_contents_->Focus(); |
| 72 } |
| 73 |
| 74 namespace { |
| 75 |
| 76 void ProcessScreenshot( |
| 77 const WebContents::ScreenshotCallback& screenshot_callback, |
| 78 scoped_ptr<cc::CopyOutputResult> output_result) { |
| 79 DCHECK(output_result->HasBitmap()); |
| 80 screenshot_callback.Run(output_result->TakeBitmap().Pass()); |
| 81 } |
| 82 |
| 83 } // namespace |
| 84 |
| 85 void WebContentsImpl::GetScreenshot(const ScreenshotCallback& callback) { |
| 86 aura::Window* window = web_contents_->GetContentNativeView(); |
| 87 |
| 88 DCHECK(window); |
| 89 DCHECK(window->layer()); |
| 90 |
| 91 window->layer()->RequestCopyOfOutput( |
| 92 cc::CopyOutputRequest::CreateBitmapRequest( |
| 93 base::Bind(&ProcessScreenshot, callback))); |
| 94 } |
| 95 |
| 96 content::WebContents* WebContentsImpl::web_contents() { |
| 97 return web_contents_.get(); |
| 98 } |
| 99 |
| 100 class WebContents::Observer::ObserverImpl |
| 101 : public content::WebContentsObserver { |
| 102 public: |
| 103 ObserverImpl(content::WebContents* web_contents, |
| 104 WebContents::Observer* observer) |
| 105 : content::WebContentsObserver(web_contents), observer_(observer) {} |
| 106 |
| 107 ~ObserverImpl() override {} |
| 108 |
| 109 void DocumentOnLoadCompletedInMainFrame() override { |
| 110 observer_->DocumentOnLoadCompletedInMainFrame(); |
| 111 } |
| 112 |
| 113 private: |
| 114 WebContents::Observer* observer_; |
| 115 }; |
| 116 |
| 117 WebContents::Observer::Observer(WebContents* web_contents) |
| 118 : observer_( |
| 119 new WebContents::Observer::ObserverImpl(web_contents->web_contents(), |
| 120 this)) {} |
| 121 |
| 122 WebContents::Observer::~Observer() {} |
| 123 |
| 124 } // namespace headless |
OLD | NEW |