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/ipc_devtools_agent_host.h"
8 #include "content/browser/devtools/protocol/devtools_protocol_handler.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/render_process_host.h"
14 BrowserContext
* WorkerDevToolsAgentHost::GetBrowserContext() {
15 RenderProcessHost
* rph
= RenderProcessHost::FromID(worker_id_
.first
);
16 return rph
? rph
->GetBrowserContext() : nullptr;
19 void WorkerDevToolsAgentHost::SendMessageToAgent(
20 IPC::Message
* message_raw
) {
21 scoped_ptr
<IPC::Message
> message(message_raw
);
22 if (state_
!= WORKER_INSPECTED
)
24 if (RenderProcessHost
* host
= RenderProcessHost::FromID(worker_id_
.first
)) {
25 message
->set_routing_id(worker_id_
.second
);
26 host
->Send(message
.release());
30 void WorkerDevToolsAgentHost::Attach() {
31 if (state_
!= WORKER_INSPECTED
) {
32 state_
= WORKER_INSPECTED
;
35 IPCDevToolsAgentHost::Attach();
38 void WorkerDevToolsAgentHost::OnClientAttached(bool reattached
) {
40 DevToolsAgentHostImpl::NotifyCallbacks(this, true);
43 void WorkerDevToolsAgentHost::OnClientDetached() {
44 if (state_
== WORKER_INSPECTED
) {
45 state_
= WORKER_UNINSPECTED
;
47 } else if (state_
== WORKER_PAUSED_FOR_REATTACH
) {
48 state_
= WORKER_UNINSPECTED
;
50 DevToolsAgentHostImpl::NotifyCallbacks(this, false);
53 bool WorkerDevToolsAgentHost::OnMessageReceived(
54 const IPC::Message
& msg
) {
55 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
57 IPC_BEGIN_MESSAGE_MAP(WorkerDevToolsAgentHost
, msg
)
58 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend
,
59 OnDispatchOnInspectorFrontend
)
60 IPC_MESSAGE_UNHANDLED(handled
= false)
65 void WorkerDevToolsAgentHost::PauseForDebugOnStart() {
66 DCHECK(state_
== WORKER_UNINSPECTED
);
67 state_
= WORKER_PAUSED_FOR_DEBUG_ON_START
;
70 bool WorkerDevToolsAgentHost::IsPausedForDebugOnStart() {
71 return state_
== WORKER_PAUSED_FOR_DEBUG_ON_START
;
74 void WorkerDevToolsAgentHost::WorkerReadyForInspection() {
75 if (state_
== WORKER_PAUSED_FOR_REATTACH
) {
77 state_
= WORKER_INSPECTED
;
83 void WorkerDevToolsAgentHost::WorkerRestarted(WorkerId worker_id
) {
84 DCHECK_EQ(WORKER_TERMINATED
, state_
);
85 state_
= IsAttached() ? WORKER_PAUSED_FOR_REATTACH
: WORKER_UNINSPECTED
;
86 worker_id_
= worker_id
;
90 void WorkerDevToolsAgentHost::WorkerDestroyed() {
91 DCHECK_NE(WORKER_TERMINATED
, state_
);
92 if (state_
== WORKER_INSPECTED
) {
94 // Client host is debugging this worker agent host.
95 base::Callback
<void(const std::string
&)> raw_message_callback(
96 base::Bind(&WorkerDevToolsAgentHost::SendMessageToClient
,
97 base::Unretained(this)));
98 devtools::inspector::Client
inspector(raw_message_callback
);
99 inspector
.TargetCrashed(
100 devtools::inspector::TargetCrashedParams::Create());
103 state_
= WORKER_TERMINATED
;
104 Release(); // Balanced in WorkerCreated().
107 bool WorkerDevToolsAgentHost::IsTerminated() {
108 return state_
== WORKER_TERMINATED
;
111 WorkerDevToolsAgentHost::WorkerDevToolsAgentHost(
113 : state_(WORKER_UNINSPECTED
),
114 worker_id_(worker_id
) {
118 WorkerDevToolsAgentHost::~WorkerDevToolsAgentHost() {
119 DCHECK_EQ(WORKER_TERMINATED
, state_
);
122 void WorkerDevToolsAgentHost::AttachToWorker() {
123 if (RenderProcessHost
* host
= RenderProcessHost::FromID(worker_id_
.first
))
124 host
->AddRoute(worker_id_
.second
, this);
127 void WorkerDevToolsAgentHost::DetachFromWorker() {
128 if (RenderProcessHost
* host
= RenderProcessHost::FromID(worker_id_
.first
))
129 host
->RemoveRoute(worker_id_
.second
);
132 void WorkerDevToolsAgentHost::WorkerCreated() {
133 AddRef(); // Balanced in WorkerDestroyed()
136 void WorkerDevToolsAgentHost::OnDispatchOnInspectorFrontend(
137 const DevToolsMessageChunk
& message
) {
141 ProcessChunkedMessageFromAgent(message
);
144 } // namespace content