OLD | NEW |
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/devtools_manager.h" | 7 #include "content/browser/devtools/devtools_manager.h" |
8 #include "content/browser/devtools/service_worker_devtools_agent_host.h" | 8 #include "content/browser/devtools/service_worker_devtools_agent_host.h" |
9 #include "content/browser/frame_host/frame_tree.h" | 9 #include "content/browser/frame_host/frame_tree.h" |
10 #include "content/browser/frame_host/frame_tree_node.h" | 10 #include "content/browser/frame_host/frame_tree_node.h" |
11 #include "content/browser/frame_host/render_frame_host_impl.h" | 11 #include "content/browser/frame_host/render_frame_host_impl.h" |
12 | 12 |
13 namespace content { | 13 namespace content { |
14 namespace devtools { | 14 namespace protocol { |
15 namespace target { | |
16 | |
17 using Response = DevToolsProtocolClient::Response; | |
18 | 15 |
19 namespace { | 16 namespace { |
20 | 17 |
21 using ScopeAgentsMap = | 18 using ScopeAgentsMap = |
22 std::map<GURL, std::unique_ptr<ServiceWorkerDevToolsAgentHost::List>>; | 19 std::map<GURL, std::unique_ptr<ServiceWorkerDevToolsAgentHost::List>>; |
23 | 20 |
24 void GetMatchingHostsByScopeMap( | 21 void GetMatchingHostsByScopeMap( |
25 const ServiceWorkerDevToolsAgentHost::List& agent_hosts, | 22 const ServiceWorkerDevToolsAgentHost::List& agent_hosts, |
26 const std::set<GURL>& urls, | 23 const std::set<GURL>& urls, |
27 ScopeAgentsMap* scope_agents_map) { | 24 ScopeAgentsMap* scope_agents_map) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 | 74 |
78 ScopeAgentsMap scope_agents_map; | 75 ScopeAgentsMap scope_agents_map; |
79 GetMatchingHostsByScopeMap(agent_hosts, urls, &scope_agents_map); | 76 GetMatchingHostsByScopeMap(agent_hosts, urls, &scope_agents_map); |
80 | 77 |
81 for (const auto& it : scope_agents_map) | 78 for (const auto& it : scope_agents_map) |
82 AddEligibleHosts(*it.second.get(), &result); | 79 AddEligibleHosts(*it.second.get(), &result); |
83 | 80 |
84 return result; | 81 return result; |
85 } | 82 } |
86 | 83 |
87 scoped_refptr<TargetInfo> CreateInfo(DevToolsAgentHost* host) { | 84 std::unique_ptr<Target::TargetInfo> CreateInfo(DevToolsAgentHost* host) { |
88 return TargetInfo::Create() | 85 return Target::TargetInfo::Create() |
89 ->set_target_id(host->GetId()) | 86 .SetTargetId(host->GetId()) |
90 ->set_title(host->GetTitle()) | 87 .SetTitle(host->GetTitle()) |
91 ->set_url(host->GetURL().spec()) | 88 .SetUrl(host->GetURL().spec()) |
92 ->set_type(host->GetType()); | 89 .SetType(host->GetType()) |
| 90 .Build(); |
93 } | 91 } |
94 | 92 |
95 } // namespace | 93 } // namespace |
96 | 94 |
97 TargetHandler::TargetHandler() | 95 TargetHandler::TargetHandler() |
98 : discover_(false), | 96 : discover_(false), |
99 auto_attach_(false), | 97 auto_attach_(false), |
100 wait_for_debugger_on_start_(false), | 98 wait_for_debugger_on_start_(false), |
101 attach_to_frames_(false), | 99 attach_to_frames_(false), |
102 render_frame_host_(nullptr) { | 100 render_frame_host_(nullptr) { |
103 } | 101 } |
104 | 102 |
105 TargetHandler::~TargetHandler() { | 103 TargetHandler::~TargetHandler() { |
106 Detached(); | 104 } |
| 105 |
| 106 void TargetHandler::Wire(UberDispatcher* dispatcher) { |
| 107 frontend_.reset(new Target::Frontend(dispatcher->channel())); |
| 108 Target::Dispatcher::wire(dispatcher, this); |
107 } | 109 } |
108 | 110 |
109 void TargetHandler::SetRenderFrameHost(RenderFrameHostImpl* render_frame_host) { | 111 void TargetHandler::SetRenderFrameHost(RenderFrameHostImpl* render_frame_host) { |
110 render_frame_host_ = render_frame_host; | 112 render_frame_host_ = render_frame_host; |
111 UpdateFrames(); | 113 UpdateFrames(); |
112 } | 114 } |
113 | 115 |
114 void TargetHandler::SetClient(std::unique_ptr<Client> client) { | 116 Response TargetHandler::Disable() { |
115 client_.swap(client); | |
116 } | |
117 | |
118 void TargetHandler::Detached() { | |
119 SetAutoAttach(false, false); | 117 SetAutoAttach(false, false); |
120 SetDiscoverTargets(false); | 118 SetDiscoverTargets(false); |
121 for (const auto& id_host : attached_hosts_) | 119 for (const auto& id_host : attached_hosts_) |
122 id_host.second->DetachClient(this); | 120 id_host.second->DetachClient(this); |
123 attached_hosts_.clear(); | 121 attached_hosts_.clear(); |
| 122 return Response::OK(); |
124 } | 123 } |
125 | 124 |
126 void TargetHandler::UpdateServiceWorkers() { | 125 void TargetHandler::UpdateServiceWorkers() { |
127 UpdateServiceWorkers(false); | 126 UpdateServiceWorkers(false); |
128 } | 127 } |
129 | 128 |
130 void TargetHandler::UpdateFrames() { | 129 void TargetHandler::UpdateFrames() { |
131 if (!auto_attach_ || !attach_to_frames_) | 130 if (!auto_attach_ || !attach_to_frames_) |
132 return; | 131 return; |
133 | 132 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 } | 190 } |
192 for (const auto& pair : new_hosts) { | 191 for (const auto& pair : new_hosts) { |
193 if (old_hosts.find(pair.first) == old_hosts.end()) | 192 if (old_hosts.find(pair.first) == old_hosts.end()) |
194 AttachToTargetInternal(pair.second.get(), waiting_for_debugger); | 193 AttachToTargetInternal(pair.second.get(), waiting_for_debugger); |
195 } | 194 } |
196 } | 195 } |
197 | 196 |
198 void TargetHandler::TargetCreatedInternal(DevToolsAgentHost* host) { | 197 void TargetHandler::TargetCreatedInternal(DevToolsAgentHost* host) { |
199 if (reported_hosts_.find(host->GetId()) != reported_hosts_.end()) | 198 if (reported_hosts_.find(host->GetId()) != reported_hosts_.end()) |
200 return; | 199 return; |
201 client_->TargetCreated( | 200 frontend_->TargetCreated(CreateInfo(host)); |
202 TargetCreatedParams::Create()->set_target_info(CreateInfo(host))); | |
203 reported_hosts_[host->GetId()] = host; | 201 reported_hosts_[host->GetId()] = host; |
204 } | 202 } |
205 | 203 |
206 void TargetHandler::TargetDestroyedInternal( | 204 void TargetHandler::TargetDestroyedInternal( |
207 DevToolsAgentHost* host) { | 205 DevToolsAgentHost* host) { |
208 auto it = reported_hosts_.find(host->GetId()); | 206 auto it = reported_hosts_.find(host->GetId()); |
209 if (it == reported_hosts_.end()) | 207 if (it == reported_hosts_.end()) |
210 return; | 208 return; |
211 client_->TargetDestroyed(TargetDestroyedParams::Create() | 209 frontend_->TargetDestroyed(host->GetId()); |
212 ->set_target_id(host->GetId())); | |
213 reported_hosts_.erase(it); | 210 reported_hosts_.erase(it); |
214 } | 211 } |
215 | 212 |
216 bool TargetHandler::AttachToTargetInternal( | 213 bool TargetHandler::AttachToTargetInternal( |
217 DevToolsAgentHost* host, bool waiting_for_debugger) { | 214 DevToolsAgentHost* host, bool waiting_for_debugger) { |
218 if (!host->AttachClient(this)) | 215 if (!host->AttachClient(this)) |
219 return false; | 216 return false; |
220 attached_hosts_[host->GetId()] = host; | 217 attached_hosts_[host->GetId()] = host; |
221 client_->AttachedToTarget(AttachedToTargetParams::Create() | 218 frontend_->AttachedToTarget(CreateInfo(host), waiting_for_debugger); |
222 ->set_target_info(CreateInfo(host)) | |
223 ->set_waiting_for_debugger(waiting_for_debugger)); | |
224 return true; | 219 return true; |
225 } | 220 } |
226 | 221 |
227 void TargetHandler::DetachFromTargetInternal(DevToolsAgentHost* host) { | 222 void TargetHandler::DetachFromTargetInternal(DevToolsAgentHost* host) { |
228 auto it = attached_hosts_.find(host->GetId()); | 223 auto it = attached_hosts_.find(host->GetId()); |
229 if (it == attached_hosts_.end()) | 224 if (it == attached_hosts_.end()) |
230 return; | 225 return; |
231 host->DetachClient(this); | 226 host->DetachClient(this); |
232 client_->DetachedFromTarget(DetachedFromTargetParams::Create()-> | 227 frontend_->DetachedFromTarget(host->GetId()); |
233 set_target_id(host->GetId())); | |
234 attached_hosts_.erase(it); | 228 attached_hosts_.erase(it); |
235 } | 229 } |
236 | 230 |
237 // ----------------- Protocol ---------------------- | 231 // ----------------- Protocol ---------------------- |
238 | 232 |
239 Response TargetHandler::SetDiscoverTargets(bool discover) { | 233 Response TargetHandler::SetDiscoverTargets(bool discover) { |
240 if (discover_ == discover) | 234 if (discover_ == discover) |
241 return Response::OK(); | 235 return Response::OK(); |
242 discover_ = discover; | 236 discover_ = discover; |
243 if (discover_) { | 237 if (discover_) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 if (attach_to_frames_) { | 271 if (attach_to_frames_) { |
278 UpdateFrames(); | 272 UpdateFrames(); |
279 } else { | 273 } else { |
280 HostsMap empty; | 274 HostsMap empty; |
281 ReattachTargetsOfType(empty, DevToolsAgentHost::kTypeFrame, false); | 275 ReattachTargetsOfType(empty, DevToolsAgentHost::kTypeFrame, false); |
282 } | 276 } |
283 return Response::OK(); | 277 return Response::OK(); |
284 } | 278 } |
285 | 279 |
286 Response TargetHandler::SetRemoteLocations( | 280 Response TargetHandler::SetRemoteLocations( |
287 const std::vector<std::unique_ptr<base::DictionaryValue>>& locations) { | 281 std::unique_ptr<protocol::Array<Target::RemoteLocation>>) { |
288 return Response::ServerError("Not supported"); | 282 return Response::Error("Not supported"); |
289 } | 283 } |
290 | 284 |
291 Response TargetHandler::AttachToTarget(const std::string& target_id, | 285 Response TargetHandler::AttachToTarget(const std::string& target_id, |
292 bool* out_success) { | 286 bool* out_success) { |
293 // TODO(dgozman): only allow reported hosts. | 287 // TODO(dgozman): only allow reported hosts. |
294 scoped_refptr<DevToolsAgentHost> agent_host = | 288 scoped_refptr<DevToolsAgentHost> agent_host = |
295 DevToolsAgentHost::GetForId(target_id); | 289 DevToolsAgentHost::GetForId(target_id); |
296 if (!agent_host) | 290 if (!agent_host) |
297 return Response::ServerError("No target with given id found"); | 291 return Response::InvalidParams("No target with given id found"); |
298 *out_success = AttachToTargetInternal(agent_host.get(), false); | 292 *out_success = AttachToTargetInternal(agent_host.get(), false); |
299 return Response::OK(); | 293 return Response::OK(); |
300 } | 294 } |
301 | 295 |
302 Response TargetHandler::DetachFromTarget(const std::string& target_id) { | 296 Response TargetHandler::DetachFromTarget(const std::string& target_id) { |
303 auto it = attached_hosts_.find(target_id); | 297 auto it = attached_hosts_.find(target_id); |
304 if (it == attached_hosts_.end()) | 298 if (it == attached_hosts_.end()) |
305 return Response::InternalError("Not attached to the target"); | 299 return Response::Error("Not attached to the target"); |
306 DevToolsAgentHost* agent_host = it->second.get(); | 300 DevToolsAgentHost* agent_host = it->second.get(); |
307 DetachFromTargetInternal(agent_host); | 301 DetachFromTargetInternal(agent_host); |
308 return Response::OK(); | 302 return Response::OK(); |
309 } | 303 } |
310 | 304 |
311 Response TargetHandler::SendMessageToTarget( | 305 Response TargetHandler::SendMessageToTarget( |
312 const std::string& target_id, | 306 const std::string& target_id, |
313 const std::string& message) { | 307 const std::string& message) { |
314 auto it = attached_hosts_.find(target_id); | 308 auto it = attached_hosts_.find(target_id); |
315 if (it == attached_hosts_.end()) | 309 if (it == attached_hosts_.end()) |
316 return Response::FallThrough(); | 310 return Response::FallThrough(); |
317 it->second->DispatchProtocolMessage(this, message); | 311 it->second->DispatchProtocolMessage(this, message); |
318 return Response::OK(); | 312 return Response::OK(); |
319 } | 313 } |
320 | 314 |
321 Response TargetHandler::GetTargetInfo( | 315 Response TargetHandler::GetTargetInfo( |
322 const std::string& target_id, | 316 const std::string& target_id, |
323 scoped_refptr<TargetInfo>* target_info) { | 317 std::unique_ptr<Target::TargetInfo>* target_info) { |
324 // TODO(dgozman): only allow reported hosts. | 318 // TODO(dgozman): only allow reported hosts. |
325 scoped_refptr<DevToolsAgentHost> agent_host( | 319 scoped_refptr<DevToolsAgentHost> agent_host( |
326 DevToolsAgentHost::GetForId(target_id)); | 320 DevToolsAgentHost::GetForId(target_id)); |
327 if (!agent_host) | 321 if (!agent_host) |
328 return Response::InvalidParams("No target with given id found"); | 322 return Response::InvalidParams("No target with given id found"); |
329 *target_info = CreateInfo(agent_host.get()); | 323 *target_info = CreateInfo(agent_host.get()); |
330 return Response::OK(); | 324 return Response::OK(); |
331 } | 325 } |
332 | 326 |
333 Response TargetHandler::ActivateTarget(const std::string& target_id) { | 327 Response TargetHandler::ActivateTarget(const std::string& target_id) { |
334 // TODO(dgozman): only allow reported hosts. | 328 // TODO(dgozman): only allow reported hosts. |
335 scoped_refptr<DevToolsAgentHost> agent_host( | 329 scoped_refptr<DevToolsAgentHost> agent_host( |
336 DevToolsAgentHost::GetForId(target_id)); | 330 DevToolsAgentHost::GetForId(target_id)); |
337 if (!agent_host) | 331 if (!agent_host) |
338 return Response::InvalidParams("No target with given id found"); | 332 return Response::InvalidParams("No target with given id found"); |
339 agent_host->Activate(); | 333 agent_host->Activate(); |
340 return Response::OK(); | 334 return Response::OK(); |
341 } | 335 } |
342 | 336 |
343 Response TargetHandler::CloseTarget(const std::string& target_id, | 337 Response TargetHandler::CloseTarget(const std::string& target_id, |
344 bool* out_success) { | 338 bool* out_success) { |
345 scoped_refptr<DevToolsAgentHost> agent_host = | 339 scoped_refptr<DevToolsAgentHost> agent_host = |
346 DevToolsAgentHost::GetForId(target_id); | 340 DevToolsAgentHost::GetForId(target_id); |
347 if (!agent_host) | 341 if (!agent_host) |
348 return Response::ServerError("No target with given id found"); | 342 return Response::InvalidParams("No target with given id found"); |
349 *out_success = agent_host->Close(); | 343 *out_success = agent_host->Close(); |
350 return Response::OK(); | 344 return Response::OK(); |
351 } | 345 } |
352 | 346 |
353 Response TargetHandler::CreateBrowserContext(std::string* out_context_id) { | 347 Response TargetHandler::CreateBrowserContext(std::string* out_context_id) { |
354 return Response::ServerError("Not supported"); | 348 return Response::Error("Not supported"); |
355 } | 349 } |
356 | 350 |
357 Response TargetHandler::DisposeBrowserContext(const std::string& context_id, | 351 Response TargetHandler::DisposeBrowserContext(const std::string& context_id, |
358 bool* out_success) { | 352 bool* out_success) { |
359 return Response::ServerError("Not supported"); | 353 return Response::Error("Not supported"); |
360 } | 354 } |
361 | 355 |
362 Response TargetHandler::CreateTarget(const std::string& url, | 356 Response TargetHandler::CreateTarget(const std::string& url, |
363 const int* width, | 357 Maybe<int> width, |
364 const int* height, | 358 Maybe<int> height, |
365 const std::string* context_id, | 359 Maybe<std::string> context_id, |
366 std::string* out_target_id) { | 360 std::string* out_target_id) { |
367 DevToolsManagerDelegate* delegate = | 361 DevToolsManagerDelegate* delegate = |
368 DevToolsManager::GetInstance()->delegate(); | 362 DevToolsManager::GetInstance()->delegate(); |
369 if (!delegate) | 363 if (!delegate) |
370 return Response::ServerError("Not supported"); | 364 return Response::Error("Not supported"); |
371 scoped_refptr<content::DevToolsAgentHost> agent_host = | 365 scoped_refptr<content::DevToolsAgentHost> agent_host = |
372 delegate->CreateNewTarget(GURL(url)); | 366 delegate->CreateNewTarget(GURL(url)); |
373 if (!agent_host) | 367 if (!agent_host) |
374 return Response::ServerError("Not supported"); | 368 return Response::Error("Not supported"); |
375 *out_target_id = agent_host->GetId(); | 369 *out_target_id = agent_host->GetId(); |
376 return Response::OK(); | 370 return Response::OK(); |
377 } | 371 } |
378 | 372 |
379 Response TargetHandler::GetTargets( | 373 Response TargetHandler::GetTargets( |
380 std::vector<scoped_refptr<TargetInfo>>* target_infos) { | 374 std::unique_ptr<protocol::Array<Target::TargetInfo>>* target_infos) { |
| 375 *target_infos = protocol::Array<Target::TargetInfo>::create(); |
381 for (const auto& host : DevToolsAgentHost::GetOrCreateAll()) | 376 for (const auto& host : DevToolsAgentHost::GetOrCreateAll()) |
382 target_infos->push_back(CreateInfo(host.get())); | 377 (*target_infos)->addItem(CreateInfo(host.get())); |
383 return Response::OK(); | 378 return Response::OK(); |
384 } | 379 } |
385 | 380 |
386 // ---------------- DevToolsAgentHostClient ---------------- | 381 // ---------------- DevToolsAgentHostClient ---------------- |
387 | 382 |
388 void TargetHandler::DispatchProtocolMessage( | 383 void TargetHandler::DispatchProtocolMessage( |
389 DevToolsAgentHost* host, | 384 DevToolsAgentHost* host, |
390 const std::string& message) { | 385 const std::string& message) { |
391 auto it = attached_hosts_.find(host->GetId()); | 386 auto it = attached_hosts_.find(host->GetId()); |
392 if (it == attached_hosts_.end()) | 387 if (it == attached_hosts_.end()) |
393 return; // Already disconnected. | 388 return; // Already disconnected. |
394 | 389 |
395 client_->ReceivedMessageFromTarget( | 390 frontend_->ReceivedMessageFromTarget(host->GetId(), message); |
396 ReceivedMessageFromTargetParams::Create()-> | |
397 set_target_id(host->GetId())-> | |
398 set_message(message)); | |
399 } | 391 } |
400 | 392 |
401 void TargetHandler::AgentHostClosed( | 393 void TargetHandler::AgentHostClosed( |
402 DevToolsAgentHost* host, | 394 DevToolsAgentHost* host, |
403 bool replaced_with_another_client) { | 395 bool replaced_with_another_client) { |
404 client_->DetachedFromTarget(DetachedFromTargetParams::Create()-> | 396 frontend_->DetachedFromTarget(host->GetId()); |
405 set_target_id(host->GetId())); | |
406 attached_hosts_.erase(host->GetId()); | 397 attached_hosts_.erase(host->GetId()); |
407 } | 398 } |
408 | 399 |
409 // -------------- DevToolsAgentHostObserver ----------------- | 400 // -------------- DevToolsAgentHostObserver ----------------- |
410 | 401 |
411 bool TargetHandler::ShouldForceDevToolsAgentHostCreation() { | 402 bool TargetHandler::ShouldForceDevToolsAgentHostCreation() { |
412 return true; | 403 return true; |
413 } | 404 } |
414 | 405 |
415 void TargetHandler::DevToolsAgentHostCreated(DevToolsAgentHost* agent_host) { | 406 void TargetHandler::DevToolsAgentHostCreated(DevToolsAgentHost* agent_host) { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 void TargetHandler::WorkerVersionDoomed( | 446 void TargetHandler::WorkerVersionDoomed( |
456 ServiceWorkerDevToolsAgentHost* host) { | 447 ServiceWorkerDevToolsAgentHost* host) { |
457 UpdateServiceWorkers(); | 448 UpdateServiceWorkers(); |
458 } | 449 } |
459 | 450 |
460 void TargetHandler::WorkerDestroyed( | 451 void TargetHandler::WorkerDestroyed( |
461 ServiceWorkerDevToolsAgentHost* host) { | 452 ServiceWorkerDevToolsAgentHost* host) { |
462 UpdateServiceWorkers(); | 453 UpdateServiceWorkers(); |
463 } | 454 } |
464 | 455 |
465 } // namespace target | 456 } // namespace protocol |
466 } // namespace devtools | |
467 } // namespace content | 457 } // namespace content |
OLD | NEW |