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_handler.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"
23 #include "content/public/browser/devtools_manager_delegate.h"
28 typedef std::map
<std::string
, DevToolsAgentHostImpl
*> Instances
;
29 base::LazyInstance
<Instances
>::Leaky g_instances
= LAZY_INSTANCE_INITIALIZER
;
31 typedef std::vector
<const DevToolsAgentHost::AgentStateCallback
*>
33 base::LazyInstance
<AgentStateCallbacks
>::Leaky g_callbacks
=
34 LAZY_INSTANCE_INITIALIZER
;
38 std::string
DevToolsAgentHost::GetProtocolVersion() {
39 return std::string(devtools::kProtocolVersion
);
43 bool DevToolsAgentHost::IsSupportedProtocolVersion(const std::string
& version
) {
44 return devtools::IsSupportedProtocolVersion(version
);
48 DevToolsAgentHost::List
DevToolsAgentHost::GetOrCreateAll() {
50 SharedWorkerDevToolsAgentHost::List shared_list
;
51 SharedWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&shared_list
);
52 for (const auto& host
: shared_list
)
53 result
.push_back(host
);
55 ServiceWorkerDevToolsAgentHost::List service_list
;
56 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&service_list
);
57 for (const auto& host
: service_list
)
58 result
.push_back(host
);
60 RenderFrameDevToolsAgentHost::AddAllAgentHosts(&result
);
64 // Called on the UI thread.
66 scoped_refptr
<DevToolsAgentHost
> DevToolsAgentHost::GetForWorker(
67 int worker_process_id
,
68 int worker_route_id
) {
69 if (scoped_refptr
<DevToolsAgentHost
> host
=
70 SharedWorkerDevToolsManager::GetInstance()
71 ->GetDevToolsAgentHostForWorker(worker_process_id
,
75 return ServiceWorkerDevToolsManager::GetInstance()
76 ->GetDevToolsAgentHostForWorker(worker_process_id
, worker_route_id
);
79 DevToolsAgentHostImpl::DevToolsAgentHostImpl()
80 : protocol_handler_(new DevToolsProtocolHandler(
81 base::Bind(&DevToolsAgentHostImpl::SendMessageToClient
,
82 base::Unretained(this)))),
83 id_(base::GenerateGUID()),
85 handle_all_commands_(false) {
86 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
87 g_instances
.Get()[id_
] = this;
90 DevToolsAgentHostImpl::~DevToolsAgentHostImpl() {
91 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
92 g_instances
.Get().erase(g_instances
.Get().find(id_
));
96 scoped_refptr
<DevToolsAgentHost
> DevToolsAgentHost::GetForId(
97 const std::string
& id
) {
98 if (g_instances
== NULL
)
100 Instances::iterator it
= g_instances
.Get().find(id
);
101 if (it
== g_instances
.Get().end())
107 scoped_refptr
<DevToolsAgentHost
> DevToolsAgentHost::Create(
108 DevToolsExternalAgentProxyDelegate
* delegate
) {
109 return new ForwardingAgentHost(delegate
);
112 void DevToolsAgentHostImpl::AttachClient(DevToolsAgentHostClient
* client
) {
113 scoped_refptr
<DevToolsAgentHostImpl
> protect(this);
115 client_
->AgentHostClosed(this, true);
122 void DevToolsAgentHostImpl::DetachClient() {
126 scoped_refptr
<DevToolsAgentHostImpl
> protect(this);
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
->Detach();
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 bool DevToolsAgentHostImpl::DispatchProtocolMessage(
232 const std::string
& message
) {
233 scoped_ptr
<base::DictionaryValue
> command
=
234 protocol_handler_
->ParseCommand(message
);
238 DevToolsManagerDelegate
* delegate
=
239 DevToolsManager::GetInstance()->delegate();
241 scoped_ptr
<base::DictionaryValue
> response(
242 delegate
->HandleCommand(this, command
.get()));
244 std::string json_response
;
245 base::JSONWriter::Write(response
.get(), &json_response
);
246 SendMessageToClient(json_response
);
251 if (!handle_all_commands_
)
252 return protocol_handler_
->HandleOptionalCommand(command
.Pass());
253 protocol_handler_
->HandleCommand(command
.Pass());
257 } // namespace content