| 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/worker_devtools_agent_host.h" | 5 #include "content/browser/devtools/worker_devtools_agent_host.h" |
| 6 | 6 |
| 7 #include "content/browser/devtools/ipc_devtools_agent_host.h" | |
| 8 #include "content/browser/devtools/protocol/devtools_protocol_handler.h" | 7 #include "content/browser/devtools/protocol/devtools_protocol_handler.h" |
| 9 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/browser/render_process_host.h" | 9 #include "content/public/browser/render_process_host.h" |
| 11 | 10 |
| 12 namespace content { | 11 namespace content { |
| 13 | 12 |
| 14 BrowserContext* WorkerDevToolsAgentHost::GetBrowserContext() { | 13 BrowserContext* WorkerDevToolsAgentHost::GetBrowserContext() { |
| 15 RenderProcessHost* rph = RenderProcessHost::FromID(worker_id_.first); | 14 RenderProcessHost* rph = RenderProcessHost::FromID(worker_id_.first); |
| 16 return rph ? rph->GetBrowserContext() : nullptr; | 15 return rph ? rph->GetBrowserContext() : nullptr; |
| 17 } | 16 } |
| 18 | 17 |
| 19 void WorkerDevToolsAgentHost::SendMessageToAgent( | |
| 20 IPC::Message* message_raw) { | |
| 21 scoped_ptr<IPC::Message> message(message_raw); | |
| 22 if (state_ != WORKER_INSPECTED) | |
| 23 return; | |
| 24 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) { | |
| 25 message->set_routing_id(worker_id_.second); | |
| 26 host->Send(message.release()); | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 void WorkerDevToolsAgentHost::Attach() { | 18 void WorkerDevToolsAgentHost::Attach() { |
| 31 if (state_ != WORKER_INSPECTED) { | 19 if (state_ != WORKER_INSPECTED) { |
| 32 state_ = WORKER_INSPECTED; | 20 state_ = WORKER_INSPECTED; |
| 33 AttachToWorker(); | 21 AttachToWorker(); |
| 34 } | 22 } |
| 35 IPCDevToolsAgentHost::Attach(); | 23 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) |
| 24 host->Send(new DevToolsAgentMsg_Attach(worker_id_.second, GetId())); |
| 25 OnAttachedStateChanged(true); |
| 26 DevToolsAgentHostImpl::NotifyCallbacks(this, true); |
| 36 } | 27 } |
| 37 | 28 |
| 38 void WorkerDevToolsAgentHost::OnClientAttached(bool reattached) { | 29 void WorkerDevToolsAgentHost::Detach() { |
| 39 if (!reattached) | 30 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) |
| 40 DevToolsAgentHostImpl::NotifyCallbacks(this, true); | 31 host->Send(new DevToolsAgentMsg_Detach(worker_id_.second)); |
| 41 } | 32 OnAttachedStateChanged(false); |
| 42 | |
| 43 void WorkerDevToolsAgentHost::OnClientDetached() { | |
| 44 if (state_ == WORKER_INSPECTED) { | 33 if (state_ == WORKER_INSPECTED) { |
| 45 state_ = WORKER_UNINSPECTED; | 34 state_ = WORKER_UNINSPECTED; |
| 46 DetachFromWorker(); | 35 DetachFromWorker(); |
| 47 } else if (state_ == WORKER_PAUSED_FOR_REATTACH) { | 36 } else if (state_ == WORKER_PAUSED_FOR_REATTACH) { |
| 48 state_ = WORKER_UNINSPECTED; | 37 state_ = WORKER_UNINSPECTED; |
| 49 } | 38 } |
| 50 DevToolsAgentHostImpl::NotifyCallbacks(this, false); | 39 DevToolsAgentHostImpl::NotifyCallbacks(this, false); |
| 51 } | 40 } |
| 52 | 41 |
| 42 bool WorkerDevToolsAgentHost::DispatchProtocolMessage( |
| 43 const std::string& message) { |
| 44 if (state_ != WORKER_INSPECTED) |
| 45 return true; |
| 46 if (DevToolsAgentHostImpl::DispatchProtocolMessage(message)) |
| 47 return true; |
| 48 |
| 49 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) { |
| 50 host->Send(new DevToolsAgentMsg_DispatchOnInspectorBackend( |
| 51 worker_id_.second, message)); |
| 52 } |
| 53 return true; |
| 54 } |
| 55 |
| 53 bool WorkerDevToolsAgentHost::OnMessageReceived( | 56 bool WorkerDevToolsAgentHost::OnMessageReceived( |
| 54 const IPC::Message& msg) { | 57 const IPC::Message& msg) { |
| 55 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 58 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 56 bool handled = true; | 59 bool handled = true; |
| 57 IPC_BEGIN_MESSAGE_MAP(WorkerDevToolsAgentHost, msg) | 60 IPC_BEGIN_MESSAGE_MAP(WorkerDevToolsAgentHost, msg) |
| 58 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend, | 61 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend, |
| 59 OnDispatchOnInspectorFrontend) | 62 OnDispatchOnInspectorFrontend) |
| 60 IPC_MESSAGE_UNHANDLED(handled = false) | 63 IPC_MESSAGE_UNHANDLED(handled = false) |
| 61 IPC_END_MESSAGE_MAP() | 64 IPC_END_MESSAGE_MAP() |
| 62 return handled; | 65 return handled; |
| 63 } | 66 } |
| 64 | 67 |
| 65 void WorkerDevToolsAgentHost::PauseForDebugOnStart() { | 68 void WorkerDevToolsAgentHost::PauseForDebugOnStart() { |
| 66 DCHECK(state_ == WORKER_UNINSPECTED); | 69 DCHECK(state_ == WORKER_UNINSPECTED); |
| 67 state_ = WORKER_PAUSED_FOR_DEBUG_ON_START; | 70 state_ = WORKER_PAUSED_FOR_DEBUG_ON_START; |
| 68 } | 71 } |
| 69 | 72 |
| 70 bool WorkerDevToolsAgentHost::IsPausedForDebugOnStart() { | 73 bool WorkerDevToolsAgentHost::IsPausedForDebugOnStart() { |
| 71 return state_ == WORKER_PAUSED_FOR_DEBUG_ON_START; | 74 return state_ == WORKER_PAUSED_FOR_DEBUG_ON_START; |
| 72 } | 75 } |
| 73 | 76 |
| 74 void WorkerDevToolsAgentHost::WorkerReadyForInspection() { | 77 void WorkerDevToolsAgentHost::WorkerReadyForInspection() { |
| 75 if (state_ == WORKER_PAUSED_FOR_REATTACH) { | 78 if (state_ == WORKER_PAUSED_FOR_REATTACH) { |
| 76 DCHECK(IsAttached()); | 79 DCHECK(IsAttached()); |
| 77 state_ = WORKER_INSPECTED; | 80 state_ = WORKER_INSPECTED; |
| 78 AttachToWorker(); | 81 AttachToWorker(); |
| 79 Reattach(); | 82 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) { |
| 83 host->Send(new DevToolsAgentMsg_Reattach( |
| 84 worker_id_.second, GetId(), state_cookie_)); |
| 85 } |
| 86 OnAttachedStateChanged(true); |
| 80 } | 87 } |
| 81 } | 88 } |
| 82 | 89 |
| 83 void WorkerDevToolsAgentHost::WorkerRestarted(WorkerId worker_id) { | 90 void WorkerDevToolsAgentHost::WorkerRestarted(WorkerId worker_id) { |
| 84 DCHECK_EQ(WORKER_TERMINATED, state_); | 91 DCHECK_EQ(WORKER_TERMINATED, state_); |
| 85 state_ = IsAttached() ? WORKER_PAUSED_FOR_REATTACH : WORKER_UNINSPECTED; | 92 state_ = IsAttached() ? WORKER_PAUSED_FOR_REATTACH : WORKER_UNINSPECTED; |
| 86 worker_id_ = worker_id; | 93 worker_id_ = worker_id; |
| 87 WorkerCreated(); | 94 WorkerCreated(); |
| 88 } | 95 } |
| 89 | 96 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 112 WorkerId worker_id) | 119 WorkerId worker_id) |
| 113 : state_(WORKER_UNINSPECTED), | 120 : state_(WORKER_UNINSPECTED), |
| 114 worker_id_(worker_id) { | 121 worker_id_(worker_id) { |
| 115 WorkerCreated(); | 122 WorkerCreated(); |
| 116 } | 123 } |
| 117 | 124 |
| 118 WorkerDevToolsAgentHost::~WorkerDevToolsAgentHost() { | 125 WorkerDevToolsAgentHost::~WorkerDevToolsAgentHost() { |
| 119 DCHECK_EQ(WORKER_TERMINATED, state_); | 126 DCHECK_EQ(WORKER_TERMINATED, state_); |
| 120 } | 127 } |
| 121 | 128 |
| 129 void WorkerDevToolsAgentHost::OnAttachedStateChanged(bool attached) { |
| 130 } |
| 131 |
| 122 void WorkerDevToolsAgentHost::AttachToWorker() { | 132 void WorkerDevToolsAgentHost::AttachToWorker() { |
| 123 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) | 133 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) |
| 124 host->AddRoute(worker_id_.second, this); | 134 host->AddRoute(worker_id_.second, this); |
| 125 } | 135 } |
| 126 | 136 |
| 127 void WorkerDevToolsAgentHost::DetachFromWorker() { | 137 void WorkerDevToolsAgentHost::DetachFromWorker() { |
| 128 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) | 138 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) |
| 129 host->RemoveRoute(worker_id_.second); | 139 host->RemoveRoute(worker_id_.second); |
| 130 } | 140 } |
| 131 | 141 |
| 132 void WorkerDevToolsAgentHost::WorkerCreated() { | 142 void WorkerDevToolsAgentHost::WorkerCreated() { |
| 133 AddRef(); // Balanced in WorkerDestroyed() | 143 AddRef(); // Balanced in WorkerDestroyed() |
| 134 } | 144 } |
| 135 | 145 |
| 136 void WorkerDevToolsAgentHost::OnDispatchOnInspectorFrontend( | 146 void WorkerDevToolsAgentHost::OnDispatchOnInspectorFrontend( |
| 137 const DevToolsMessageChunk& message) { | 147 const DevToolsMessageChunk& message) { |
| 138 if (!IsAttached()) | 148 if (!IsAttached()) |
| 139 return; | 149 return; |
| 140 | 150 |
| 141 ProcessChunkedMessageFromAgent(message); | 151 ProcessChunkedMessageFromAgent(message); |
| 142 } | 152 } |
| 143 | 153 |
| 144 } // namespace content | 154 } // namespace content |
| OLD | NEW |