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 | 9 |
9 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
10 #include "base/guid.h" | 11 #include "base/guid.h" |
11 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
12 #include "content/browser/devtools/devtools_manager_impl.h" | 13 #include "content/browser/devtools/devtools_manager_impl.h" |
13 #include "content/browser/devtools/forwarding_agent_host.h" | 14 #include "content/browser/devtools/forwarding_agent_host.h" |
14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/devtools_manager_delegate.h" |
15 | 17 |
16 namespace content { | 18 namespace content { |
17 | 19 |
18 namespace { | 20 namespace { |
19 typedef std::map<std::string, DevToolsAgentHostImpl*> Instances; | 21 typedef std::map<std::string, DevToolsAgentHostImpl*> Instances; |
20 base::LazyInstance<Instances>::Leaky g_instances = LAZY_INSTANCE_INITIALIZER; | 22 base::LazyInstance<Instances>::Leaky g_instances = LAZY_INSTANCE_INITIALIZER; |
| 23 |
| 24 typedef std::vector<const DevToolsAgentHost::AgentStateCallback*> |
| 25 AgentStateCallbacks; |
| 26 base::LazyInstance<AgentStateCallbacks>::Leaky g_callbacks = |
| 27 LAZY_INSTANCE_INITIALIZER; |
21 } // namespace | 28 } // namespace |
22 | 29 |
23 DevToolsAgentHostImpl::DevToolsAgentHostImpl() | 30 DevToolsAgentHostImpl::DevToolsAgentHostImpl() |
24 : close_listener_(NULL), | 31 : id_(base::GenerateGUID()), |
25 id_(base::GenerateGUID()) { | 32 client_(NULL) { |
26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
27 g_instances.Get()[id_] = this; | 34 g_instances.Get()[id_] = this; |
28 } | 35 } |
29 | 36 |
30 DevToolsAgentHostImpl::~DevToolsAgentHostImpl() { | 37 DevToolsAgentHostImpl::~DevToolsAgentHostImpl() { |
31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
32 g_instances.Get().erase(g_instances.Get().find(id_)); | 39 g_instances.Get().erase(g_instances.Get().find(id_)); |
33 } | 40 } |
34 | 41 |
35 //static | 42 //static |
36 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForId( | 43 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForId( |
37 const std::string& id) { | 44 const std::string& id) { |
38 if (g_instances == NULL) | 45 if (g_instances == NULL) |
39 return NULL; | 46 return NULL; |
40 Instances::iterator it = g_instances.Get().find(id); | 47 Instances::iterator it = g_instances.Get().find(id); |
41 if (it == g_instances.Get().end()) | 48 if (it == g_instances.Get().end()) |
42 return NULL; | 49 return NULL; |
43 return it->second; | 50 return it->second; |
44 } | 51 } |
45 | 52 |
46 //static | 53 //static |
47 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::Create( | 54 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::Create( |
48 DevToolsExternalAgentProxyDelegate* delegate) { | 55 DevToolsExternalAgentProxyDelegate* delegate) { |
49 return new ForwardingAgentHost(delegate); | 56 return new ForwardingAgentHost(delegate); |
50 } | 57 } |
51 | 58 |
| 59 void DevToolsAgentHostImpl::AttachClient(Client* client) { |
| 60 scoped_refptr<DevToolsAgentHostImpl> protect(this); |
| 61 if (client_) { |
| 62 client_->AgentHostClosed(this, true); |
| 63 Detach(); |
| 64 NotifyCallbacks(this, false); |
| 65 } |
| 66 client_ = client; |
| 67 NotifyCallbacks(this, true); |
| 68 Attach(); |
| 69 } |
| 70 |
| 71 void DevToolsAgentHostImpl::DetachClient() { |
| 72 if (!client_) |
| 73 return; |
| 74 |
| 75 scoped_refptr<DevToolsAgentHostImpl> protect(this); |
| 76 client_ = NULL; |
| 77 Detach(); |
| 78 NotifyCallbacks(this, false); |
| 79 } |
| 80 |
52 bool DevToolsAgentHostImpl::IsAttached() { | 81 bool DevToolsAgentHostImpl::IsAttached() { |
53 return !!DevToolsManagerImpl::GetInstance()->GetDevToolsClientHostFor(this); | 82 return !!client_; |
54 } | 83 } |
55 | 84 |
56 void DevToolsAgentHostImpl::InspectElement(int x, int y) { | 85 void DevToolsAgentHostImpl::InspectElement(int x, int y) { |
57 } | 86 } |
58 | 87 |
59 std::string DevToolsAgentHostImpl::GetId() { | 88 std::string DevToolsAgentHostImpl::GetId() { |
60 return id_; | 89 return id_; |
61 } | 90 } |
62 | 91 |
63 RenderViewHost* DevToolsAgentHostImpl::GetRenderViewHost() { | 92 RenderViewHost* DevToolsAgentHostImpl::GetRenderViewHost() { |
64 return NULL; | 93 return NULL; |
65 } | 94 } |
66 | 95 |
67 void DevToolsAgentHostImpl::DisconnectRenderViewHost() {} | 96 void DevToolsAgentHostImpl::DisconnectRenderViewHost() {} |
68 | 97 |
69 void DevToolsAgentHostImpl::ConnectRenderViewHost(RenderViewHost* rvh) {} | 98 void DevToolsAgentHostImpl::ConnectRenderViewHost(RenderViewHost* rvh) {} |
70 | 99 |
71 bool DevToolsAgentHostImpl::IsWorker() const { | 100 bool DevToolsAgentHostImpl::IsWorker() const { |
72 return false; | 101 return false; |
73 } | 102 } |
74 | 103 |
75 void DevToolsAgentHostImpl::NotifyCloseListener() { | 104 void DevToolsAgentHostImpl::HostClosed() { |
76 if (close_listener_) { | 105 if (!client_) |
77 scoped_refptr<DevToolsAgentHostImpl> protect(this); | 106 return; |
78 close_listener_->AgentHostClosing(this); | 107 |
79 close_listener_ = NULL; | 108 scoped_refptr<DevToolsAgentHostImpl> protect(this); |
| 109 client_->AgentHostClosed(this, false); |
| 110 client_ = NULL; |
| 111 NotifyCallbacks(this, false); |
| 112 } |
| 113 |
| 114 void DevToolsAgentHostImpl::SendMessageToClient(const std::string& message) { |
| 115 if (!client_) |
| 116 return; |
| 117 client_->DispatchProtocolMessage(this, message); |
| 118 } |
| 119 |
| 120 // static |
| 121 void DevToolsAgentHost::DetachAllClients() { |
| 122 if (g_instances == NULL) |
| 123 return; |
| 124 |
| 125 // Make a copy, since detaching may lead to agent destruction, which |
| 126 // removes it from the instances. |
| 127 Instances copy = g_instances.Get(); |
| 128 for (Instances::iterator it(copy.begin()); it != copy.end(); ++it) { |
| 129 DevToolsAgentHostImpl* agent_host = it->second; |
| 130 if (agent_host->client_) { |
| 131 scoped_refptr<DevToolsAgentHostImpl> protect(agent_host); |
| 132 agent_host->client_->AgentHostClosed(agent_host, true); |
| 133 agent_host->client_ = NULL; |
| 134 agent_host->Detach(); |
| 135 DevToolsAgentHostImpl::NotifyCallbacks(agent_host, false); |
| 136 } |
80 } | 137 } |
81 } | 138 } |
82 | 139 |
| 140 // static |
| 141 void DevToolsAgentHost::AddAgentStateCallback( |
| 142 const AgentStateCallback& callback) { |
| 143 g_callbacks.Get().push_back(&callback); |
| 144 } |
| 145 |
| 146 // static |
| 147 void DevToolsAgentHost::RemoveAgentStateCallback( |
| 148 const AgentStateCallback& callback) { |
| 149 if (g_callbacks == NULL) |
| 150 return; |
| 151 |
| 152 AgentStateCallbacks* callbacks_ = g_callbacks.Pointer(); |
| 153 AgentStateCallbacks::iterator it = |
| 154 std::find(callbacks_->begin(), callbacks_->end(), &callback); |
| 155 DCHECK(it != callbacks_->end()); |
| 156 callbacks_->erase(it); |
| 157 } |
| 158 |
| 159 // static |
| 160 void DevToolsAgentHostImpl::NotifyCallbacks( |
| 161 DevToolsAgentHostImpl* agent_host, bool attached) { |
| 162 if (attached) |
| 163 DevToolsManagerImpl::GetInstance()->OnClientAttached(); |
| 164 else |
| 165 DevToolsManagerImpl::GetInstance()->OnClientDetached(); |
| 166 |
| 167 AgentStateCallbacks copy = g_callbacks.Get(); |
| 168 DevToolsManagerImpl* manager = DevToolsManagerImpl::GetInstance(); |
| 169 if (manager->delegate()) |
| 170 manager->delegate()->DevToolsAgentStateChanged(agent_host, attached); |
| 171 for (AgentStateCallbacks::iterator it = copy.begin(); it != copy.end(); ++it) |
| 172 (*it)->Run(agent_host, attached); |
| 173 } |
| 174 |
| 175 void DevToolsAgentHostImpl::Inspect(BrowserContext* browser_context) { |
| 176 DevToolsManagerImpl* manager = DevToolsManagerImpl::GetInstance(); |
| 177 if (manager->delegate()) |
| 178 manager->delegate()->Inspect(browser_context, this); |
| 179 } |
| 180 |
83 } // namespace content | 181 } // namespace content |
OLD | NEW |