| OLD | NEW |
| 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/devtools_agent_host.h" |
| 11 #include "content/public/browser/navigation_handle.h" | 12 #include "content/public/browser/navigation_handle.h" |
| 12 #include "content/public/browser/render_frame_host.h" | 13 #include "content/public/browser/render_frame_host.h" |
| 13 #include "content/public/browser/render_process_host.h" | 14 #include "content/public/browser/render_process_host.h" |
| 14 #include "content/public/browser/render_view_host.h" | 15 #include "content/public/browser/render_view_host.h" |
| 15 #include "content/public/browser/render_widget_host_view.h" | 16 #include "content/public/browser/render_widget_host_view.h" |
| 16 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/browser/web_contents_delegate.h" | 18 #include "content/public/browser/web_contents_delegate.h" |
| 18 #include "content/public/browser/web_contents_observer.h" | 19 #include "content/public/browser/web_contents_observer.h" |
| 19 #include "content/public/common/bindings_policy.h" | 20 #include "content/public/common/bindings_policy.h" |
| 20 #include "content/public/common/service_registry.h" | 21 #include "content/public/common/service_registry.h" |
| 21 #include "content/public/renderer/render_frame.h" | 22 #include "content/public/renderer/render_frame.h" |
| 23 #include "headless/lib/browser/headless_devtools_client_impl.h" |
| 22 #include "ui/aura/window.h" | 24 #include "ui/aura/window.h" |
| 23 | 25 |
| 24 namespace headless { | 26 namespace headless { |
| 25 | 27 |
| 26 class WebContentsObserverAdapter : public content::WebContentsObserver { | 28 class WebContentsObserverAdapter : public content::WebContentsObserver { |
| 27 public: | 29 public: |
| 28 WebContentsObserverAdapter(content::WebContents* web_contents, | 30 WebContentsObserverAdapter(content::WebContents* web_contents, |
| 29 HeadlessWebContents::Observer* observer) | 31 HeadlessWebContents::Observer* observer) |
| 30 : content::WebContentsObserver(web_contents), observer_(observer) {} | 32 : content::WebContentsObserver(web_contents), observer_(observer) {} |
| 31 | 33 |
| 32 ~WebContentsObserverAdapter() override {} | 34 ~WebContentsObserverAdapter() override {} |
| 33 | 35 |
| 34 void RenderViewReady() override { observer_->WebContentsReady(); } | 36 void RenderViewReady() override { |
| 37 DCHECK(web_contents()->GetMainFrame()->IsRenderFrameLive()); |
| 38 observer_->DevToolsTargetReady(); |
| 39 } |
| 35 | 40 |
| 36 void DocumentOnLoadCompletedInMainFrame() override { | 41 void DocumentOnLoadCompletedInMainFrame() override { |
| 37 observer_->DocumentOnLoadCompletedInMainFrame(); | 42 observer_->DocumentOnLoadCompletedInMainFrame(); |
| 38 } | 43 } |
| 39 | 44 |
| 40 void DidFinishNavigation( | 45 void DidFinishNavigation( |
| 41 content::NavigationHandle* navigation_handle) override { | 46 content::NavigationHandle* navigation_handle) override { |
| 42 observer_->DidFinishNavigation(navigation_handle->HasCommitted() && | 47 observer_->DidFinishNavigation(navigation_handle->HasCommitted() && |
| 43 !navigation_handle->IsErrorPage()); | 48 !navigation_handle->IsErrorPage()); |
| 44 } | 49 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 observer_map_[observer] = base::WrapUnique( | 105 observer_map_[observer] = base::WrapUnique( |
| 101 new WebContentsObserverAdapter(web_contents_.get(), observer)); | 106 new WebContentsObserverAdapter(web_contents_.get(), observer)); |
| 102 } | 107 } |
| 103 | 108 |
| 104 void HeadlessWebContentsImpl::RemoveObserver(Observer* observer) { | 109 void HeadlessWebContentsImpl::RemoveObserver(Observer* observer) { |
| 105 ObserverMap::iterator it = observer_map_.find(observer); | 110 ObserverMap::iterator it = observer_map_.find(observer); |
| 106 DCHECK(it != observer_map_.end()); | 111 DCHECK(it != observer_map_.end()); |
| 107 observer_map_.erase(it); | 112 observer_map_.erase(it); |
| 108 } | 113 } |
| 109 | 114 |
| 115 HeadlessDevToolsTarget* HeadlessWebContentsImpl::GetDevToolsTarget() { |
| 116 return web_contents()->GetMainFrame()->IsRenderFrameLive() ? this : nullptr; |
| 117 } |
| 118 |
| 119 void HeadlessWebContentsImpl::AttachClient(HeadlessDevToolsClient* client) { |
| 120 if (!agent_host_) |
| 121 agent_host_ = content::DevToolsAgentHost::GetOrCreateFor(web_contents()); |
| 122 HeadlessDevToolsClientImpl::From(client)->AttachToHost(agent_host_.get()); |
| 123 } |
| 124 |
| 125 void HeadlessWebContentsImpl::DetachClient(HeadlessDevToolsClient* client) { |
| 126 DCHECK(agent_host_); |
| 127 HeadlessDevToolsClientImpl::From(client)->DetachFromHost(agent_host_.get()); |
| 128 } |
| 129 |
| 110 content::WebContents* HeadlessWebContentsImpl::web_contents() const { | 130 content::WebContents* HeadlessWebContentsImpl::web_contents() const { |
| 111 return web_contents_.get(); | 131 return web_contents_.get(); |
| 112 } | 132 } |
| 113 | 133 |
| 114 } // namespace headless | 134 } // namespace headless |
| OLD | NEW |