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