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

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: rebased 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(Client* client) {
60 if (client_) {
61 client_->AgentHostDetached(this, REPLACED_WITH_ANOTHER_CLIENT);
62 NotifyCallbacks(this, false);
63 Detach();
64 } else {
65 // While client is attached, we should not die.
66 AddRef(); // Balanced in DetachInternal.
pfeldman 2014/08/07 15:40:30 Why is this necessary? Clients will hold agents to
dgozman 2014/08/07 16:51:52 Done.
67 DevToolsManagerImpl::GetInstance()->OnClientAttached();
68 }
69 client_ = client;
70 NotifyCallbacks(this, true);
71 Attach();
72 }
73
74 void DevToolsAgentHostImpl::DetachClient() {
75 DetachInternal(DETACHED_BY_CLIENT, true);
76 }
77
52 bool DevToolsAgentHostImpl::IsAttached() { 78 bool DevToolsAgentHostImpl::IsAttached() {
53 return !!DevToolsManagerImpl::GetInstance()->GetDevToolsClientHostFor(this); 79 return !!client_;
54 } 80 }
55 81
56 void DevToolsAgentHostImpl::InspectElement(int x, int y) { 82 void DevToolsAgentHostImpl::InspectElement(int x, int y) {
57 } 83 }
58 84
59 std::string DevToolsAgentHostImpl::GetId() { 85 std::string DevToolsAgentHostImpl::GetId() {
60 return id_; 86 return id_;
61 } 87 }
62 88
63 RenderViewHost* DevToolsAgentHostImpl::GetRenderViewHost() { 89 RenderViewHost* DevToolsAgentHostImpl::GetRenderViewHost() {
64 return NULL; 90 return NULL;
65 } 91 }
66 92
67 void DevToolsAgentHostImpl::DisconnectRenderViewHost() {} 93 void DevToolsAgentHostImpl::DisconnectRenderViewHost() {}
68 94
69 void DevToolsAgentHostImpl::ConnectRenderViewHost(RenderViewHost* rvh) {} 95 void DevToolsAgentHostImpl::ConnectRenderViewHost(RenderViewHost* rvh) {}
70 96
71 bool DevToolsAgentHostImpl::IsWorker() const { 97 bool DevToolsAgentHostImpl::IsWorker() const {
72 return false; 98 return false;
73 } 99 }
74 100
75 void DevToolsAgentHostImpl::NotifyCloseListener() { 101 void DevToolsAgentHostImpl::HostClosed() {
76 if (close_listener_) { 102 // Don't call Detach, since host is already gone - nothing to detach from.
77 scoped_refptr<DevToolsAgentHostImpl> protect(this); 103 DetachInternal(HOST_CLOSED, false);
78 close_listener_->AgentHostClosing(this); 104 }
79 close_listener_ = NULL; 105
106 void DevToolsAgentHostImpl::SendMessageToClient(const std::string& message) {
107 if (!client_)
108 return;
109 client_->SendMessageFromAgentHost(this, message);
110 }
111
112 void DevToolsAgentHostImpl::DetachInternal(
113 DetachReason reason, bool need_detach) {
pfeldman 2014/08/07 15:40:30 This seems error-prone. The reason should be enoug
dgozman 2014/08/07 16:51:52 Done.
114 if (!client_)
115 return;
116
117 client_->AgentHostDetached(this, reason);
118 client_ = NULL;
119 NotifyCallbacks(this, false);
120 if (need_detach)
121 Detach();
122 Release(); // Balanced in AttachClient.
123 DevToolsManagerImpl::GetInstance()->OnClientDetached();
124 }
125
126 // static
127 void DevToolsAgentHost::DetachAllClients() {
128 if (g_instances == NULL)
129 return;
130
131 // Make a copy, since detaching may lead to agent destruction, which
132 // removes it from the instances.
133 Instances copy = g_instances.Get();
134 for (Instances::iterator it(copy.begin()); it != copy.end(); ++it) {
135 it->second->DetachInternal(HOST_CLOSED, true);
80 } 136 }
81 } 137 }
82 138
139 // static
140 void DevToolsAgentHost::AddAgentStateCallback(
141 const AgentStateCallback& callback) {
142 g_callbacks.Get().push_back(&callback);
143 }
144
145 // static
146 void DevToolsAgentHost::RemoveAgentStateCallback(
147 const AgentStateCallback& callback) {
148 if (g_callbacks == NULL)
149 return;
150
151 AgentStateCallbacks* callbacks_ = g_callbacks.Pointer();
152 AgentStateCallbacks::iterator it =
153 std::find(callbacks_->begin(), callbacks_->end(), &callback);
154 DCHECK(it != callbacks_->end());
155 callbacks_->erase(it);
156 }
157
158 // static
159 void DevToolsAgentHostImpl::NotifyCallbacks(
160 DevToolsAgentHostImpl* agent_host, bool attached) {
161 AgentStateCallbacks copy = g_callbacks.Get();
162 DevToolsManagerImpl* manager = DevToolsManagerImpl::GetInstance();
163 if (manager->delegate())
164 manager->delegate()->DevToolsAgentStateChanged(agent_host, attached);
165 for (AgentStateCallbacks::iterator it = copy.begin(); it != copy.end(); ++it)
166 (*it)->Run(agent_host, attached);
167 }
168
169 void DevToolsAgentHostImpl::Inspect(BrowserContext* browser_context) {
170 DevToolsManagerImpl* manager = DevToolsManagerImpl::GetInstance();
171 if (manager->delegate())
172 manager->delegate()->Inspect(browser_context, this);
173 }
174
83 } // namespace content 175 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698