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