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