Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / devtools / devtools_agent_host_impl.cc
blobcba575ea0ef35d354040c7bce70a613201f78e1f
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/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"
25 namespace content {
27 namespace {
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*>
32 AgentStateCallbacks;
33 base::LazyInstance<AgentStateCallbacks>::Leaky g_callbacks =
34 LAZY_INSTANCE_INITIALIZER;
35 } // namespace
37 // static
38 std::string DevToolsAgentHost::GetProtocolVersion() {
39 return std::string(devtools::kProtocolVersion);
42 // static
43 bool DevToolsAgentHost::IsSupportedProtocolVersion(const std::string& version) {
44 return devtools::IsSupportedProtocolVersion(version);
47 // static
48 DevToolsAgentHost::List DevToolsAgentHost::GetOrCreateAll() {
49 List result;
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);
61 return result;
64 // Called on the UI thread.
65 // static
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,
72 worker_route_id)) {
73 return host;
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()),
84 client_(NULL),
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_));
95 // static
96 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForId(
97 const std::string& id) {
98 if (g_instances == NULL)
99 return NULL;
100 Instances::iterator it = g_instances.Get().find(id);
101 if (it == g_instances.Get().end())
102 return NULL;
103 return it->second;
106 //static
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);
114 if (client_) {
115 client_->AgentHostClosed(this, true);
116 Detach();
118 client_ = client;
119 Attach();
122 void DevToolsAgentHostImpl::DetachClient() {
123 if (!client_)
124 return;
126 scoped_refptr<DevToolsAgentHostImpl> protect(this);
127 client_ = NULL;
128 Detach();
131 bool DevToolsAgentHostImpl::IsAttached() {
132 return !!client_;
135 void DevToolsAgentHostImpl::InspectElement(int x, int y) {
138 std::string DevToolsAgentHostImpl::GetId() {
139 return id_;
142 BrowserContext* DevToolsAgentHostImpl::GetBrowserContext() {
143 return nullptr;
146 WebContents* DevToolsAgentHostImpl::GetWebContents() {
147 return NULL;
150 void DevToolsAgentHostImpl::DisconnectWebContents() {
153 void DevToolsAgentHostImpl::ConnectWebContents(WebContents* wc) {
156 void DevToolsAgentHostImpl::HostClosed() {
157 if (!client_)
158 return;
160 scoped_refptr<DevToolsAgentHostImpl> protect(this);
161 // Clear |client_| before notifying it.
162 DevToolsAgentHostClient* client = client_;
163 client_ = NULL;
164 client->AgentHostClosed(this, false);
167 void DevToolsAgentHostImpl::SendMessageToClient(const std::string& message) {
168 if (!client_)
169 return;
170 client_->DispatchProtocolMessage(this, message);
173 // static
174 void DevToolsAgentHost::DetachAllClients() {
175 if (g_instances == NULL)
176 return;
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();
194 // static
195 void DevToolsAgentHost::AddAgentStateCallback(
196 const AgentStateCallback& callback) {
197 g_callbacks.Get().push_back(&callback);
200 // static
201 void DevToolsAgentHost::RemoveAgentStateCallback(
202 const AgentStateCallback& callback) {
203 if (g_callbacks == NULL)
204 return;
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);
213 // static
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);
235 if (!command)
236 return true;
238 DevToolsManagerDelegate* delegate =
239 DevToolsManager::GetInstance()->delegate();
240 if (delegate) {
241 scoped_ptr<base::DictionaryValue> response(
242 delegate->HandleCommand(this, command.get()));
243 if (response) {
244 std::string json_response;
245 base::JSONWriter::Write(response.get(), &json_response);
246 SendMessageToClient(json_response);
247 return true;
251 if (!handle_all_commands_)
252 return protocol_handler_->HandleOptionalCommand(command.Pass());
253 protocol_handler_->HandleCommand(command.Pass());
254 return true;
257 } // namespace content