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.
5 #include "content/browser/devtools/worker_devtools_agent_host.h"
7 #include "content/browser/devtools/devtools_protocol_handler.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "content/public/browser/render_process_host.h"
13 BrowserContext
* WorkerDevToolsAgentHost::GetBrowserContext() {
14 RenderProcessHost
* rph
= RenderProcessHost::FromID(worker_id_
.first
);
15 return rph
? rph
->GetBrowserContext() : nullptr;
18 void WorkerDevToolsAgentHost::Attach() {
19 if (state_
!= WORKER_INSPECTED
) {
20 state_
= WORKER_INSPECTED
;
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);
29 void WorkerDevToolsAgentHost::Detach() {
30 if (RenderProcessHost
* host
= RenderProcessHost::FromID(worker_id_
.first
))
31 host
->Send(new DevToolsAgentMsg_Detach(worker_id_
.second
));
32 OnAttachedStateChanged(false);
33 if (state_
== WORKER_INSPECTED
) {
34 state_
= WORKER_UNINSPECTED
;
36 } else if (state_
== WORKER_PAUSED_FOR_REATTACH
) {
37 state_
= WORKER_UNINSPECTED
;
39 DevToolsAgentHostImpl::NotifyCallbacks(this, false);
42 bool WorkerDevToolsAgentHost::DispatchProtocolMessage(
43 const std::string
& message
) {
44 if (state_
!= WORKER_INSPECTED
)
48 if (protocol_handler_
->HandleOptionalMessage(message
, &call_id
))
51 if (RenderProcessHost
* host
= RenderProcessHost::FromID(worker_id_
.first
)) {
52 host
->Send(new DevToolsAgentMsg_DispatchOnInspectorBackend(
53 worker_id_
.second
, message
));
58 bool WorkerDevToolsAgentHost::OnMessageReceived(
59 const IPC::Message
& msg
) {
60 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
62 IPC_BEGIN_MESSAGE_MAP(WorkerDevToolsAgentHost
, msg
)
63 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend
,
64 OnDispatchOnInspectorFrontend
)
65 IPC_MESSAGE_UNHANDLED(handled
= false)
70 void WorkerDevToolsAgentHost::PauseForDebugOnStart() {
71 DCHECK(state_
== WORKER_UNINSPECTED
);
72 state_
= WORKER_PAUSED_FOR_DEBUG_ON_START
;
75 bool WorkerDevToolsAgentHost::IsPausedForDebugOnStart() {
76 return state_
== WORKER_PAUSED_FOR_DEBUG_ON_START
;
79 void WorkerDevToolsAgentHost::WorkerReadyForInspection() {
80 if (state_
== WORKER_PAUSED_FOR_REATTACH
) {
82 state_
= WORKER_INSPECTED
;
84 if (RenderProcessHost
* host
= RenderProcessHost::FromID(worker_id_
.first
)) {
85 host
->Send(new DevToolsAgentMsg_Reattach(
86 worker_id_
.second
, GetId(), chunk_processor_
.state_cookie()));
88 OnAttachedStateChanged(true);
92 void WorkerDevToolsAgentHost::WorkerRestarted(WorkerId worker_id
) {
93 DCHECK_EQ(WORKER_TERMINATED
, state_
);
94 state_
= IsAttached() ? WORKER_PAUSED_FOR_REATTACH
: WORKER_UNINSPECTED
;
95 worker_id_
= worker_id
;
99 void WorkerDevToolsAgentHost::WorkerDestroyed() {
100 DCHECK_NE(WORKER_TERMINATED
, state_
);
101 if (state_
== WORKER_INSPECTED
) {
102 DCHECK(IsAttached());
103 // Client host is debugging this worker agent host.
104 base::Callback
<void(const std::string
&)> raw_message_callback(
105 base::Bind(&WorkerDevToolsAgentHost::SendMessageToClient
,
106 base::Unretained(this)));
107 devtools::inspector::Client
inspector(raw_message_callback
);
108 inspector
.TargetCrashed(
109 devtools::inspector::TargetCrashedParams::Create());
112 state_
= WORKER_TERMINATED
;
113 Release(); // Balanced in WorkerCreated().
116 bool WorkerDevToolsAgentHost::IsTerminated() {
117 return state_
== WORKER_TERMINATED
;
120 WorkerDevToolsAgentHost::WorkerDevToolsAgentHost(
122 : protocol_handler_(new DevToolsProtocolHandler(
124 base::Bind(&WorkerDevToolsAgentHost::SendMessageToClient
,
125 base::Unretained(this)))),
127 base::Bind(&WorkerDevToolsAgentHost::SendMessageToClient
,
128 base::Unretained(this))),
129 state_(WORKER_UNINSPECTED
),
130 worker_id_(worker_id
) {
134 WorkerDevToolsAgentHost::~WorkerDevToolsAgentHost() {
135 DCHECK_EQ(WORKER_TERMINATED
, state_
);
138 void WorkerDevToolsAgentHost::OnAttachedStateChanged(bool attached
) {
141 void WorkerDevToolsAgentHost::AttachToWorker() {
142 if (RenderProcessHost
* host
= RenderProcessHost::FromID(worker_id_
.first
))
143 host
->AddRoute(worker_id_
.second
, this);
146 void WorkerDevToolsAgentHost::DetachFromWorker() {
147 if (RenderProcessHost
* host
= RenderProcessHost::FromID(worker_id_
.first
))
148 host
->RemoveRoute(worker_id_
.second
);
151 void WorkerDevToolsAgentHost::WorkerCreated() {
152 AddRef(); // Balanced in WorkerDestroyed()
155 void WorkerDevToolsAgentHost::OnDispatchOnInspectorFrontend(
156 const DevToolsMessageChunk
& message
) {
160 chunk_processor_
.ProcessChunkedMessageFromAgent(message
);
163 } // namespace content