OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/embedded_worker_devtools_manager.h" | 5 #include "content/browser/devtools/service_worker_devtools_manager.h" |
6 | 6 |
7 #include "content/browser/devtools/devtools_manager.h" | 7 #include "content/browser/devtools/devtools_manager.h" |
8 #include "content/browser/devtools/ipc_devtools_agent_host.h" | 8 #include "content/browser/devtools/ipc_devtools_agent_host.h" |
9 #include "content/browser/devtools/service_worker_devtools_agent_host.h" | 9 #include "content/browser/devtools/service_worker_devtools_agent_host.h" |
10 #include "content/browser/devtools/shared_worker_devtools_agent_host.h" | 10 #include "content/browser/devtools/shared_worker_devtools_agent_host.h" |
11 #include "content/browser/shared_worker/shared_worker_instance.h" | 11 #include "content/browser/shared_worker/shared_worker_instance.h" |
12 #include "content/common/devtools_messages.h" | 12 #include "content/common/devtools_messages.h" |
13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/render_process_host.h" | 14 #include "content/public/browser/render_process_host.h" |
15 #include "content/public/browser/worker_service.h" | 15 #include "content/public/browser/worker_service.h" |
16 #include "ipc/ipc_listener.h" | 16 #include "ipc/ipc_listener.h" |
17 | 17 |
18 namespace content { | 18 namespace content { |
19 | 19 |
20 // Called on the UI thread. | 20 ServiceWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier( |
21 // static | |
22 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForWorker( | |
23 int worker_process_id, | |
24 int worker_route_id) { | |
25 return EmbeddedWorkerDevToolsManager::GetInstance() | |
26 ->GetDevToolsAgentHostForWorker(worker_process_id, worker_route_id); | |
27 } | |
28 | |
29 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier( | |
30 const ServiceWorkerContextCore* context, | 21 const ServiceWorkerContextCore* context, |
31 base::WeakPtr<ServiceWorkerContextCore> context_weak, | 22 base::WeakPtr<ServiceWorkerContextCore> context_weak, |
32 int64 version_id, | 23 int64 version_id, |
33 const GURL& url) | 24 const GURL& url) |
34 : context_(context), | 25 : context_(context), |
35 context_weak_(context_weak), | 26 context_weak_(context_weak), |
36 version_id_(version_id), | 27 version_id_(version_id), |
37 url_(url) { | 28 url_(url) { |
38 } | 29 } |
39 | 30 |
40 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier( | 31 ServiceWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier( |
41 const ServiceWorkerIdentifier& other) | 32 const ServiceWorkerIdentifier& other) |
42 : context_(other.context_), | 33 : context_(other.context_), |
43 context_weak_(other.context_weak_), | 34 context_weak_(other.context_weak_), |
44 version_id_(other.version_id_), | 35 version_id_(other.version_id_), |
45 url_(other.url_) { | 36 url_(other.url_) { |
46 } | 37 } |
47 | 38 |
48 EmbeddedWorkerDevToolsManager:: | 39 ServiceWorkerDevToolsManager:: |
49 ServiceWorkerIdentifier::~ServiceWorkerIdentifier() { | 40 ServiceWorkerIdentifier::~ServiceWorkerIdentifier() { |
50 } | 41 } |
51 | 42 |
52 bool EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::Matches( | 43 bool ServiceWorkerDevToolsManager::ServiceWorkerIdentifier::Matches( |
53 const ServiceWorkerIdentifier& other) const { | 44 const ServiceWorkerIdentifier& other) const { |
54 return context_ == other.context_ && version_id_ == other.version_id_; | 45 return context_ == other.context_ && version_id_ == other.version_id_; |
55 } | 46 } |
56 | 47 |
57 const ServiceWorkerContextCore* | 48 // static |
58 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::context() const { | 49 ServiceWorkerDevToolsManager* ServiceWorkerDevToolsManager::GetInstance() { |
59 return context_; | 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 51 return Singleton<ServiceWorkerDevToolsManager>::get(); |
60 } | 52 } |
61 | 53 |
62 base::WeakPtr<ServiceWorkerContextCore> | 54 bool ServiceWorkerDevToolsManager::WorkerCreated( |
63 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::context_weak() const { | |
64 return context_weak_; | |
65 } | |
66 | |
67 int64 | |
68 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::version_id() const { | |
69 return version_id_; | |
70 } | |
71 | |
72 GURL EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::url() const { | |
73 return url_; | |
74 } | |
75 | |
76 // static | |
77 EmbeddedWorkerDevToolsManager* EmbeddedWorkerDevToolsManager::GetInstance() { | |
78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
79 return Singleton<EmbeddedWorkerDevToolsManager>::get(); | |
80 } | |
81 | |
82 DevToolsAgentHostImpl* | |
83 EmbeddedWorkerDevToolsManager::GetDevToolsAgentHostForWorker( | |
84 int worker_process_id, | |
85 int worker_route_id) { | |
86 AgentHostMap::iterator it = workers_.find( | |
87 WorkerId(worker_process_id, worker_route_id)); | |
88 return it == workers_.end() ? NULL : it->second; | |
89 } | |
90 | |
91 EmbeddedWorkerDevToolsManager::EmbeddedWorkerDevToolsManager() | |
92 : debug_service_worker_on_start_(false) { | |
93 } | |
94 | |
95 EmbeddedWorkerDevToolsManager::~EmbeddedWorkerDevToolsManager() { | |
96 } | |
97 | |
98 bool EmbeddedWorkerDevToolsManager::SharedWorkerCreated( | |
99 int worker_process_id, | |
100 int worker_route_id, | |
101 const SharedWorkerInstance& instance) { | |
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
103 const WorkerId id(worker_process_id, worker_route_id); | |
104 AgentHostMap::iterator it = FindExistingSharedWorkerAgentHost(instance); | |
105 if (it == workers_.end()) { | |
106 workers_[id] = new SharedWorkerDevToolsAgentHost(id, instance); | |
107 DevToolsManager::GetInstance()->AgentHostChanged(workers_[id]); | |
108 return false; | |
109 } | |
110 WorkerRestarted(id, it); | |
111 return true; | |
112 } | |
113 | |
114 bool EmbeddedWorkerDevToolsManager::ServiceWorkerCreated( | |
115 int worker_process_id, | 55 int worker_process_id, |
116 int worker_route_id, | 56 int worker_route_id, |
117 const ServiceWorkerIdentifier& service_worker_id) { | 57 const ServiceWorkerIdentifier& service_worker_id) { |
118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
119 const WorkerId id(worker_process_id, worker_route_id); | 59 const WorkerId id(worker_process_id, worker_route_id); |
120 AgentHostMap::iterator it = | 60 AgentHostMap::iterator it = FindExistingWorkerAgentHost(service_worker_id); |
121 FindExistingServiceWorkerAgentHost(service_worker_id); | 61 if (it == workers().end()) { |
122 if (it == workers_.end()) { | 62 workers()[id] = new ServiceWorkerDevToolsAgentHost( |
123 workers_[id] = new ServiceWorkerDevToolsAgentHost( | |
124 id, service_worker_id, debug_service_worker_on_start_); | 63 id, service_worker_id, debug_service_worker_on_start_); |
125 DevToolsManager::GetInstance()->AgentHostChanged(workers_[id]); | 64 DevToolsManager::GetInstance()->AgentHostChanged(workers()[id]); |
126 return debug_service_worker_on_start_; | 65 return debug_service_worker_on_start_; |
127 } | 66 } |
128 WorkerRestarted(id, it); | 67 WorkerRestarted(id, it); |
129 return true; | 68 return true; |
130 } | 69 } |
131 | 70 |
132 void EmbeddedWorkerDevToolsManager::WorkerDestroyed(int worker_process_id, | 71 void ServiceWorkerDevToolsManager::WorkerStopIgnored(int worker_process_id, |
133 int worker_route_id) { | |
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
135 const WorkerId id(worker_process_id, worker_route_id); | |
136 AgentHostMap::iterator it = workers_.find(id); | |
137 DCHECK(it != workers_.end()); | |
138 scoped_refptr<EmbeddedWorkerDevToolsAgentHost> agent_host(it->second); | |
139 agent_host->WorkerDestroyed(); | |
140 DevToolsManager::GetInstance()->AgentHostChanged(agent_host); | |
141 } | |
142 | |
143 void EmbeddedWorkerDevToolsManager::WorkerStopIgnored(int worker_process_id, | |
144 int worker_route_id) { | 72 int worker_route_id) { |
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
146 // TODO(pfeldman): Show a console message to tell the user that UA didn't | 74 // TODO(pfeldman): Show a console message to tell the user that UA didn't |
147 // terminate the worker because devtools is attached. | 75 // terminate the worker because devtools is attached. |
148 } | 76 } |
149 | 77 |
150 void EmbeddedWorkerDevToolsManager::WorkerReadyForInspection( | 78 ServiceWorkerDevToolsManager::ServiceWorkerDevToolsManager() |
151 int worker_process_id, | 79 : debug_service_worker_on_start_(false) { |
152 int worker_route_id) { | |
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
154 const WorkerId id(worker_process_id, worker_route_id); | |
155 AgentHostMap::iterator it = workers_.find(id); | |
156 DCHECK(it != workers_.end()); | |
157 it->second->WorkerReadyForInspection(); | |
158 } | 80 } |
159 | 81 |
160 void EmbeddedWorkerDevToolsManager::RemoveInspectedWorkerData(WorkerId id) { | 82 ServiceWorkerDevToolsManager::~ServiceWorkerDevToolsManager() { |
161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
162 workers_.erase(id); | |
163 } | 83 } |
164 | 84 |
165 EmbeddedWorkerDevToolsManager::AgentHostMap::iterator | 85 ServiceWorkerDevToolsManager::AgentHostMap::iterator |
166 EmbeddedWorkerDevToolsManager::FindExistingSharedWorkerAgentHost( | 86 ServiceWorkerDevToolsManager::FindExistingWorkerAgentHost( |
167 const SharedWorkerInstance& instance) { | 87 const ServiceWorkerIdentifier& service_worker_id) { |
168 AgentHostMap::iterator it = workers_.begin(); | 88 AgentHostMap::iterator it = workers().begin(); |
169 for (; it != workers_.end(); ++it) { | 89 for (; it != workers().end(); ++it) { |
170 if (it->second->Matches(instance)) | 90 if (static_cast<ServiceWorkerDevToolsAgentHost*>( |
| 91 it->second)->Matches(service_worker_id)) |
171 break; | 92 break; |
172 } | 93 } |
173 return it; | 94 return it; |
174 } | 95 } |
175 | 96 |
176 EmbeddedWorkerDevToolsManager::AgentHostMap::iterator | |
177 EmbeddedWorkerDevToolsManager::FindExistingServiceWorkerAgentHost( | |
178 const ServiceWorkerIdentifier& service_worker_id) { | |
179 AgentHostMap::iterator it = workers_.begin(); | |
180 for (; it != workers_.end(); ++it) { | |
181 if (it->second->Matches(service_worker_id)) | |
182 break; | |
183 } | |
184 return it; | |
185 } | |
186 | |
187 DevToolsAgentHost::List | |
188 EmbeddedWorkerDevToolsManager::GetOrCreateAllAgentHosts() { | |
189 DevToolsAgentHost::List result; | |
190 EmbeddedWorkerDevToolsManager* instance = GetInstance(); | |
191 for (AgentHostMap::iterator it = instance->workers_.begin(); | |
192 it != instance->workers_.end(); ++it) { | |
193 if (!it->second->IsTerminated()) | |
194 result.push_back(it->second); | |
195 } | |
196 return result; | |
197 } | |
198 | |
199 void EmbeddedWorkerDevToolsManager::WorkerRestarted( | |
200 const WorkerId& id, | |
201 const AgentHostMap::iterator& it) { | |
202 EmbeddedWorkerDevToolsAgentHost* agent_host = it->second; | |
203 agent_host->WorkerRestarted(id); | |
204 workers_.erase(it); | |
205 workers_[id] = agent_host; | |
206 DevToolsManager::GetInstance()->AgentHostChanged(agent_host); | |
207 } | |
208 | |
209 void EmbeddedWorkerDevToolsManager::ResetForTesting() { | |
210 workers_.clear(); | |
211 } | |
212 | |
213 } // namespace content | 97 } // namespace content |
OLD | NEW |