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/lazy_instance.h"
13 #include "content/browser/devtools/devtools_manager.h"
14 #include "content/browser/devtools/forwarding_agent_host.h"
15 #include "content/browser/devtools/service_worker_devtools_manager.h"
16 #include "content/browser/devtools/shared_worker_devtools_manager.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/devtools_manager_delegate.h"
23 typedef std::map
<std::string
, DevToolsAgentHostImpl
*> Instances
;
24 base::LazyInstance
<Instances
>::Leaky g_instances
= LAZY_INSTANCE_INITIALIZER
;
26 typedef std::vector
<const DevToolsAgentHost::AgentStateCallback
*>
28 base::LazyInstance
<AgentStateCallbacks
>::Leaky g_callbacks
=
29 LAZY_INSTANCE_INITIALIZER
;
33 DevToolsAgentHost::List
DevToolsAgentHost::GetOrCreateAll() {
35 SharedWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&result
);
36 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&result
);
37 std::vector
<WebContents
*> wc_list
=
38 DevToolsAgentHostImpl::GetInspectableWebContents();
39 for (std::vector
<WebContents
*>::iterator it
= wc_list
.begin();
40 it
!= wc_list
.end(); ++it
) {
41 result
.push_back(GetOrCreateFor(*it
));
46 DevToolsAgentHostImpl::DevToolsAgentHostImpl()
47 : id_(base::GenerateGUID()),
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
50 g_instances
.Get()[id_
] = this;
53 DevToolsAgentHostImpl::~DevToolsAgentHostImpl() {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
55 g_instances
.Get().erase(g_instances
.Get().find(id_
));
59 scoped_refptr
<DevToolsAgentHost
> DevToolsAgentHost::GetForId(
60 const std::string
& id
) {
61 if (g_instances
== NULL
)
63 Instances::iterator it
= g_instances
.Get().find(id
);
64 if (it
== g_instances
.Get().end())
70 scoped_refptr
<DevToolsAgentHost
> DevToolsAgentHost::Create(
71 DevToolsExternalAgentProxyDelegate
* delegate
) {
72 return new ForwardingAgentHost(delegate
);
75 void DevToolsAgentHostImpl::AttachClient(DevToolsAgentHostClient
* client
) {
76 scoped_refptr
<DevToolsAgentHostImpl
> protect(this);
78 client_
->AgentHostClosed(this, true);
83 DevToolsManager::GetInstance()->AgentHostChanged(this);
86 void DevToolsAgentHostImpl::DetachClient() {
90 scoped_refptr
<DevToolsAgentHostImpl
> protect(this);
93 DevToolsManager::GetInstance()->AgentHostChanged(this);
96 bool DevToolsAgentHostImpl::IsAttached() {
100 void DevToolsAgentHostImpl::InspectElement(int x
, int y
) {
103 std::string
DevToolsAgentHostImpl::GetId() {
107 BrowserContext
* DevToolsAgentHostImpl::GetBrowserContext() {
111 WebContents
* DevToolsAgentHostImpl::GetWebContents() {
115 void DevToolsAgentHostImpl::DisconnectWebContents() {
118 void DevToolsAgentHostImpl::ConnectWebContents(WebContents
* wc
) {
121 bool DevToolsAgentHostImpl::IsWorker() const {
125 void DevToolsAgentHostImpl::HostClosed() {
129 scoped_refptr
<DevToolsAgentHostImpl
> protect(this);
130 // Clear |client_| before notifying it.
131 DevToolsAgentHostClient
* client
= client_
;
133 client
->AgentHostClosed(this, false);
134 DevToolsManager::GetInstance()->AgentHostChanged(this);
137 void DevToolsAgentHostImpl::SendMessageToClient(const std::string
& message
) {
140 client_
->DispatchProtocolMessage(this, message
);
144 void DevToolsAgentHost::DetachAllClients() {
145 if (g_instances
== NULL
)
148 // Make a copy, since detaching may lead to agent destruction, which
149 // removes it from the instances.
150 Instances copy
= g_instances
.Get();
151 for (Instances::iterator
it(copy
.begin()); it
!= copy
.end(); ++it
) {
152 DevToolsAgentHostImpl
* agent_host
= it
->second
;
153 if (agent_host
->client_
) {
154 scoped_refptr
<DevToolsAgentHostImpl
> protect(agent_host
);
155 // Clear |client_| before notifying it.
156 DevToolsAgentHostClient
* client
= agent_host
->client_
;
157 agent_host
->client_
= NULL
;
158 client
->AgentHostClosed(agent_host
, true);
159 agent_host
->Detach();
160 DevToolsManager::GetInstance()->AgentHostChanged(protect
);
166 void DevToolsAgentHost::AddAgentStateCallback(
167 const AgentStateCallback
& callback
) {
168 g_callbacks
.Get().push_back(&callback
);
172 void DevToolsAgentHost::RemoveAgentStateCallback(
173 const AgentStateCallback
& callback
) {
174 if (g_callbacks
== NULL
)
177 AgentStateCallbacks
* callbacks_
= g_callbacks
.Pointer();
178 AgentStateCallbacks::iterator it
=
179 std::find(callbacks_
->begin(), callbacks_
->end(), &callback
);
180 DCHECK(it
!= callbacks_
->end());
181 callbacks_
->erase(it
);
185 void DevToolsAgentHostImpl::NotifyCallbacks(
186 DevToolsAgentHostImpl
* agent_host
, bool attached
) {
187 AgentStateCallbacks
copy(g_callbacks
.Get());
188 DevToolsManager
* manager
= DevToolsManager::GetInstance();
189 if (manager
->delegate())
190 manager
->delegate()->DevToolsAgentStateChanged(agent_host
, attached
);
191 for (AgentStateCallbacks::iterator it
= copy
.begin(); it
!= copy
.end(); ++it
)
192 (*it
)->Run(agent_host
, attached
);
195 void DevToolsAgentHostImpl::Inspect(BrowserContext
* browser_context
) {
196 DevToolsManager
* manager
= DevToolsManager::GetInstance();
197 if (manager
->delegate())
198 manager
->delegate()->Inspect(browser_context
, this);
201 } // namespace content