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