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/embedded_worker_devtools_agent_host.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/common/devtools_messages.h" |
| 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/browser/render_process_host.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 EmbeddedWorkerDevToolsAgentHost::EmbeddedWorkerDevToolsAgentHost( |
| 17 WorkerId worker_id, |
| 18 const SharedWorkerInstance& shared_worker) |
| 19 : shared_worker_(new SharedWorkerInstance(shared_worker)), |
| 20 state_(WORKER_UNINSPECTED), |
| 21 worker_id_(worker_id) { |
| 22 WorkerCreated(); |
| 23 } |
| 24 |
| 25 EmbeddedWorkerDevToolsAgentHost::EmbeddedWorkerDevToolsAgentHost( |
| 26 WorkerId worker_id, |
| 27 const ServiceWorkerIdentifier& service_worker, |
| 28 bool debug_service_worker_on_start) |
| 29 : service_worker_(new ServiceWorkerIdentifier(service_worker)), |
| 30 state_(WORKER_UNINSPECTED), |
| 31 worker_id_(worker_id) { |
| 32 if (debug_service_worker_on_start) |
| 33 state_ = WORKER_PAUSED_FOR_DEBUG_ON_START; |
| 34 WorkerCreated(); |
| 35 } |
| 36 |
| 37 bool EmbeddedWorkerDevToolsAgentHost::IsWorker() const { |
| 38 return true; |
| 39 } |
| 40 |
| 41 void EmbeddedWorkerDevToolsAgentHost::SendMessageToAgent( |
| 42 IPC::Message* message_raw) { |
| 43 scoped_ptr<IPC::Message> message(message_raw); |
| 44 if (state_ != WORKER_INSPECTED) |
| 45 return; |
| 46 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) { |
| 47 message->set_routing_id(worker_id_.second); |
| 48 host->Send(message.release()); |
| 49 } |
| 50 } |
| 51 |
| 52 void EmbeddedWorkerDevToolsAgentHost::Attach() { |
| 53 if (state_ != WORKER_INSPECTED) { |
| 54 state_ = WORKER_INSPECTED; |
| 55 AttachToWorker(); |
| 56 } |
| 57 IPCDevToolsAgentHost::Attach(); |
| 58 } |
| 59 |
| 60 void EmbeddedWorkerDevToolsAgentHost::OnClientDetached() { |
| 61 if (state_ == WORKER_INSPECTED) { |
| 62 state_ = WORKER_UNINSPECTED; |
| 63 DetachFromWorker(); |
| 64 } else if (state_ == WORKER_PAUSED_FOR_REATTACH) { |
| 65 state_ = WORKER_UNINSPECTED; |
| 66 } |
| 67 } |
| 68 |
| 69 bool EmbeddedWorkerDevToolsAgentHost::OnMessageReceived( |
| 70 const IPC::Message& msg) { |
| 71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 72 bool handled = true; |
| 73 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerDevToolsAgentHost, msg) |
| 74 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend, |
| 75 OnDispatchOnInspectorFrontend) |
| 76 IPC_MESSAGE_HANDLER(DevToolsHostMsg_SaveAgentRuntimeState, |
| 77 OnSaveAgentRuntimeState) |
| 78 IPC_MESSAGE_UNHANDLED(handled = false) |
| 79 IPC_END_MESSAGE_MAP() |
| 80 return handled; |
| 81 } |
| 82 |
| 83 void EmbeddedWorkerDevToolsAgentHost::WorkerContextStarted() { |
| 84 if (state_ == WORKER_PAUSED_FOR_DEBUG_ON_START) { |
| 85 RenderProcessHost* rph = RenderProcessHost::FromID(worker_id_.first); |
| 86 DevToolsManagerImpl::GetInstance()->Inspect(rph->GetBrowserContext(), this); |
| 87 } else if (state_ == WORKER_PAUSED_FOR_REATTACH) { |
| 88 DCHECK(IsAttached()); |
| 89 state_ = WORKER_INSPECTED; |
| 90 AttachToWorker(); |
| 91 Reattach(saved_agent_state_); |
| 92 } |
| 93 } |
| 94 |
| 95 void EmbeddedWorkerDevToolsAgentHost::WorkerRestarted(WorkerId worker_id) { |
| 96 DCHECK_EQ(WORKER_TERMINATED, state_); |
| 97 state_ = IsAttached() ? WORKER_PAUSED_FOR_REATTACH : WORKER_UNINSPECTED; |
| 98 worker_id_ = worker_id; |
| 99 WorkerCreated(); |
| 100 } |
| 101 |
| 102 void EmbeddedWorkerDevToolsAgentHost::WorkerDestroyed() { |
| 103 DCHECK_NE(WORKER_TERMINATED, state_); |
| 104 if (state_ == WORKER_INSPECTED) { |
| 105 DCHECK(IsAttached()); |
| 106 // Client host is debugging this worker agent host. |
| 107 std::string notification = |
| 108 DevToolsProtocol::CreateNotification( |
| 109 devtools::Worker::disconnectedFromWorker::kName, NULL)->Serialize(); |
| 110 DevToolsManagerImpl::GetInstance()->DispatchOnInspectorFrontend( |
| 111 this, notification); |
| 112 DetachFromWorker(); |
| 113 } |
| 114 state_ = WORKER_TERMINATED; |
| 115 Release(); // Balanced in WorkerCreated() |
| 116 } |
| 117 |
| 118 bool EmbeddedWorkerDevToolsAgentHost::Matches( |
| 119 const SharedWorkerInstance& other) { |
| 120 return shared_worker_ && shared_worker_->Matches(other); |
| 121 } |
| 122 |
| 123 bool EmbeddedWorkerDevToolsAgentHost::Matches( |
| 124 const ServiceWorkerIdentifier& other) { |
| 125 return service_worker_ && service_worker_->Matches(other); |
| 126 } |
| 127 |
| 128 EmbeddedWorkerDevToolsAgentHost::~EmbeddedWorkerDevToolsAgentHost() { |
| 129 DCHECK_EQ(WORKER_TERMINATED, state_); |
| 130 EmbeddedWorkerDevToolsManager::GetInstance()->RemoveInspectedWorkerData( |
| 131 worker_id_); |
| 132 } |
| 133 |
| 134 void EmbeddedWorkerDevToolsAgentHost::AttachToWorker() { |
| 135 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) |
| 136 host->AddRoute(worker_id_.second, this); |
| 137 } |
| 138 |
| 139 void EmbeddedWorkerDevToolsAgentHost::DetachFromWorker() { |
| 140 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) |
| 141 host->RemoveRoute(worker_id_.second); |
| 142 } |
| 143 |
| 144 void EmbeddedWorkerDevToolsAgentHost::WorkerCreated() { |
| 145 AddRef(); // Balanced in WorkerDestroyed() |
| 146 } |
| 147 |
| 148 void EmbeddedWorkerDevToolsAgentHost::OnDispatchOnInspectorFrontend( |
| 149 const std::string& message) { |
| 150 DevToolsManagerImpl::GetInstance()->DispatchOnInspectorFrontend( |
| 151 this, message); |
| 152 } |
| 153 |
| 154 void EmbeddedWorkerDevToolsAgentHost::OnSaveAgentRuntimeState( |
| 155 const std::string& state) { |
| 156 saved_agent_state_ = state; |
| 157 } |
| 158 |
| 159 } // namespace content |
OLD | NEW |