DevTools: cut host and port from webSocketDebuggerUrl in addition to ws:// prefix
[chromium-blink-merge.git] / content / browser / devtools / devtools_agent_host_impl.cc
blobcff2fe8e5ac68b114f65263eb33a365a4769c555
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_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"
24 namespace content {
26 namespace {
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*>
31 AgentStateCallbacks;
32 base::LazyInstance<AgentStateCallbacks>::Leaky g_callbacks =
33 LAZY_INSTANCE_INITIALIZER;
34 } // namespace
36 // static
37 std::string DevToolsAgentHost::GetProtocolVersion() {
38 return std::string(devtools::kProtocolVersion);
41 // static
42 bool DevToolsAgentHost::IsSupportedProtocolVersion(const std::string& version) {
43 return devtools::IsSupportedProtocolVersion(version);
46 // static
47 DevToolsAgentHost::List DevToolsAgentHost::GetOrCreateAll() {
48 List result;
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);
60 return result;
63 // Called on the UI thread.
64 // static
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,
71 worker_route_id)) {
72 return host;
74 return ServiceWorkerDevToolsManager::GetInstance()
75 ->GetDevToolsAgentHostForWorker(worker_process_id, worker_route_id);
78 DevToolsAgentHostImpl::DevToolsAgentHostImpl()
79 : id_(base::GenerateGUID()),
80 client_(NULL) {
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_));
90 // static
91 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForId(
92 const std::string& id) {
93 if (g_instances == NULL)
94 return NULL;
95 Instances::iterator it = g_instances.Get().find(id);
96 if (it == g_instances.Get().end())
97 return NULL;
98 return it->second;
101 //static
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);
109 if (client_) {
110 client_->AgentHostClosed(this, true);
111 Detach();
113 client_ = client;
114 Attach();
117 void DevToolsAgentHostImpl::DetachClient() {
118 if (!client_)
119 return;
121 scoped_refptr<DevToolsAgentHostImpl> protect(this);
122 client_ = NULL;
123 Detach();
126 bool DevToolsAgentHostImpl::IsAttached() {
127 return !!client_;
130 void DevToolsAgentHostImpl::InspectElement(int x, int y) {
133 std::string DevToolsAgentHostImpl::GetId() {
134 return id_;
137 BrowserContext* DevToolsAgentHostImpl::GetBrowserContext() {
138 return nullptr;
141 WebContents* DevToolsAgentHostImpl::GetWebContents() {
142 return NULL;
145 void DevToolsAgentHostImpl::DisconnectWebContents() {
148 void DevToolsAgentHostImpl::ConnectWebContents(WebContents* wc) {
151 void DevToolsAgentHostImpl::HostClosed() {
152 if (!client_)
153 return;
155 scoped_refptr<DevToolsAgentHostImpl> protect(this);
156 // Clear |client_| before notifying it.
157 DevToolsAgentHostClient* client = client_;
158 client_ = NULL;
159 client->AgentHostClosed(this, false);
162 void DevToolsAgentHostImpl::SendMessageToClient(const std::string& message) {
163 if (!client_)
164 return;
165 client_->DispatchProtocolMessage(this, message);
168 // static
169 void DevToolsAgentHost::DetachAllClients() {
170 if (g_instances == NULL)
171 return;
173 // Make a copy, since detaching may lead to agent destruction, which
174 // removes it from the instances.
175 Instances copy = g_instances.Get();
176 for (Instances::iterator it(copy.begin()); it != copy.end(); ++it) {
177 DevToolsAgentHostImpl* agent_host = it->second;
178 if (agent_host->client_) {
179 scoped_refptr<DevToolsAgentHostImpl> protect(agent_host);
180 // Clear |client_| before notifying it.
181 DevToolsAgentHostClient* client = agent_host->client_;
182 agent_host->client_ = NULL;
183 client->AgentHostClosed(agent_host, true);
184 agent_host->Detach();
189 // static
190 void DevToolsAgentHost::AddAgentStateCallback(
191 const AgentStateCallback& callback) {
192 g_callbacks.Get().push_back(&callback);
195 // static
196 void DevToolsAgentHost::RemoveAgentStateCallback(
197 const AgentStateCallback& callback) {
198 if (g_callbacks == NULL)
199 return;
201 AgentStateCallbacks* callbacks_ = g_callbacks.Pointer();
202 AgentStateCallbacks::iterator it =
203 std::find(callbacks_->begin(), callbacks_->end(), &callback);
204 DCHECK(it != callbacks_->end());
205 callbacks_->erase(it);
208 // static
209 void DevToolsAgentHostImpl::NotifyCallbacks(
210 DevToolsAgentHostImpl* agent_host, bool attached) {
211 AgentStateCallbacks copy(g_callbacks.Get());
212 DevToolsManager* manager = DevToolsManager::GetInstance();
213 manager->AgentHostStateChanged(agent_host, attached);
214 if (manager->delegate())
215 manager->delegate()->DevToolsAgentStateChanged(agent_host, attached);
216 for (AgentStateCallbacks::iterator it = copy.begin(); it != copy.end(); ++it)
217 (*it)->Run(agent_host, attached);
220 void DevToolsAgentHostImpl::Inspect(BrowserContext* browser_context) {
221 DevToolsManager* manager = DevToolsManager::GetInstance();
222 if (manager->delegate())
223 manager->delegate()->Inspect(browser_context, this);
226 // DevToolsMessageChunkProcessor -----------------------------------------------
228 DevToolsMessageChunkProcessor::DevToolsMessageChunkProcessor(
229 const SendMessageCallback& callback)
230 : callback_(callback),
231 message_buffer_size_(0),
232 last_call_id_(0) {
235 DevToolsMessageChunkProcessor::~DevToolsMessageChunkProcessor() {
238 void DevToolsMessageChunkProcessor::ProcessChunkedMessageFromAgent(
239 const DevToolsMessageChunk& chunk) {
240 if (chunk.is_last && !chunk.post_state.empty())
241 state_cookie_ = chunk.post_state;
242 if (chunk.is_last)
243 last_call_id_ = chunk.call_id;
245 if (chunk.is_first && chunk.is_last) {
246 CHECK(message_buffer_size_ == 0);
247 callback_.Run(chunk.data);
248 return;
251 if (chunk.is_first) {
252 message_buffer_ = std::string();
253 message_buffer_.reserve(chunk.message_size);
254 message_buffer_size_ = chunk.message_size;
257 CHECK(message_buffer_.size() + chunk.data.size() <=
258 message_buffer_size_);
259 message_buffer_.append(chunk.data);
261 if (chunk.is_last) {
262 CHECK(message_buffer_.size() == message_buffer_size_);
263 callback_.Run(message_buffer_);
264 message_buffer_ = std::string();
265 message_buffer_size_ = 0;
269 } // namespace content