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