OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/devtools/devtools_agent_host_impl.h" | 5 #include "content/browser/devtools/devtools_agent_host_impl.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/guid.h" | 11 #include "base/guid.h" |
12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
13 #include "content/browser/devtools/devtools_manager_impl.h" | 13 #include "content/browser/devtools/devtools_manager.h" |
14 #include "content/browser/devtools/embedded_worker_devtools_manager.h" | 14 #include "content/browser/devtools/embedded_worker_devtools_manager.h" |
15 #include "content/browser/devtools/forwarding_agent_host.h" | 15 #include "content/browser/devtools/forwarding_agent_host.h" |
16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/devtools_manager_delegate.h" | 17 #include "content/public/browser/devtools_manager_delegate.h" |
18 | 18 |
19 namespace content { | 19 namespace content { |
20 | 20 |
21 namespace { | 21 namespace { |
22 typedef std::map<std::string, DevToolsAgentHostImpl*> Instances; | 22 typedef std::map<std::string, DevToolsAgentHostImpl*> Instances; |
23 base::LazyInstance<Instances>::Leaky g_instances = LAZY_INSTANCE_INITIALIZER; | 23 base::LazyInstance<Instances>::Leaky g_instances = LAZY_INSTANCE_INITIALIZER; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 DevToolsExternalAgentProxyDelegate* delegate) { | 69 DevToolsExternalAgentProxyDelegate* delegate) { |
70 return new ForwardingAgentHost(delegate); | 70 return new ForwardingAgentHost(delegate); |
71 } | 71 } |
72 | 72 |
73 void DevToolsAgentHostImpl::AttachClient(DevToolsAgentHostClient* client) { | 73 void DevToolsAgentHostImpl::AttachClient(DevToolsAgentHostClient* client) { |
74 scoped_refptr<DevToolsAgentHostImpl> protect(this); | 74 scoped_refptr<DevToolsAgentHostImpl> protect(this); |
75 if (client_) { | 75 if (client_) { |
76 client_->AgentHostClosed(this, true); | 76 client_->AgentHostClosed(this, true); |
77 Detach(); | 77 Detach(); |
78 } else { | 78 } else { |
79 DevToolsManagerImpl::GetInstance()->OnClientAttached(); | 79 DevToolsManager::GetInstance()->OnClientAttached(); |
80 } | 80 } |
81 client_ = client; | 81 client_ = client; |
82 Attach(); | 82 Attach(); |
83 } | 83 } |
84 | 84 |
85 void DevToolsAgentHostImpl::DetachClient() { | 85 void DevToolsAgentHostImpl::DetachClient() { |
86 if (!client_) | 86 if (!client_) |
87 return; | 87 return; |
88 | 88 |
89 scoped_refptr<DevToolsAgentHostImpl> protect(this); | 89 scoped_refptr<DevToolsAgentHostImpl> protect(this); |
90 client_ = NULL; | 90 client_ = NULL; |
91 DevToolsManagerImpl::GetInstance()->OnClientDetached(); | 91 DevToolsManager::GetInstance()->OnClientDetached(); |
92 Detach(); | 92 Detach(); |
93 } | 93 } |
94 | 94 |
95 bool DevToolsAgentHostImpl::IsAttached() { | 95 bool DevToolsAgentHostImpl::IsAttached() { |
96 return !!client_; | 96 return !!client_; |
97 } | 97 } |
98 | 98 |
99 void DevToolsAgentHostImpl::InspectElement(int x, int y) { | 99 void DevToolsAgentHostImpl::InspectElement(int x, int y) { |
100 } | 100 } |
101 | 101 |
(...skipping 16 matching lines...) Expand all Loading... |
118 } | 118 } |
119 | 119 |
120 void DevToolsAgentHostImpl::HostClosed() { | 120 void DevToolsAgentHostImpl::HostClosed() { |
121 if (!client_) | 121 if (!client_) |
122 return; | 122 return; |
123 | 123 |
124 scoped_refptr<DevToolsAgentHostImpl> protect(this); | 124 scoped_refptr<DevToolsAgentHostImpl> protect(this); |
125 // Clear |client_| before notifying it. | 125 // Clear |client_| before notifying it. |
126 DevToolsAgentHostClient* client = client_; | 126 DevToolsAgentHostClient* client = client_; |
127 client_ = NULL; | 127 client_ = NULL; |
128 DevToolsManagerImpl::GetInstance()->OnClientDetached(); | 128 DevToolsManager::GetInstance()->OnClientDetached(); |
129 client->AgentHostClosed(this, false); | 129 client->AgentHostClosed(this, false); |
130 } | 130 } |
131 | 131 |
132 void DevToolsAgentHostImpl::SendMessageToClient(const std::string& message) { | 132 void DevToolsAgentHostImpl::SendMessageToClient(const std::string& message) { |
133 if (!client_) | 133 if (!client_) |
134 return; | 134 return; |
135 client_->DispatchProtocolMessage(this, message); | 135 client_->DispatchProtocolMessage(this, message); |
136 } | 136 } |
137 | 137 |
138 // static | 138 // static |
139 void DevToolsAgentHost::DetachAllClients() { | 139 void DevToolsAgentHost::DetachAllClients() { |
140 if (g_instances == NULL) | 140 if (g_instances == NULL) |
141 return; | 141 return; |
142 | 142 |
143 // Make a copy, since detaching may lead to agent destruction, which | 143 // Make a copy, since detaching may lead to agent destruction, which |
144 // removes it from the instances. | 144 // removes it from the instances. |
145 Instances copy = g_instances.Get(); | 145 Instances copy = g_instances.Get(); |
146 for (Instances::iterator it(copy.begin()); it != copy.end(); ++it) { | 146 for (Instances::iterator it(copy.begin()); it != copy.end(); ++it) { |
147 DevToolsAgentHostImpl* agent_host = it->second; | 147 DevToolsAgentHostImpl* agent_host = it->second; |
148 if (agent_host->client_) { | 148 if (agent_host->client_) { |
149 scoped_refptr<DevToolsAgentHostImpl> protect(agent_host); | 149 scoped_refptr<DevToolsAgentHostImpl> protect(agent_host); |
150 // Clear |client_| before notifying it. | 150 // Clear |client_| before notifying it. |
151 DevToolsAgentHostClient* client = agent_host->client_; | 151 DevToolsAgentHostClient* client = agent_host->client_; |
152 agent_host->client_ = NULL; | 152 agent_host->client_ = NULL; |
153 DevToolsManagerImpl::GetInstance()->OnClientDetached(); | 153 DevToolsManager::GetInstance()->OnClientDetached(); |
154 client->AgentHostClosed(agent_host, true); | 154 client->AgentHostClosed(agent_host, true); |
155 agent_host->Detach(); | 155 agent_host->Detach(); |
156 } | 156 } |
157 } | 157 } |
158 } | 158 } |
159 | 159 |
160 // static | 160 // static |
161 void DevToolsAgentHost::AddAgentStateCallback( | 161 void DevToolsAgentHost::AddAgentStateCallback( |
162 const AgentStateCallback& callback) { | 162 const AgentStateCallback& callback) { |
163 g_callbacks.Get().push_back(&callback); | 163 g_callbacks.Get().push_back(&callback); |
164 } | 164 } |
165 | 165 |
166 // static | 166 // static |
167 void DevToolsAgentHost::RemoveAgentStateCallback( | 167 void DevToolsAgentHost::RemoveAgentStateCallback( |
168 const AgentStateCallback& callback) { | 168 const AgentStateCallback& callback) { |
169 if (g_callbacks == NULL) | 169 if (g_callbacks == NULL) |
170 return; | 170 return; |
171 | 171 |
172 AgentStateCallbacks* callbacks_ = g_callbacks.Pointer(); | 172 AgentStateCallbacks* callbacks_ = g_callbacks.Pointer(); |
173 AgentStateCallbacks::iterator it = | 173 AgentStateCallbacks::iterator it = |
174 std::find(callbacks_->begin(), callbacks_->end(), &callback); | 174 std::find(callbacks_->begin(), callbacks_->end(), &callback); |
175 DCHECK(it != callbacks_->end()); | 175 DCHECK(it != callbacks_->end()); |
176 callbacks_->erase(it); | 176 callbacks_->erase(it); |
177 } | 177 } |
178 | 178 |
179 // static | 179 // static |
180 void DevToolsAgentHostImpl::NotifyCallbacks( | 180 void DevToolsAgentHostImpl::NotifyCallbacks( |
181 DevToolsAgentHostImpl* agent_host, bool attached) { | 181 DevToolsAgentHostImpl* agent_host, bool attached) { |
182 AgentStateCallbacks copy(g_callbacks.Get()); | 182 AgentStateCallbacks copy(g_callbacks.Get()); |
183 DevToolsManagerImpl* manager = DevToolsManagerImpl::GetInstance(); | 183 DevToolsManager* manager = DevToolsManager::GetInstance(); |
184 if (manager->delegate()) | 184 if (manager->delegate()) |
185 manager->delegate()->DevToolsAgentStateChanged(agent_host, attached); | 185 manager->delegate()->DevToolsAgentStateChanged(agent_host, attached); |
186 for (AgentStateCallbacks::iterator it = copy.begin(); it != copy.end(); ++it) | 186 for (AgentStateCallbacks::iterator it = copy.begin(); it != copy.end(); ++it) |
187 (*it)->Run(agent_host, attached); | 187 (*it)->Run(agent_host, attached); |
188 } | 188 } |
189 | 189 |
190 void DevToolsAgentHostImpl::Inspect(BrowserContext* browser_context) { | 190 void DevToolsAgentHostImpl::Inspect(BrowserContext* browser_context) { |
191 DevToolsManagerImpl* manager = DevToolsManagerImpl::GetInstance(); | 191 DevToolsManager* manager = DevToolsManager::GetInstance(); |
192 if (manager->delegate()) | 192 if (manager->delegate()) |
193 manager->delegate()->Inspect(browser_context, this); | 193 manager->delegate()->Inspect(browser_context, this); |
194 } | 194 } |
195 | 195 |
196 } // namespace content | 196 } // namespace content |
OLD | NEW |