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

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

Issue 2383743003: [DevTools] Auto-attach to cross-process subframes under experiment. (Closed)
Patch Set: some ui Created 4 years, 2 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 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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 wait_for_debugger_on_start_(false), 90 wait_for_debugger_on_start_(false),
91 attach_to_frames_(false),
91 render_frame_host_(nullptr) { 92 render_frame_host_(nullptr) {
92 } 93 }
93 94
94 TargetHandler::~TargetHandler() { 95 TargetHandler::~TargetHandler() {
95 Disable(); 96 Disable();
96 } 97 }
97 98
98 void TargetHandler::SetRenderFrameHost(RenderFrameHostImpl* render_frame_host) { 99 void TargetHandler::SetRenderFrameHost(RenderFrameHostImpl* render_frame_host) {
99 render_frame_host_ = render_frame_host; 100 render_frame_host_ = render_frame_host;
101 UpdateFrames();
100 } 102 }
101 103
102 void TargetHandler::SetClient(std::unique_ptr<Client> client) { 104 void TargetHandler::SetClient(std::unique_ptr<Client> client) {
103 client_.swap(client); 105 client_.swap(client);
104 } 106 }
105 107
106 void TargetHandler::Detached() { 108 void TargetHandler::Detached() {
107 Disable(); 109 Disable();
108 } 110 }
109 111
110 void TargetHandler::UpdateServiceWorkers() { 112 void TargetHandler::UpdateServiceWorkers() {
111 UpdateServiceWorkers(false); 113 UpdateServiceWorkers(false);
112 } 114 }
113 115
116 void TargetHandler::UpdateFrames() {
117 if (!enabled_ || !attach_to_frames_)
118 return;
119
120 HostsMap new_hosts;
121 if (render_frame_host_) {
122 FrameTreeNode* root = render_frame_host_->frame_tree_node();
123 std::queue<FrameTreeNode*> queue;
124 queue.push(root);
125 while (!queue.empty()) {
126 FrameTreeNode* node = queue.front();
127 queue.pop();
128 bool cross_process = node->current_frame_host()->IsCrossProcessSubframe();
129 if (node != root && cross_process) {
130 scoped_refptr<DevToolsAgentHost> new_host =
131 DevToolsAgentHost::GetOrCreateFor(node->current_frame_host());
132 new_hosts[new_host->GetId()] = new_host;
133 } else {
134 for (size_t i = 0; i < node->child_count(); ++i)
135 queue.push(node->child_at(i));
136 }
137 }
138 }
139
140 // TODO(dgozman): support wait_for_debugger_on_start_.
141 ReattachTargetsOfType(new_hosts, DevToolsAgentHost::kTypeFrame, false);
142 }
143
114 void TargetHandler::UpdateServiceWorkers(bool waiting_for_debugger) { 144 void TargetHandler::UpdateServiceWorkers(bool waiting_for_debugger) {
115 if (!enabled_) 145 if (!enabled_)
116 return; 146 return;
117 147
118 frame_urls_.clear(); 148 frame_urls_.clear();
119 BrowserContext* browser_context = nullptr; 149 BrowserContext* browser_context = nullptr;
120 if (render_frame_host_) { 150 if (render_frame_host_) {
121 // TODO(dgozman): do not traverse inside cross-process subframes. 151 // TODO(dgozman): do not traverse inside cross-process subframes.
122 for (FrameTreeNode* node : 152 for (FrameTreeNode* node :
123 render_frame_host_->frame_tree_node()->frame_tree()->Nodes()) { 153 render_frame_host_->frame_tree_node()->frame_tree()->Nodes()) {
124 frame_urls_.insert(node->current_url()); 154 frame_urls_.insert(node->current_url());
125 } 155 }
126 browser_context = render_frame_host_->GetProcess()->GetBrowserContext(); 156 browser_context = render_frame_host_->GetProcess()->GetBrowserContext();
127 } 157 }
128 158
129 std::map<std::string, scoped_refptr<DevToolsAgentHost>> old_hosts = 159 auto matching = GetMatchingServiceWorkers(browser_context, frame_urls_);
130 attached_hosts_; 160 HostsMap new_hosts;
131 ServiceWorkerDevToolsAgentHost::Map new_hosts = 161 for (const auto& pair : matching)
132 GetMatchingServiceWorkers(browser_context, frame_urls_); 162 new_hosts[pair.first] = pair.second;
163 ReattachTargetsOfType(
164 new_hosts, DevToolsAgentHost::kTypeServiceWorker, waiting_for_debugger);
165 }
133 166
167 void TargetHandler::ReattachTargetsOfType(
168 const HostsMap& new_hosts,
169 const std::string& type,
170 bool waiting_for_debugger) {
171 HostsMap old_hosts = attached_hosts_;
134 for (const auto& pair : old_hosts) { 172 for (const auto& pair : old_hosts) {
135 if (pair.second->GetType() == DevToolsAgentHost::kTypeServiceWorker && 173 if (pair.second->GetType() == type &&
136 new_hosts.find(pair.first) == new_hosts.end()) { 174 new_hosts.find(pair.first) == new_hosts.end()) {
137 DetachFromTargetInternal(pair.second.get()); 175 DetachFromTargetInternal(pair.second.get());
138 } 176 }
139 } 177 }
140
141 for (const auto& pair : new_hosts) { 178 for (const auto& pair : new_hosts) {
142 if (old_hosts.find(pair.first) == old_hosts.end()) 179 if (old_hosts.find(pair.first) == old_hosts.end())
143 AttachToTargetInternal(pair.second.get(), waiting_for_debugger); 180 AttachToTargetInternal(pair.second.get(), waiting_for_debugger);
144 } 181 }
145 } 182 }
146 183
147 void TargetHandler::AttachToTargetInternal( 184 void TargetHandler::AttachToTargetInternal(
148 DevToolsAgentHost* host, bool waiting_for_debugger) { 185 DevToolsAgentHost* host, bool waiting_for_debugger) {
149 if (host->IsAttached()) 186 if (host->IsAttached())
150 return; 187 return;
(...skipping 17 matching lines...) Expand all
168 } 205 }
169 206
170 // ----------------- Protocol ---------------------- 207 // ----------------- Protocol ----------------------
171 208
172 Response TargetHandler::Enable() { 209 Response TargetHandler::Enable() {
173 if (enabled_) 210 if (enabled_)
174 return Response::OK(); 211 return Response::OK();
175 enabled_ = true; 212 enabled_ = true;
176 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); 213 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this);
177 UpdateServiceWorkers(); 214 UpdateServiceWorkers();
215 UpdateFrames();
178 return Response::OK(); 216 return Response::OK();
179 } 217 }
180 218
181 Response TargetHandler::Disable() { 219 Response TargetHandler::Disable() {
182 if (!enabled_) 220 if (!enabled_)
183 return Response::OK(); 221 return Response::OK();
184 enabled_ = false; 222 enabled_ = false;
185 wait_for_debugger_on_start_ = false; 223 wait_for_debugger_on_start_ = false;
186 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this); 224 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this);
187 for (const auto& pair : attached_hosts_) 225 for (const auto& pair : attached_hosts_)
188 pair.second->DetachClient(this); 226 pair.second->DetachClient(this);
189 attached_hosts_.clear(); 227 attached_hosts_.clear();
190 return Response::OK(); 228 return Response::OK();
191 } 229 }
192 230
193 Response TargetHandler::SetWaitForDebuggerOnStart(bool value) { 231 Response TargetHandler::SetWaitForDebuggerOnStart(bool value) {
194 wait_for_debugger_on_start_ = value; 232 wait_for_debugger_on_start_ = value;
195 return Response::OK(); 233 return Response::OK();
196 } 234 }
197 235
236 Response TargetHandler::SetAttachToFrames(bool value) {
237 if (attach_to_frames_ == value)
238 return Response::OK();
239 attach_to_frames_ = value;
240 if (attach_to_frames_) {
241 UpdateFrames();
242 } else {
243 HostsMap empty;
244 ReattachTargetsOfType(empty, DevToolsAgentHost::kTypeFrame, false);
245 }
246 return Response::OK();
247 }
248
198 Response TargetHandler::SendMessageToTarget( 249 Response TargetHandler::SendMessageToTarget(
199 const std::string& target_id, 250 const std::string& target_id,
200 const std::string& message) { 251 const std::string& message) {
201 auto it = attached_hosts_.find(target_id); 252 auto it = attached_hosts_.find(target_id);
202 if (it == attached_hosts_.end()) 253 if (it == attached_hosts_.end())
203 return Response::InternalError("Not attached to the target"); 254 return Response::InternalError("Not attached to the target");
204 it->second->DispatchProtocolMessage(this, message); 255 it->second->DispatchProtocolMessage(this, message);
205 return Response::OK(); 256 return Response::OK();
206 } 257 }
207 258
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 } 339 }
289 340
290 void TargetHandler::WorkerDestroyed( 341 void TargetHandler::WorkerDestroyed(
291 ServiceWorkerDevToolsAgentHost* host) { 342 ServiceWorkerDevToolsAgentHost* host) {
292 UpdateServiceWorkers(); 343 UpdateServiceWorkers();
293 } 344 }
294 345
295 } // namespace target 346 } // namespace target
296 } // namespace devtools 347 } // namespace devtools
297 } // namespace content 348 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/devtools/protocol/target_handler.h ('k') | content/browser/devtools/render_frame_devtools_agent_host.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698