1 // Copyright (c) 2012 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/devtools_agent_host_impl.h"
10 #include "base/basictypes.h"
11 #include "base/guid.h"
12 #include "base/json/json_writer.h"
13 #include "base/lazy_instance.h"
14 #include "content/browser/devtools/devtools_manager.h"
15 #include "content/browser/devtools/forwarding_agent_host.h"
16 #include "content/browser/devtools/protocol/devtools_protocol_dispatcher.h"
17 #include "content/browser/devtools/render_frame_devtools_agent_host.h"
18 #include "content/browser/devtools/service_worker_devtools_agent_host.h"
19 #include "content/browser/devtools/service_worker_devtools_manager.h"
20 #include "content/browser/devtools/shared_worker_devtools_agent_host.h"
21 #include "content/browser/devtools/shared_worker_devtools_manager.h"
22 #include "content/public/browser/browser_thread.h"
27 typedef std::map
<std::string
, DevToolsAgentHostImpl
*> Instances
;
28 base::LazyInstance
<Instances
>::Leaky g_instances
= LAZY_INSTANCE_INITIALIZER
;
30 typedef std::vector
<const DevToolsAgentHost::AgentStateCallback
*>
32 base::LazyInstance
<AgentStateCallbacks
>::Leaky g_callbacks
=
33 LAZY_INSTANCE_INITIALIZER
;
37 std::string
DevToolsAgentHost::GetProtocolVersion() {
38 return std::string(devtools::kProtocolVersion
);
42 bool DevToolsAgentHost::IsSupportedProtocolVersion(const std::string
& version
) {
43 return devtools::IsSupportedProtocolVersion(version
);
47 DevToolsAgentHost::List
DevToolsAgentHost::GetOrCreateAll() {
49 SharedWorkerDevToolsAgentHost::List shared_list
;
50 SharedWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&shared_list
);
51 for (const auto& host
: shared_list
)
52 result
.push_back(host
);
54 ServiceWorkerDevToolsAgentHost::List service_list
;
55 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&service_list
);
56 for (const auto& host
: service_list
)
57 result
.push_back(host
);
59 RenderFrameDevToolsAgentHost::AddAllAgentHosts(&result
);
63 // Called on the UI thread.
65 scoped_refptr
<DevToolsAgentHost
> DevToolsAgentHost::GetForWorker(
66 int worker_process_id
,
67 int worker_route_id
) {
68 if (scoped_refptr
<DevToolsAgentHost
> host
=
69 SharedWorkerDevToolsManager::GetInstance()
70 ->GetDevToolsAgentHostForWorker(worker_process_id
,
74 return ServiceWorkerDevToolsManager::GetInstance()
75 ->GetDevToolsAgentHostForWorker(worker_process_id
, worker_route_id
);
78 DevToolsAgentHostImpl::DevToolsAgentHostImpl()
79 : id_(base::GenerateGUID()),
81 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
82 g_instances
.Get()[id_
] = this;
85 DevToolsAgentHostImpl::~DevToolsAgentHostImpl() {
86 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
87 g_instances
.Get().erase(g_instances
.Get().find(id_
));
91 scoped_refptr
<DevToolsAgentHost
> DevToolsAgentHost::GetForId(
92 const std::string
& id
) {
93 if (g_instances
== NULL
)
95 Instances::iterator it
= g_instances
.Get().find(id
);
96 if (it
== g_instances
.Get().end())
102 scoped_refptr
<DevToolsAgentHost
> DevToolsAgentHost::Create(
103 DevToolsExternalAgentProxyDelegate
* delegate
) {
104 return new ForwardingAgentHost(delegate
);
107 void DevToolsAgentHostImpl::AttachClient(DevToolsAgentHostClient
* client
) {
108 scoped_refptr
<DevToolsAgentHostImpl
> protect(this);
110 client_
->AgentHostClosed(this, true);
117 void DevToolsAgentHostImpl::DetachClient() {
121 scoped_refptr
<DevToolsAgentHostImpl
> protect(this);
126 void DevToolsAgentHostImpl::InnerDetach() {
128 io_context_
.DiscardAllStreams();
131 bool DevToolsAgentHostImpl::IsAttached() {
135 void DevToolsAgentHostImpl::InspectElement(int x
, int y
) {
138 std::string
DevToolsAgentHostImpl::GetId() {
142 BrowserContext
* DevToolsAgentHostImpl::GetBrowserContext() {
146 WebContents
* DevToolsAgentHostImpl::GetWebContents() {
150 void DevToolsAgentHostImpl::DisconnectWebContents() {
153 void DevToolsAgentHostImpl::ConnectWebContents(WebContents
* wc
) {
156 void DevToolsAgentHostImpl::HostClosed() {
160 scoped_refptr
<DevToolsAgentHostImpl
> protect(this);
161 // Clear |client_| before notifying it.
162 DevToolsAgentHostClient
* client
= client_
;
164 client
->AgentHostClosed(this, false);
167 void DevToolsAgentHostImpl::SendMessageToClient(const std::string
& message
) {
170 client_
->DispatchProtocolMessage(this, message
);
174 void DevToolsAgentHost::DetachAllClients() {
175 if (g_instances
== NULL
)
178 // Make a copy, since detaching may lead to agent destruction, which
179 // removes it from the instances.
180 Instances copy
= g_instances
.Get();
181 for (Instances::iterator
it(copy
.begin()); it
!= copy
.end(); ++it
) {
182 DevToolsAgentHostImpl
* agent_host
= it
->second
;
183 if (agent_host
->client_
) {
184 scoped_refptr
<DevToolsAgentHostImpl
> protect(agent_host
);
185 // Clear |client_| before notifying it.
186 DevToolsAgentHostClient
* client
= agent_host
->client_
;
187 agent_host
->client_
= NULL
;
188 client
->AgentHostClosed(agent_host
, true);
189 agent_host
->InnerDetach();
195 void DevToolsAgentHost::AddAgentStateCallback(
196 const AgentStateCallback
& callback
) {
197 g_callbacks
.Get().push_back(&callback
);
201 void DevToolsAgentHost::RemoveAgentStateCallback(
202 const AgentStateCallback
& callback
) {
203 if (g_callbacks
== NULL
)
206 AgentStateCallbacks
* callbacks_
= g_callbacks
.Pointer();
207 AgentStateCallbacks::iterator it
=
208 std::find(callbacks_
->begin(), callbacks_
->end(), &callback
);
209 DCHECK(it
!= callbacks_
->end());
210 callbacks_
->erase(it
);
214 void DevToolsAgentHostImpl::NotifyCallbacks(
215 DevToolsAgentHostImpl
* agent_host
, bool attached
) {
216 AgentStateCallbacks
copy(g_callbacks
.Get());
217 DevToolsManager
* manager
= DevToolsManager::GetInstance();
218 manager
->AgentHostStateChanged(agent_host
, attached
);
219 if (manager
->delegate())
220 manager
->delegate()->DevToolsAgentStateChanged(agent_host
, attached
);
221 for (AgentStateCallbacks::iterator it
= copy
.begin(); it
!= copy
.end(); ++it
)
222 (*it
)->Run(agent_host
, attached
);
225 void DevToolsAgentHostImpl::Inspect(BrowserContext
* browser_context
) {
226 DevToolsManager
* manager
= DevToolsManager::GetInstance();
227 if (manager
->delegate())
228 manager
->delegate()->Inspect(browser_context
, this);
231 // DevToolsMessageChunkProcessor -----------------------------------------------
233 DevToolsMessageChunkProcessor::DevToolsMessageChunkProcessor(
234 const SendMessageCallback
& callback
)
235 : callback_(callback
),
236 message_buffer_size_(0),
240 DevToolsMessageChunkProcessor::~DevToolsMessageChunkProcessor() {
243 void DevToolsMessageChunkProcessor::ProcessChunkedMessageFromAgent(
244 const DevToolsMessageChunk
& chunk
) {
245 if (chunk
.is_last
&& !chunk
.post_state
.empty())
246 state_cookie_
= chunk
.post_state
;
248 last_call_id_
= chunk
.call_id
;
250 if (chunk
.is_first
&& chunk
.is_last
) {
251 CHECK(message_buffer_size_
== 0);
252 callback_
.Run(chunk
.data
);
256 if (chunk
.is_first
) {
257 message_buffer_
= std::string();
258 message_buffer_
.reserve(chunk
.message_size
);
259 message_buffer_size_
= chunk
.message_size
;
262 CHECK(message_buffer_
.size() + chunk
.data
.size() <=
263 message_buffer_size_
);
264 message_buffer_
.append(chunk
.data
);
267 CHECK(message_buffer_
.size() == message_buffer_size_
);
268 callback_
.Run(message_buffer_
);
269 message_buffer_
= std::string();
270 message_buffer_size_
= 0;
274 } // namespace content