| Index: content/browser/devtools/devtools_agent_host_impl.cc
|
| diff --git a/content/browser/devtools/devtools_agent_host_impl.cc b/content/browser/devtools/devtools_agent_host_impl.cc
|
| index bcb60b55b7832b0348550bead85a164ffffc1539..780e1a8012f1b2065a0817f0bb59aaa9bfb873c2 100644
|
| --- a/content/browser/devtools/devtools_agent_host_impl.cc
|
| +++ b/content/browser/devtools/devtools_agent_host_impl.cc
|
| @@ -5,6 +5,7 @@
|
| #include "content/browser/devtools/devtools_agent_host_impl.h"
|
|
|
| #include <map>
|
| +#include <vector>
|
|
|
| #include "base/basictypes.h"
|
| #include "base/guid.h"
|
| @@ -12,17 +13,23 @@
|
| #include "content/browser/devtools/devtools_manager_impl.h"
|
| #include "content/browser/devtools/forwarding_agent_host.h"
|
| #include "content/public/browser/browser_thread.h"
|
| +#include "content/public/browser/devtools_manager_delegate.h"
|
|
|
| namespace content {
|
|
|
| namespace {
|
| typedef std::map<std::string, DevToolsAgentHostImpl*> Instances;
|
| base::LazyInstance<Instances>::Leaky g_instances = LAZY_INSTANCE_INITIALIZER;
|
| +
|
| +typedef std::vector<const DevToolsAgentHost::AgentStateCallback*>
|
| + AgentStateCallbacks;
|
| +base::LazyInstance<AgentStateCallbacks>::Leaky g_callbacks =
|
| + LAZY_INSTANCE_INITIALIZER;
|
| } // namespace
|
|
|
| DevToolsAgentHostImpl::DevToolsAgentHostImpl()
|
| - : close_listener_(NULL),
|
| - id_(base::GenerateGUID()) {
|
| + : id_(base::GenerateGUID()),
|
| + client_(NULL) {
|
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
|
| g_instances.Get()[id_] = this;
|
| }
|
| @@ -49,8 +56,30 @@ scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::Create(
|
| return new ForwardingAgentHost(delegate);
|
| }
|
|
|
| +void DevToolsAgentHostImpl::AttachClient(Client* client) {
|
| + scoped_refptr<DevToolsAgentHostImpl> protect(this);
|
| + if (client_) {
|
| + client_->AgentHostClosed(this, true);
|
| + Detach();
|
| + NotifyCallbacks(this, false);
|
| + }
|
| + client_ = client;
|
| + NotifyCallbacks(this, true);
|
| + Attach();
|
| +}
|
| +
|
| +void DevToolsAgentHostImpl::DetachClient() {
|
| + if (!client_)
|
| + return;
|
| +
|
| + scoped_refptr<DevToolsAgentHostImpl> protect(this);
|
| + client_ = NULL;
|
| + Detach();
|
| + NotifyCallbacks(this, false);
|
| +}
|
| +
|
| bool DevToolsAgentHostImpl::IsAttached() {
|
| - return !!DevToolsManagerImpl::GetInstance()->GetDevToolsClientHostFor(this);
|
| + return !!client_;
|
| }
|
|
|
| void DevToolsAgentHostImpl::InspectElement(int x, int y) {
|
| @@ -72,12 +101,81 @@ bool DevToolsAgentHostImpl::IsWorker() const {
|
| return false;
|
| }
|
|
|
| -void DevToolsAgentHostImpl::NotifyCloseListener() {
|
| - if (close_listener_) {
|
| - scoped_refptr<DevToolsAgentHostImpl> protect(this);
|
| - close_listener_->AgentHostClosing(this);
|
| - close_listener_ = NULL;
|
| +void DevToolsAgentHostImpl::HostClosed() {
|
| + if (!client_)
|
| + return;
|
| +
|
| + scoped_refptr<DevToolsAgentHostImpl> protect(this);
|
| + client_->AgentHostClosed(this, false);
|
| + client_ = NULL;
|
| + NotifyCallbacks(this, false);
|
| +}
|
| +
|
| +void DevToolsAgentHostImpl::SendMessageToClient(const std::string& message) {
|
| + if (!client_)
|
| + return;
|
| + client_->DispatchProtocolMessage(this, message);
|
| +}
|
| +
|
| +// static
|
| +void DevToolsAgentHost::DetachAllClients() {
|
| + if (g_instances == NULL)
|
| + return;
|
| +
|
| + // Make a copy, since detaching may lead to agent destruction, which
|
| + // removes it from the instances.
|
| + Instances copy = g_instances.Get();
|
| + for (Instances::iterator it(copy.begin()); it != copy.end(); ++it) {
|
| + DevToolsAgentHostImpl* agent_host = it->second;
|
| + if (agent_host->client_) {
|
| + scoped_refptr<DevToolsAgentHostImpl> protect(agent_host);
|
| + agent_host->client_->AgentHostClosed(agent_host, true);
|
| + agent_host->client_ = NULL;
|
| + agent_host->Detach();
|
| + DevToolsAgentHostImpl::NotifyCallbacks(agent_host, false);
|
| + }
|
| }
|
| }
|
|
|
| +// static
|
| +void DevToolsAgentHost::AddAgentStateCallback(
|
| + const AgentStateCallback& callback) {
|
| + g_callbacks.Get().push_back(&callback);
|
| +}
|
| +
|
| +// static
|
| +void DevToolsAgentHost::RemoveAgentStateCallback(
|
| + const AgentStateCallback& callback) {
|
| + if (g_callbacks == NULL)
|
| + return;
|
| +
|
| + AgentStateCallbacks* callbacks_ = g_callbacks.Pointer();
|
| + AgentStateCallbacks::iterator it =
|
| + std::find(callbacks_->begin(), callbacks_->end(), &callback);
|
| + DCHECK(it != callbacks_->end());
|
| + callbacks_->erase(it);
|
| +}
|
| +
|
| +// static
|
| +void DevToolsAgentHostImpl::NotifyCallbacks(
|
| + DevToolsAgentHostImpl* agent_host, bool attached) {
|
| + if (attached)
|
| + DevToolsManagerImpl::GetInstance()->OnClientAttached();
|
| + else
|
| + DevToolsManagerImpl::GetInstance()->OnClientDetached();
|
| +
|
| + AgentStateCallbacks copy = g_callbacks.Get();
|
| + DevToolsManagerImpl* manager = DevToolsManagerImpl::GetInstance();
|
| + if (manager->delegate())
|
| + manager->delegate()->DevToolsAgentStateChanged(agent_host, attached);
|
| + for (AgentStateCallbacks::iterator it = copy.begin(); it != copy.end(); ++it)
|
| + (*it)->Run(agent_host, attached);
|
| +}
|
| +
|
| +void DevToolsAgentHostImpl::Inspect(BrowserContext* browser_context) {
|
| + DevToolsManagerImpl* manager = DevToolsManagerImpl::GetInstance();
|
| + if (manager->delegate())
|
| + manager->delegate()->Inspect(browser_context, this);
|
| +}
|
| +
|
| } // namespace content
|
|
|