ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / content / browser / devtools / devtools_agent_host_impl.cc
blob5274b8376850c350bbf4280119cc8fcad1fd4267
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"
7 #include <map>
8 #include <vector>
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"
20 namespace content {
22 namespace {
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*>
27 AgentStateCallbacks;
28 base::LazyInstance<AgentStateCallbacks>::Leaky g_callbacks =
29 LAZY_INSTANCE_INITIALIZER;
30 } // namespace
32 // static
33 DevToolsAgentHost::List DevToolsAgentHost::GetOrCreateAll() {
34 List result;
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));
43 return result;
46 DevToolsAgentHostImpl::DevToolsAgentHostImpl()
47 : id_(base::GenerateGUID()),
48 client_(NULL) {
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_));
58 // static
59 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForId(
60 const std::string& id) {
61 if (g_instances == NULL)
62 return NULL;
63 Instances::iterator it = g_instances.Get().find(id);
64 if (it == g_instances.Get().end())
65 return NULL;
66 return it->second;
69 //static
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);
77 if (client_) {
78 client_->AgentHostClosed(this, true);
79 Detach();
81 client_ = client;
82 Attach();
83 DevToolsManager::GetInstance()->AgentHostChanged(this);
86 void DevToolsAgentHostImpl::DetachClient() {
87 if (!client_)
88 return;
90 scoped_refptr<DevToolsAgentHostImpl> protect(this);
91 client_ = NULL;
92 Detach();
93 DevToolsManager::GetInstance()->AgentHostChanged(this);
96 bool DevToolsAgentHostImpl::IsAttached() {
97 return !!client_;
100 void DevToolsAgentHostImpl::InspectElement(int x, int y) {
103 std::string DevToolsAgentHostImpl::GetId() {
104 return id_;
107 BrowserContext* DevToolsAgentHostImpl::GetBrowserContext() {
108 return nullptr;
111 WebContents* DevToolsAgentHostImpl::GetWebContents() {
112 return NULL;
115 void DevToolsAgentHostImpl::DisconnectWebContents() {
118 void DevToolsAgentHostImpl::ConnectWebContents(WebContents* wc) {
121 bool DevToolsAgentHostImpl::IsWorker() const {
122 return false;
125 void DevToolsAgentHostImpl::HostClosed() {
126 if (!client_)
127 return;
129 scoped_refptr<DevToolsAgentHostImpl> protect(this);
130 // Clear |client_| before notifying it.
131 DevToolsAgentHostClient* client = client_;
132 client_ = NULL;
133 client->AgentHostClosed(this, false);
134 DevToolsManager::GetInstance()->AgentHostChanged(this);
137 void DevToolsAgentHostImpl::SendMessageToClient(const std::string& message) {
138 if (!client_)
139 return;
140 client_->DispatchProtocolMessage(this, message);
143 // static
144 void DevToolsAgentHost::DetachAllClients() {
145 if (g_instances == NULL)
146 return;
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);
165 // static
166 void DevToolsAgentHost::AddAgentStateCallback(
167 const AgentStateCallback& callback) {
168 g_callbacks.Get().push_back(&callback);
171 // static
172 void DevToolsAgentHost::RemoveAgentStateCallback(
173 const AgentStateCallback& callback) {
174 if (g_callbacks == NULL)
175 return;
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);
184 // static
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