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

Side by Side Diff: content/browser/devtools/protocol/service_worker_handler.cc

Issue 998173002: Revise ServiceWorker DevTools protocols. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: ServiceWorkerContextWatcher Created 5 years, 9 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/protocol/service_worker_handler.h" 5 #include "content/browser/devtools/protocol/service_worker_handler.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/containers/scoped_ptr_hash_map.h"
8 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
9 #include "content/browser/devtools/service_worker_devtools_agent_host.h" 10 #include "content/browser/devtools/service_worker_devtools_agent_host.h"
10 #include "content/browser/devtools/service_worker_devtools_manager.h" 11 #include "content/browser/devtools/service_worker_devtools_manager.h"
11 #include "content/browser/service_worker/service_worker_context_observer.h" 12 #include "content/browser/service_worker/service_worker_context_watcher.h"
12 #include "content/browser/service_worker/service_worker_context_wrapper.h" 13 #include "content/browser/service_worker/service_worker_context_wrapper.h"
14 #include "content/browser/service_worker/service_worker_version.h"
13 #include "content/public/browser/browser_context.h" 15 #include "content/public/browser/browser_context.h"
14 #include "content/public/browser/browser_thread.h" 16 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/devtools_agent_host.h" 17 #include "content/public/browser/devtools_agent_host.h"
16 #include "content/public/browser/render_frame_host.h" 18 #include "content/public/browser/render_frame_host.h"
17 #include "content/public/browser/render_process_host.h" 19 #include "content/public/browser/render_process_host.h"
18 #include "content/public/browser/storage_partition.h" 20 #include "content/public/browser/storage_partition.h"
19 #include "url/gurl.h" 21 #include "url/gurl.h"
20 22
21 // Windows headers will redefine SendMessage. 23 // Windows headers will redefine SendMessage.
22 #ifdef SendMessage 24 #ifdef SendMessage
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 ->set_status(GetVersionStatusString(version_info.status))); 78 ->set_status(GetVersionStatusString(version_info.status)));
77 return version; 79 return version;
78 } 80 }
79 81
80 scoped_refptr<ServiceWorkerRegistration> CreateRegistrationDictionaryValue( 82 scoped_refptr<ServiceWorkerRegistration> CreateRegistrationDictionaryValue(
81 const ServiceWorkerRegistrationInfo& registration_info) { 83 const ServiceWorkerRegistrationInfo& registration_info) {
82 scoped_refptr<ServiceWorkerRegistration> registration( 84 scoped_refptr<ServiceWorkerRegistration> registration(
83 ServiceWorkerRegistration::Create() 85 ServiceWorkerRegistration::Create()
84 ->set_registration_id( 86 ->set_registration_id(
85 base::Int64ToString(registration_info.registration_id)) 87 base::Int64ToString(registration_info.registration_id))
86 ->set_scope_url(registration_info.pattern.spec())); 88 ->set_scope_url(registration_info.pattern.spec())
87 if (registration_info.active_version.version_id != 89 ->set_is_deleted(registration_info.is_deleted));
88 kInvalidServiceWorkerVersionId) {
89 registration->set_active_version(
90 CreateVersionDictionaryValue(registration_info.active_version));
91 }
92 if (registration_info.waiting_version.version_id !=
93 kInvalidServiceWorkerVersionId) {
94 registration->set_waiting_version(
95 CreateVersionDictionaryValue(registration_info.waiting_version));
96 }
97 if (registration_info.installing_version.version_id !=
98 kInvalidServiceWorkerVersionId) {
99 registration->set_installing_version(
kinuko 2015/03/13 09:09:21 Ok so we assume version.state info should be enoug
horo 2015/03/13 10:03:49 Yes.
100 CreateVersionDictionaryValue(registration_info.installing_version));
101 }
102 return registration; 90 return registration;
103 } 91 }
104 92
105 } // namespace 93 } // namespace
106 94
107 using Response = DevToolsProtocolClient::Response; 95 using Response = DevToolsProtocolClient::Response;
108 96
109 class ServiceWorkerHandler::ContextObserver
110 : public ServiceWorkerContextObserver,
111 public base::RefCountedThreadSafe<ContextObserver> {
112 public:
113 ContextObserver(scoped_refptr<ServiceWorkerContextWrapper> context,
114 base::WeakPtr<ServiceWorkerHandler> handler);
115 void Start();
116 void Stop();
117
118 private:
119 friend class base::RefCountedThreadSafe<ContextObserver>;
120 ~ContextObserver() override;
121 void GetStoredRegistrationsOnIOThread();
122 void OnStoredRegistrationsOnIOThread(
123 const std::vector<ServiceWorkerRegistrationInfo>& registrations);
124 void StopOnIOThread();
125
126 void OnVersionUpdated(int64 version_id);
127 void OnRegistrationUpdated(int64 registration_id);
128
129 // ServiceWorkerContextObserver implements
130 void OnRunningStateChanged(int64 version_id) override;
131 void OnVersionStateChanged(int64 version_id) override;
132 void OnRegistrationStored(int64 registration_id,
133 const GURL& pattern) override;
134 void OnRegistrationDeleted(int64 registration_id,
135 const GURL& pattern) override;
136
137 scoped_refptr<ServiceWorkerContextWrapper> context_;
138 base::WeakPtr<ServiceWorkerHandler> handler_;
139 };
140
141 ServiceWorkerHandler::ContextObserver::ContextObserver(
142 scoped_refptr<ServiceWorkerContextWrapper> context,
143 base::WeakPtr<ServiceWorkerHandler> handler)
144 : context_(context), handler_(handler) {
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
146 }
147
148 void ServiceWorkerHandler::ContextObserver::Start() {
149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
150 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
151 base::Bind(&ServiceWorkerHandler::ContextObserver::
152 GetStoredRegistrationsOnIOThread,
153 this));
154 }
155
156 void ServiceWorkerHandler::ContextObserver::Stop() {
157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
158 BrowserThread::PostTask(
159 BrowserThread::IO, FROM_HERE,
160 base::Bind(&ServiceWorkerHandler::ContextObserver::StopOnIOThread, this));
161 }
162
163 void ServiceWorkerHandler::ContextObserver::GetStoredRegistrationsOnIOThread() {
164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
165 context_->context()->storage()->GetAllRegistrations(base::Bind(
166 &ServiceWorkerHandler::ContextObserver::OnStoredRegistrationsOnIOThread,
167 this));
168 }
169
170 void ServiceWorkerHandler::ContextObserver::OnStoredRegistrationsOnIOThread(
171 const std::vector<ServiceWorkerRegistrationInfo>& registrations) {
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
173 context_->AddObserver(this);
174 BrowserThread::PostTask(
175 BrowserThread::UI, FROM_HERE,
176 base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationUpdated, handler_,
177 registrations));
178 BrowserThread::PostTask(
179 BrowserThread::UI, FROM_HERE,
180 base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationUpdated, handler_,
181 context_->context()->GetAllLiveRegistrationInfo()));
182 BrowserThread::PostTask(
183 BrowserThread::UI, FROM_HERE,
184 base::Bind(&ServiceWorkerHandler::OnWorkerVersionUpdated, handler_,
185 context_->context()->GetAllLiveVersionInfo()));
186 }
187
188 void ServiceWorkerHandler::ContextObserver::StopOnIOThread() {
189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
190 context_->RemoveObserver(this);
191 }
192
193 ServiceWorkerHandler::ContextObserver::~ContextObserver() {
194 }
195
196 void ServiceWorkerHandler::ContextObserver::OnVersionUpdated(int64 version_id) {
197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
198 content::ServiceWorkerVersion* version =
199 context_->context()->GetLiveVersion(version_id);
200 if (!version)
201 return;
202 OnRegistrationUpdated(version->registration_id());
203 std::vector<ServiceWorkerVersionInfo> versions;
204 versions.push_back(version->GetInfo());
205 BrowserThread::PostTask(
206 BrowserThread::UI, FROM_HERE,
207 base::Bind(&ServiceWorkerHandler::OnWorkerVersionUpdated, handler_,
208 versions));
209 }
210
211 void ServiceWorkerHandler::ContextObserver::OnRegistrationUpdated(
212 int64 registration_id) {
213 content::ServiceWorkerRegistration* registration =
214 context_->context()->GetLiveRegistration(registration_id);
215 if (!registration)
216 return;
217 std::vector<ServiceWorkerRegistrationInfo> registrations;
218 registrations.push_back(registration->GetInfo());
219 BrowserThread::PostTask(
220 BrowserThread::UI, FROM_HERE,
221 base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationUpdated, handler_,
222 registrations));
223 }
224
225 void ServiceWorkerHandler::ContextObserver::OnRunningStateChanged(
226 int64 version_id) {
227 OnVersionUpdated(version_id);
228 }
229
230 void ServiceWorkerHandler::ContextObserver::OnVersionStateChanged(
231 int64 version_id) {
232 OnVersionUpdated(version_id);
233 }
234
235 void ServiceWorkerHandler::ContextObserver::OnRegistrationStored(
236 int64 registration_id,
237 const GURL& pattern) {
238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
239 content::ServiceWorkerRegistration* registration =
240 context_->context()->GetLiveRegistration(registration_id);
241 DCHECK(registration);
242 std::vector<ServiceWorkerRegistrationInfo> registrations;
243 registrations.push_back(registration->GetInfo());
244 BrowserThread::PostTask(
245 BrowserThread::UI, FROM_HERE,
246 base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationUpdated, handler_,
247 registrations));
248 }
249
250 void ServiceWorkerHandler::ContextObserver::OnRegistrationDeleted(
251 int64 registration_id,
252 const GURL& pattern) {
253 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
254 BrowserThread::PostTask(
255 BrowserThread::UI, FROM_HERE,
256 base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationDeleted, handler_,
257 registration_id));
258 }
259
260 ServiceWorkerHandler::ServiceWorkerHandler() 97 ServiceWorkerHandler::ServiceWorkerHandler()
261 : enabled_(false), weak_factory_(this) { 98 : enabled_(false), weak_factory_(this) {
262 } 99 }
263 100
264 ServiceWorkerHandler::~ServiceWorkerHandler() { 101 ServiceWorkerHandler::~ServiceWorkerHandler() {
265 Disable(); 102 Disable();
266 } 103 }
267 104
268 void ServiceWorkerHandler::SetRenderFrameHost( 105 void ServiceWorkerHandler::SetRenderFrameHost(
269 RenderFrameHost* render_frame_host) { 106 RenderFrameHost* render_frame_host) {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 return Response::InternalError("Could not connect to the context"); 155 return Response::InternalError("Could not connect to the context");
319 enabled_ = true; 156 enabled_ = true;
320 157
321 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); 158 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this);
322 159
323 ServiceWorkerDevToolsAgentHost::List agent_hosts; 160 ServiceWorkerDevToolsAgentHost::List agent_hosts;
324 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&agent_hosts); 161 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&agent_hosts);
325 for (auto host : agent_hosts) 162 for (auto host : agent_hosts)
326 WorkerReadyForInspection(host.get()); 163 WorkerReadyForInspection(host.get());
327 164
328 context_observer_ = new ContextObserver(context_, weak_factory_.GetWeakPtr()); 165 context_watcher_ = new ServiceWorkerContextWatcher(
329 context_observer_->Start(); 166 context_, base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationUpdated,
167 weak_factory_.GetWeakPtr()),
168 base::Bind(&ServiceWorkerHandler::OnWorkerVersionUpdated,
169 weak_factory_.GetWeakPtr()));
170 context_watcher_->Start();
330 return Response::OK(); 171 return Response::OK();
331 } 172 }
332 173
333 Response ServiceWorkerHandler::Disable() { 174 Response ServiceWorkerHandler::Disable() {
334 if (!enabled_) 175 if (!enabled_)
335 return Response::OK(); 176 return Response::OK();
336 enabled_ = false; 177 enabled_ = false;
337 178
338 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this); 179 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this);
339 for (const auto& pair : attached_hosts_) 180 for (const auto& pair : attached_hosts_)
340 pair.second->DetachClient(); 181 pair.second->DetachClient();
341 attached_hosts_.clear(); 182 attached_hosts_.clear();
342 DCHECK(context_observer_); 183 DCHECK(context_watcher_);
343 context_observer_->Stop(); 184 context_watcher_->Stop();
344 context_observer_ = nullptr; 185 context_watcher_ = nullptr;
345 return Response::OK(); 186 return Response::OK();
346 } 187 }
347 188
348 Response ServiceWorkerHandler::SendMessage( 189 Response ServiceWorkerHandler::SendMessage(
349 const std::string& worker_id, 190 const std::string& worker_id,
350 const std::string& message) { 191 const std::string& message) {
351 auto it = attached_hosts_.find(worker_id); 192 auto it = attached_hosts_.find(worker_id);
352 if (it == attached_hosts_.end()) 193 if (it == attached_hosts_.end())
353 return Response::InternalError("Not connected to the worker"); 194 return Response::InternalError("Not connected to the worker");
354 it->second->DispatchProtocolMessage(message); 195 it->second->DispatchProtocolMessage(message);
(...skipping 24 matching lines...) Expand all
379 void ServiceWorkerHandler::OnWorkerVersionUpdated( 220 void ServiceWorkerHandler::OnWorkerVersionUpdated(
380 const std::vector<ServiceWorkerVersionInfo>& versions) { 221 const std::vector<ServiceWorkerVersionInfo>& versions) {
381 std::vector<scoped_refptr<ServiceWorkerVersion>> version_values; 222 std::vector<scoped_refptr<ServiceWorkerVersion>> version_values;
382 for (const auto& version : versions) { 223 for (const auto& version : versions) {
383 version_values.push_back(CreateVersionDictionaryValue(version)); 224 version_values.push_back(CreateVersionDictionaryValue(version));
384 } 225 }
385 client_->WorkerVersionUpdated( 226 client_->WorkerVersionUpdated(
386 WorkerVersionUpdatedParams::Create()->set_versions(version_values)); 227 WorkerVersionUpdatedParams::Create()->set_versions(version_values));
387 } 228 }
388 229
389 void ServiceWorkerHandler::OnWorkerRegistrationDeleted(int64 registration_id) {
390 client_->WorkerRegistrationDeleted(
391 WorkerRegistrationDeletedParams::Create()->set_registration_id(
392 base::Int64ToString(registration_id)));
393 }
394
395 void ServiceWorkerHandler::DispatchProtocolMessage( 230 void ServiceWorkerHandler::DispatchProtocolMessage(
396 DevToolsAgentHost* host, 231 DevToolsAgentHost* host,
397 const std::string& message) { 232 const std::string& message) {
398 233
399 auto it = attached_hosts_.find(host->GetId()); 234 auto it = attached_hosts_.find(host->GetId());
400 if (it == attached_hosts_.end()) 235 if (it == attached_hosts_.end())
401 return; // Already disconnected. 236 return; // Already disconnected.
402 237
403 client_->DispatchMessage( 238 client_->DispatchMessage(
404 DispatchMessageParams::Create()-> 239 DispatchMessageParams::Create()->
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 bool ServiceWorkerHandler::MatchesInspectedPage( 280 bool ServiceWorkerHandler::MatchesInspectedPage(
446 ServiceWorkerDevToolsAgentHost* host) { 281 ServiceWorkerDevToolsAgentHost* host) {
447 // TODO(pfeldman): match based on scope. 282 // TODO(pfeldman): match based on scope.
448 // TODO(pfeldman): match iframes. 283 // TODO(pfeldman): match iframes.
449 return host->GetURL().host() == url_.host(); 284 return host->GetURL().host() == url_.host();
450 } 285 }
451 286
452 } // namespace service_worker 287 } // namespace service_worker
453 } // namespace devtools 288 } // namespace devtools
454 } // namespace content 289 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698