| 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/shared_worker_devtools_manager.h" | 5 #include "content/browser/devtools/embedded_worker_devtools_manager.h" |
| 6 | 6 |
| 7 #include "content/browser/devtools/devtools_manager_impl.h" | 7 #include "content/browser/devtools/devtools_manager_impl.h" |
| 8 #include "content/browser/devtools/devtools_protocol.h" | 8 #include "content/browser/devtools/devtools_protocol.h" |
| 9 #include "content/browser/devtools/devtools_protocol_constants.h" | 9 #include "content/browser/devtools/devtools_protocol_constants.h" |
| 10 #include "content/browser/devtools/ipc_devtools_agent_host.h" | 10 #include "content/browser/devtools/ipc_devtools_agent_host.h" |
| 11 #include "content/browser/shared_worker/shared_worker_instance.h" | 11 #include "content/browser/shared_worker/shared_worker_instance.h" |
| 12 #include "content/common/devtools_messages.h" | 12 #include "content/common/devtools_messages.h" |
| 13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/render_process_host.h" | 14 #include "content/public/browser/render_process_host.h" |
| 15 #include "content/public/browser/worker_service.h" | 15 #include "content/public/browser/worker_service.h" |
| 16 #include "ipc/ipc_listener.h" | 16 #include "ipc/ipc_listener.h" |
| 17 | 17 |
| 18 namespace content { | 18 namespace content { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 bool SendMessageToWorker(const SharedWorkerDevToolsManager::WorkerId& worker_id, | 22 bool SendMessageToWorker( |
| 23 IPC::Message* message) { | 23 const EmbeddedWorkerDevToolsManager::WorkerId& worker_id, |
| 24 IPC::Message* message) { |
| 24 RenderProcessHost* host = RenderProcessHost::FromID(worker_id.first); | 25 RenderProcessHost* host = RenderProcessHost::FromID(worker_id.first); |
| 25 if (!host) { | 26 if (!host) { |
| 26 delete message; | 27 delete message; |
| 27 return false; | 28 return false; |
| 28 } | 29 } |
| 29 message->set_routing_id(worker_id.second); | 30 message->set_routing_id(worker_id.second); |
| 30 host->Send(message); | 31 host->Send(message); |
| 31 return true; | 32 return true; |
| 32 } | 33 } |
| 33 | 34 |
| 34 } // namespace | 35 } // namespace |
| 35 | 36 |
| 36 class SharedWorkerDevToolsManager::SharedWorkerDevToolsAgentHost | 37 class EmbeddedWorkerDevToolsManager::EmbeddedWorkerDevToolsAgentHost |
| 37 : public IPCDevToolsAgentHost, | 38 : public IPCDevToolsAgentHost, |
| 38 public IPC::Listener { | 39 public IPC::Listener { |
| 39 public: | 40 public: |
| 40 explicit SharedWorkerDevToolsAgentHost(WorkerId worker_id) | 41 explicit EmbeddedWorkerDevToolsAgentHost(WorkerId worker_id) |
| 41 : worker_id_(worker_id), worker_attached_(true) { | 42 : worker_id_(worker_id), worker_attached_(true) { |
| 42 AddRef(); | 43 AddRef(); |
| 43 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) | 44 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) |
| 44 host->AddRoute(worker_id_.second, this); | 45 host->AddRoute(worker_id_.second, this); |
| 45 } | 46 } |
| 46 | 47 |
| 47 // IPCDevToolsAgentHost implementation. | 48 // IPCDevToolsAgentHost implementation. |
| 48 virtual void SendMessageToAgent(IPC::Message* message) OVERRIDE { | 49 virtual void SendMessageToAgent(IPC::Message* message) OVERRIDE { |
| 49 if (worker_attached_) | 50 if (worker_attached_) |
| 50 SendMessageToWorker(worker_id_, message); | 51 SendMessageToWorker(worker_id_, message); |
| 51 else | 52 else |
| 52 delete message; | 53 delete message; |
| 53 } | 54 } |
| 54 virtual void OnClientAttached() OVERRIDE {} | 55 virtual void OnClientAttached() OVERRIDE {} |
| 55 virtual void OnClientDetached() OVERRIDE {} | 56 virtual void OnClientDetached() OVERRIDE {} |
| 56 | 57 |
| 57 // IPC::Listener implementation. | 58 // IPC::Listener implementation. |
| 58 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE { | 59 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE { |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 60 bool handled = true; | 61 bool handled = true; |
| 61 IPC_BEGIN_MESSAGE_MAP(SharedWorkerDevToolsAgentHost, msg) | 62 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerDevToolsAgentHost, msg) |
| 62 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend, | 63 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend, |
| 63 OnDispatchOnInspectorFrontend) | 64 OnDispatchOnInspectorFrontend) |
| 64 IPC_MESSAGE_HANDLER(DevToolsHostMsg_SaveAgentRuntimeState, | 65 IPC_MESSAGE_HANDLER(DevToolsHostMsg_SaveAgentRuntimeState, |
| 65 OnSaveAgentRuntimeState) | 66 OnSaveAgentRuntimeState) |
| 66 IPC_MESSAGE_UNHANDLED(handled = false) | 67 IPC_MESSAGE_UNHANDLED(handled = false) |
| 67 IPC_END_MESSAGE_MAP() | 68 IPC_END_MESSAGE_MAP() |
| 68 return handled; | 69 return handled; |
| 69 } | 70 } |
| 70 | 71 |
| 71 void ReattachToWorker(WorkerId worker_id) { | 72 void ReattachToWorker(WorkerId worker_id) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 82 CHECK(worker_attached_); | 83 CHECK(worker_attached_); |
| 83 worker_attached_ = false; | 84 worker_attached_ = false; |
| 84 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) | 85 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) |
| 85 host->RemoveRoute(worker_id_.second); | 86 host->RemoveRoute(worker_id_.second); |
| 86 Release(); | 87 Release(); |
| 87 } | 88 } |
| 88 | 89 |
| 89 WorkerId worker_id() const { return worker_id_; } | 90 WorkerId worker_id() const { return worker_id_; } |
| 90 | 91 |
| 91 private: | 92 private: |
| 92 virtual ~SharedWorkerDevToolsAgentHost() { | 93 virtual ~EmbeddedWorkerDevToolsAgentHost() { |
| 93 CHECK(!worker_attached_); | 94 CHECK(!worker_attached_); |
| 94 SharedWorkerDevToolsManager::GetInstance()->RemoveInspectedWorkerData(this); | 95 EmbeddedWorkerDevToolsManager::GetInstance()->RemoveInspectedWorkerData( |
| 96 this); |
| 95 } | 97 } |
| 96 | 98 |
| 97 void OnDispatchOnInspectorFrontend(const std::string& message) { | 99 void OnDispatchOnInspectorFrontend(const std::string& message) { |
| 98 DevToolsManagerImpl::GetInstance()->DispatchOnInspectorFrontend(this, | 100 DevToolsManagerImpl::GetInstance()->DispatchOnInspectorFrontend(this, |
| 99 message); | 101 message); |
| 100 } | 102 } |
| 101 | 103 |
| 102 void OnSaveAgentRuntimeState(const std::string& state) { state_ = state; } | 104 void OnSaveAgentRuntimeState(const std::string& state) { state_ = state; } |
| 103 | 105 |
| 104 WorkerId worker_id_; | 106 WorkerId worker_id_; |
| 105 bool worker_attached_; | 107 bool worker_attached_; |
| 106 std::string state_; | 108 std::string state_; |
| 107 DISALLOW_COPY_AND_ASSIGN(SharedWorkerDevToolsAgentHost); | 109 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerDevToolsAgentHost); |
| 108 }; | 110 }; |
| 109 | 111 |
| 110 // static | 112 // static |
| 111 SharedWorkerDevToolsManager* SharedWorkerDevToolsManager::GetInstance() { | 113 EmbeddedWorkerDevToolsManager* EmbeddedWorkerDevToolsManager::GetInstance() { |
| 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 113 return Singleton<SharedWorkerDevToolsManager>::get(); | 115 return Singleton<EmbeddedWorkerDevToolsManager>::get(); |
| 114 } | 116 } |
| 115 | 117 |
| 116 DevToolsAgentHost* SharedWorkerDevToolsManager::GetDevToolsAgentHostForWorker( | 118 DevToolsAgentHost* EmbeddedWorkerDevToolsManager::GetDevToolsAgentHostForWorker( |
| 117 int worker_process_id, | 119 int worker_process_id, |
| 118 int worker_route_id) { | 120 int worker_route_id) { |
| 119 WorkerId id(worker_process_id, worker_route_id); | 121 WorkerId id(worker_process_id, worker_route_id); |
| 120 | 122 |
| 121 WorkerInfoMap::iterator it = workers_.find(id); | 123 WorkerInfoMap::iterator it = workers_.find(id); |
| 122 if (it == workers_.end()) | 124 if (it == workers_.end()) |
| 123 return NULL; | 125 return NULL; |
| 124 | 126 |
| 125 WorkerInfo* info = it->second; | 127 WorkerInfo* info = it->second; |
| 126 if (info->state() != WORKER_UNINSPECTED) | 128 if (info->state() != WORKER_UNINSPECTED) |
| 127 return info->agent_host(); | 129 return info->agent_host(); |
| 128 | 130 |
| 129 SharedWorkerDevToolsAgentHost* agent_host = | 131 EmbeddedWorkerDevToolsAgentHost* agent_host = |
| 130 new SharedWorkerDevToolsAgentHost(id); | 132 new EmbeddedWorkerDevToolsAgentHost(id); |
| 131 info->set_agent_host(agent_host); | 133 info->set_agent_host(agent_host); |
| 132 info->set_state(WORKER_INSPECTED); | 134 info->set_state(WORKER_INSPECTED); |
| 133 return agent_host; | 135 return agent_host; |
| 134 } | 136 } |
| 135 | 137 |
| 136 SharedWorkerDevToolsManager::SharedWorkerDevToolsManager() {} | 138 EmbeddedWorkerDevToolsManager::EmbeddedWorkerDevToolsManager() { |
| 139 } |
| 137 | 140 |
| 138 SharedWorkerDevToolsManager::~SharedWorkerDevToolsManager() {} | 141 EmbeddedWorkerDevToolsManager::~EmbeddedWorkerDevToolsManager() { |
| 142 } |
| 139 | 143 |
| 140 bool SharedWorkerDevToolsManager::WorkerCreated( | 144 bool EmbeddedWorkerDevToolsManager::SharedWorkerCreated( |
| 141 int worker_process_id, | 145 int worker_process_id, |
| 142 int worker_route_id, | 146 int worker_route_id, |
| 143 const SharedWorkerInstance& instance) { | 147 const SharedWorkerInstance& instance) { |
| 144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 145 const WorkerId id(worker_process_id, worker_route_id); | 149 const WorkerId id(worker_process_id, worker_route_id); |
| 146 WorkerInfoMap::iterator it = FindExistingWorkerInfo(instance); | 150 WorkerInfoMap::iterator it = FindExistingSharedWorkerInfo(instance); |
| 147 if (it == workers_.end()) { | 151 if (it == workers_.end()) { |
| 148 scoped_ptr<WorkerInfo> info(new WorkerInfo(instance)); | 152 scoped_ptr<WorkerInfo> info(new WorkerInfo(instance)); |
| 149 workers_.set(id, info.Pass()); | 153 workers_.set(id, info.Pass()); |
| 150 return false; | 154 return false; |
| 151 } | 155 } |
| 152 DCHECK_EQ(WORKER_TERMINATED, it->second->state()); | 156 DCHECK_EQ(WORKER_TERMINATED, it->second->state()); |
| 153 scoped_ptr<WorkerInfo> info = workers_.take_and_erase(it); | 157 scoped_ptr<WorkerInfo> info = workers_.take_and_erase(it); |
| 154 info->set_state(WORKER_PAUSED); | 158 info->set_state(WORKER_PAUSED); |
| 155 workers_.set(id, info.Pass()); | 159 workers_.set(id, info.Pass()); |
| 156 return true; | 160 return true; |
| 157 } | 161 } |
| 158 | 162 |
| 159 void SharedWorkerDevToolsManager::WorkerDestroyed(int worker_process_id, | 163 void EmbeddedWorkerDevToolsManager::WorkerDestroyed(int worker_process_id, |
| 160 int worker_route_id) { | 164 int worker_route_id) { |
| 161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 162 const WorkerId id(worker_process_id, worker_route_id); | 166 const WorkerId id(worker_process_id, worker_route_id); |
| 163 WorkerInfoMap::iterator it = workers_.find(id); | 167 WorkerInfoMap::iterator it = workers_.find(id); |
| 164 DCHECK(it != workers_.end()); | 168 DCHECK(it != workers_.end()); |
| 165 WorkerInfo* info = it->second; | 169 WorkerInfo* info = it->second; |
| 166 switch (info->state()) { | 170 switch (info->state()) { |
| 167 case WORKER_UNINSPECTED: | 171 case WORKER_UNINSPECTED: |
| 168 workers_.erase(it); | 172 workers_.erase(it); |
| 169 break; | 173 break; |
| 170 case WORKER_INSPECTED: { | 174 case WORKER_INSPECTED: { |
| 171 SharedWorkerDevToolsAgentHost* agent_host = info->agent_host(); | 175 EmbeddedWorkerDevToolsAgentHost* agent_host = info->agent_host(); |
| 172 if (!agent_host->IsAttached()) { | 176 if (!agent_host->IsAttached()) { |
| 173 scoped_ptr<WorkerInfo> worker_info = workers_.take_and_erase(it); | 177 scoped_ptr<WorkerInfo> worker_info = workers_.take_and_erase(it); |
| 174 agent_host->DetachFromWorker(); | 178 agent_host->DetachFromWorker(); |
| 175 return; | 179 return; |
| 176 } | 180 } |
| 177 info->set_state(WORKER_TERMINATED); | 181 info->set_state(WORKER_TERMINATED); |
| 178 // Client host is debugging this worker agent host. | 182 // Client host is debugging this worker agent host. |
| 179 std::string notification = | 183 std::string notification = |
| 180 DevToolsProtocol::CreateNotification( | 184 DevToolsProtocol::CreateNotification( |
| 181 devtools::Worker::disconnectedFromWorker::kName, NULL) | 185 devtools::Worker::disconnectedFromWorker::kName, NULL) |
| 182 ->Serialize(); | 186 ->Serialize(); |
| 183 DevToolsManagerImpl::GetInstance()->DispatchOnInspectorFrontend( | 187 DevToolsManagerImpl::GetInstance()->DispatchOnInspectorFrontend( |
| 184 agent_host, notification); | 188 agent_host, notification); |
| 185 agent_host->DetachFromWorker(); | 189 agent_host->DetachFromWorker(); |
| 186 break; | 190 break; |
| 187 } | 191 } |
| 188 case WORKER_TERMINATED: | 192 case WORKER_TERMINATED: |
| 189 NOTREACHED(); | 193 NOTREACHED(); |
| 190 break; | 194 break; |
| 191 case WORKER_PAUSED: { | 195 case WORKER_PAUSED: { |
| 192 scoped_ptr<WorkerInfo> worker_info = workers_.take_and_erase(it); | 196 scoped_ptr<WorkerInfo> worker_info = workers_.take_and_erase(it); |
| 193 worker_info->set_state(WORKER_TERMINATED); | 197 worker_info->set_state(WORKER_TERMINATED); |
| 194 const WorkerId old_id = worker_info->agent_host()->worker_id(); | 198 const WorkerId old_id = worker_info->agent_host()->worker_id(); |
| 195 workers_.set(old_id, worker_info.Pass()); | 199 workers_.set(old_id, worker_info.Pass()); |
| 196 break; | 200 break; |
| 197 } | 201 } |
| 198 } | 202 } |
| 199 } | 203 } |
| 200 | 204 |
| 201 void SharedWorkerDevToolsManager::WorkerContextStarted(int worker_process_id, | 205 void EmbeddedWorkerDevToolsManager::WorkerContextStarted(int worker_process_id, |
| 202 int worker_route_id) { | 206 int worker_route_id) { |
| 203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 204 const WorkerId id(worker_process_id, worker_route_id); | 208 const WorkerId id(worker_process_id, worker_route_id); |
| 205 WorkerInfoMap::iterator it = workers_.find(id); | 209 WorkerInfoMap::iterator it = workers_.find(id); |
| 206 DCHECK(it != workers_.end()); | 210 DCHECK(it != workers_.end()); |
| 207 WorkerInfo* info = it->second; | 211 WorkerInfo* info = it->second; |
| 208 if (info->state() != WORKER_PAUSED) | 212 if (info->state() != WORKER_PAUSED) |
| 209 return; | 213 return; |
| 210 info->agent_host()->ReattachToWorker(id); | 214 info->agent_host()->ReattachToWorker(id); |
| 211 info->set_state(WORKER_INSPECTED); | 215 info->set_state(WORKER_INSPECTED); |
| 212 } | 216 } |
| 213 | 217 |
| 214 void SharedWorkerDevToolsManager::RemoveInspectedWorkerData( | 218 void EmbeddedWorkerDevToolsManager::RemoveInspectedWorkerData( |
| 215 SharedWorkerDevToolsAgentHost* agent_host) { | 219 EmbeddedWorkerDevToolsAgentHost* agent_host) { |
| 216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 217 const WorkerId id(agent_host->worker_id()); | 221 const WorkerId id(agent_host->worker_id()); |
| 218 scoped_ptr<WorkerInfo> worker_info = workers_.take_and_erase(id); | 222 scoped_ptr<WorkerInfo> worker_info = workers_.take_and_erase(id); |
| 219 if (worker_info) { | 223 if (worker_info) { |
| 220 DCHECK_EQ(WORKER_TERMINATED, worker_info->state()); | 224 DCHECK_EQ(WORKER_TERMINATED, worker_info->state()); |
| 221 return; | 225 return; |
| 222 } | 226 } |
| 223 for (WorkerInfoMap::iterator it = workers_.begin(); it != workers_.end(); | 227 for (WorkerInfoMap::iterator it = workers_.begin(); it != workers_.end(); |
| 224 ++it) { | 228 ++it) { |
| 225 if (it->second->agent_host() == agent_host) { | 229 if (it->second->agent_host() == agent_host) { |
| 226 DCHECK_EQ(WORKER_PAUSED, it->second->state()); | 230 DCHECK_EQ(WORKER_PAUSED, it->second->state()); |
| 227 SendMessageToWorker( | 231 SendMessageToWorker( |
| 228 it->first, | 232 it->first, |
| 229 new DevToolsAgentMsg_ResumeWorkerContext(it->first.second)); | 233 new DevToolsAgentMsg_ResumeWorkerContext(it->first.second)); |
| 230 it->second->set_agent_host(NULL); | 234 it->second->set_agent_host(NULL); |
| 231 it->second->set_state(WORKER_UNINSPECTED); | 235 it->second->set_state(WORKER_UNINSPECTED); |
| 232 return; | 236 return; |
| 233 } | 237 } |
| 234 } | 238 } |
| 235 } | 239 } |
| 236 | 240 |
| 237 SharedWorkerDevToolsManager::WorkerInfoMap::iterator | 241 EmbeddedWorkerDevToolsManager::WorkerInfoMap::iterator |
| 238 SharedWorkerDevToolsManager::FindExistingWorkerInfo( | 242 EmbeddedWorkerDevToolsManager::FindExistingSharedWorkerInfo( |
| 239 const SharedWorkerInstance& instance) { | 243 const SharedWorkerInstance& instance) { |
| 240 WorkerInfoMap::iterator it = workers_.begin(); | 244 WorkerInfoMap::iterator it = workers_.begin(); |
| 241 for (; it != workers_.end(); ++it) { | 245 for (; it != workers_.end(); ++it) { |
| 242 if (it->second->instance().Matches(instance)) | 246 if (it->second->instance().Matches(instance)) |
| 243 break; | 247 break; |
| 244 } | 248 } |
| 245 return it; | 249 return it; |
| 246 } | 250 } |
| 247 | 251 |
| 248 void SharedWorkerDevToolsManager::ResetForTesting() { workers_.clear(); } | 252 void EmbeddedWorkerDevToolsManager::ResetForTesting() { |
| 253 workers_.clear(); |
| 254 } |
| 249 | 255 |
| 250 } // namespace content | 256 } // namespace content |
| OLD | NEW |