Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(559)

Side by Side Diff: content/browser/devtools/devtools_agent_host_impl.cc

Issue 449043002: [DevTools] Make DevTools clients talk directly to DevToolsAgentHost instead of using DevToolsManage… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixed DetachAllClients Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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(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 DevToolsManagerImpl::GetInstance()->OnClientDetached();
78 Detach();
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 WebContents* DevToolsAgentHostImpl::GetWebContents() { 92 WebContents* DevToolsAgentHostImpl::GetWebContents() {
64 return NULL; 93 return NULL;
65 } 94 }
66 95
67 void DevToolsAgentHostImpl::DisconnectWebContents() { 96 void DevToolsAgentHostImpl::DisconnectWebContents() {
68 } 97 }
69 98
70 void DevToolsAgentHostImpl::ConnectWebContents(WebContents* wc) { 99 void DevToolsAgentHostImpl::ConnectWebContents(WebContents* wc) {
71 } 100 }
72 101
73 bool DevToolsAgentHostImpl::IsWorker() const { 102 bool DevToolsAgentHostImpl::IsWorker() const {
74 return false; 103 return false;
75 } 104 }
76 105
77 void DevToolsAgentHostImpl::NotifyCloseListener() { 106 void DevToolsAgentHostImpl::HostClosed() {
78 if (close_listener_) { 107 if (!client_)
79 scoped_refptr<DevToolsAgentHostImpl> protect(this); 108 return;
80 close_listener_->AgentHostClosing(this); 109
81 close_listener_ = NULL; 110 scoped_refptr<DevToolsAgentHostImpl> protect(this);
111 // Clear |client_| before notifying it.
112 DevToolsAgentHostClient* client = client_;
113 client_ = NULL;
114 DevToolsManagerImpl::GetInstance()->OnClientDetached();
115 client->AgentHostClosed(this, false);
116 }
117
118 void DevToolsAgentHostImpl::SendMessageToClient(const std::string& message) {
119 if (!client_)
120 return;
121 client_->DispatchProtocolMessage(this, message);
122 }
123
124 // static
125 void DevToolsAgentHost::DetachAllClients() {
126 if (g_instances == NULL)
127 return;
128
129 // Make a copy, since detaching may lead to agent destruction, which
130 // removes it from the instances.
131 Instances copy = g_instances.Get();
132 for (Instances::iterator it(copy.begin()); it != copy.end(); ++it) {
133 DevToolsAgentHostImpl* agent_host = it->second;
134 if (agent_host->client_) {
135 scoped_refptr<DevToolsAgentHostImpl> protect(agent_host);
136 // Clear |client_| before notifying it.
137 DevToolsAgentHostClient* client = agent_host->client_;
138 agent_host->client_ = NULL;
139 DevToolsManagerImpl::GetInstance()->OnClientDetached();
140 client->AgentHostClosed(agent_host, true);
141 agent_host->Detach();
142 }
82 } 143 }
83 } 144 }
84 145
146 // static
147 void DevToolsAgentHost::AddAgentStateCallback(
148 const AgentStateCallback& callback) {
149 g_callbacks.Get().push_back(&callback);
150 }
151
152 // static
153 void DevToolsAgentHost::RemoveAgentStateCallback(
154 const AgentStateCallback& callback) {
155 if (g_callbacks == NULL)
156 return;
157
158 AgentStateCallbacks* callbacks_ = g_callbacks.Pointer();
159 AgentStateCallbacks::iterator it =
160 std::find(callbacks_->begin(), callbacks_->end(), &callback);
161 DCHECK(it != callbacks_->end());
162 callbacks_->erase(it);
163 }
164
165 // static
166 void DevToolsAgentHostImpl::NotifyCallbacks(
167 DevToolsAgentHostImpl* agent_host, bool attached) {
168 AgentStateCallbacks copy(g_callbacks.Get());
169 DevToolsManagerImpl* manager = DevToolsManagerImpl::GetInstance();
170 if (manager->delegate())
171 manager->delegate()->DevToolsAgentStateChanged(agent_host, attached);
172 for (AgentStateCallbacks::iterator it = copy.begin(); it != copy.end(); ++it)
173 (*it)->Run(agent_host, attached);
174 }
175
176 void DevToolsAgentHostImpl::Inspect(BrowserContext* browser_context) {
177 DevToolsManagerImpl* manager = DevToolsManagerImpl::GetInstance();
178 if (manager->delegate())
179 manager->delegate()->Inspect(browser_context, this);
180 }
181
85 } // namespace content 182 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/devtools/devtools_agent_host_impl.h ('k') | content/browser/devtools/devtools_http_handler_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698