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

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

Issue 2509813006: HeadlessWebContents:Observer to observe render process exit status (Closed)
Patch Set: Make it safe to call Shutdown() Created 4 years, 1 month 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 <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 HeadlessWebContents::Observer* observer) 47 HeadlessWebContents::Observer* observer)
48 : content::WebContentsObserver(web_contents), observer_(observer) {} 48 : content::WebContentsObserver(web_contents), observer_(observer) {}
49 49
50 ~WebContentsObserverAdapter() override {} 50 ~WebContentsObserverAdapter() override {}
51 51
52 void RenderViewReady() override { 52 void RenderViewReady() override {
53 DCHECK(web_contents()->GetMainFrame()->IsRenderFrameLive()); 53 DCHECK(web_contents()->GetMainFrame()->IsRenderFrameLive());
54 observer_->DevToolsTargetReady(); 54 observer_->DevToolsTargetReady();
55 } 55 }
56 56
57 HeadlessWebContents::Observer* observer() { return observer_; }
58
57 private: 59 private:
58 HeadlessWebContents::Observer* observer_; // Not owned. 60 HeadlessWebContents::Observer* observer_; // Not owned.
59 61
60 DISALLOW_COPY_AND_ASSIGN(WebContentsObserverAdapter); 62 DISALLOW_COPY_AND_ASSIGN(WebContentsObserverAdapter);
61 }; 63 };
62 64
63 class HeadlessWebContentsImpl::Delegate : public content::WebContentsDelegate { 65 class HeadlessWebContentsImpl::Delegate : public content::WebContentsDelegate {
64 public: 66 public:
65 explicit Delegate(HeadlessBrowserContextImpl* browser_context) 67 explicit Delegate(HeadlessBrowserContextImpl* browser_context)
66 : browser_context_(browser_context) {} 68 : browser_context_(browser_context) {}
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 } 134 }
133 135
134 HeadlessWebContentsImpl::HeadlessWebContentsImpl( 136 HeadlessWebContentsImpl::HeadlessWebContentsImpl(
135 content::WebContents* web_contents, 137 content::WebContents* web_contents,
136 HeadlessBrowserContextImpl* browser_context) 138 HeadlessBrowserContextImpl* browser_context)
137 : content::WebContentsObserver(web_contents), 139 : content::WebContentsObserver(web_contents),
138 web_contents_delegate_( 140 web_contents_delegate_(
139 new HeadlessWebContentsImpl::Delegate(browser_context)), 141 new HeadlessWebContentsImpl::Delegate(browser_context)),
140 web_contents_(web_contents), 142 web_contents_(web_contents),
141 agent_host_(content::DevToolsAgentHost::GetOrCreateFor(web_contents)), 143 agent_host_(content::DevToolsAgentHost::GetOrCreateFor(web_contents)),
142 browser_context_(browser_context) { 144 browser_context_(browser_context),
145 render_process_host_(web_contents->GetRenderProcessHost()) {
143 web_contents_->SetDelegate(web_contents_delegate_.get()); 146 web_contents_->SetDelegate(web_contents_delegate_.get());
147 render_process_host_->AddObserver(this);
144 } 148 }
145 149
146 HeadlessWebContentsImpl::~HeadlessWebContentsImpl() { 150 HeadlessWebContentsImpl::~HeadlessWebContentsImpl() {
147 web_contents_->Close(); 151 web_contents_->Close();
152 if (render_process_host_)
153 render_process_host_->RemoveObserver(this);
148 } 154 }
149 155
150 void HeadlessWebContentsImpl::RenderFrameCreated( 156 void HeadlessWebContentsImpl::RenderFrameCreated(
151 content::RenderFrameHost* render_frame_host) { 157 content::RenderFrameHost* render_frame_host) {
152 if (!mojo_services_.empty()) { 158 if (!mojo_services_.empty()) {
153 render_frame_host->GetRenderViewHost()->AllowBindings( 159 render_frame_host->GetRenderViewHost()->AllowBindings(
154 content::BINDINGS_POLICY_HEADLESS); 160 content::BINDINGS_POLICY_HEADLESS);
155 } 161 }
156 162
157 service_manager::InterfaceRegistry* interface_registry = 163 service_manager::InterfaceRegistry* interface_registry =
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 observer_map_[observer] = base::MakeUnique<WebContentsObserverAdapter>( 195 observer_map_[observer] = base::MakeUnique<WebContentsObserverAdapter>(
190 web_contents_.get(), observer); 196 web_contents_.get(), observer);
191 } 197 }
192 198
193 void HeadlessWebContentsImpl::RemoveObserver(Observer* observer) { 199 void HeadlessWebContentsImpl::RemoveObserver(Observer* observer) {
194 ObserverMap::iterator it = observer_map_.find(observer); 200 ObserverMap::iterator it = observer_map_.find(observer);
195 DCHECK(it != observer_map_.end()); 201 DCHECK(it != observer_map_.end());
196 observer_map_.erase(it); 202 observer_map_.erase(it);
197 } 203 }
198 204
205 void HeadlessWebContentsImpl::RenderProcessExited(
206 content::RenderProcessHost* host,
207 base::TerminationStatus status,
208 int exit_code) {
209 DCHECK_EQ(render_process_host_, host);
210 for (const auto& pair : observer_map_) {
211 pair.second->observer()->RenderProcessExited(status, exit_code);
212 }
213 }
214
215 void HeadlessWebContentsImpl::RenderProcessHostDestroyed(
216 content::RenderProcessHost* host) {
217 DCHECK_EQ(render_process_host_, host);
218 render_process_host_ = nullptr;
219 }
220
199 HeadlessDevToolsTarget* HeadlessWebContentsImpl::GetDevToolsTarget() { 221 HeadlessDevToolsTarget* HeadlessWebContentsImpl::GetDevToolsTarget() {
200 return web_contents()->GetMainFrame()->IsRenderFrameLive() ? this : nullptr; 222 return web_contents()->GetMainFrame()->IsRenderFrameLive() ? this : nullptr;
201 } 223 }
202 224
203 void HeadlessWebContentsImpl::AttachClient(HeadlessDevToolsClient* client) { 225 void HeadlessWebContentsImpl::AttachClient(HeadlessDevToolsClient* client) {
204 HeadlessDevToolsClientImpl::From(client)->AttachToHost(agent_host_.get()); 226 HeadlessDevToolsClientImpl::From(client)->AttachToHost(agent_host_.get());
205 } 227 }
206 228
207 void HeadlessWebContentsImpl::DetachClient(HeadlessDevToolsClient* client) { 229 void HeadlessWebContentsImpl::DetachClient(HeadlessDevToolsClient* client) {
208 DCHECK(agent_host_); 230 DCHECK(agent_host_);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 HeadlessWebContents::Builder::MojoService::MojoService() {} 279 HeadlessWebContents::Builder::MojoService::MojoService() {}
258 280
259 HeadlessWebContents::Builder::MojoService::MojoService( 281 HeadlessWebContents::Builder::MojoService::MojoService(
260 const std::string& service_name, 282 const std::string& service_name,
261 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& service_factory) 283 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& service_factory)
262 : service_name(service_name), service_factory(service_factory) {} 284 : service_name(service_name), service_factory(service_factory) {}
263 285
264 HeadlessWebContents::Builder::MojoService::~MojoService() {} 286 HeadlessWebContents::Builder::MojoService::~MojoService() {}
265 287
266 } // namespace headless 288 } // namespace headless
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698