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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 for (const auto& it : scope_agents_map) | 80 for (const auto& it : scope_agents_map) |
81 AddEligibleHosts(*it.second.get(), &result); | 81 AddEligibleHosts(*it.second.get(), &result); |
82 | 82 |
83 return result; | 83 return result; |
84 } | 84 } |
85 | 85 |
86 } // namespace | 86 } // namespace |
87 | 87 |
88 TargetHandler::TargetHandler() | 88 TargetHandler::TargetHandler() |
89 : enabled_(false), | 89 : enabled_(false), |
| 90 auto_attach_(false), |
90 wait_for_debugger_on_start_(false), | 91 wait_for_debugger_on_start_(false), |
91 attach_to_frames_(false), | 92 attach_to_frames_(false), |
92 render_frame_host_(nullptr) { | 93 render_frame_host_(nullptr) { |
93 } | 94 } |
94 | 95 |
95 TargetHandler::~TargetHandler() { | 96 TargetHandler::~TargetHandler() { |
96 Disable(); | 97 Disable(); |
97 } | 98 } |
98 | 99 |
99 void TargetHandler::SetRenderFrameHost(RenderFrameHostImpl* render_frame_host) { | 100 void TargetHandler::SetRenderFrameHost(RenderFrameHostImpl* render_frame_host) { |
100 render_frame_host_ = render_frame_host; | 101 render_frame_host_ = render_frame_host; |
101 UpdateFrames(); | 102 UpdateFrames(); |
102 } | 103 } |
103 | 104 |
104 void TargetHandler::SetClient(std::unique_ptr<Client> client) { | 105 void TargetHandler::SetClient(std::unique_ptr<Client> client) { |
105 client_.swap(client); | 106 client_.swap(client); |
106 } | 107 } |
107 | 108 |
108 void TargetHandler::Detached() { | 109 void TargetHandler::Detached() { |
109 Disable(); | 110 Disable(); |
110 } | 111 } |
111 | 112 |
112 void TargetHandler::UpdateServiceWorkers() { | 113 void TargetHandler::UpdateServiceWorkers() { |
113 UpdateServiceWorkers(false); | 114 UpdateServiceWorkers(false); |
114 } | 115 } |
115 | 116 |
116 void TargetHandler::UpdateFrames() { | 117 void TargetHandler::UpdateFrames() { |
117 if (!enabled_ || !attach_to_frames_) | 118 if (!enabled_ || !auto_attach_ || !attach_to_frames_) |
118 return; | 119 return; |
119 | 120 |
120 HostsMap new_hosts; | 121 HostsMap new_hosts; |
121 if (render_frame_host_) { | 122 if (render_frame_host_) { |
122 FrameTreeNode* root = render_frame_host_->frame_tree_node(); | 123 FrameTreeNode* root = render_frame_host_->frame_tree_node(); |
123 std::queue<FrameTreeNode*> queue; | 124 std::queue<FrameTreeNode*> queue; |
124 queue.push(root); | 125 queue.push(root); |
125 while (!queue.empty()) { | 126 while (!queue.empty()) { |
126 FrameTreeNode* node = queue.front(); | 127 FrameTreeNode* node = queue.front(); |
127 queue.pop(); | 128 queue.pop(); |
128 bool cross_process = node->current_frame_host()->IsCrossProcessSubframe(); | 129 bool cross_process = node->current_frame_host()->IsCrossProcessSubframe(); |
129 if (node != root && cross_process) { | 130 if (node != root && cross_process) { |
130 scoped_refptr<DevToolsAgentHost> new_host = | 131 scoped_refptr<DevToolsAgentHost> new_host = |
131 DevToolsAgentHost::GetOrCreateFor(node->current_frame_host()); | 132 DevToolsAgentHost::GetOrCreateFor(node->current_frame_host()); |
132 new_hosts[new_host->GetId()] = new_host; | 133 new_hosts[new_host->GetId()] = new_host; |
133 } else { | 134 } else { |
134 for (size_t i = 0; i < node->child_count(); ++i) | 135 for (size_t i = 0; i < node->child_count(); ++i) |
135 queue.push(node->child_at(i)); | 136 queue.push(node->child_at(i)); |
136 } | 137 } |
137 } | 138 } |
138 } | 139 } |
139 | 140 |
140 // TODO(dgozman): support wait_for_debugger_on_start_. | 141 // TODO(dgozman): support wait_for_debugger_on_start_. |
141 ReattachTargetsOfType(new_hosts, DevToolsAgentHost::kTypeFrame, false); | 142 ReattachTargetsOfType(new_hosts, DevToolsAgentHost::kTypeFrame, false); |
142 } | 143 } |
143 | 144 |
144 void TargetHandler::UpdateServiceWorkers(bool waiting_for_debugger) { | 145 void TargetHandler::UpdateServiceWorkers(bool waiting_for_debugger) { |
145 if (!enabled_) | 146 if (!enabled_ || !auto_attach_) |
146 return; | 147 return; |
147 | 148 |
148 frame_urls_.clear(); | 149 frame_urls_.clear(); |
149 BrowserContext* browser_context = nullptr; | 150 BrowserContext* browser_context = nullptr; |
150 if (render_frame_host_) { | 151 if (render_frame_host_) { |
151 // TODO(dgozman): do not traverse inside cross-process subframes. | 152 // TODO(dgozman): do not traverse inside cross-process subframes. |
152 for (FrameTreeNode* node : | 153 for (FrameTreeNode* node : |
153 render_frame_host_->frame_tree_node()->frame_tree()->Nodes()) { | 154 render_frame_host_->frame_tree_node()->frame_tree()->Nodes()) { |
154 frame_urls_.insert(node->current_url()); | 155 frame_urls_.insert(node->current_url()); |
155 } | 156 } |
(...skipping 10 matching lines...) Expand all Loading... |
166 | 167 |
167 void TargetHandler::ReattachTargetsOfType( | 168 void TargetHandler::ReattachTargetsOfType( |
168 const HostsMap& new_hosts, | 169 const HostsMap& new_hosts, |
169 const std::string& type, | 170 const std::string& type, |
170 bool waiting_for_debugger) { | 171 bool waiting_for_debugger) { |
171 HostsMap old_hosts = attached_hosts_; | 172 HostsMap old_hosts = attached_hosts_; |
172 for (const auto& pair : old_hosts) { | 173 for (const auto& pair : old_hosts) { |
173 if (pair.second->GetType() == type && | 174 if (pair.second->GetType() == type && |
174 new_hosts.find(pair.first) == new_hosts.end()) { | 175 new_hosts.find(pair.first) == new_hosts.end()) { |
175 DetachFromTargetInternal(pair.second.get()); | 176 DetachFromTargetInternal(pair.second.get()); |
| 177 TargetRemovedInternal(pair.second.get()); |
176 } | 178 } |
177 } | 179 } |
178 for (const auto& pair : new_hosts) { | 180 for (const auto& pair : new_hosts) { |
179 if (old_hosts.find(pair.first) == old_hosts.end()) | 181 if (old_hosts.find(pair.first) == old_hosts.end()) { |
| 182 TargetCreatedInternal(pair.second.get()); |
180 AttachToTargetInternal(pair.second.get(), waiting_for_debugger); | 183 AttachToTargetInternal(pair.second.get(), waiting_for_debugger); |
| 184 } |
181 } | 185 } |
182 } | 186 } |
183 | 187 |
| 188 void TargetHandler::TargetCreatedInternal(DevToolsAgentHost* host) { |
| 189 client_->TargetCreated( |
| 190 TargetCreatedParams::Create()->set_target_info( |
| 191 TargetInfo::Create()->set_target_id(host->GetId()) |
| 192 ->set_title(host->GetTitle()) |
| 193 ->set_url(host->GetURL().spec()) |
| 194 ->set_type(host->GetType()))); |
| 195 } |
| 196 |
| 197 void TargetHandler::TargetRemovedInternal(DevToolsAgentHost* host) { |
| 198 client_->TargetRemoved(TargetRemovedParams::Create() |
| 199 ->set_target_id(host->GetId())); |
| 200 } |
| 201 |
184 void TargetHandler::AttachToTargetInternal( | 202 void TargetHandler::AttachToTargetInternal( |
185 DevToolsAgentHost* host, bool waiting_for_debugger) { | 203 DevToolsAgentHost* host, bool waiting_for_debugger) { |
186 if (host->IsAttached()) | 204 if (host->IsAttached()) |
187 return; | 205 return; |
188 attached_hosts_[host->GetId()] = host; | 206 attached_hosts_[host->GetId()] = host; |
189 host->AttachClient(this); | 207 host->AttachClient(this); |
190 client_->TargetCreated(TargetCreatedParams::Create() | 208 client_->AttachedToTarget(AttachedToTargetParams::Create() |
191 ->set_target_id(host->GetId()) | 209 ->set_target_id(host->GetId()) |
192 ->set_url(host->GetURL().spec()) | |
193 ->set_type(host->GetType()) | |
194 ->set_waiting_for_debugger(waiting_for_debugger)); | 210 ->set_waiting_for_debugger(waiting_for_debugger)); |
195 } | 211 } |
196 | 212 |
197 void TargetHandler::DetachFromTargetInternal(DevToolsAgentHost* host) { | 213 void TargetHandler::DetachFromTargetInternal(DevToolsAgentHost* host) { |
198 auto it = attached_hosts_.find(host->GetId()); | 214 auto it = attached_hosts_.find(host->GetId()); |
199 if (it == attached_hosts_.end()) | 215 if (it == attached_hosts_.end()) |
200 return; | 216 return; |
201 host->DetachClient(this); | 217 host->DetachClient(this); |
202 client_->TargetRemoved(TargetRemovedParams::Create()-> | 218 client_->DetachedFromTarget(DetachedFromTargetParams::Create()-> |
203 set_target_id(host->GetId())); | 219 set_target_id(host->GetId())); |
204 attached_hosts_.erase(it); | 220 attached_hosts_.erase(it); |
205 } | 221 } |
206 | 222 |
207 // ----------------- Protocol ---------------------- | 223 // ----------------- Protocol ---------------------- |
208 | 224 |
209 Response TargetHandler::Enable() { | 225 Response TargetHandler::Enable() { |
210 if (enabled_) | 226 if (enabled_) |
211 return Response::OK(); | 227 return Response::OK(); |
212 enabled_ = true; | 228 enabled_ = true; |
213 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); | 229 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); |
214 UpdateServiceWorkers(); | 230 UpdateServiceWorkers(); |
215 UpdateFrames(); | 231 UpdateFrames(); |
216 return Response::OK(); | 232 return Response::OK(); |
217 } | 233 } |
218 | 234 |
219 Response TargetHandler::Disable() { | 235 Response TargetHandler::Disable() { |
220 if (!enabled_) | 236 if (!enabled_) |
221 return Response::OK(); | 237 return Response::OK(); |
222 enabled_ = false; | 238 enabled_ = false; |
| 239 auto_attach_ = false; |
223 wait_for_debugger_on_start_ = false; | 240 wait_for_debugger_on_start_ = false; |
224 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this); | 241 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this); |
225 for (const auto& pair : attached_hosts_) | 242 for (const auto& pair : attached_hosts_) |
226 pair.second->DetachClient(this); | 243 pair.second->DetachClient(this); |
227 attached_hosts_.clear(); | 244 attached_hosts_.clear(); |
228 return Response::OK(); | 245 return Response::OK(); |
229 } | 246 } |
230 | 247 |
231 Response TargetHandler::SetWaitForDebuggerOnStart(bool value) { | 248 Response TargetHandler::SetAutoAttach( |
232 wait_for_debugger_on_start_ = value; | 249 bool auto_attach, bool wait_for_debugger_on_start) { |
| 250 wait_for_debugger_on_start_ = wait_for_debugger_on_start; |
| 251 if (auto_attach_ == auto_attach) |
| 252 return Response::OK(); |
| 253 auto_attach_ = auto_attach; |
| 254 if (auto_attach_) { |
| 255 UpdateServiceWorkers(); |
| 256 UpdateFrames(); |
| 257 } |
233 return Response::OK(); | 258 return Response::OK(); |
234 } | 259 } |
235 | 260 |
236 Response TargetHandler::SetAttachToFrames(bool value) { | 261 Response TargetHandler::SetAttachToFrames(bool value) { |
237 if (attach_to_frames_ == value) | 262 if (attach_to_frames_ == value) |
238 return Response::OK(); | 263 return Response::OK(); |
239 attach_to_frames_ = value; | 264 attach_to_frames_ = value; |
240 if (attach_to_frames_) { | 265 if (attach_to_frames_) { |
241 UpdateFrames(); | 266 UpdateFrames(); |
242 } else { | 267 } else { |
(...skipping 13 matching lines...) Expand all Loading... |
256 return Response::OK(); | 281 return Response::OK(); |
257 } | 282 } |
258 | 283 |
259 Response TargetHandler::GetTargetInfo( | 284 Response TargetHandler::GetTargetInfo( |
260 const std::string& target_id, | 285 const std::string& target_id, |
261 scoped_refptr<TargetInfo>* target_info) { | 286 scoped_refptr<TargetInfo>* target_info) { |
262 scoped_refptr<DevToolsAgentHost> agent_host( | 287 scoped_refptr<DevToolsAgentHost> agent_host( |
263 DevToolsAgentHost::GetForId(target_id)); | 288 DevToolsAgentHost::GetForId(target_id)); |
264 if (!agent_host) | 289 if (!agent_host) |
265 return Response::InvalidParams("No target with such id"); | 290 return Response::InvalidParams("No target with such id"); |
266 *target_info =TargetInfo::Create() | 291 *target_info = TargetInfo::Create() |
267 ->set_target_id(agent_host->GetId()) | 292 ->set_target_id(agent_host->GetId()) |
268 ->set_type(agent_host->GetType()) | 293 ->set_type(agent_host->GetType()) |
269 ->set_title(agent_host->GetTitle()) | 294 ->set_title(agent_host->GetTitle()) |
270 ->set_url(agent_host->GetURL().spec()); | 295 ->set_url(agent_host->GetURL().spec()); |
271 return Response::OK(); | 296 return Response::OK(); |
272 } | 297 } |
273 | 298 |
274 Response TargetHandler::ActivateTarget(const std::string& target_id) { | 299 Response TargetHandler::ActivateTarget(const std::string& target_id) { |
275 scoped_refptr<DevToolsAgentHost> agent_host( | 300 scoped_refptr<DevToolsAgentHost> agent_host( |
276 DevToolsAgentHost::GetForId(target_id)); | 301 DevToolsAgentHost::GetForId(target_id)); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 } | 364 } |
340 | 365 |
341 void TargetHandler::WorkerDestroyed( | 366 void TargetHandler::WorkerDestroyed( |
342 ServiceWorkerDevToolsAgentHost* host) { | 367 ServiceWorkerDevToolsAgentHost* host) { |
343 UpdateServiceWorkers(); | 368 UpdateServiceWorkers(); |
344 } | 369 } |
345 | 370 |
346 } // namespace target | 371 } // namespace target |
347 } // namespace devtools | 372 } // namespace devtools |
348 } // namespace content | 373 } // namespace content |
OLD | NEW |