Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Side by Side Diff: headless/lib/browser/headless_web_contents_impl.cc

Issue 1891843004: [headless] AddWindowTreeClient (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes according to review Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "headless/lib/browser/headless_web_contents_impl.h" 5 #include "headless/lib/browser/headless_web_contents_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/memory/weak_ptr.h" 9 #include "base/memory/weak_ptr.h"
10 #include "base/trace_event/trace_event.h" 10 #include "base/trace_event/trace_event.h"
11 #include "content/public/browser/navigation_handle.h" 11 #include "content/public/browser/navigation_handle.h"
12 #include "content/public/browser/render_frame_host.h" 12 #include "content/public/browser/render_frame_host.h"
13 #include "content/public/browser/render_process_host.h" 13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/render_view_host.h" 14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/render_widget_host_view.h" 15 #include "content/public/browser/render_widget_host_view.h"
16 #include "content/public/browser/web_contents.h" 16 #include "content/public/browser/web_contents.h"
17 #include "content/public/browser/web_contents_delegate.h" 17 #include "content/public/browser/web_contents_delegate.h"
18 #include "content/public/browser/web_contents_observer.h" 18 #include "content/public/browser/web_contents_observer.h"
19 #include "content/public/common/bindings_policy.h" 19 #include "content/public/common/bindings_policy.h"
20 #include "content/public/common/service_registry.h" 20 #include "content/public/common/service_registry.h"
21 #include "content/public/renderer/render_frame.h" 21 #include "content/public/renderer/render_frame.h"
22 #include "headless/lib/browser/headless_browser_impl.h"
22 #include "ui/aura/window.h" 23 #include "ui/aura/window.h"
23 24
24 namespace headless { 25 namespace headless {
25 26
26 class WebContentsObserverAdapter : public content::WebContentsObserver { 27 class WebContentsObserverAdapter : public content::WebContentsObserver {
27 public: 28 public:
28 WebContentsObserverAdapter(content::WebContents* web_contents, 29 WebContentsObserverAdapter(content::WebContents* web_contents,
29 HeadlessWebContents::Observer* observer) 30 HeadlessWebContents::Observer* observer)
30 : content::WebContentsObserver(web_contents), observer_(observer) {} 31 : content::WebContentsObserver(web_contents), observer_(observer) {}
31 32
(...skipping 12 matching lines...) Expand all
44 } 45 }
45 46
46 private: 47 private:
47 HeadlessWebContents::Observer* observer_; // Not owned. 48 HeadlessWebContents::Observer* observer_; // Not owned.
48 49
49 DISALLOW_COPY_AND_ASSIGN(WebContentsObserverAdapter); 50 DISALLOW_COPY_AND_ASSIGN(WebContentsObserverAdapter);
50 }; 51 };
51 52
52 class HeadlessWebContentsImpl::Delegate : public content::WebContentsDelegate { 53 class HeadlessWebContentsImpl::Delegate : public content::WebContentsDelegate {
53 public: 54 public:
54 Delegate() {} 55 explicit Delegate(HeadlessBrowserImpl* browser) : browser_(browser) {}
56
57 void WebContentsCreated(content::WebContents* source_contents,
58 int opener_render_frame_id,
59 const std::string& frame_name,
60 const GURL& target_url,
61 content::WebContents* new_contents) override {
62 browser_->RegisterWebContents(
63 HeadlessWebContentsImpl::CreateFromWebContents(new_contents, browser_));
64 }
55 65
56 private: 66 private:
67 HeadlessBrowserImpl* browser_; // Not owned.
57 DISALLOW_COPY_AND_ASSIGN(Delegate); 68 DISALLOW_COPY_AND_ASSIGN(Delegate);
58 }; 69 };
59 70
60 HeadlessWebContentsImpl::HeadlessWebContentsImpl( 71 // static
61 content::BrowserContext* browser_context, 72 std::unique_ptr<HeadlessWebContentsImpl> HeadlessWebContentsImpl::Create(
73 content::BrowserContext* context,
62 aura::Window* parent_window, 74 aura::Window* parent_window,
63 const gfx::Size& initial_size) 75 const gfx::Size& initial_size,
64 : web_contents_delegate_(new HeadlessWebContentsImpl::Delegate()) { 76 HeadlessBrowserImpl* browser) {
65 content::WebContents::CreateParams create_params(browser_context, nullptr); 77 content::WebContents::CreateParams create_params(context, nullptr);
66 create_params.initial_size = initial_size; 78 create_params.initial_size = initial_size;
67 79
68 web_contents_.reset(content::WebContents::Create(create_params)); 80 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents =
69 web_contents_->SetDelegate(web_contents_delegate_.get()); 81 base::WrapUnique(new HeadlessWebContentsImpl(
82 content::WebContents::Create(create_params), browser));
70 83
84 headless_web_contents->InitializeScreen(parent_window, initial_size);
85
86 return headless_web_contents;
87 }
88
89 // static
90 std::unique_ptr<HeadlessWebContentsImpl>
91 HeadlessWebContentsImpl::CreateFromWebContents(
92 content::WebContents* web_contents,
93 HeadlessBrowserImpl* browser) {
94 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents =
95 base::WrapUnique(new HeadlessWebContentsImpl(web_contents, browser));
96
97 return headless_web_contents;
98 }
99
100 void HeadlessWebContentsImpl::InitializeScreen(aura::Window* parent_window,
101 const gfx::Size& initial_size) {
71 aura::Window* contents = web_contents_->GetNativeView(); 102 aura::Window* contents = web_contents_->GetNativeView();
72 DCHECK(!parent_window->Contains(contents)); 103 DCHECK(!parent_window->Contains(contents));
73 parent_window->AddChild(contents); 104 parent_window->AddChild(contents);
74 contents->Show(); 105 contents->Show();
75 106
76 contents->SetBounds(gfx::Rect(initial_size)); 107 contents->SetBounds(gfx::Rect(initial_size));
77 content::RenderWidgetHostView* host_view = 108 content::RenderWidgetHostView* host_view =
78 web_contents_->GetRenderWidgetHostView(); 109 web_contents_->GetRenderWidgetHostView();
79 if (host_view) 110 if (host_view)
80 host_view->SetSize(initial_size); 111 host_view->SetSize(initial_size);
81 } 112 }
82 113
114 HeadlessWebContentsImpl::HeadlessWebContentsImpl(
115 content::WebContents* web_contents,
116 HeadlessBrowserImpl* browser)
117 : web_contents_delegate_(new HeadlessWebContentsImpl::Delegate(browser)),
118 web_contents_(web_contents),
119 browser_(browser) {
120 web_contents_->SetDelegate(web_contents_delegate_.get());
121 }
122
83 HeadlessWebContentsImpl::~HeadlessWebContentsImpl() { 123 HeadlessWebContentsImpl::~HeadlessWebContentsImpl() {
84 web_contents_->Close(); 124 web_contents_->Close();
85 } 125 }
86 126
87 bool HeadlessWebContentsImpl::OpenURL(const GURL& url) { 127 bool HeadlessWebContentsImpl::OpenURL(const GURL& url) {
88 if (!url.is_valid()) 128 if (!url.is_valid())
89 return false; 129 return false;
90 content::NavigationController::LoadURLParams params(url); 130 content::NavigationController::LoadURLParams params(url);
91 params.transition_type = ui::PageTransitionFromInt( 131 params.transition_type = ui::PageTransitionFromInt(
92 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); 132 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR);
93 web_contents_->GetController().LoadURLWithParams(params); 133 web_contents_->GetController().LoadURLWithParams(params);
94 web_contents_->Focus(); 134 web_contents_->Focus();
95 return true; 135 return true;
96 } 136 }
97 137
138 void HeadlessWebContentsImpl::Close() {
139 browser_->DestroyWebContents(this);
140 }
141
98 void HeadlessWebContentsImpl::AddObserver(Observer* observer) { 142 void HeadlessWebContentsImpl::AddObserver(Observer* observer) {
99 DCHECK(observer_map_.find(observer) == observer_map_.end()); 143 DCHECK(observer_map_.find(observer) == observer_map_.end());
100 observer_map_[observer] = base::WrapUnique( 144 observer_map_[observer] = base::WrapUnique(
101 new WebContentsObserverAdapter(web_contents_.get(), observer)); 145 new WebContentsObserverAdapter(web_contents_.get(), observer));
102 } 146 }
103 147
104 void HeadlessWebContentsImpl::RemoveObserver(Observer* observer) { 148 void HeadlessWebContentsImpl::RemoveObserver(Observer* observer) {
105 ObserverMap::iterator it = observer_map_.find(observer); 149 ObserverMap::iterator it = observer_map_.find(observer);
106 DCHECK(it != observer_map_.end()); 150 DCHECK(it != observer_map_.end());
107 observer_map_.erase(it); 151 observer_map_.erase(it);
108 } 152 }
109 153
110 content::WebContents* HeadlessWebContentsImpl::web_contents() const { 154 content::WebContents* HeadlessWebContentsImpl::web_contents() const {
111 return web_contents_.get(); 155 return web_contents_.get();
112 } 156 }
113 157
114 } // namespace headless 158 } // namespace headless
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698