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 bool WorkerDevToolsAgentHost::IsWorker() const {
18 BrowserContext
* WorkerDevToolsAgentHost::GetBrowserContext() {
19 RenderProcessHost
* rph
= RenderProcessHost::FromID(worker_id_
.first
);
20 return rph
? rph
->GetBrowserContext() : nullptr;
23 void WorkerDevToolsAgentHost::SendMessageToAgent(
24 IPC::Message
* message_raw
) {
25 scoped_ptr
<IPC::Message
> message(message_raw
);
26 if (state_
!= WORKER_INSPECTED
)
28 if (RenderProcessHost
* host
= RenderProcessHost::FromID(worker_id_
.first
)) {
29 message
->set_routing_id(worker_id_
.second
);
30 host
->Send(message
.release());
34 void WorkerDevToolsAgentHost::Attach() {
35 if (state_
!= WORKER_INSPECTED
) {
36 state_
= WORKER_INSPECTED
;
39 IPCDevToolsAgentHost::Attach();
42 void WorkerDevToolsAgentHost::OnClientAttached() {
43 DevToolsAgentHostImpl::NotifyCallbacks(this, true);
46 void WorkerDevToolsAgentHost::OnClientDetached() {
47 if (state_
== WORKER_INSPECTED
) {
48 state_
= WORKER_UNINSPECTED
;
50 } else if (state_
== WORKER_PAUSED_FOR_REATTACH
) {
51 state_
= WORKER_UNINSPECTED
;
53 DevToolsAgentHostImpl::NotifyCallbacks(this, false);
56 bool WorkerDevToolsAgentHost::OnMessageReceived(
57 const IPC::Message
& msg
) {
58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
60 IPC_BEGIN_MESSAGE_MAP(WorkerDevToolsAgentHost
, msg
)
61 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend
,
62 OnDispatchOnInspectorFrontend
)
63 IPC_MESSAGE_UNHANDLED(handled
= false)
68 void WorkerDevToolsAgentHost::WorkerReadyForInspection() {
69 if (state_
== WORKER_PAUSED_FOR_DEBUG_ON_START
) {
70 RenderProcessHost
* rph
= RenderProcessHost::FromID(worker_id_
.first
);
71 Inspect(rph
->GetBrowserContext());
72 } else if (state_
== WORKER_PAUSED_FOR_REATTACH
) {
74 state_
= WORKER_INSPECTED
;
80 void WorkerDevToolsAgentHost::WorkerRestarted(WorkerId worker_id
) {
81 DCHECK_EQ(WORKER_TERMINATED
, state_
);
82 state_
= IsAttached() ? WORKER_PAUSED_FOR_REATTACH
: WORKER_UNINSPECTED
;
83 worker_id_
= worker_id
;
87 void WorkerDevToolsAgentHost::WorkerDestroyed() {
88 DCHECK_NE(WORKER_TERMINATED
, state_
);
89 if (state_
== WORKER_INSPECTED
) {
91 // Client host is debugging this worker agent host.
92 base::Callback
<void(const std::string
&)> raw_message_callback(
93 base::Bind(&WorkerDevToolsAgentHost::SendMessageToClient
,
94 base::Unretained(this)));
95 devtools::worker::Client
worker(raw_message_callback
);
96 worker
.DisconnectedFromWorker(
97 devtools::worker::DisconnectedFromWorkerParams::Create());
100 state_
= WORKER_TERMINATED
;
101 Release(); // Balanced in WorkerCreated().
104 bool WorkerDevToolsAgentHost::IsTerminated() {
105 return state_
== WORKER_TERMINATED
;
108 WorkerDevToolsAgentHost::WorkerDevToolsAgentHost(
110 : state_(WORKER_UNINSPECTED
),
111 worker_id_(worker_id
) {
115 WorkerDevToolsAgentHost::~WorkerDevToolsAgentHost() {
116 DCHECK_EQ(WORKER_TERMINATED
, state_
);
119 void WorkerDevToolsAgentHost::AttachToWorker() {
120 if (RenderProcessHost
* host
= RenderProcessHost::FromID(worker_id_
.first
))
121 host
->AddRoute(worker_id_
.second
, this);
124 void WorkerDevToolsAgentHost::DetachFromWorker() {
125 if (RenderProcessHost
* host
= RenderProcessHost::FromID(worker_id_
.first
))
126 host
->RemoveRoute(worker_id_
.second
);
129 void WorkerDevToolsAgentHost::WorkerCreated() {
130 AddRef(); // Balanced in WorkerDestroyed()
133 void WorkerDevToolsAgentHost::OnDispatchOnInspectorFrontend(
134 const DevToolsMessageChunk
& message
) {
138 ProcessChunkedMessageFromAgent(message
);
141 } // namespace content