| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/embedded_worker_devtools_agent_host.h" | 5 #include "content/browser/devtools/embedded_worker_devtools_agent_host.h" |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | |
| 8 #include "content/browser/devtools/protocol/devtools_protocol_handler.h" | 7 #include "content/browser/devtools/protocol/devtools_protocol_handler.h" |
| 9 #include "content/browser/service_worker/service_worker_context_core.h" | |
| 10 #include "content/browser/service_worker/service_worker_version.h" | |
| 11 #include "content/browser/shared_worker/shared_worker_service_impl.h" | |
| 12 #include "content/common/devtools_messages.h" | 8 #include "content/common/devtools_messages.h" |
| 13 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/render_process_host.h" | 10 #include "content/public/browser/render_process_host.h" |
| 15 | 11 |
| 16 namespace content { | 12 namespace content { |
| 17 | 13 |
| 18 namespace { | |
| 19 | |
| 20 void TerminateSharedWorkerOnIO( | |
| 21 EmbeddedWorkerDevToolsAgentHost::WorkerId worker_id) { | |
| 22 SharedWorkerServiceImpl::GetInstance()->TerminateWorker( | |
| 23 worker_id.first, worker_id.second); | |
| 24 } | |
| 25 | |
| 26 void StatusNoOp(ServiceWorkerStatusCode status) { | |
| 27 } | |
| 28 | |
| 29 void TerminateServiceWorkerOnIO( | |
| 30 base::WeakPtr<ServiceWorkerContextCore> context_weak, | |
| 31 int64 version_id) { | |
| 32 if (ServiceWorkerContextCore* context = context_weak.get()) { | |
| 33 if (ServiceWorkerVersion* version = context->GetLiveVersion(version_id)) | |
| 34 version->StopWorker(base::Bind(&StatusNoOp)); | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 void SetDevToolsAttachedOnIO( | |
| 39 base::WeakPtr<ServiceWorkerContextCore> context_weak, | |
| 40 int64 version_id, | |
| 41 bool attached) { | |
| 42 if (ServiceWorkerContextCore* context = context_weak.get()) { | |
| 43 if (ServiceWorkerVersion* version = context->GetLiveVersion(version_id)) | |
| 44 version->SetDevToolsAttached(attached); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 50 EmbeddedWorkerDevToolsAgentHost::EmbeddedWorkerDevToolsAgentHost( | |
| 51 WorkerId worker_id, | |
| 52 const SharedWorkerInstance& shared_worker) | |
| 53 : shared_worker_(new SharedWorkerInstance(shared_worker)), | |
| 54 state_(WORKER_UNINSPECTED), | |
| 55 worker_id_(worker_id) { | |
| 56 WorkerCreated(); | |
| 57 } | |
| 58 | |
| 59 EmbeddedWorkerDevToolsAgentHost::EmbeddedWorkerDevToolsAgentHost( | |
| 60 WorkerId worker_id, | |
| 61 const ServiceWorkerIdentifier& service_worker, | |
| 62 bool debug_service_worker_on_start) | |
| 63 : service_worker_(new ServiceWorkerIdentifier(service_worker)), | |
| 64 state_(WORKER_UNINSPECTED), | |
| 65 worker_id_(worker_id) { | |
| 66 if (debug_service_worker_on_start) | |
| 67 state_ = WORKER_PAUSED_FOR_DEBUG_ON_START; | |
| 68 WorkerCreated(); | |
| 69 } | |
| 70 | |
| 71 bool EmbeddedWorkerDevToolsAgentHost::IsWorker() const { | 14 bool EmbeddedWorkerDevToolsAgentHost::IsWorker() const { |
| 72 return true; | 15 return true; |
| 73 } | 16 } |
| 74 | 17 |
| 75 DevToolsAgentHost::Type EmbeddedWorkerDevToolsAgentHost::GetType() { | 18 BrowserContext* EmbeddedWorkerDevToolsAgentHost::GetBrowserContext() { |
| 76 return shared_worker_ ? TYPE_SHARED_WORKER : TYPE_SERVICE_WORKER; | 19 RenderProcessHost* rph = RenderProcessHost::FromID(worker_id_.first); |
| 77 } | 20 return rph ? rph->GetBrowserContext() : nullptr; |
| 78 | |
| 79 std::string EmbeddedWorkerDevToolsAgentHost::GetTitle() { | |
| 80 if (shared_worker_ && shared_worker_->name().length()) | |
| 81 return base::UTF16ToUTF8(shared_worker_->name()); | |
| 82 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) { | |
| 83 return base::StringPrintf("Worker pid:%d", | |
| 84 base::GetProcId(host->GetHandle())); | |
| 85 } | |
| 86 return ""; | |
| 87 } | |
| 88 | |
| 89 GURL EmbeddedWorkerDevToolsAgentHost::GetURL() { | |
| 90 if (shared_worker_) | |
| 91 return shared_worker_->url(); | |
| 92 if (service_worker_) | |
| 93 return service_worker_->url(); | |
| 94 return GURL(); | |
| 95 } | |
| 96 | |
| 97 bool EmbeddedWorkerDevToolsAgentHost::Activate() { | |
| 98 return false; | |
| 99 } | |
| 100 | |
| 101 bool EmbeddedWorkerDevToolsAgentHost::Close() { | |
| 102 if (shared_worker_) { | |
| 103 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 104 base::Bind(&TerminateSharedWorkerOnIO, worker_id_)); | |
| 105 return true; | |
| 106 } | |
| 107 if (service_worker_) { | |
| 108 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 109 base::Bind(&TerminateServiceWorkerOnIO, | |
| 110 service_worker_->context_weak(), | |
| 111 service_worker_->version_id())); | |
| 112 return true; | |
| 113 } | |
| 114 return false; | |
| 115 } | 21 } |
| 116 | 22 |
| 117 void EmbeddedWorkerDevToolsAgentHost::SendMessageToAgent( | 23 void EmbeddedWorkerDevToolsAgentHost::SendMessageToAgent( |
| 118 IPC::Message* message_raw) { | 24 IPC::Message* message_raw) { |
| 119 scoped_ptr<IPC::Message> message(message_raw); | 25 scoped_ptr<IPC::Message> message(message_raw); |
| 120 if (state_ != WORKER_INSPECTED) | 26 if (state_ != WORKER_INSPECTED) |
| 121 return; | 27 return; |
| 122 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) { | 28 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) { |
| 123 message->set_routing_id(worker_id_.second); | 29 message->set_routing_id(worker_id_.second); |
| 124 host->Send(message.release()); | 30 host->Send(message.release()); |
| 125 } | 31 } |
| 126 } | 32 } |
| 127 | 33 |
| 128 void EmbeddedWorkerDevToolsAgentHost::Attach() { | 34 void EmbeddedWorkerDevToolsAgentHost::Attach() { |
| 129 if (state_ != WORKER_INSPECTED) { | 35 if (state_ != WORKER_INSPECTED) { |
| 130 state_ = WORKER_INSPECTED; | 36 state_ = WORKER_INSPECTED; |
| 131 AttachToWorker(); | 37 AttachToWorker(); |
| 132 } | 38 } |
| 133 IPCDevToolsAgentHost::Attach(); | 39 IPCDevToolsAgentHost::Attach(); |
| 134 } | 40 } |
| 135 | 41 |
| 136 void EmbeddedWorkerDevToolsAgentHost::OnClientAttached() { | 42 void EmbeddedWorkerDevToolsAgentHost::OnClientAttached() { |
| 137 DevToolsAgentHostImpl::NotifyCallbacks(this, true); | 43 DevToolsAgentHostImpl::NotifyCallbacks(this, true); |
| 138 if (service_worker_) { | |
| 139 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 140 base::Bind(&SetDevToolsAttachedOnIO, | |
| 141 service_worker_->context_weak(), | |
| 142 service_worker_->version_id(), | |
| 143 true)); | |
| 144 } | |
| 145 } | 44 } |
| 146 | 45 |
| 147 void EmbeddedWorkerDevToolsAgentHost::OnClientDetached() { | 46 void EmbeddedWorkerDevToolsAgentHost::OnClientDetached() { |
| 148 if (state_ == WORKER_INSPECTED) { | 47 if (state_ == WORKER_INSPECTED) { |
| 149 state_ = WORKER_UNINSPECTED; | 48 state_ = WORKER_UNINSPECTED; |
| 150 DetachFromWorker(); | 49 DetachFromWorker(); |
| 151 } else if (state_ == WORKER_PAUSED_FOR_REATTACH) { | 50 } else if (state_ == WORKER_PAUSED_FOR_REATTACH) { |
| 152 state_ = WORKER_UNINSPECTED; | 51 state_ = WORKER_UNINSPECTED; |
| 153 } | 52 } |
| 154 DevToolsAgentHostImpl::NotifyCallbacks(this, false); | 53 DevToolsAgentHostImpl::NotifyCallbacks(this, false); |
| 155 if (service_worker_) { | |
| 156 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 157 base::Bind(&SetDevToolsAttachedOnIO, | |
| 158 service_worker_->context_weak(), | |
| 159 service_worker_->version_id(), | |
| 160 false)); | |
| 161 } | |
| 162 } | 54 } |
| 163 | 55 |
| 164 bool EmbeddedWorkerDevToolsAgentHost::OnMessageReceived( | 56 bool EmbeddedWorkerDevToolsAgentHost::OnMessageReceived( |
| 165 const IPC::Message& msg) { | 57 const IPC::Message& msg) { |
| 166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 167 bool handled = true; | 59 bool handled = true; |
| 168 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerDevToolsAgentHost, msg) | 60 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerDevToolsAgentHost, msg) |
| 169 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend, | 61 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend, |
| 170 OnDispatchOnInspectorFrontend) | 62 OnDispatchOnInspectorFrontend) |
| 171 IPC_MESSAGE_HANDLER(DevToolsHostMsg_SaveAgentRuntimeState, | 63 IPC_MESSAGE_HANDLER(DevToolsHostMsg_SaveAgentRuntimeState, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 201 // Client host is debugging this worker agent host. | 93 // Client host is debugging this worker agent host. |
| 202 base::Callback<void(const std::string&)> raw_message_callback( | 94 base::Callback<void(const std::string&)> raw_message_callback( |
| 203 base::Bind(&EmbeddedWorkerDevToolsAgentHost::SendMessageToClient, | 95 base::Bind(&EmbeddedWorkerDevToolsAgentHost::SendMessageToClient, |
| 204 base::Unretained(this))); | 96 base::Unretained(this))); |
| 205 devtools::worker::Client worker(raw_message_callback); | 97 devtools::worker::Client worker(raw_message_callback); |
| 206 worker.DisconnectedFromWorker( | 98 worker.DisconnectedFromWorker( |
| 207 devtools::worker::DisconnectedFromWorkerParams::Create()); | 99 devtools::worker::DisconnectedFromWorkerParams::Create()); |
| 208 DetachFromWorker(); | 100 DetachFromWorker(); |
| 209 } | 101 } |
| 210 state_ = WORKER_TERMINATED; | 102 state_ = WORKER_TERMINATED; |
| 211 Release(); // Balanced in WorkerCreated() | 103 Release(); // Balanced in WorkerCreated(). |
| 212 } | |
| 213 | |
| 214 bool EmbeddedWorkerDevToolsAgentHost::Matches( | |
| 215 const SharedWorkerInstance& other) { | |
| 216 return shared_worker_ && shared_worker_->Matches(other); | |
| 217 } | |
| 218 | |
| 219 bool EmbeddedWorkerDevToolsAgentHost::Matches( | |
| 220 const ServiceWorkerIdentifier& other) { | |
| 221 return service_worker_ && service_worker_->Matches(other); | |
| 222 } | 104 } |
| 223 | 105 |
| 224 bool EmbeddedWorkerDevToolsAgentHost::IsTerminated() { | 106 bool EmbeddedWorkerDevToolsAgentHost::IsTerminated() { |
| 225 return state_ == WORKER_TERMINATED; | 107 return state_ == WORKER_TERMINATED; |
| 226 } | 108 } |
| 227 | 109 |
| 110 bool EmbeddedWorkerDevToolsAgentHost::Matches( |
| 111 const SharedWorkerInstance& other) { |
| 112 return false; |
| 113 } |
| 114 |
| 115 bool EmbeddedWorkerDevToolsAgentHost::Matches( |
| 116 const ServiceWorkerIdentifier& other) { |
| 117 return false; |
| 118 } |
| 119 |
| 120 EmbeddedWorkerDevToolsAgentHost::EmbeddedWorkerDevToolsAgentHost( |
| 121 WorkerId worker_id) |
| 122 : state_(WORKER_UNINSPECTED), |
| 123 worker_id_(worker_id) { |
| 124 WorkerCreated(); |
| 125 } |
| 126 |
| 228 EmbeddedWorkerDevToolsAgentHost::~EmbeddedWorkerDevToolsAgentHost() { | 127 EmbeddedWorkerDevToolsAgentHost::~EmbeddedWorkerDevToolsAgentHost() { |
| 229 DCHECK_EQ(WORKER_TERMINATED, state_); | 128 DCHECK_EQ(WORKER_TERMINATED, state_); |
| 230 EmbeddedWorkerDevToolsManager::GetInstance()->RemoveInspectedWorkerData( | 129 EmbeddedWorkerDevToolsManager::GetInstance()->RemoveInspectedWorkerData( |
| 231 worker_id_); | 130 worker_id_); |
| 232 } | 131 } |
| 233 | 132 |
| 234 void EmbeddedWorkerDevToolsAgentHost::AttachToWorker() { | 133 void EmbeddedWorkerDevToolsAgentHost::AttachToWorker() { |
| 235 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) | 134 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) |
| 236 host->AddRoute(worker_id_.second, this); | 135 host->AddRoute(worker_id_.second, this); |
| 237 } | 136 } |
| 238 | 137 |
| 239 void EmbeddedWorkerDevToolsAgentHost::DetachFromWorker() { | 138 void EmbeddedWorkerDevToolsAgentHost::DetachFromWorker() { |
| 240 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) | 139 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) |
| 241 host->RemoveRoute(worker_id_.second); | 140 host->RemoveRoute(worker_id_.second); |
| 242 } | 141 } |
| 243 | 142 |
| 244 void EmbeddedWorkerDevToolsAgentHost::WorkerCreated() { | 143 void EmbeddedWorkerDevToolsAgentHost::WorkerCreated() { |
| 245 AddRef(); // Balanced in WorkerDestroyed() | 144 AddRef(); // Balanced in WorkerDestroyed() |
| 246 } | 145 } |
| 247 | 146 |
| 248 void EmbeddedWorkerDevToolsAgentHost::OnDispatchOnInspectorFrontend( | 147 void EmbeddedWorkerDevToolsAgentHost::OnDispatchOnInspectorFrontend( |
| 249 const std::string& message, | 148 const std::string& message, |
| 250 uint32 total_size) { | 149 uint32 total_size) { |
| 251 if (!IsAttached()) | 150 if (!IsAttached()) |
| 252 return; | 151 return; |
| 253 | 152 |
| 254 ProcessChunkedMessageFromAgent(message, total_size); | 153 ProcessChunkedMessageFromAgent(message, total_size); |
| 255 } | 154 } |
| 256 | 155 |
| 257 BrowserContext* EmbeddedWorkerDevToolsAgentHost::GetBrowserContext() { | |
| 258 RenderProcessHost* rph = RenderProcessHost::FromID(worker_id_.first); | |
| 259 return rph ? rph->GetBrowserContext() : nullptr; | |
| 260 } | |
| 261 | |
| 262 void EmbeddedWorkerDevToolsAgentHost::OnSaveAgentRuntimeState( | 156 void EmbeddedWorkerDevToolsAgentHost::OnSaveAgentRuntimeState( |
| 263 const std::string& state) { | 157 const std::string& state) { |
| 264 saved_agent_state_ = state; | 158 saved_agent_state_ = state; |
| 265 } | 159 } |
| 266 | 160 |
| 267 } // namespace content | 161 } // namespace content |
| OLD | NEW |