summaryrefslogtreecommitdiffstats
path: root/content/browser/devtools/devtools_agent_host_impl.cc
diff options
context:
space:
mode:
Diffstat (limited to 'content/browser/devtools/devtools_agent_host_impl.cc')
-rw-r--r--content/browser/devtools/devtools_agent_host_impl.cc109
1 files changed, 101 insertions, 8 deletions
diff --git a/content/browser/devtools/devtools_agent_host_impl.cc b/content/browser/devtools/devtools_agent_host_impl.cc
index 9d4e707..1309eac 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(DevToolsAgentHostClient* client) {
+ scoped_refptr<DevToolsAgentHostImpl> protect(this);
+ if (client_) {
+ client_->AgentHostClosed(this, true);
+ Detach();
+ } else {
+ DevToolsManagerImpl::GetInstance()->OnClientAttached();
+ }
+ client_ = client;
+ Attach();
+}
+
+void DevToolsAgentHostImpl::DetachClient() {
+ if (!client_)
+ return;
+
+ scoped_refptr<DevToolsAgentHostImpl> protect(this);
+ client_ = NULL;
+ Detach();
+ DevToolsManagerImpl::GetInstance()->OnClientDetached();
+}
+
bool DevToolsAgentHostImpl::IsAttached() {
- return !!DevToolsManagerImpl::GetInstance()->GetDevToolsClientHostFor(this);
+ return !!client_;
}
void DevToolsAgentHostImpl::InspectElement(int x, int y) {
@@ -74,12 +103,76 @@ 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;
+ DevToolsManagerImpl::GetInstance()->OnClientDetached();
+}
+
+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();
+ DevToolsManagerImpl::GetInstance()->OnClientDetached();
+ }
}
}
+// 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) {
+ 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