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

Side by Side Diff: content/browser/devtools/protocol/target_handler.cc

Issue 2408133004: [DevTools] Implement Target.setDiscoverTargets method. (Closed)
Patch Set: new fancy range-based iteration in observer list! Created 4 years, 2 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/protocol/target_handler.h" 5 #include "content/browser/devtools/protocol/target_handler.h"
6 6
7 #include "content/browser/devtools/service_worker_devtools_agent_host.h" 7 #include "content/browser/devtools/service_worker_devtools_agent_host.h"
8 #include "content/browser/frame_host/frame_tree.h" 8 #include "content/browser/frame_host/frame_tree.h"
9 #include "content/browser/frame_host/frame_tree_node.h" 9 #include "content/browser/frame_host/frame_tree_node.h"
10 #include "content/browser/frame_host/render_frame_host_impl.h" 10 #include "content/browser/frame_host/render_frame_host_impl.h"
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 168
169 void TargetHandler::ReattachTargetsOfType( 169 void TargetHandler::ReattachTargetsOfType(
170 const HostsMap& new_hosts, 170 const HostsMap& new_hosts,
171 const std::string& type, 171 const std::string& type,
172 bool waiting_for_debugger) { 172 bool waiting_for_debugger) {
173 HostsMap old_hosts = attached_hosts_; 173 HostsMap old_hosts = attached_hosts_;
174 for (const auto& pair : old_hosts) { 174 for (const auto& pair : old_hosts) {
175 if (pair.second->GetType() == type && 175 if (pair.second->GetType() == type &&
176 new_hosts.find(pair.first) == new_hosts.end()) { 176 new_hosts.find(pair.first) == new_hosts.end()) {
177 DetachFromTargetInternal(pair.second.get()); 177 DetachFromTargetInternal(pair.second.get());
178 TargetRemovedInternal(pair.second.get()); 178 TargetDestroyedInternal(pair.second.get(), true);
179 } 179 }
180 } 180 }
181 for (const auto& pair : new_hosts) { 181 for (const auto& pair : new_hosts) {
182 if (old_hosts.find(pair.first) == old_hosts.end()) { 182 if (old_hosts.find(pair.first) == old_hosts.end()) {
183 TargetCreatedInternal(pair.second.get()); 183 TargetCreatedInternal(pair.second.get());
184 AttachToTargetInternal(pair.second.get(), waiting_for_debugger); 184 AttachToTargetInternal(pair.second.get(), waiting_for_debugger);
185 } 185 }
186 } 186 }
187 } 187 }
188 188
189 void TargetHandler::TargetCreatedInternal(DevToolsAgentHost* host) { 189 void TargetHandler::TargetCreatedInternal(DevToolsAgentHost* host) {
190 if (reported_hosts_.find(host->GetId()) != reported_hosts_.end())
191 return;
190 client_->TargetCreated( 192 client_->TargetCreated(
191 TargetCreatedParams::Create()->set_target_info( 193 TargetCreatedParams::Create()->set_target_info(
192 TargetInfo::Create()->set_target_id(host->GetId()) 194 TargetInfo::Create()->set_target_id(host->GetId())
193 ->set_title(host->GetTitle()) 195 ->set_title(host->GetTitle())
194 ->set_url(host->GetURL().spec()) 196 ->set_url(host->GetURL().spec())
195 ->set_type(host->GetType()))); 197 ->set_type(host->GetType())));
198 reported_hosts_[host->GetId()] = host;
196 } 199 }
197 200
198 void TargetHandler::TargetRemovedInternal(DevToolsAgentHost* host) { 201 void TargetHandler::TargetDestroyedInternal(
199 client_->TargetRemoved(TargetRemovedParams::Create() 202 DevToolsAgentHost* host,
203 bool keep_for_discovery) {
204 if (discover_ && keep_for_discovery)
205 return;
206 auto it = reported_hosts_.find(host->GetId());
207 if (it == reported_hosts_.end())
208 return;
209 client_->TargetDestroyed(TargetDestroyedParams::Create()
200 ->set_target_id(host->GetId())); 210 ->set_target_id(host->GetId()));
211 reported_hosts_.erase(it);
201 } 212 }
202 213
203 bool TargetHandler::AttachToTargetInternal( 214 bool TargetHandler::AttachToTargetInternal(
204 DevToolsAgentHost* host, bool waiting_for_debugger) { 215 DevToolsAgentHost* host, bool waiting_for_debugger) {
205 if (!host->AttachClient(this)) 216 if (!host->AttachClient(this))
206 return false; 217 return false;
207 attached_hosts_[host->GetId()] = host; 218 attached_hosts_[host->GetId()] = host;
208 client_->AttachedToTarget(AttachedToTargetParams::Create() 219 client_->AttachedToTarget(AttachedToTargetParams::Create()
209 ->set_target_id(host->GetId()) 220 ->set_target_id(host->GetId())
210 ->set_waiting_for_debugger(waiting_for_debugger)); 221 ->set_waiting_for_debugger(waiting_for_debugger));
211 return true; 222 return true;
212 } 223 }
213 224
214 void TargetHandler::DetachFromTargetInternal(DevToolsAgentHost* host) { 225 void TargetHandler::DetachFromTargetInternal(DevToolsAgentHost* host) {
215 auto it = attached_hosts_.find(host->GetId()); 226 auto it = attached_hosts_.find(host->GetId());
216 if (it == attached_hosts_.end()) 227 if (it == attached_hosts_.end())
217 return; 228 return;
218 host->DetachClient(this); 229 host->DetachClient(this);
219 client_->DetachedFromTarget(DetachedFromTargetParams::Create()-> 230 client_->DetachedFromTarget(DetachedFromTargetParams::Create()->
220 set_target_id(host->GetId())); 231 set_target_id(host->GetId()));
221 attached_hosts_.erase(it); 232 attached_hosts_.erase(it);
222 } 233 }
223 234
224 // ----------------- Protocol ---------------------- 235 // ----------------- Protocol ----------------------
225 236
226 Response TargetHandler::SetDiscoverTargets(bool discover) { 237 Response TargetHandler::SetDiscoverTargets(bool discover) {
227 if (discover_ == discover) 238 if (discover_ == discover)
228 return Response::OK(); 239 return Response::OK();
229 discover_ = discover; 240 discover_ = discover;
230 // TODO(dgozman): observe all agent hosts here. 241 if (discover_) {
242 DevToolsManager::GetInstance()->AddObserver(this);
243 } else {
244 DevToolsManager::GetInstance()->RemoveObserver(this);
245 RawHostsMap copy = reported_hosts_;
246 for (const auto& id_host : copy) {
247 if (attached_hosts_.find(id_host.first) == attached_hosts_.end())
248 TargetDestroyedInternal(id_host.second, false);
249 }
250 }
231 return Response::OK(); 251 return Response::OK();
232 } 252 }
233 253
234 Response TargetHandler::SetAutoAttach( 254 Response TargetHandler::SetAutoAttach(
235 bool auto_attach, bool wait_for_debugger_on_start) { 255 bool auto_attach, bool wait_for_debugger_on_start) {
236 wait_for_debugger_on_start_ = wait_for_debugger_on_start; 256 wait_for_debugger_on_start_ = wait_for_debugger_on_start;
237 if (auto_attach_ == auto_attach) 257 if (auto_attach_ == auto_attach)
238 return Response::OK(); 258 return Response::OK();
239 auto_attach_ = auto_attach; 259 auto_attach_ = auto_attach;
240 if (auto_attach_) { 260 if (auto_attach_) {
(...skipping 17 matching lines...) Expand all
258 UpdateFrames(); 278 UpdateFrames();
259 } else { 279 } else {
260 HostsMap empty; 280 HostsMap empty;
261 ReattachTargetsOfType(empty, DevToolsAgentHost::kTypeFrame, false); 281 ReattachTargetsOfType(empty, DevToolsAgentHost::kTypeFrame, false);
262 } 282 }
263 return Response::OK(); 283 return Response::OK();
264 } 284 }
265 285
266 Response TargetHandler::AttachToTarget(const std::string& target_id, 286 Response TargetHandler::AttachToTarget(const std::string& target_id,
267 bool* out_success) { 287 bool* out_success) {
268 scoped_refptr<DevToolsAgentHost> agent_host( 288 auto it = reported_hosts_.find(target_id);
269 DevToolsAgentHost::GetForId(target_id)); 289 if (it == reported_hosts_.end())
270 if (!agent_host)
271 return Response::InvalidParams("No target with such id"); 290 return Response::InvalidParams("No target with such id");
272 *out_success = AttachToTargetInternal(agent_host.get(), false); 291 *out_success = AttachToTargetInternal(it->second, false);
273 return Response::OK(); 292 return Response::OK();
274 } 293 }
275 294
276 Response TargetHandler::DetachFromTarget(const std::string& target_id) { 295 Response TargetHandler::DetachFromTarget(const std::string& target_id) {
277 auto it = attached_hosts_.find(target_id); 296 auto it = attached_hosts_.find(target_id);
278 if (it == attached_hosts_.end()) 297 if (it == attached_hosts_.end())
279 return Response::InternalError("Not attached to the target"); 298 return Response::InternalError("Not attached to the target");
280 DetachFromTargetInternal(it->second.get()); 299 DevToolsAgentHost* agent_host = it->second.get();
300 DetachFromTargetInternal(agent_host);
301 TargetDestroyedInternal(agent_host, true);
281 return Response::OK(); 302 return Response::OK();
282 } 303 }
283 304
284 Response TargetHandler::SendMessageToTarget( 305 Response TargetHandler::SendMessageToTarget(
285 const std::string& target_id, 306 const std::string& target_id,
286 const std::string& message) { 307 const std::string& message) {
287 auto it = attached_hosts_.find(target_id); 308 auto it = attached_hosts_.find(target_id);
288 if (it == attached_hosts_.end()) 309 if (it == attached_hosts_.end())
289 return Response::InternalError("Not attached to the target"); 310 return Response::InternalError("Not attached to the target");
290 it->second->DispatchProtocolMessage(this, message); 311 it->second->DispatchProtocolMessage(this, message);
291 return Response::OK(); 312 return Response::OK();
292 } 313 }
293 314
294 Response TargetHandler::GetTargetInfo( 315 Response TargetHandler::GetTargetInfo(
295 const std::string& target_id, 316 const std::string& target_id,
296 scoped_refptr<TargetInfo>* target_info) { 317 scoped_refptr<TargetInfo>* target_info) {
318 // TODO(dgozman): only allow reported hosts.
297 scoped_refptr<DevToolsAgentHost> agent_host( 319 scoped_refptr<DevToolsAgentHost> agent_host(
298 DevToolsAgentHost::GetForId(target_id)); 320 DevToolsAgentHost::GetForId(target_id));
299 if (!agent_host) 321 if (!agent_host)
300 return Response::InvalidParams("No target with such id"); 322 return Response::InvalidParams("No target with such id");
301 *target_info = TargetInfo::Create() 323 *target_info = TargetInfo::Create()
302 ->set_target_id(agent_host->GetId()) 324 ->set_target_id(agent_host->GetId())
303 ->set_type(agent_host->GetType()) 325 ->set_type(agent_host->GetType())
304 ->set_title(agent_host->GetTitle()) 326 ->set_title(agent_host->GetTitle())
305 ->set_url(agent_host->GetURL().spec()); 327 ->set_url(agent_host->GetURL().spec());
306 return Response::OK(); 328 return Response::OK();
307 } 329 }
308 330
309 Response TargetHandler::ActivateTarget(const std::string& target_id) { 331 Response TargetHandler::ActivateTarget(const std::string& target_id) {
332 // TODO(dgozman): only allow reported hosts.
310 scoped_refptr<DevToolsAgentHost> agent_host( 333 scoped_refptr<DevToolsAgentHost> agent_host(
311 DevToolsAgentHost::GetForId(target_id)); 334 DevToolsAgentHost::GetForId(target_id));
312 if (!agent_host) 335 if (!agent_host)
313 return Response::InvalidParams("No target with such id"); 336 return Response::InvalidParams("No target with such id");
314 agent_host->Activate(); 337 agent_host->Activate();
315 return Response::OK(); 338 return Response::OK();
316 } 339 }
317 340
318 // ---------------- DevToolsAgentHostClient ---------------- 341 // ---------------- DevToolsAgentHostClient ----------------
319 342
320 void TargetHandler::DispatchProtocolMessage( 343 void TargetHandler::DispatchProtocolMessage(
321 DevToolsAgentHost* host, 344 DevToolsAgentHost* host,
322 const std::string& message) { 345 const std::string& message) {
323 auto it = attached_hosts_.find(host->GetId()); 346 auto it = attached_hosts_.find(host->GetId());
324 if (it == attached_hosts_.end()) 347 if (it == attached_hosts_.end())
325 return; // Already disconnected. 348 return; // Already disconnected.
326 349
327 client_->ReceivedMessageFromTarget( 350 client_->ReceivedMessageFromTarget(
328 ReceivedMessageFromTargetParams::Create()-> 351 ReceivedMessageFromTargetParams::Create()->
329 set_target_id(host->GetId())-> 352 set_target_id(host->GetId())->
330 set_message(message)); 353 set_message(message));
331 } 354 }
332 355
333 void TargetHandler::AgentHostClosed( 356 void TargetHandler::AgentHostClosed(
334 DevToolsAgentHost* host, 357 DevToolsAgentHost* host,
335 bool replaced_with_another_client) { 358 bool replaced_with_another_client) {
336 client_->DetachedFromTarget(DetachedFromTargetParams::Create()-> 359 client_->DetachedFromTarget(DetachedFromTargetParams::Create()->
337 set_target_id(host->GetId())); 360 set_target_id(host->GetId()));
338 attached_hosts_.erase(host->GetId()); 361 attached_hosts_.erase(host->GetId());
339 TargetRemovedInternal(host); 362 TargetDestroyedInternal(host, false);
363 }
364
365 // -------------- DevToolsManager::Observer -----------------
366
367 void TargetHandler::AgentHostCreated(DevToolsAgentHostImpl* agent_host) {
368 DCHECK(attached_hosts_.find(agent_host->GetId()) == attached_hosts_.end());
369 TargetCreatedInternal(agent_host);
370 }
371
372 void TargetHandler::AgentHostDestroyed(DevToolsAgentHostImpl* agent_host) {
373 DCHECK(attached_hosts_.find(agent_host->GetId()) == attached_hosts_.end());
374 TargetDestroyedInternal(agent_host, false);
340 } 375 }
341 376
342 // -------- ServiceWorkerDevToolsManager::Observer ---------- 377 // -------- ServiceWorkerDevToolsManager::Observer ----------
343 378
344 void TargetHandler::WorkerCreated( 379 void TargetHandler::WorkerCreated(
345 ServiceWorkerDevToolsAgentHost* host) { 380 ServiceWorkerDevToolsAgentHost* host) {
346 BrowserContext* browser_context = nullptr; 381 BrowserContext* browser_context = nullptr;
347 if (render_frame_host_) 382 if (render_frame_host_)
348 browser_context = render_frame_host_->GetProcess()->GetBrowserContext(); 383 browser_context = render_frame_host_->GetProcess()->GetBrowserContext();
349 auto hosts = GetMatchingServiceWorkers(browser_context, frame_urls_); 384 auto hosts = GetMatchingServiceWorkers(browser_context, frame_urls_);
(...skipping 25 matching lines...) Expand all
375 } 410 }
376 411
377 void TargetHandler::WorkerDestroyed( 412 void TargetHandler::WorkerDestroyed(
378 ServiceWorkerDevToolsAgentHost* host) { 413 ServiceWorkerDevToolsAgentHost* host) {
379 UpdateServiceWorkers(); 414 UpdateServiceWorkers();
380 } 415 }
381 416
382 } // namespace target 417 } // namespace target
383 } // namespace devtools 418 } // namespace devtools
384 } // namespace content 419 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698