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

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

Issue 988003002: DevTools: pick sw targets to attach to on the backend side. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/service_worker_handler.h" 5 #include "content/browser/devtools/protocol/service_worker_handler.h"
6 6
7 #include "content/public/browser/devtools_agent_host.h" 7 #include "content/browser/devtools/service_worker_devtools_agent_host.h"
8 #include "content/browser/devtools/service_worker_devtools_manager.h"
8 9
9 namespace content { 10 namespace content {
10 namespace devtools { 11 namespace devtools {
11 namespace service_worker { 12 namespace service_worker {
12 13
13 using Response = DevToolsProtocolClient::Response; 14 using Response = DevToolsProtocolClient::Response;
14 15
15 ServiceWorkerHandler::ServiceWorkerHandler() { 16 ServiceWorkerHandler::ServiceWorkerHandler()
17 : enabled_(false) {
16 } 18 }
17 19
18 ServiceWorkerHandler::~ServiceWorkerHandler() { 20 ServiceWorkerHandler::~ServiceWorkerHandler() {
19 Disable(); 21 Disable();
20 } 22 }
21 23
22 void ServiceWorkerHandler::SetClient( 24 void ServiceWorkerHandler::SetClient(
23 scoped_ptr<Client> client) { 25 scoped_ptr<Client> client) {
24 client_.swap(client); 26 client_.swap(client);
25 } 27 }
26 28
29 void ServiceWorkerHandler::SetURL(const GURL& url) {
30 url_ = url;
31 if (enabled_) {
32 for (auto pair : attached_hosts_) {
33 if (!MatchesInspectedPage(pair.second.get()))
34 WorkerDestroyed(pair.second.get());
35 }
36
37 ServiceWorkerDevToolsAgentHost::List agent_hosts;
38 ServiceWorkerDevToolsManager::GetInstance()->
39 AddAllAgentHosts(&agent_hosts);
40 for (auto host : agent_hosts) {
41 if (!MatchesInspectedPage(host.get()))
42 continue;
43 if (attached_hosts_.find(host->GetId()) != attached_hosts_.end())
44 continue;
45 // TODO(pfeldman): workers are created concurrently, we need
46 // to get notification earlier to go through the Created/Ready
47 // lifecycle.
48 WorkerReadyForInspection(host.get());
49 }
50 }
51 }
52
27 void ServiceWorkerHandler::Detached() { 53 void ServiceWorkerHandler::Detached() {
28 Disable(); 54 Disable();
29 } 55 }
30 56
31 Response ServiceWorkerHandler::Enable() { 57 Response ServiceWorkerHandler::Enable() {
32 DevToolsAgentHost::List agent_hosts; 58 if (enabled_)
59 return Response::OK();
60 enabled_ = true;
61
62 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this);
63
64 ServiceWorkerDevToolsAgentHost::List agent_hosts;
33 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&agent_hosts); 65 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&agent_hosts);
34 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this);
35 for (auto host : agent_hosts) 66 for (auto host : agent_hosts)
36 WorkerCreated(host.get()); 67 WorkerReadyForInspection(host.get());
37 return Response::OK(); 68 return Response::OK();
38 } 69 }
39 70
40 Response ServiceWorkerHandler::Disable() { 71 Response ServiceWorkerHandler::Disable() {
72 if (!enabled_)
73 return Response::OK();
74 enabled_ = false;
75
41 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this); 76 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this);
42 for (const auto& pair : attached_hosts_) 77 for (const auto& pair : attached_hosts_)
43 pair.second->DetachClient(); 78 pair.second->DetachClient();
44 attached_hosts_.clear(); 79 attached_hosts_.clear();
45 return Response::OK(); 80 return Response::OK();
46 } 81 }
47 82
48 Response ServiceWorkerHandler::SendMessage( 83 Response ServiceWorkerHandler::SendMessage(
49 const std::string& worker_id, 84 const std::string& worker_id,
50 const std::string& message) { 85 const std::string& message) {
51 auto it = attached_hosts_.find(worker_id); 86 auto it = attached_hosts_.find(worker_id);
52 if (it == attached_hosts_.end()) 87 if (it == attached_hosts_.end())
53 return Response::InternalError("Not connected to the worker"); 88 return Response::InternalError("Not connected to the worker");
54
55 it->second->DispatchProtocolMessage(message); 89 it->second->DispatchProtocolMessage(message);
56 return Response::OK(); 90 return Response::OK();
57 } 91 }
58 92
59 Response ServiceWorkerHandler::Attach(const std::string& worker_id) {
60 scoped_refptr<DevToolsAgentHost> host =
61 DevToolsAgentHost::GetForId(worker_id);
62 if (!host)
63 return Response::InternalError("No such worker available");
64
65 if (host->IsAttached())
66 return Response::InternalError("Another client is already attached");
67
68 attached_hosts_[worker_id] = host;
69 host->AttachClient(this);
70 return Response::OK();
71 }
72
73 Response ServiceWorkerHandler::Detach(const std::string& worker_id) {
74 auto it = attached_hosts_.find(worker_id);
75 if (it == attached_hosts_.end())
76 return Response::InternalError("Not connected to the worker");
77
78 attached_hosts_.erase(worker_id);
79 it->second->DetachClient();
80 return Response::OK();
81 }
82
83 void ServiceWorkerHandler::DispatchProtocolMessage( 93 void ServiceWorkerHandler::DispatchProtocolMessage(
84 DevToolsAgentHost* host, 94 DevToolsAgentHost* host,
85 const std::string& message) { 95 const std::string& message) {
96
86 auto it = attached_hosts_.find(host->GetId()); 97 auto it = attached_hosts_.find(host->GetId());
87 if (it == attached_hosts_.end()) 98 if (it == attached_hosts_.end())
88 return; // Already disconnected. 99 return; // Already disconnected.
89 100
90 client_->DispatchMessage( 101 client_->DispatchMessage(
91 DispatchMessageParams::Create()-> 102 DispatchMessageParams::Create()->
92 set_worker_id(host->GetId())-> 103 set_worker_id(host->GetId())->
93 set_message(message)); 104 set_message(message));
94 } 105 }
95 106
96 void ServiceWorkerHandler::AgentHostClosed( 107 void ServiceWorkerHandler::AgentHostClosed(
97 DevToolsAgentHost* host, 108 DevToolsAgentHost* host,
98 bool replaced_with_another_client) { 109 bool replaced_with_another_client) {
99 WorkerDestroyed(host); 110 WorkerDestroyed(static_cast<ServiceWorkerDevToolsAgentHost*>(host));
100 } 111 }
101 112
102 void ServiceWorkerHandler::WorkerCreated( 113 void ServiceWorkerHandler::WorkerCreated(
103 DevToolsAgentHost* host) { 114 ServiceWorkerDevToolsAgentHost* host) {
115 if (!MatchesInspectedPage(host))
116 return;
117 host->PauseForDebugOnStart();
118 }
119
120 void ServiceWorkerHandler::WorkerReadyForInspection(
121 ServiceWorkerDevToolsAgentHost* host) {
122 if (host->IsAttached() || !MatchesInspectedPage(host))
123 return;
124
125 attached_hosts_[host->GetId()] = host;
126 host->AttachClient(this);
104 client_->WorkerCreated(WorkerCreatedParams::Create()-> 127 client_->WorkerCreated(WorkerCreatedParams::Create()->
105 set_worker_id(host->GetId())-> 128 set_worker_id(host->GetId())->
106 set_url(host->GetURL().spec())); 129 set_url(host->GetURL().spec()));
107 } 130 }
108 131
109 void ServiceWorkerHandler::WorkerDestroyed( 132 void ServiceWorkerHandler::WorkerDestroyed(
110 DevToolsAgentHost* host) { 133 ServiceWorkerDevToolsAgentHost* host) {
111 auto it = attached_hosts_.find(host->GetId()); 134 auto it = attached_hosts_.find(host->GetId());
112 if (it == attached_hosts_.end()) 135 if (it == attached_hosts_.end())
113 return; 136 return;
137 it->second->DetachClient();
114 client_->WorkerTerminated(WorkerTerminatedParams::Create()-> 138 client_->WorkerTerminated(WorkerTerminatedParams::Create()->
115 set_worker_id(host->GetId())); 139 set_worker_id(host->GetId()));
116 attached_hosts_.erase(it); 140 attached_hosts_.erase(it);
117 } 141 }
118 142
143 bool ServiceWorkerHandler::MatchesInspectedPage(
144 ServiceWorkerDevToolsAgentHost* host) {
145 // TODO(pfeldman): match based on scope.
146 // TODO(pfeldman): match iframes.
147 return host->GetURL().host() == url_.host();
148 }
149
119 } // namespace service_worker 150 } // namespace service_worker
120 } // namespace devtools 151 } // namespace devtools
121 } // namespace content 152 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698