Index: headless/lib/web_contents_impl.cc |
diff --git a/headless/lib/web_contents_impl.cc b/headless/lib/web_contents_impl.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0c3fe694e38139a12bd63489db9d3682360ab135 |
--- /dev/null |
+++ b/headless/lib/web_contents_impl.cc |
@@ -0,0 +1,124 @@ |
+// 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/web_contents_impl.h" |
+ |
+#include "base/bind.h" |
+#include "cc/output/copy_output_request.h" |
+#include "cc/output/copy_output_result.h" |
+#include "content/public/browser/render_frame_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/renderer/render_frame.h" |
+#include "headless/lib/web_frame_impl.h" |
+#include "headless/public/web_frame.h" |
+#include "ui/aura/env.h" |
+#include "ui/aura/window.h" |
+#include "ui/aura/window_tree_host.h" |
+ |
+namespace headless { |
+ |
+class WebContentsImpl::Delegate : public content::WebContentsDelegate {}; |
+ |
+WebContentsImpl::WebContentsImpl(content::BrowserContext* browser_context, |
+ const gfx::Size& initial_size) |
+ : web_contents_delegate_(new WebContentsImpl::Delegate()) { |
+ content::WebContents::CreateParams create_params(browser_context, NULL); |
+ 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); |
+} |
+ |
+WebContentsImpl::~WebContentsImpl() {} |
+ |
+scoped_ptr<WebFrame> WebContentsImpl::main_frame() { |
+ auto routing_id = web_contents_->GetMainFrame()->GetRoutingID(); |
+ // We do hope that renderer and browser live in the same process. |
+ content::RenderFrame* render_frame = |
+ content::RenderFrame::FromRoutingID(routing_id); |
+ |
+ return scoped_ptr<WebFrame>(new WebFrameImpl(render_frame->GetWebFrame())); |
+} |
+ |
+void WebContentsImpl::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(); |
+} |
+ |
+namespace { |
+ |
+void ProcessScreenshot( |
+ const WebContents::ScreenshotCallback& screenshot_callback, |
+ scoped_ptr<cc::CopyOutputResult> output_result) { |
+ DCHECK(output_result->HasBitmap()); |
+ screenshot_callback.Run(output_result->TakeBitmap().Pass()); |
+} |
+ |
+} // namespace |
+ |
+void WebContentsImpl::GetScreenshot(const ScreenshotCallback& callback) { |
+ aura::Window* window = web_contents_->GetContentNativeView(); |
+ |
+ DCHECK(window); |
+ DCHECK(window->layer()); |
+ |
+ window->layer()->RequestCopyOfOutput( |
+ cc::CopyOutputRequest::CreateBitmapRequest( |
+ base::Bind(&ProcessScreenshot, callback))); |
+} |
+ |
+content::WebContents* WebContentsImpl::web_contents() { |
+ return web_contents_.get(); |
+} |
+ |
+class WebContents::Observer::ObserverImpl |
+ : public content::WebContentsObserver { |
+ public: |
+ ObserverImpl(content::WebContents* web_contents, |
+ WebContents::Observer* observer) |
+ : content::WebContentsObserver(web_contents), observer_(observer) {} |
+ |
+ ~ObserverImpl() override {} |
+ |
+ void DocumentOnLoadCompletedInMainFrame() override { |
+ observer_->DocumentOnLoadCompletedInMainFrame(); |
+ } |
+ |
+ private: |
+ WebContents::Observer* observer_; |
+}; |
+ |
+WebContents::Observer::Observer(WebContents* web_contents) |
+ : observer_( |
+ new WebContents::Observer::ObserverImpl(web_contents->web_contents(), |
+ this)) {} |
+ |
+WebContents::Observer::~Observer() {} |
+ |
+} // namespace headless |