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