| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_auto_attacher.h" |
| 6 | 6 |
| 7 #include "content/browser/devtools/devtools_manager.h" | |
| 8 #include "content/browser/devtools/devtools_session.h" | |
| 9 #include "content/browser/devtools/render_frame_devtools_agent_host.h" | 7 #include "content/browser/devtools/render_frame_devtools_agent_host.h" |
| 10 #include "content/browser/devtools/service_worker_devtools_agent_host.h" | 8 #include "content/browser/devtools/service_worker_devtools_agent_host.h" |
| 11 #include "content/browser/frame_host/frame_tree.h" | 9 #include "content/browser/frame_host/frame_tree.h" |
| 12 #include "content/browser/frame_host/frame_tree_node.h" | 10 #include "content/browser/frame_host/frame_tree_node.h" |
| 13 #include "content/browser/frame_host/render_frame_host_impl.h" | 11 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 14 | 12 |
| 15 namespace content { | 13 namespace content { |
| 16 namespace protocol { | 14 namespace protocol { |
| 17 | 15 |
| 18 namespace { | 16 namespace { |
| 19 | 17 |
| 20 using ScopeAgentsMap = | 18 using ScopeAgentsMap = |
| 21 std::map<GURL, std::unique_ptr<ServiceWorkerDevToolsAgentHost::List>>; | 19 std::map<GURL, std::unique_ptr<ServiceWorkerDevToolsAgentHost::List>>; |
| 22 | 20 |
| 23 void GetMatchingHostsByScopeMap( | 21 void GetMatchingHostsByScopeMap( |
| 24 const ServiceWorkerDevToolsAgentHost::List& agent_hosts, | 22 const ServiceWorkerDevToolsAgentHost::List& agent_hosts, |
| 25 const std::set<GURL>& urls, | 23 const base::flat_set<GURL>& urls, |
| 26 ScopeAgentsMap* scope_agents_map) { | 24 ScopeAgentsMap* scope_agents_map) { |
| 27 std::set<base::StringPiece> host_name_set; | 25 base::flat_set<base::StringPiece> host_name_set; |
| 28 for (const GURL& url : urls) | 26 for (const GURL& url : urls) |
| 29 host_name_set.insert(url.host_piece()); | 27 host_name_set.insert(url.host_piece()); |
| 30 for (const auto& host : agent_hosts) { | 28 for (const auto& host : agent_hosts) { |
| 31 if (host_name_set.find(host->scope().host_piece()) == host_name_set.end()) | 29 if (host_name_set.find(host->scope().host_piece()) == host_name_set.end()) |
| 32 continue; | 30 continue; |
| 33 const auto& it = scope_agents_map->find(host->scope()); | 31 const auto& it = scope_agents_map->find(host->scope()); |
| 34 if (it == scope_agents_map->end()) { | 32 if (it == scope_agents_map->end()) { |
| 35 std::unique_ptr<ServiceWorkerDevToolsAgentHost::List> new_list( | 33 std::unique_ptr<ServiceWorkerDevToolsAgentHost::List> new_list( |
| 36 new ServiceWorkerDevToolsAgentHost::List()); | 34 new ServiceWorkerDevToolsAgentHost::List()); |
| 37 new_list->push_back(host); | 35 new_list->push_back(host); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 58 if (host->version_doomed_time().is_null() || | 56 if (host->version_doomed_time().is_null() || |
| 59 (last_installed_time < last_doomed_time && | 57 (last_installed_time < last_doomed_time && |
| 60 last_doomed_time == host->version_doomed_time())) { | 58 last_doomed_time == host->version_doomed_time())) { |
| 61 (*result)[host->GetId()] = host; | 59 (*result)[host->GetId()] = host; |
| 62 } | 60 } |
| 63 } | 61 } |
| 64 } | 62 } |
| 65 | 63 |
| 66 ServiceWorkerDevToolsAgentHost::Map GetMatchingServiceWorkers( | 64 ServiceWorkerDevToolsAgentHost::Map GetMatchingServiceWorkers( |
| 67 BrowserContext* browser_context, | 65 BrowserContext* browser_context, |
| 68 const std::set<GURL>& urls) { | 66 const base::flat_set<GURL>& urls) { |
| 69 ServiceWorkerDevToolsAgentHost::Map result; | 67 ServiceWorkerDevToolsAgentHost::Map result; |
| 70 if (!browser_context) | 68 if (!browser_context) |
| 71 return result; | 69 return result; |
| 72 | 70 |
| 73 ServiceWorkerDevToolsAgentHost::List agent_hosts; | 71 ServiceWorkerDevToolsAgentHost::List agent_hosts; |
| 74 ServiceWorkerDevToolsManager::GetInstance() | 72 ServiceWorkerDevToolsManager::GetInstance() |
| 75 ->AddAllAgentHostsForBrowserContext(browser_context, &agent_hosts); | 73 ->AddAllAgentHostsForBrowserContext(browser_context, &agent_hosts); |
| 76 | 74 |
| 77 ScopeAgentsMap scope_agents_map; | 75 ScopeAgentsMap scope_agents_map; |
| 78 GetMatchingHostsByScopeMap(agent_hosts, urls, &scope_agents_map); | 76 GetMatchingHostsByScopeMap(agent_hosts, urls, &scope_agents_map); |
| 79 | 77 |
| 80 for (const auto& it : scope_agents_map) | 78 for (const auto& it : scope_agents_map) |
| 81 AddEligibleHosts(*it.second.get(), &result); | 79 AddEligibleHosts(*it.second.get(), &result); |
| 82 | 80 |
| 83 return result; | 81 return result; |
| 84 } | 82 } |
| 85 | 83 |
| 86 std::unique_ptr<Target::TargetInfo> CreateInfo(DevToolsAgentHost* host) { | |
| 87 return Target::TargetInfo::Create() | |
| 88 .SetTargetId(host->GetId()) | |
| 89 .SetTitle(host->GetTitle()) | |
| 90 .SetUrl(host->GetURL().spec()) | |
| 91 .SetType(host->GetType()) | |
| 92 .SetAttached(host->IsAttached()) | |
| 93 .Build(); | |
| 94 } | |
| 95 | |
| 96 } // namespace | 84 } // namespace |
| 97 | 85 |
| 98 TargetHandler::TargetHandler() | 86 TargetAutoAttacher::TargetAutoAttacher(AttachCallback attach_callback, |
| 99 : DevToolsDomainHandler(Target::Metainfo::domainName), | 87 DetachCallback detach_callback) |
| 100 discover_(false), | 88 : attach_callback_(attach_callback), |
| 89 detach_callback_(detach_callback), |
| 90 render_frame_host_(nullptr), |
| 101 auto_attach_(false), | 91 auto_attach_(false), |
| 102 wait_for_debugger_on_start_(false), | 92 wait_for_debugger_on_start_(false), |
| 103 attach_to_frames_(false), | 93 attach_to_frames_(false) {} |
| 104 render_frame_host_(nullptr) { | 94 |
| 95 TargetAutoAttacher::~TargetAutoAttacher() {} |
| 96 |
| 97 void TargetAutoAttacher::SetRenderFrameHost( |
| 98 RenderFrameHostImpl* render_frame_host) { |
| 99 render_frame_host_ = render_frame_host; |
| 100 UpdateFrames(); |
| 101 ReattachServiceWorkers(false); |
| 105 } | 102 } |
| 106 | 103 |
| 107 TargetHandler::~TargetHandler() { | 104 void TargetAutoAttacher::UpdateServiceWorkers() { |
| 105 ReattachServiceWorkers(false); |
| 108 } | 106 } |
| 109 | 107 |
| 110 // static | 108 void TargetAutoAttacher::UpdateFrames() { |
| 111 std::vector<TargetHandler*> TargetHandler::ForAgentHost( | |
| 112 DevToolsAgentHostImpl* host) { | |
| 113 return DevToolsSession::HandlersForAgentHost<TargetHandler>( | |
| 114 host, Target::Metainfo::domainName); | |
| 115 } | |
| 116 | |
| 117 void TargetHandler::Wire(UberDispatcher* dispatcher) { | |
| 118 frontend_.reset(new Target::Frontend(dispatcher->channel())); | |
| 119 Target::Dispatcher::wire(dispatcher, this); | |
| 120 } | |
| 121 | |
| 122 void TargetHandler::SetRenderFrameHost(RenderFrameHostImpl* render_frame_host) { | |
| 123 render_frame_host_ = render_frame_host; | |
| 124 UpdateFrames(); | |
| 125 } | |
| 126 | |
| 127 Response TargetHandler::Disable() { | |
| 128 SetAutoAttach(false, false); | |
| 129 SetDiscoverTargets(false); | |
| 130 for (const auto& id_host : attached_hosts_) | |
| 131 id_host.second->DetachClient(this); | |
| 132 attached_hosts_.clear(); | |
| 133 return Response::OK(); | |
| 134 } | |
| 135 | |
| 136 void TargetHandler::UpdateServiceWorkers() { | |
| 137 UpdateServiceWorkers(false); | |
| 138 } | |
| 139 | |
| 140 void TargetHandler::UpdateFrames() { | |
| 141 if (!auto_attach_ || !attach_to_frames_) | 109 if (!auto_attach_ || !attach_to_frames_) |
| 142 return; | 110 return; |
| 143 | 111 |
| 144 HostsMap new_hosts; | 112 Hosts new_hosts; |
| 145 if (render_frame_host_) { | 113 if (render_frame_host_) { |
| 146 FrameTreeNode* root = render_frame_host_->frame_tree_node(); | 114 FrameTreeNode* root = render_frame_host_->frame_tree_node(); |
| 147 std::queue<FrameTreeNode*> queue; | 115 std::queue<FrameTreeNode*> queue; |
| 148 queue.push(root); | 116 queue.push(root); |
| 149 while (!queue.empty()) { | 117 while (!queue.empty()) { |
| 150 FrameTreeNode* node = queue.front(); | 118 FrameTreeNode* node = queue.front(); |
| 151 queue.pop(); | 119 queue.pop(); |
| 152 bool cross_process = node->current_frame_host()->IsCrossProcessSubframe(); | 120 bool cross_process = node->current_frame_host()->IsCrossProcessSubframe(); |
| 153 if (node != root && cross_process) { | 121 if (node != root && cross_process) { |
| 154 scoped_refptr<DevToolsAgentHost> new_host = | 122 scoped_refptr<DevToolsAgentHost> new_host = |
| 155 RenderFrameDevToolsAgentHost::GetOrCreateFor(node); | 123 RenderFrameDevToolsAgentHost::GetOrCreateFor(node); |
| 156 new_hosts[new_host->GetId()] = new_host; | 124 new_hosts.insert(new_host); |
| 157 } else { | 125 } else { |
| 158 for (size_t i = 0; i < node->child_count(); ++i) | 126 for (size_t i = 0; i < node->child_count(); ++i) |
| 159 queue.push(node->child_at(i)); | 127 queue.push(node->child_at(i)); |
| 160 } | 128 } |
| 161 } | 129 } |
| 162 } | 130 } |
| 163 | 131 |
| 164 // TODO(dgozman): support wait_for_debugger_on_start_. | 132 // TODO(dgozman): support wait_for_debugger_on_start_. |
| 165 ReattachTargetsOfType(new_hosts, DevToolsAgentHost::kTypeFrame, false); | 133 ReattachTargetsOfType(new_hosts, DevToolsAgentHost::kTypeFrame, false); |
| 166 } | 134 } |
| 167 | 135 |
| 168 void TargetHandler::UpdateServiceWorkers(bool waiting_for_debugger) { | 136 void TargetAutoAttacher::AgentHostClosed(DevToolsAgentHost* host) { |
| 137 auto_attached_hosts_.erase(host); |
| 138 } |
| 139 |
| 140 void TargetAutoAttacher::ReattachServiceWorkers(bool waiting_for_debugger) { |
| 169 if (!auto_attach_) | 141 if (!auto_attach_) |
| 170 return; | 142 return; |
| 171 | 143 |
| 172 frame_urls_.clear(); | 144 frame_urls_.clear(); |
| 173 BrowserContext* browser_context = nullptr; | 145 BrowserContext* browser_context = nullptr; |
| 174 if (render_frame_host_) { | 146 if (render_frame_host_) { |
| 175 // TODO(dgozman): do not traverse inside cross-process subframes. | 147 // TODO(dgozman): do not traverse inside cross-process subframes. |
| 176 for (FrameTreeNode* node : | 148 for (FrameTreeNode* node : |
| 177 render_frame_host_->frame_tree_node()->frame_tree()->Nodes()) { | 149 render_frame_host_->frame_tree_node()->frame_tree()->Nodes()) { |
| 178 frame_urls_.insert(node->current_url()); | 150 frame_urls_.insert(node->current_url()); |
| 179 } | 151 } |
| 180 browser_context = render_frame_host_->GetProcess()->GetBrowserContext(); | 152 browser_context = render_frame_host_->GetProcess()->GetBrowserContext(); |
| 181 } | 153 } |
| 182 | 154 |
| 183 auto matching = GetMatchingServiceWorkers(browser_context, frame_urls_); | 155 auto matching = GetMatchingServiceWorkers(browser_context, frame_urls_); |
| 184 HostsMap new_hosts; | 156 Hosts new_hosts; |
| 185 for (const auto& pair : matching) { | 157 for (const auto& pair : matching) { |
| 186 if (pair.second->IsReadyForInspection()) | 158 if (pair.second->IsReadyForInspection()) |
| 187 new_hosts[pair.first] = pair.second; | 159 new_hosts.insert(pair.second); |
| 188 } | 160 } |
| 189 ReattachTargetsOfType( | 161 ReattachTargetsOfType(new_hosts, DevToolsAgentHost::kTypeServiceWorker, |
| 190 new_hosts, DevToolsAgentHost::kTypeServiceWorker, waiting_for_debugger); | 162 waiting_for_debugger); |
| 191 } | 163 } |
| 192 | 164 |
| 193 void TargetHandler::ReattachTargetsOfType( | 165 void TargetAutoAttacher::ReattachTargetsOfType(const Hosts& new_hosts, |
| 194 const HostsMap& new_hosts, | 166 const std::string& type, |
| 195 const std::string& type, | 167 bool waiting_for_debugger) { |
| 196 bool waiting_for_debugger) { | 168 Hosts old_hosts = auto_attached_hosts_; |
| 197 HostsMap old_hosts = attached_hosts_; | 169 for (auto& it : old_hosts) { |
| 198 for (const auto& pair : old_hosts) { | 170 DevToolsAgentHost* host = it.get(); |
| 199 if (pair.second->GetType() == type && | 171 if (host->GetType() == type && new_hosts.find(host) == new_hosts.end()) { |
| 200 new_hosts.find(pair.first) == new_hosts.end()) { | 172 auto_attached_hosts_.erase(host); |
| 201 DetachFromTargetInternal(pair.second.get()); | 173 detach_callback_.Run(host); |
| 202 } | 174 } |
| 203 } | 175 } |
| 204 for (const auto& pair : new_hosts) { | 176 for (auto& it : new_hosts) { |
| 205 if (old_hosts.find(pair.first) == old_hosts.end()) | 177 DevToolsAgentHost* host = it.get(); |
| 206 AttachToTargetInternal(pair.second.get(), waiting_for_debugger); | 178 if (old_hosts.find(host) == old_hosts.end()) { |
| 179 if (attach_callback_.Run(host, waiting_for_debugger)) |
| 180 auto_attached_hosts_.insert(host); |
| 181 } |
| 207 } | 182 } |
| 208 } | 183 } |
| 209 | 184 |
| 210 void TargetHandler::TargetCreatedInternal(DevToolsAgentHost* host) { | 185 void TargetAutoAttacher::SetAutoAttach(bool auto_attach, |
| 211 if (reported_hosts_.find(host->GetId()) != reported_hosts_.end()) | 186 bool wait_for_debugger_on_start) { |
| 212 return; | |
| 213 frontend_->TargetCreated(CreateInfo(host)); | |
| 214 reported_hosts_[host->GetId()] = host; | |
| 215 } | |
| 216 | |
| 217 void TargetHandler::TargetInfoChangedInternal(DevToolsAgentHost* host) { | |
| 218 if (reported_hosts_.find(host->GetId()) == reported_hosts_.end()) | |
| 219 return; | |
| 220 frontend_->TargetInfoChanged(CreateInfo(host)); | |
| 221 } | |
| 222 | |
| 223 void TargetHandler::TargetDestroyedInternal(DevToolsAgentHost* host) { | |
| 224 auto it = reported_hosts_.find(host->GetId()); | |
| 225 if (it == reported_hosts_.end()) | |
| 226 return; | |
| 227 if (discover_) | |
| 228 frontend_->TargetDestroyed(host->GetId()); | |
| 229 reported_hosts_.erase(it); | |
| 230 } | |
| 231 | |
| 232 bool TargetHandler::AttachToTargetInternal( | |
| 233 DevToolsAgentHost* host, bool waiting_for_debugger) { | |
| 234 attached_hosts_[host->GetId()] = host; | |
| 235 if (!host->AttachClient(this)) { | |
| 236 attached_hosts_.erase(host->GetId()); | |
| 237 return false; | |
| 238 } | |
| 239 frontend_->AttachedToTarget(CreateInfo(host), waiting_for_debugger); | |
| 240 return true; | |
| 241 } | |
| 242 | |
| 243 void TargetHandler::DetachFromTargetInternal(DevToolsAgentHost* host) { | |
| 244 auto it = attached_hosts_.find(host->GetId()); | |
| 245 if (it == attached_hosts_.end()) | |
| 246 return; | |
| 247 host->DetachClient(this); | |
| 248 frontend_->DetachedFromTarget(host->GetId()); | |
| 249 attached_hosts_.erase(it); | |
| 250 } | |
| 251 | |
| 252 // ----------------- Protocol ---------------------- | |
| 253 | |
| 254 Response TargetHandler::SetDiscoverTargets(bool discover) { | |
| 255 if (discover_ == discover) | |
| 256 return Response::OK(); | |
| 257 discover_ = discover; | |
| 258 if (discover_) { | |
| 259 DevToolsAgentHost::AddObserver(this); | |
| 260 } else { | |
| 261 DevToolsAgentHost::RemoveObserver(this); | |
| 262 RawHostsMap copy = reported_hosts_; | |
| 263 for (const auto& id_host : copy) | |
| 264 TargetDestroyedInternal(id_host.second); | |
| 265 } | |
| 266 return Response::OK(); | |
| 267 } | |
| 268 | |
| 269 Response TargetHandler::SetAutoAttach( | |
| 270 bool auto_attach, bool wait_for_debugger_on_start) { | |
| 271 wait_for_debugger_on_start_ = wait_for_debugger_on_start; | 187 wait_for_debugger_on_start_ = wait_for_debugger_on_start; |
| 272 if (auto_attach_ == auto_attach) | 188 if (auto_attach_ == auto_attach) |
| 273 return Response::FallThrough(); | 189 return; |
| 274 auto_attach_ = auto_attach; | 190 auto_attach_ = auto_attach; |
| 275 if (auto_attach_) { | 191 if (auto_attach_) { |
| 276 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); | 192 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); |
| 277 UpdateServiceWorkers(); | 193 ReattachServiceWorkers(false); |
| 278 UpdateFrames(); | 194 UpdateFrames(); |
| 279 } else { | 195 } else { |
| 280 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this); | 196 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this); |
| 281 HostsMap empty; | 197 Hosts empty; |
| 282 ReattachTargetsOfType(empty, DevToolsAgentHost::kTypeFrame, false); | 198 ReattachTargetsOfType(empty, DevToolsAgentHost::kTypeFrame, false); |
| 283 ReattachTargetsOfType(empty, DevToolsAgentHost::kTypeServiceWorker, false); | 199 ReattachTargetsOfType(empty, DevToolsAgentHost::kTypeServiceWorker, false); |
| 200 DCHECK(auto_attached_hosts_.empty()); |
| 284 } | 201 } |
| 285 return Response::FallThrough(); | |
| 286 } | 202 } |
| 287 | 203 |
| 288 Response TargetHandler::SetAttachToFrames(bool value) { | 204 void TargetAutoAttacher::SetAttachToFrames(bool attach_to_frames) { |
| 289 if (attach_to_frames_ == value) | 205 if (attach_to_frames_ == attach_to_frames) |
| 290 return Response::OK(); | 206 return; |
| 291 attach_to_frames_ = value; | 207 attach_to_frames_ = attach_to_frames; |
| 292 if (attach_to_frames_) { | 208 if (attach_to_frames_) { |
| 293 UpdateFrames(); | 209 UpdateFrames(); |
| 294 } else { | 210 } else { |
| 295 HostsMap empty; | 211 Hosts empty; |
| 296 ReattachTargetsOfType(empty, DevToolsAgentHost::kTypeFrame, false); | 212 ReattachTargetsOfType(empty, DevToolsAgentHost::kTypeFrame, false); |
| 297 } | 213 } |
| 298 return Response::OK(); | |
| 299 } | |
| 300 | |
| 301 Response TargetHandler::SetRemoteLocations( | |
| 302 std::unique_ptr<protocol::Array<Target::RemoteLocation>>) { | |
| 303 return Response::Error("Not supported"); | |
| 304 } | |
| 305 | |
| 306 Response TargetHandler::AttachToTarget(const std::string& target_id, | |
| 307 bool* out_success) { | |
| 308 // TODO(dgozman): only allow reported hosts. | |
| 309 scoped_refptr<DevToolsAgentHost> agent_host = | |
| 310 DevToolsAgentHost::GetForId(target_id); | |
| 311 if (!agent_host) | |
| 312 return Response::InvalidParams("No target with given id found"); | |
| 313 *out_success = AttachToTargetInternal(agent_host.get(), false); | |
| 314 return Response::OK(); | |
| 315 } | |
| 316 | |
| 317 Response TargetHandler::DetachFromTarget(const std::string& target_id) { | |
| 318 auto it = attached_hosts_.find(target_id); | |
| 319 if (it == attached_hosts_.end()) | |
| 320 return Response::Error("Not attached to the target"); | |
| 321 DevToolsAgentHost* agent_host = it->second.get(); | |
| 322 DetachFromTargetInternal(agent_host); | |
| 323 return Response::OK(); | |
| 324 } | |
| 325 | |
| 326 Response TargetHandler::SendMessageToTarget( | |
| 327 const std::string& target_id, | |
| 328 const std::string& message) { | |
| 329 auto it = attached_hosts_.find(target_id); | |
| 330 if (it == attached_hosts_.end()) | |
| 331 return Response::FallThrough(); | |
| 332 it->second->DispatchProtocolMessage(this, message); | |
| 333 return Response::OK(); | |
| 334 } | |
| 335 | |
| 336 Response TargetHandler::GetTargetInfo( | |
| 337 const std::string& target_id, | |
| 338 std::unique_ptr<Target::TargetInfo>* target_info) { | |
| 339 // TODO(dgozman): only allow reported hosts. | |
| 340 scoped_refptr<DevToolsAgentHost> agent_host( | |
| 341 DevToolsAgentHost::GetForId(target_id)); | |
| 342 if (!agent_host) | |
| 343 return Response::InvalidParams("No target with given id found"); | |
| 344 *target_info = CreateInfo(agent_host.get()); | |
| 345 return Response::OK(); | |
| 346 } | |
| 347 | |
| 348 Response TargetHandler::ActivateTarget(const std::string& target_id) { | |
| 349 // TODO(dgozman): only allow reported hosts. | |
| 350 scoped_refptr<DevToolsAgentHost> agent_host( | |
| 351 DevToolsAgentHost::GetForId(target_id)); | |
| 352 if (!agent_host) | |
| 353 return Response::InvalidParams("No target with given id found"); | |
| 354 agent_host->Activate(); | |
| 355 return Response::OK(); | |
| 356 } | |
| 357 | |
| 358 Response TargetHandler::CloseTarget(const std::string& target_id, | |
| 359 bool* out_success) { | |
| 360 scoped_refptr<DevToolsAgentHost> agent_host = | |
| 361 DevToolsAgentHost::GetForId(target_id); | |
| 362 if (!agent_host) | |
| 363 return Response::InvalidParams("No target with given id found"); | |
| 364 *out_success = agent_host->Close(); | |
| 365 return Response::OK(); | |
| 366 } | |
| 367 | |
| 368 Response TargetHandler::CreateBrowserContext(std::string* out_context_id) { | |
| 369 return Response::Error("Not supported"); | |
| 370 } | |
| 371 | |
| 372 Response TargetHandler::DisposeBrowserContext(const std::string& context_id, | |
| 373 bool* out_success) { | |
| 374 return Response::Error("Not supported"); | |
| 375 } | |
| 376 | |
| 377 Response TargetHandler::CreateTarget(const std::string& url, | |
| 378 Maybe<int> width, | |
| 379 Maybe<int> height, | |
| 380 Maybe<std::string> context_id, | |
| 381 std::string* out_target_id) { | |
| 382 DevToolsManagerDelegate* delegate = | |
| 383 DevToolsManager::GetInstance()->delegate(); | |
| 384 if (!delegate) | |
| 385 return Response::Error("Not supported"); | |
| 386 scoped_refptr<content::DevToolsAgentHost> agent_host = | |
| 387 delegate->CreateNewTarget(GURL(url)); | |
| 388 if (!agent_host) | |
| 389 return Response::Error("Not supported"); | |
| 390 *out_target_id = agent_host->GetId(); | |
| 391 return Response::OK(); | |
| 392 } | |
| 393 | |
| 394 Response TargetHandler::GetTargets( | |
| 395 std::unique_ptr<protocol::Array<Target::TargetInfo>>* target_infos) { | |
| 396 *target_infos = protocol::Array<Target::TargetInfo>::create(); | |
| 397 for (const auto& host : DevToolsAgentHost::GetOrCreateAll()) | |
| 398 (*target_infos)->addItem(CreateInfo(host.get())); | |
| 399 return Response::OK(); | |
| 400 } | |
| 401 | |
| 402 // ---------------- DevToolsAgentHostClient ---------------- | |
| 403 | |
| 404 void TargetHandler::DispatchProtocolMessage( | |
| 405 DevToolsAgentHost* host, | |
| 406 const std::string& message) { | |
| 407 auto it = attached_hosts_.find(host->GetId()); | |
| 408 if (it == attached_hosts_.end()) | |
| 409 return; // Already disconnected. | |
| 410 | |
| 411 frontend_->ReceivedMessageFromTarget(host->GetId(), message); | |
| 412 } | |
| 413 | |
| 414 void TargetHandler::AgentHostClosed( | |
| 415 DevToolsAgentHost* host, | |
| 416 bool replaced_with_another_client) { | |
| 417 frontend_->DetachedFromTarget(host->GetId()); | |
| 418 attached_hosts_.erase(host->GetId()); | |
| 419 } | |
| 420 | |
| 421 // -------------- DevToolsAgentHostObserver ----------------- | |
| 422 | |
| 423 bool TargetHandler::ShouldForceDevToolsAgentHostCreation() { | |
| 424 return true; | |
| 425 } | |
| 426 | |
| 427 void TargetHandler::DevToolsAgentHostCreated(DevToolsAgentHost* agent_host) { | |
| 428 // If we start discovering late, all existing agent hosts will be reported, | |
| 429 // but we could have already attached to some. | |
| 430 TargetCreatedInternal(agent_host); | |
| 431 } | |
| 432 | |
| 433 void TargetHandler::DevToolsAgentHostDestroyed(DevToolsAgentHost* agent_host) { | |
| 434 DCHECK(attached_hosts_.find(agent_host->GetId()) == attached_hosts_.end()); | |
| 435 TargetDestroyedInternal(agent_host); | |
| 436 } | |
| 437 | |
| 438 void TargetHandler::DevToolsAgentHostAttached(DevToolsAgentHost* host) { | |
| 439 TargetInfoChangedInternal(host); | |
| 440 } | |
| 441 | |
| 442 void TargetHandler::DevToolsAgentHostDetached(DevToolsAgentHost* host) { | |
| 443 TargetInfoChangedInternal(host); | |
| 444 } | 214 } |
| 445 | 215 |
| 446 // -------- ServiceWorkerDevToolsManager::Observer ---------- | 216 // -------- ServiceWorkerDevToolsManager::Observer ---------- |
| 447 | 217 |
| 448 void TargetHandler::WorkerCreated( | 218 void TargetAutoAttacher::WorkerCreated(ServiceWorkerDevToolsAgentHost* host) { |
| 449 ServiceWorkerDevToolsAgentHost* host) { | |
| 450 BrowserContext* browser_context = nullptr; | 219 BrowserContext* browser_context = nullptr; |
| 451 if (render_frame_host_) | 220 if (render_frame_host_) |
| 452 browser_context = render_frame_host_->GetProcess()->GetBrowserContext(); | 221 browser_context = render_frame_host_->GetProcess()->GetBrowserContext(); |
| 453 auto hosts = GetMatchingServiceWorkers(browser_context, frame_urls_); | 222 auto hosts = GetMatchingServiceWorkers(browser_context, frame_urls_); |
| 454 if (hosts.find(host->GetId()) != hosts.end() && !host->IsAttached() && | 223 if (hosts.find(host->GetId()) != hosts.end() && !host->IsAttached() && |
| 455 !host->IsPausedForDebugOnStart() && wait_for_debugger_on_start_) { | 224 !host->IsPausedForDebugOnStart() && wait_for_debugger_on_start_) { |
| 456 host->PauseForDebugOnStart(); | 225 host->PauseForDebugOnStart(); |
| 457 } | 226 } |
| 458 } | 227 } |
| 459 | 228 |
| 460 void TargetHandler::WorkerReadyForInspection( | 229 void TargetAutoAttacher::WorkerReadyForInspection( |
| 461 ServiceWorkerDevToolsAgentHost* host) { | 230 ServiceWorkerDevToolsAgentHost* host) { |
| 462 DCHECK(host->IsReadyForInspection()); | 231 DCHECK(host->IsReadyForInspection()); |
| 463 if (ServiceWorkerDevToolsManager::GetInstance() | 232 if (ServiceWorkerDevToolsManager::GetInstance() |
| 464 ->debug_service_worker_on_start()) { | 233 ->debug_service_worker_on_start()) { |
| 465 // When debug_service_worker_on_start is true, a new DevTools window will | 234 // When debug_service_worker_on_start is true, a new DevTools window will |
| 466 // be opened in ServiceWorkerDevToolsManager::WorkerReadyForInspection. | 235 // be opened in ServiceWorkerDevToolsManager::WorkerReadyForInspection. |
| 467 return; | 236 return; |
| 468 } | 237 } |
| 469 UpdateServiceWorkers(host->IsPausedForDebugOnStart()); | 238 ReattachServiceWorkers(host->IsPausedForDebugOnStart()); |
| 470 } | 239 } |
| 471 | 240 |
| 472 void TargetHandler::WorkerVersionInstalled( | 241 void TargetAutoAttacher::WorkerVersionInstalled( |
| 473 ServiceWorkerDevToolsAgentHost* host) { | 242 ServiceWorkerDevToolsAgentHost* host) { |
| 474 UpdateServiceWorkers(); | 243 ReattachServiceWorkers(false); |
| 475 } | 244 } |
| 476 | 245 |
| 477 void TargetHandler::WorkerVersionDoomed( | 246 void TargetAutoAttacher::WorkerVersionDoomed( |
| 478 ServiceWorkerDevToolsAgentHost* host) { | 247 ServiceWorkerDevToolsAgentHost* host) { |
| 479 UpdateServiceWorkers(); | 248 ReattachServiceWorkers(false); |
| 480 } | 249 } |
| 481 | 250 |
| 482 void TargetHandler::WorkerDestroyed( | 251 void TargetAutoAttacher::WorkerDestroyed(ServiceWorkerDevToolsAgentHost* host) { |
| 483 ServiceWorkerDevToolsAgentHost* host) { | 252 ReattachServiceWorkers(false); |
| 484 UpdateServiceWorkers(); | |
| 485 } | 253 } |
| 486 | 254 |
| 487 } // namespace protocol | 255 } // namespace protocol |
| 488 } // namespace content | 256 } // namespace content |
| OLD | NEW |