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

Side by Side Diff: content/browser/devtools/render_view_devtools_agent_host.cc

Issue 442303002: DevTools: migrate DevTools APIs to use WebContents instead of RenderViewHost. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review comments addressed. Created 6 years, 4 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "content/browser/devtools/render_view_devtools_agent_host.h" 5 #include "content/browser/devtools/render_view_devtools_agent_host.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/lazy_instance.h" 8 #include "base/lazy_instance.h"
9 #include "content/browser/child_process_security_policy_impl.h" 9 #include "content/browser/child_process_security_policy_impl.h"
10 #include "content/browser/devtools/devtools_manager_impl.h" 10 #include "content/browser/devtools/devtools_manager_impl.h"
(...skipping 28 matching lines...) Expand all
39 39
40 //Returns RenderViewDevToolsAgentHost attached to any of RenderViewHost 40 //Returns RenderViewDevToolsAgentHost attached to any of RenderViewHost
41 //instances associated with |web_contents| 41 //instances associated with |web_contents|
42 static RenderViewDevToolsAgentHost* FindAgentHost(WebContents* web_contents) { 42 static RenderViewDevToolsAgentHost* FindAgentHost(WebContents* web_contents) {
43 if (g_instances == NULL) 43 if (g_instances == NULL)
44 return NULL; 44 return NULL;
45 RenderViewHostDelegate* delegate = 45 RenderViewHostDelegate* delegate =
46 static_cast<WebContentsImpl*>(web_contents); 46 static_cast<WebContentsImpl*>(web_contents);
47 for (Instances::iterator it = g_instances.Get().begin(); 47 for (Instances::iterator it = g_instances.Get().begin();
48 it != g_instances.Get().end(); ++it) { 48 it != g_instances.Get().end(); ++it) {
49 RenderViewHost* rvh = (*it)->render_view_host(); 49 RenderViewHost* rvh = (*it)->render_view_host();
dgozman 2014/08/06 19:22:20 Shouldn't this just check web contents? (*it)->Ge
pfeldman 2014/08/07 09:03:09 Done.
50 if (rvh && rvh->GetDelegate() == delegate) 50 if (rvh && rvh->GetDelegate() == delegate)
51 return *it; 51 return *it;
52 } 52 }
53 return NULL; 53 return NULL;
54 } 54 }
55 55
56 static RenderViewDevToolsAgentHost* FindAgentHost(RenderViewHost* rvh) {
57 if (g_instances == NULL)
58 return NULL;
59 for (Instances::iterator it = g_instances.Get().begin();
60 it != g_instances.Get().end(); ++it) {
61 if (rvh == (*it)->render_view_host())
62 return *it;
63 }
64 return NULL;
65 }
66
67 } // namespace 56 } // namespace
68 57
69 scoped_refptr<DevToolsAgentHost> 58 scoped_refptr<DevToolsAgentHost>
70 DevToolsAgentHost::GetOrCreateFor(WebContents* web_contents) { 59 DevToolsAgentHost::GetOrCreateFor(WebContents* web_contents) {
71 RenderViewDevToolsAgentHost* result = FindAgentHost(web_contents); 60 RenderViewDevToolsAgentHost* result = FindAgentHost(web_contents);
72 if (!result) 61 if (!result)
73 result = new RenderViewDevToolsAgentHost(web_contents->GetRenderViewHost()); 62 result = new RenderViewDevToolsAgentHost(web_contents->GetRenderViewHost());
74 return result; 63 return result;
75 } 64 }
76 65
77 // static 66 // static
78 scoped_refptr<DevToolsAgentHost> 67 bool DevToolsAgentHost::HasFor(WebContents* web_contents) {
79 DevToolsAgentHost::GetOrCreateFor(RenderViewHost* rvh) { 68 return FindAgentHost(web_contents) != NULL;
80 RenderViewDevToolsAgentHost* result = FindAgentHost(rvh);
81 if (!result)
82 result = new RenderViewDevToolsAgentHost(rvh);
83 return result;
84 }
85
86 // static
87 bool DevToolsAgentHost::HasFor(RenderViewHost* rvh) {
88 return FindAgentHost(rvh) != NULL;
89 } 69 }
90 70
91 // static 71 // static
92 bool DevToolsAgentHost::IsDebuggerAttached(WebContents* web_contents) { 72 bool DevToolsAgentHost::IsDebuggerAttached(WebContents* web_contents) {
93 if (g_instances == NULL) 73 RenderViewDevToolsAgentHost* agent_host = FindAgentHost(web_contents);
94 return false; 74 return agent_host && agent_host->IsAttached();
95 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
96 if (!devtools_manager)
97 return false;
98 RenderViewHostDelegate* delegate =
99 static_cast<WebContentsImpl*>(web_contents);
100 for (Instances::iterator it = g_instances.Get().begin();
101 it != g_instances.Get().end(); ++it) {
102 RenderViewHost* rvh = (*it)->render_view_host_;
103 if (rvh && rvh->GetDelegate() != delegate)
104 continue;
105 if ((*it)->IsAttached())
106 return true;
107 }
108 return false;
109 } 75 }
110 76
111 //static 77 //static
112 std::vector<RenderViewHost*> DevToolsAgentHost::GetValidRenderViewHosts() { 78 std::vector<WebContents*> DevToolsAgentHost::GetInspectableWebContents() {
113 std::vector<RenderViewHost*> result; 79 std::vector<WebContents*> result;
114 scoped_ptr<RenderWidgetHostIterator> widgets( 80 scoped_ptr<RenderWidgetHostIterator> widgets(
115 RenderWidgetHost::GetRenderWidgetHosts()); 81 RenderWidgetHost::GetRenderWidgetHosts());
116 while (RenderWidgetHost* widget = widgets->GetNextHost()) { 82 while (RenderWidgetHost* widget = widgets->GetNextHost()) {
117 // Ignore processes that don't have a connection, such as crashed contents. 83 // Ignore processes that don't have a connection, such as crashed contents.
118 if (!widget->GetProcess()->HasConnection()) 84 if (!widget->GetProcess()->HasConnection())
119 continue; 85 continue;
120 if (!widget->IsRenderView()) 86 if (!widget->IsRenderView())
121 continue; 87 continue;
122 88
123 RenderViewHost* rvh = RenderViewHost::From(widget); 89 RenderViewHost* rvh = RenderViewHost::From(widget);
124 WebContents* web_contents = WebContents::FromRenderViewHost(rvh); 90 WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
125 if (!web_contents) 91 if (web_contents)
126 continue; 92 result.push_back(web_contents);
dgozman 2014/08/06 19:22:20 You will get duplicates here for prerender and nav
pfeldman 2014/08/07 09:03:09 Done.
127
128 // Don't report a RenderViewHost if it is not the current RenderViewHost
129 // for some WebContents (this filters out pre-render RVHs and similar).
130 // However report a RenderViewHost created for an out of process iframe.
131 // TODO (kaznacheev): Revisit this when it is clear how OOP iframes
132 // interact with pre-rendering.
133 // TODO (kaznacheev): GetMainFrame() call is a temporary hack. Iterate over
134 // all RenderFrameHost instances when multiple OOP frames are supported.
135 if (rvh != web_contents->GetRenderViewHost() &&
136 !rvh->GetMainFrame()->IsCrossProcessSubframe()) {
137 continue;
138 }
139
140 result.push_back(rvh);
141 } 93 }
142 return result; 94 return result;
143 } 95 }
144 96
145 // static 97 // static
146 void RenderViewDevToolsAgentHost::OnCancelPendingNavigation( 98 void RenderViewDevToolsAgentHost::OnCancelPendingNavigation(
147 RenderViewHost* pending, 99 RenderViewHost* pending,
148 RenderViewHost* current) { 100 RenderViewHost* current) {
149 RenderViewDevToolsAgentHost* agent_host = FindAgentHost(pending); 101 WebContents* web_contents = WebContents::FromRenderViewHost(pending);
102 RenderViewDevToolsAgentHost* agent_host = FindAgentHost(web_contents);
150 if (!agent_host) 103 if (!agent_host)
151 return; 104 return;
152 agent_host->DisconnectRenderViewHost(); 105 agent_host->DisconnectRenderViewHost();
153 agent_host->ConnectRenderViewHost(current); 106 agent_host->ConnectRenderViewHost(current);
154 } 107 }
155 108
156 // static
157 bool RenderViewDevToolsAgentHost::DispatchIPCMessage(
158 RenderViewHost* source,
159 const IPC::Message& message) {
160 RenderViewDevToolsAgentHost* agent_host = FindAgentHost(source);
161 return agent_host && agent_host->DispatchIPCMessage(message);
162 }
163
164 RenderViewDevToolsAgentHost::RenderViewDevToolsAgentHost(RenderViewHost* rvh) 109 RenderViewDevToolsAgentHost::RenderViewDevToolsAgentHost(RenderViewHost* rvh)
165 : render_view_host_(NULL), 110 : render_view_host_(NULL),
166 overrides_handler_(new RendererOverridesHandler(this)), 111 overrides_handler_(new RendererOverridesHandler(this)),
167 tracing_handler_( 112 tracing_handler_(
168 new DevToolsTracingHandler(DevToolsTracingHandler::Renderer)), 113 new DevToolsTracingHandler(DevToolsTracingHandler::Renderer)),
169 power_handler_(new DevToolsPowerHandler()), 114 power_handler_(new DevToolsPowerHandler()),
170 reattaching_(false) { 115 reattaching_(false) {
171 SetRenderViewHost(rvh); 116 SetRenderViewHost(rvh);
172 DevToolsProtocol::Notifier notifier(base::Bind( 117 DevToolsProtocol::Notifier notifier(base::Bind(
173 &RenderViewDevToolsAgentHost::OnDispatchOnInspectorFrontend, 118 &RenderViewDevToolsAgentHost::OnDispatchOnInspectorFrontend,
174 base::Unretained(this))); 119 base::Unretained(this)));
175 overrides_handler_->SetNotifier(notifier); 120 overrides_handler_->SetNotifier(notifier);
176 tracing_handler_->SetNotifier(notifier); 121 tracing_handler_->SetNotifier(notifier);
177 power_handler_->SetNotifier(notifier); 122 power_handler_->SetNotifier(notifier);
178 g_instances.Get().push_back(this); 123 g_instances.Get().push_back(this);
179 AddRef(); // Balanced in RenderViewHostDestroyed. 124 AddRef(); // Balanced in RenderViewHostDestroyed.
180 } 125 }
181 126
182 RenderViewHost* RenderViewDevToolsAgentHost::GetRenderViewHost() { 127 WebContents* RenderViewDevToolsAgentHost::GetWebContents() {
183 return render_view_host_; 128 return web_contents();
184 } 129 }
185 130
186 void RenderViewDevToolsAgentHost::DispatchOnInspectorBackend( 131 void RenderViewDevToolsAgentHost::DispatchOnInspectorBackend(
187 const std::string& message) { 132 const std::string& message) {
188 std::string error_message; 133 std::string error_message;
189 134
190 scoped_ptr<base::DictionaryValue> message_dict( 135 scoped_ptr<base::DictionaryValue> message_dict(
191 DevToolsProtocol::ParseMessage(message, &error_message)); 136 DevToolsProtocol::ParseMessage(message, &error_message));
192 scoped_refptr<DevToolsProtocol::Command> command = 137 scoped_refptr<DevToolsProtocol::Command> command =
193 DevToolsProtocol::ParseCommand(message_dict.get(), &error_message); 138 DevToolsProtocol::ParseCommand(message_dict.get(), &error_message);
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 #if defined(OS_ANDROID) 301 #if defined(OS_ANDROID)
357 case base::TERMINATION_STATUS_OOM_PROTECTED: 302 case base::TERMINATION_STATUS_OOM_PROTECTED:
358 #endif 303 #endif
359 RenderViewCrashed(); 304 RenderViewCrashed();
360 break; 305 break;
361 default: 306 default:
362 break; 307 break;
363 } 308 }
364 } 309 }
365 310
311 bool RenderViewDevToolsAgentHost::OnMessageReceived(
312 const IPC::Message& message,
313 RenderFrameHost* render_frame_host) {
314 return DispatchIPCMessage(message);
315 }
316
317 bool RenderViewDevToolsAgentHost::OnMessageReceived(
318 const IPC::Message& message) {
319 return DispatchIPCMessage(message);
320 }
321
366 void RenderViewDevToolsAgentHost::DidAttachInterstitialPage() { 322 void RenderViewDevToolsAgentHost::DidAttachInterstitialPage() {
367 if (!render_view_host_) 323 if (!render_view_host_)
368 return; 324 return;
369 // The rvh set in AboutToNavigateRenderView turned out to be interstitial. 325 // The rvh set in AboutToNavigateRenderView turned out to be interstitial.
370 // Connect back to the real one. 326 // Connect back to the real one.
371 WebContents* web_contents = 327 WebContents* web_contents =
372 WebContents::FromRenderViewHost(render_view_host_); 328 WebContents::FromRenderViewHost(render_view_host_);
373 if (!web_contents) 329 if (!web_contents)
374 return; 330 return;
375 DisconnectRenderViewHost(); 331 DisconnectRenderViewHost();
(...skipping 24 matching lines...) Expand all
400 356
401 void RenderViewDevToolsAgentHost::ClearRenderViewHost() { 357 void RenderViewDevToolsAgentHost::ClearRenderViewHost() {
402 DCHECK(render_view_host_); 358 DCHECK(render_view_host_);
403 registrar_.Remove( 359 registrar_.Remove(
404 this, 360 this,
405 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, 361 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
406 content::Source<RenderWidgetHost>(render_view_host_)); 362 content::Source<RenderWidgetHost>(render_view_host_));
407 render_view_host_ = NULL; 363 render_view_host_ = NULL;
408 } 364 }
409 365
366 void RenderViewDevToolsAgentHost::DisconnectWebContents() {
367 DisconnectRenderViewHost();
368 }
369
370 void RenderViewDevToolsAgentHost::ConnectWebContents(WebContents* wc) {
371 ConnectRenderViewHost(wc->GetRenderViewHost());
372 }
373
410 void RenderViewDevToolsAgentHost::ConnectRenderViewHost(RenderViewHost* rvh) { 374 void RenderViewDevToolsAgentHost::ConnectRenderViewHost(RenderViewHost* rvh) {
411 SetRenderViewHost(rvh); 375 SetRenderViewHost(rvh);
412 if (IsAttached()) 376 if (IsAttached())
413 Reattach(state_); 377 Reattach(state_);
414 } 378 }
415 379
416 void RenderViewDevToolsAgentHost::DisconnectRenderViewHost() { 380 void RenderViewDevToolsAgentHost::DisconnectRenderViewHost() {
417 ClientDetachedFromRenderer(); 381 ClientDetachedFromRenderer();
418 ClearRenderViewHost(); 382 ClearRenderViewHost();
419 } 383 }
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
482 DevToolsProtocol::ParseNotification(message); 446 DevToolsProtocol::ParseNotification(message);
483 447
484 if (notification) { 448 if (notification) {
485 tracing_handler_->HandleNotification(notification); 449 tracing_handler_->HandleNotification(notification);
486 } 450 }
487 DevToolsManagerImpl::GetInstance()->DispatchOnInspectorFrontend( 451 DevToolsManagerImpl::GetInstance()->DispatchOnInspectorFrontend(
488 this, message); 452 this, message);
489 } 453 }
490 454
491 } // namespace content 455 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698