[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / devtools / devtools_agent_host_impl.cc
blob4d935519c07e0fa2ee919e11ee9ec83bf5e751c8
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 InnerDetach();
113 client_ = client;
114 Attach();
117 void DevToolsAgentHostImpl::DetachClient() {
118 if (!client_)
119 return;
121 scoped_refptr<DevToolsAgentHostImpl> protect(this);
122 client_ = NULL;
123 InnerDetach();
126 void DevToolsAgentHostImpl::InnerDetach() {
127 Detach();
128 io_context_.DiscardAllStreams();
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->InnerDetach();
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 // DevToolsMessageChunkProcessor -----------------------------------------------
233 DevToolsMessageChunkProcessor::DevToolsMessageChunkProcessor(
234 const SendMessageCallback& callback)
235 : callback_(callback),
236 message_buffer_size_(0),
237 last_call_id_(0) {
240 DevToolsMessageChunkProcessor::~DevToolsMessageChunkProcessor() {
243 void DevToolsMessageChunkProcessor::ProcessChunkedMessageFromAgent(
244 const DevToolsMessageChunk& chunk) {
245 if (chunk.is_last && !chunk.post_state.empty())
246 state_cookie_ = chunk.post_state;
247 if (chunk.is_last)
248 last_call_id_ = chunk.call_id;
250 if (chunk.is_first && chunk.is_last) {
251 CHECK(message_buffer_size_ == 0);
252 callback_.Run(chunk.data);
253 return;
256 if (chunk.is_first) {
257 message_buffer_ = std::string();
258 message_buffer_.reserve(chunk.message_size);
259 message_buffer_size_ = chunk.message_size;
262 CHECK(message_buffer_.size() + chunk.data.size() <=
263 message_buffer_size_);
264 message_buffer_.append(chunk.data);
266 if (chunk.is_last) {
267 CHECK(message_buffer_.size() == message_buffer_size_);
268 callback_.Run(message_buffer_);
269 message_buffer_ = std::string();
270 message_buffer_size_ = 0;
274 } // namespace content