| 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/service_worker_devtools_agent_host.h" | 8 #include "content/browser/devtools/service_worker_devtools_agent_host.h" |
| 8 #include "content/browser/frame_host/frame_tree.h" | 9 #include "content/browser/frame_host/frame_tree.h" |
| 9 #include "content/browser/frame_host/frame_tree_node.h" | 10 #include "content/browser/frame_host/frame_tree_node.h" |
| 10 #include "content/browser/frame_host/render_frame_host_impl.h" | 11 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 11 | 12 |
| 12 namespace content { | 13 namespace content { |
| 13 namespace devtools { | 14 namespace devtools { |
| 14 namespace target { | 15 namespace target { |
| 15 | 16 |
| 16 using Response = DevToolsProtocolClient::Response; | 17 using Response = DevToolsProtocolClient::Response; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 | 77 |
| 77 ScopeAgentsMap scope_agents_map; | 78 ScopeAgentsMap scope_agents_map; |
| 78 GetMatchingHostsByScopeMap(agent_hosts, urls, &scope_agents_map); | 79 GetMatchingHostsByScopeMap(agent_hosts, urls, &scope_agents_map); |
| 79 | 80 |
| 80 for (const auto& it : scope_agents_map) | 81 for (const auto& it : scope_agents_map) |
| 81 AddEligibleHosts(*it.second.get(), &result); | 82 AddEligibleHosts(*it.second.get(), &result); |
| 82 | 83 |
| 83 return result; | 84 return result; |
| 84 } | 85 } |
| 85 | 86 |
| 87 scoped_refptr<TargetInfo> CreateInfo(DevToolsAgentHost* host) { |
| 88 return TargetInfo::Create() |
| 89 ->set_target_id(host->GetId()) |
| 90 ->set_title(host->GetTitle()) |
| 91 ->set_url(host->GetURL().spec()) |
| 92 ->set_type(host->GetType()); |
| 93 } |
| 94 |
| 86 } // namespace | 95 } // namespace |
| 87 | 96 |
| 88 TargetHandler::TargetHandler() | 97 TargetHandler::TargetHandler() |
| 89 : discover_(false), | 98 : discover_(false), |
| 90 auto_attach_(false), | 99 auto_attach_(false), |
| 91 wait_for_debugger_on_start_(false), | 100 wait_for_debugger_on_start_(false), |
| 92 attach_to_frames_(false), | 101 attach_to_frames_(false), |
| 93 render_frame_host_(nullptr) { | 102 render_frame_host_(nullptr) { |
| 94 } | 103 } |
| 95 | 104 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 for (const auto& pair : new_hosts) { | 192 for (const auto& pair : new_hosts) { |
| 184 if (old_hosts.find(pair.first) == old_hosts.end()) | 193 if (old_hosts.find(pair.first) == old_hosts.end()) |
| 185 AttachToTargetInternal(pair.second.get(), waiting_for_debugger); | 194 AttachToTargetInternal(pair.second.get(), waiting_for_debugger); |
| 186 } | 195 } |
| 187 } | 196 } |
| 188 | 197 |
| 189 void TargetHandler::TargetCreatedInternal(DevToolsAgentHost* host) { | 198 void TargetHandler::TargetCreatedInternal(DevToolsAgentHost* host) { |
| 190 if (reported_hosts_.find(host->GetId()) != reported_hosts_.end()) | 199 if (reported_hosts_.find(host->GetId()) != reported_hosts_.end()) |
| 191 return; | 200 return; |
| 192 client_->TargetCreated( | 201 client_->TargetCreated( |
| 193 TargetCreatedParams::Create()->set_target_info( | 202 TargetCreatedParams::Create()->set_target_info(CreateInfo(host))); |
| 194 TargetInfo::Create()->set_target_id(host->GetId()) | |
| 195 ->set_title(host->GetTitle()) | |
| 196 ->set_url(host->GetURL().spec()) | |
| 197 ->set_type(host->GetType()))); | |
| 198 reported_hosts_[host->GetId()] = host; | 203 reported_hosts_[host->GetId()] = host; |
| 199 } | 204 } |
| 200 | 205 |
| 201 void TargetHandler::TargetDestroyedInternal( | 206 void TargetHandler::TargetDestroyedInternal( |
| 202 DevToolsAgentHost* host) { | 207 DevToolsAgentHost* host) { |
| 203 auto it = reported_hosts_.find(host->GetId()); | 208 auto it = reported_hosts_.find(host->GetId()); |
| 204 if (it == reported_hosts_.end()) | 209 if (it == reported_hosts_.end()) |
| 205 return; | 210 return; |
| 206 client_->TargetDestroyed(TargetDestroyedParams::Create() | 211 client_->TargetDestroyed(TargetDestroyedParams::Create() |
| 207 ->set_target_id(host->GetId())); | 212 ->set_target_id(host->GetId())); |
| 208 reported_hosts_.erase(it); | 213 reported_hosts_.erase(it); |
| 209 } | 214 } |
| 210 | 215 |
| 211 bool TargetHandler::AttachToTargetInternal( | 216 bool TargetHandler::AttachToTargetInternal( |
| 212 DevToolsAgentHost* host, bool waiting_for_debugger) { | 217 DevToolsAgentHost* host, bool waiting_for_debugger) { |
| 213 if (!host->AttachClient(this)) | 218 if (!host->AttachClient(this)) |
| 214 return false; | 219 return false; |
| 215 attached_hosts_[host->GetId()] = host; | 220 attached_hosts_[host->GetId()] = host; |
| 216 client_->AttachedToTarget(AttachedToTargetParams::Create() | 221 client_->AttachedToTarget(AttachedToTargetParams::Create() |
| 217 ->set_target_info( | 222 ->set_target_info(CreateInfo(host)) |
| 218 TargetInfo::Create()->set_target_id(host->GetId()) | |
| 219 ->set_title(host->GetTitle()) | |
| 220 ->set_url(host->GetURL().spec()) | |
| 221 ->set_type(host->GetType())) | |
| 222 ->set_waiting_for_debugger(waiting_for_debugger)); | 223 ->set_waiting_for_debugger(waiting_for_debugger)); |
| 223 return true; | 224 return true; |
| 224 } | 225 } |
| 225 | 226 |
| 226 void TargetHandler::DetachFromTargetInternal(DevToolsAgentHost* host) { | 227 void TargetHandler::DetachFromTargetInternal(DevToolsAgentHost* host) { |
| 227 auto it = attached_hosts_.find(host->GetId()); | 228 auto it = attached_hosts_.find(host->GetId()); |
| 228 if (it == attached_hosts_.end()) | 229 if (it == attached_hosts_.end()) |
| 229 return; | 230 return; |
| 230 host->DetachClient(this); | 231 host->DetachClient(this); |
| 231 client_->DetachedFromTarget(DetachedFromTargetParams::Create()-> | 232 client_->DetachedFromTarget(DetachedFromTargetParams::Create()-> |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 return Response::OK(); | 283 return Response::OK(); |
| 283 } | 284 } |
| 284 | 285 |
| 285 Response TargetHandler::SetRemoteLocations( | 286 Response TargetHandler::SetRemoteLocations( |
| 286 const std::vector<std::unique_ptr<base::DictionaryValue>>& locations) { | 287 const std::vector<std::unique_ptr<base::DictionaryValue>>& locations) { |
| 287 return Response::ServerError("Not supported"); | 288 return Response::ServerError("Not supported"); |
| 288 } | 289 } |
| 289 | 290 |
| 290 Response TargetHandler::AttachToTarget(const std::string& target_id, | 291 Response TargetHandler::AttachToTarget(const std::string& target_id, |
| 291 bool* out_success) { | 292 bool* out_success) { |
| 292 auto it = reported_hosts_.find(target_id); | 293 // TODO(dgozman): only allow reported hosts. |
| 293 if (it == reported_hosts_.end()) | 294 scoped_refptr<DevToolsAgentHost> agent_host = |
| 294 return Response::InvalidParams("No target with such id"); | 295 DevToolsAgentHost::GetForId(target_id); |
| 295 *out_success = AttachToTargetInternal(it->second, false); | 296 if (!agent_host) |
| 297 return Response::ServerError("No target with given id found"); |
| 298 *out_success = AttachToTargetInternal(agent_host.get(), false); |
| 296 return Response::OK(); | 299 return Response::OK(); |
| 297 } | 300 } |
| 298 | 301 |
| 299 Response TargetHandler::DetachFromTarget(const std::string& target_id) { | 302 Response TargetHandler::DetachFromTarget(const std::string& target_id) { |
| 300 auto it = attached_hosts_.find(target_id); | 303 auto it = attached_hosts_.find(target_id); |
| 301 if (it == attached_hosts_.end()) | 304 if (it == attached_hosts_.end()) |
| 302 return Response::InternalError("Not attached to the target"); | 305 return Response::InternalError("Not attached to the target"); |
| 303 DevToolsAgentHost* agent_host = it->second.get(); | 306 DevToolsAgentHost* agent_host = it->second.get(); |
| 304 DetachFromTargetInternal(agent_host); | 307 DetachFromTargetInternal(agent_host); |
| 305 return Response::OK(); | 308 return Response::OK(); |
| 306 } | 309 } |
| 307 | 310 |
| 308 Response TargetHandler::SendMessageToTarget( | 311 Response TargetHandler::SendMessageToTarget( |
| 309 const std::string& target_id, | 312 const std::string& target_id, |
| 310 const std::string& message) { | 313 const std::string& message) { |
| 311 auto it = attached_hosts_.find(target_id); | 314 auto it = attached_hosts_.find(target_id); |
| 312 if (it == attached_hosts_.end()) | 315 if (it == attached_hosts_.end()) |
| 313 return Response::InternalError("Not attached to the target"); | 316 return Response::InternalError("Not attached to the target"); |
| 314 it->second->DispatchProtocolMessage(this, message); | 317 it->second->DispatchProtocolMessage(this, message); |
| 315 return Response::OK(); | 318 return Response::OK(); |
| 316 } | 319 } |
| 317 | 320 |
| 318 Response TargetHandler::GetTargetInfo( | 321 Response TargetHandler::GetTargetInfo( |
| 319 const std::string& target_id, | 322 const std::string& target_id, |
| 320 scoped_refptr<TargetInfo>* target_info) { | 323 scoped_refptr<TargetInfo>* target_info) { |
| 321 // TODO(dgozman): only allow reported hosts. | 324 // TODO(dgozman): only allow reported hosts. |
| 322 scoped_refptr<DevToolsAgentHost> agent_host( | 325 scoped_refptr<DevToolsAgentHost> agent_host( |
| 323 DevToolsAgentHost::GetForId(target_id)); | 326 DevToolsAgentHost::GetForId(target_id)); |
| 324 if (!agent_host) | 327 if (!agent_host) |
| 325 return Response::InvalidParams("No target with such id"); | 328 return Response::InvalidParams("No target with given id found"); |
| 326 *target_info = TargetInfo::Create() | 329 *target_info = CreateInfo(agent_host.get()); |
| 327 ->set_target_id(agent_host->GetId()) | |
| 328 ->set_type(agent_host->GetType()) | |
| 329 ->set_title(agent_host->GetTitle()) | |
| 330 ->set_url(agent_host->GetURL().spec()); | |
| 331 return Response::OK(); | 330 return Response::OK(); |
| 332 } | 331 } |
| 333 | 332 |
| 334 Response TargetHandler::ActivateTarget(const std::string& target_id) { | 333 Response TargetHandler::ActivateTarget(const std::string& target_id) { |
| 335 // TODO(dgozman): only allow reported hosts. | 334 // TODO(dgozman): only allow reported hosts. |
| 336 scoped_refptr<DevToolsAgentHost> agent_host( | 335 scoped_refptr<DevToolsAgentHost> agent_host( |
| 337 DevToolsAgentHost::GetForId(target_id)); | 336 DevToolsAgentHost::GetForId(target_id)); |
| 338 if (!agent_host) | 337 if (!agent_host) |
| 339 return Response::InvalidParams("No target with such id"); | 338 return Response::InvalidParams("No target with given id found"); |
| 340 agent_host->Activate(); | 339 agent_host->Activate(); |
| 341 return Response::OK(); | 340 return Response::OK(); |
| 342 } | 341 } |
| 343 | 342 |
| 343 Response TargetHandler::CloseTarget(const std::string& target_id, |
| 344 bool* out_success) { |
| 345 scoped_refptr<DevToolsAgentHost> agent_host = |
| 346 DevToolsAgentHost::GetForId(target_id); |
| 347 if (!agent_host) |
| 348 return Response::ServerError("No target with given id found"); |
| 349 *out_success = agent_host->Close(); |
| 350 return Response::OK(); |
| 351 } |
| 352 |
| 353 Response TargetHandler::CreateBrowserContext(std::string* out_context_id) { |
| 354 return Response::ServerError("Not supported"); |
| 355 } |
| 356 |
| 357 Response TargetHandler::DisposeBrowserContext(const std::string& context_id, |
| 358 bool* out_success) { |
| 359 return Response::ServerError("Not supported"); |
| 360 } |
| 361 |
| 362 Response TargetHandler::CreateTarget(const std::string& url, |
| 363 const int* width, |
| 364 const int* height, |
| 365 const std::string* context_id, |
| 366 std::string* out_target_id) { |
| 367 DevToolsManagerDelegate* delegate = |
| 368 DevToolsManager::GetInstance()->delegate(); |
| 369 if (!delegate) |
| 370 return Response::ServerError("Not supported"); |
| 371 scoped_refptr<content::DevToolsAgentHost> agent_host = |
| 372 delegate->CreateNewTarget(GURL(url)); |
| 373 if (!agent_host) |
| 374 return Response::ServerError("Not supported"); |
| 375 *out_target_id = agent_host->GetId(); |
| 376 return Response::OK(); |
| 377 } |
| 378 |
| 379 Response TargetHandler::GetTargets( |
| 380 std::vector<scoped_refptr<TargetInfo>>* target_infos) { |
| 381 for (const auto& host : DevToolsAgentHost::GetOrCreateAll()) |
| 382 target_infos->push_back(CreateInfo(host.get())); |
| 383 return Response::OK(); |
| 384 } |
| 385 |
| 344 // ---------------- DevToolsAgentHostClient ---------------- | 386 // ---------------- DevToolsAgentHostClient ---------------- |
| 345 | 387 |
| 346 void TargetHandler::DispatchProtocolMessage( | 388 void TargetHandler::DispatchProtocolMessage( |
| 347 DevToolsAgentHost* host, | 389 DevToolsAgentHost* host, |
| 348 const std::string& message) { | 390 const std::string& message) { |
| 349 auto it = attached_hosts_.find(host->GetId()); | 391 auto it = attached_hosts_.find(host->GetId()); |
| 350 if (it == attached_hosts_.end()) | 392 if (it == attached_hosts_.end()) |
| 351 return; // Already disconnected. | 393 return; // Already disconnected. |
| 352 | 394 |
| 353 client_->ReceivedMessageFromTarget( | 395 client_->ReceivedMessageFromTarget( |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 } | 458 } |
| 417 | 459 |
| 418 void TargetHandler::WorkerDestroyed( | 460 void TargetHandler::WorkerDestroyed( |
| 419 ServiceWorkerDevToolsAgentHost* host) { | 461 ServiceWorkerDevToolsAgentHost* host) { |
| 420 UpdateServiceWorkers(); | 462 UpdateServiceWorkers(); |
| 421 } | 463 } |
| 422 | 464 |
| 423 } // namespace target | 465 } // namespace target |
| 424 } // namespace devtools | 466 } // namespace devtools |
| 425 } // namespace content | 467 } // namespace content |
| OLD | NEW |