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 WebContentsObserverAdapter : public content::WebContentsObserver { |
| 25 public: |
| 26 WebContentsObserverAdapter(content::WebContents* web_contents, |
| 27 HeadlessWebContents::Observer* observer) |
| 28 : content::WebContentsObserver(web_contents), observer_(observer) {} |
| 29 |
| 30 ~WebContentsObserverAdapter() override {} |
| 31 |
| 32 void DocumentOnLoadCompletedInMainFrame() override { |
| 33 observer_->DocumentOnLoadCompletedInMainFrame(); |
| 34 } |
| 35 |
| 36 private: |
| 37 HeadlessWebContents::Observer* observer_; // Not owned. |
| 38 |
| 39 DISALLOW_COPY_AND_ASSIGN(WebContentsObserverAdapter); |
| 40 }; |
| 41 |
| 42 class HeadlessWebContentsImpl::Delegate : public content::WebContentsDelegate { |
| 43 public: |
| 44 Delegate() {} |
| 45 |
| 46 private: |
| 47 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 48 }; |
| 49 |
| 50 HeadlessWebContentsImpl::HeadlessWebContentsImpl( |
| 51 content::BrowserContext* browser_context, |
| 52 aura::Window* parent_window, |
| 53 const gfx::Size& initial_size) |
| 54 : web_contents_delegate_(new HeadlessWebContentsImpl::Delegate()) { |
| 55 content::WebContents::CreateParams create_params(browser_context, nullptr); |
| 56 create_params.initial_size = initial_size; |
| 57 |
| 58 web_contents_.reset(content::WebContents::Create(create_params)); |
| 59 web_contents_->SetDelegate(web_contents_delegate_.get()); |
| 60 |
| 61 aura::Window* contents = web_contents_->GetNativeView(); |
| 62 DCHECK(!parent_window->Contains(contents)); |
| 63 parent_window->AddChild(contents); |
| 64 contents->Show(); |
| 65 |
| 66 contents->SetBounds(gfx::Rect(initial_size)); |
| 67 content::RenderWidgetHostView* host_view = |
| 68 web_contents_->GetRenderWidgetHostView(); |
| 69 if (host_view) |
| 70 host_view->SetSize(initial_size); |
| 71 } |
| 72 |
| 73 HeadlessWebContentsImpl::~HeadlessWebContentsImpl() { |
| 74 web_contents_->Close(); |
| 75 } |
| 76 |
| 77 bool HeadlessWebContentsImpl::OpenURL(const GURL& url) { |
| 78 if (!url.is_valid()) |
| 79 return false; |
| 80 content::NavigationController::LoadURLParams params(url); |
| 81 params.transition_type = ui::PageTransitionFromInt( |
| 82 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); |
| 83 web_contents_->GetController().LoadURLWithParams(params); |
| 84 web_contents_->Focus(); |
| 85 return true; |
| 86 } |
| 87 |
| 88 void HeadlessWebContentsImpl::AddObserver(Observer* observer) { |
| 89 DCHECK(observer_map_.find(observer) == observer_map_.end()); |
| 90 observer_map_[observer] = make_scoped_ptr( |
| 91 new WebContentsObserverAdapter(web_contents_.get(), observer)); |
| 92 } |
| 93 |
| 94 void HeadlessWebContentsImpl::RemoveObserver(Observer* observer) { |
| 95 ObserverMap::iterator it = observer_map_.find(observer); |
| 96 DCHECK(it != observer_map_.end()); |
| 97 observer_map_.erase(it); |
| 98 } |
| 99 |
| 100 content::WebContents* HeadlessWebContentsImpl::web_contents() const { |
| 101 return web_contents_.get(); |
| 102 } |
| 103 |
| 104 } // namespace headless |
OLD | NEW |