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

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: use DCHECK_CURRENTLY_ON 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/frame_host/frame_tree.h" 12 #include "content/browser/frame_host/frame_tree.h"
12 #include "content/browser/frame_host/frame_tree_node.h" 13 #include "content/browser/frame_host/frame_tree_node.h"
13 #include "content/browser/frame_host/render_frame_host_impl.h" 14 #include "content/browser/frame_host/render_frame_host_impl.h"
14 #include "content/browser/service_worker/service_worker_context_observer.h" 15 #include "content/browser/service_worker/service_worker_context_watcher.h"
15 #include "content/browser/service_worker/service_worker_context_wrapper.h" 16 #include "content/browser/service_worker/service_worker_context_wrapper.h"
17 #include "content/browser/service_worker/service_worker_version.h"
16 #include "content/public/browser/browser_context.h" 18 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/browser_thread.h" 19 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/devtools_agent_host.h" 20 #include "content/public/browser/devtools_agent_host.h"
19 #include "content/public/browser/render_frame_host.h" 21 #include "content/public/browser/render_frame_host.h"
20 #include "content/public/browser/render_process_host.h" 22 #include "content/public/browser/render_process_host.h"
21 #include "content/public/browser/storage_partition.h" 23 #include "content/public/browser/storage_partition.h"
22 #include "url/gurl.h" 24 #include "url/gurl.h"
23 25
24 // Windows headers will redefine SendMessage. 26 // Windows headers will redefine SendMessage.
25 #ifdef SendMessage 27 #ifdef SendMessage
26 #undef SendMessage 28 #undef SendMessage
27 #endif 29 #endif
28 30
29 namespace content { 31 namespace content {
30 namespace devtools { 32 namespace devtools {
31 namespace service_worker { 33 namespace service_worker {
32 34
33 namespace { 35 namespace {
34 36
37 const char kServiceWorkerVersionRunningStatusStopped[] = "stopped";
38 const char kServiceWorkerVersionRunningStatusStarting[] = "starting";
39 const char kServiceWorkerVersionRunningStatusRunning[] = "running";
40 const char kServiceWorkerVersionRunningStatusStopping[] = "stopping";
41
42 const char kServiceWorkerVersionStatusNew[] = "new";
43 const char kServiceWorkerVersionStatusInstalling[] = "installing";
44 const char kServiceWorkerVersionStatusInstalled[] = "installed";
45 const char kServiceWorkerVersionStatusActivating[] = "activating";
46 const char kServiceWorkerVersionStatusActivated[] = "activated";
47 const char kServiceWorkerVersionStatusRedundant[] = "redundant";
48
35 const std::string GetVersionRunningStatusString( 49 const std::string GetVersionRunningStatusString(
36 content::ServiceWorkerVersion::RunningStatus running_status) { 50 content::ServiceWorkerVersion::RunningStatus running_status) {
37 switch (running_status) { 51 switch (running_status) {
38 case content::ServiceWorkerVersion::STOPPED: 52 case content::ServiceWorkerVersion::STOPPED:
39 return service_worker_version::kRunningStatusStopped; 53 return kServiceWorkerVersionRunningStatusStopped;
40 case content::ServiceWorkerVersion::STARTING: 54 case content::ServiceWorkerVersion::STARTING:
41 return service_worker_version::kRunningStatusStarting; 55 return kServiceWorkerVersionRunningStatusStarting;
42 case content::ServiceWorkerVersion::RUNNING: 56 case content::ServiceWorkerVersion::RUNNING:
43 return service_worker_version::kRunningStatusRunning; 57 return kServiceWorkerVersionRunningStatusRunning;
44 case content::ServiceWorkerVersion::STOPPING: 58 case content::ServiceWorkerVersion::STOPPING:
45 return service_worker_version::kRunningStatusStopping; 59 return kServiceWorkerVersionRunningStatusStopping;
46 } 60 }
47 return ""; 61 return "";
48 } 62 }
49 63
50 const std::string GetVersionStatusString( 64 const std::string GetVersionStatusString(
51 content::ServiceWorkerVersion::Status status) { 65 content::ServiceWorkerVersion::Status status) {
52 switch (status) { 66 switch (status) {
53 case content::ServiceWorkerVersion::NEW: 67 case content::ServiceWorkerVersion::NEW:
54 return service_worker_version::kStatusNew; 68 return kServiceWorkerVersionStatusNew;
55 case content::ServiceWorkerVersion::INSTALLING: 69 case content::ServiceWorkerVersion::INSTALLING:
56 return service_worker_version::kStatusInstalling; 70 return kServiceWorkerVersionStatusInstalling;
57 case content::ServiceWorkerVersion::INSTALLED: 71 case content::ServiceWorkerVersion::INSTALLED:
58 return service_worker_version::kStatusInstalled; 72 return kServiceWorkerVersionStatusInstalled;
59 case content::ServiceWorkerVersion::ACTIVATING: 73 case content::ServiceWorkerVersion::ACTIVATING:
60 return service_worker_version::kStatusActivating; 74 return kServiceWorkerVersionStatusActivating;
61 case content::ServiceWorkerVersion::ACTIVATED: 75 case content::ServiceWorkerVersion::ACTIVATED:
62 return service_worker_version::kStatusActivated; 76 return kServiceWorkerVersionStatusActivated;
63 case content::ServiceWorkerVersion::REDUNDANT: 77 case content::ServiceWorkerVersion::REDUNDANT:
64 return service_worker_version::kStatusRedundant; 78 return kServiceWorkerVersionStatusRedundant;
65 } 79 }
66 return ""; 80 return "";
67 } 81 }
68 82
69 scoped_refptr<ServiceWorkerVersion> CreateVersionDictionaryValue( 83 scoped_refptr<ServiceWorkerVersion> CreateVersionDictionaryValue(
70 const ServiceWorkerVersionInfo& version_info) { 84 const ServiceWorkerVersionInfo& version_info) {
71 scoped_refptr<ServiceWorkerVersion> version( 85 scoped_refptr<ServiceWorkerVersion> version(
72 ServiceWorkerVersion::Create() 86 ServiceWorkerVersion::Create()
73 ->set_version_id(base::Int64ToString(version_info.version_id)) 87 ->set_version_id(base::Int64ToString(version_info.version_id))
74 ->set_registration_id( 88 ->set_registration_id(
75 base::Int64ToString(version_info.registration_id)) 89 base::Int64ToString(version_info.registration_id))
76 ->set_script_url(version_info.script_url.spec()) 90 ->set_script_url(version_info.script_url.spec())
77 ->set_running_status( 91 ->set_running_status(
78 GetVersionRunningStatusString(version_info.running_status)) 92 GetVersionRunningStatusString(version_info.running_status))
79 ->set_status(GetVersionStatusString(version_info.status))); 93 ->set_status(GetVersionStatusString(version_info.status)));
80 return version; 94 return version;
81 } 95 }
82 96
83 scoped_refptr<ServiceWorkerRegistration> CreateRegistrationDictionaryValue( 97 scoped_refptr<ServiceWorkerRegistration> CreateRegistrationDictionaryValue(
84 const ServiceWorkerRegistrationInfo& registration_info) { 98 const ServiceWorkerRegistrationInfo& registration_info) {
85 scoped_refptr<ServiceWorkerRegistration> registration( 99 scoped_refptr<ServiceWorkerRegistration> registration(
86 ServiceWorkerRegistration::Create() 100 ServiceWorkerRegistration::Create()
87 ->set_registration_id( 101 ->set_registration_id(
88 base::Int64ToString(registration_info.registration_id)) 102 base::Int64ToString(registration_info.registration_id))
89 ->set_scope_url(registration_info.pattern.spec())); 103 ->set_scope_url(registration_info.pattern.spec())
90 if (registration_info.active_version.version_id != 104 ->set_is_deleted(registration_info.delete_flag ==
91 kInvalidServiceWorkerVersionId) { 105 ServiceWorkerRegistrationInfo::IS_DELETED));
92 registration->set_active_version(
93 CreateVersionDictionaryValue(registration_info.active_version));
94 }
95 if (registration_info.waiting_version.version_id !=
96 kInvalidServiceWorkerVersionId) {
97 registration->set_waiting_version(
98 CreateVersionDictionaryValue(registration_info.waiting_version));
99 }
100 if (registration_info.installing_version.version_id !=
101 kInvalidServiceWorkerVersionId) {
102 registration->set_installing_version(
103 CreateVersionDictionaryValue(registration_info.installing_version));
104 }
105 return registration; 106 return registration;
106 } 107 }
107 108
108 scoped_refptr<ServiceWorkerDevToolsAgentHost> GetMatchingServiceWorker( 109 scoped_refptr<ServiceWorkerDevToolsAgentHost> GetMatchingServiceWorker(
109 const ServiceWorkerDevToolsAgentHost::List& agent_hosts, 110 const ServiceWorkerDevToolsAgentHost::List& agent_hosts,
110 const GURL& url) { 111 const GURL& url) {
111 scoped_refptr<ServiceWorkerDevToolsAgentHost> best_host; 112 scoped_refptr<ServiceWorkerDevToolsAgentHost> best_host;
112 std::string best_scope; 113 std::string best_scope;
113 for (auto host : agent_hosts) { 114 for (auto host : agent_hosts) {
114 if (host->GetURL().host() != url.host()) 115 if (host->GetURL().host() != url.host())
(...skipping 26 matching lines...) Expand all
141 142
142 bool CollectURLs(std::set<GURL>* urls, FrameTreeNode* tree_node) { 143 bool CollectURLs(std::set<GURL>* urls, FrameTreeNode* tree_node) {
143 urls->insert(tree_node->current_url()); 144 urls->insert(tree_node->current_url());
144 return false; 145 return false;
145 } 146 }
146 147
147 } // namespace 148 } // namespace
148 149
149 using Response = DevToolsProtocolClient::Response; 150 using Response = DevToolsProtocolClient::Response;
150 151
151 class ServiceWorkerHandler::ContextObserver
152 : public ServiceWorkerContextObserver,
153 public base::RefCountedThreadSafe<ContextObserver> {
154 public:
155 ContextObserver(scoped_refptr<ServiceWorkerContextWrapper> context,
156 base::WeakPtr<ServiceWorkerHandler> handler);
157 void Start();
158 void Stop();
159
160 private:
161 friend class base::RefCountedThreadSafe<ContextObserver>;
162 ~ContextObserver() override;
163 void GetStoredRegistrationsOnIOThread();
164 void OnStoredRegistrationsOnIOThread(
165 const std::vector<ServiceWorkerRegistrationInfo>& registrations);
166 void StopOnIOThread();
167
168 void OnVersionUpdated(int64 version_id);
169 void OnRegistrationUpdated(int64 registration_id);
170
171 // ServiceWorkerContextObserver implements
172 void OnRunningStateChanged(int64 version_id) override;
173 void OnVersionStateChanged(int64 version_id) override;
174 void OnRegistrationStored(int64 registration_id,
175 const GURL& pattern) override;
176 void OnRegistrationDeleted(int64 registration_id,
177 const GURL& pattern) override;
178
179 scoped_refptr<ServiceWorkerContextWrapper> context_;
180 base::WeakPtr<ServiceWorkerHandler> handler_;
181 };
182
183 ServiceWorkerHandler::ContextObserver::ContextObserver(
184 scoped_refptr<ServiceWorkerContextWrapper> context,
185 base::WeakPtr<ServiceWorkerHandler> handler)
186 : context_(context), handler_(handler) {
187 DCHECK_CURRENTLY_ON(BrowserThread::UI);
188 }
189
190 void ServiceWorkerHandler::ContextObserver::Start() {
191 DCHECK_CURRENTLY_ON(BrowserThread::UI);
192 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
193 base::Bind(&ServiceWorkerHandler::ContextObserver::
194 GetStoredRegistrationsOnIOThread,
195 this));
196 }
197
198 void ServiceWorkerHandler::ContextObserver::Stop() {
199 DCHECK_CURRENTLY_ON(BrowserThread::UI);
200 BrowserThread::PostTask(
201 BrowserThread::IO, FROM_HERE,
202 base::Bind(&ServiceWorkerHandler::ContextObserver::StopOnIOThread, this));
203 }
204
205 void ServiceWorkerHandler::ContextObserver::GetStoredRegistrationsOnIOThread() {
206 DCHECK_CURRENTLY_ON(BrowserThread::IO);
207 context_->context()->storage()->GetAllRegistrations(base::Bind(
208 &ServiceWorkerHandler::ContextObserver::OnStoredRegistrationsOnIOThread,
209 this));
210 }
211
212 void ServiceWorkerHandler::ContextObserver::OnStoredRegistrationsOnIOThread(
213 const std::vector<ServiceWorkerRegistrationInfo>& registrations) {
214 DCHECK_CURRENTLY_ON(BrowserThread::IO);
215 context_->AddObserver(this);
216 BrowserThread::PostTask(
217 BrowserThread::UI, FROM_HERE,
218 base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationUpdated, handler_,
219 registrations));
220 BrowserThread::PostTask(
221 BrowserThread::UI, FROM_HERE,
222 base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationUpdated, handler_,
223 context_->context()->GetAllLiveRegistrationInfo()));
224 BrowserThread::PostTask(
225 BrowserThread::UI, FROM_HERE,
226 base::Bind(&ServiceWorkerHandler::OnWorkerVersionUpdated, handler_,
227 context_->context()->GetAllLiveVersionInfo()));
228 }
229
230 void ServiceWorkerHandler::ContextObserver::StopOnIOThread() {
231 DCHECK_CURRENTLY_ON(BrowserThread::IO);
232 context_->RemoveObserver(this);
233 }
234
235 ServiceWorkerHandler::ContextObserver::~ContextObserver() {
236 }
237
238 void ServiceWorkerHandler::ContextObserver::OnVersionUpdated(int64 version_id) {
239 DCHECK_CURRENTLY_ON(BrowserThread::IO);
240 content::ServiceWorkerVersion* version =
241 context_->context()->GetLiveVersion(version_id);
242 if (!version)
243 return;
244 OnRegistrationUpdated(version->registration_id());
245 std::vector<ServiceWorkerVersionInfo> versions;
246 versions.push_back(version->GetInfo());
247 BrowserThread::PostTask(
248 BrowserThread::UI, FROM_HERE,
249 base::Bind(&ServiceWorkerHandler::OnWorkerVersionUpdated, handler_,
250 versions));
251 }
252
253 void ServiceWorkerHandler::ContextObserver::OnRegistrationUpdated(
254 int64 registration_id) {
255 content::ServiceWorkerRegistration* registration =
256 context_->context()->GetLiveRegistration(registration_id);
257 if (!registration)
258 return;
259 std::vector<ServiceWorkerRegistrationInfo> registrations;
260 registrations.push_back(registration->GetInfo());
261 BrowserThread::PostTask(
262 BrowserThread::UI, FROM_HERE,
263 base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationUpdated, handler_,
264 registrations));
265 }
266
267 void ServiceWorkerHandler::ContextObserver::OnRunningStateChanged(
268 int64 version_id) {
269 OnVersionUpdated(version_id);
270 }
271
272 void ServiceWorkerHandler::ContextObserver::OnVersionStateChanged(
273 int64 version_id) {
274 OnVersionUpdated(version_id);
275 }
276
277 void ServiceWorkerHandler::ContextObserver::OnRegistrationStored(
278 int64 registration_id,
279 const GURL& pattern) {
280 DCHECK_CURRENTLY_ON(BrowserThread::IO);
281 content::ServiceWorkerRegistration* registration =
282 context_->context()->GetLiveRegistration(registration_id);
283 DCHECK(registration);
284 std::vector<ServiceWorkerRegistrationInfo> registrations;
285 registrations.push_back(registration->GetInfo());
286 BrowserThread::PostTask(
287 BrowserThread::UI, FROM_HERE,
288 base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationUpdated, handler_,
289 registrations));
290 }
291
292 void ServiceWorkerHandler::ContextObserver::OnRegistrationDeleted(
293 int64 registration_id,
294 const GURL& pattern) {
295 DCHECK_CURRENTLY_ON(BrowserThread::IO);
296 BrowserThread::PostTask(
297 BrowserThread::UI, FROM_HERE,
298 base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationDeleted, handler_,
299 registration_id));
300 }
301
302 ServiceWorkerHandler::ServiceWorkerHandler() 152 ServiceWorkerHandler::ServiceWorkerHandler()
303 : enabled_(false), weak_factory_(this) { 153 : enabled_(false), weak_factory_(this) {
304 } 154 }
305 155
306 ServiceWorkerHandler::~ServiceWorkerHandler() { 156 ServiceWorkerHandler::~ServiceWorkerHandler() {
307 Disable(); 157 Disable();
308 } 158 }
309 159
310 void ServiceWorkerHandler::SetRenderFrameHost( 160 void ServiceWorkerHandler::SetRenderFrameHost(
311 RenderFrameHostImpl* render_frame_host) { 161 RenderFrameHostImpl* render_frame_host) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 } 207 }
358 208
359 Response ServiceWorkerHandler::Enable() { 209 Response ServiceWorkerHandler::Enable() {
360 if (enabled_) 210 if (enabled_)
361 return Response::OK(); 211 return Response::OK();
362 if (!context_) 212 if (!context_)
363 return Response::InternalError("Could not connect to the context"); 213 return Response::InternalError("Could not connect to the context");
364 enabled_ = true; 214 enabled_ = true;
365 215
366 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); 216 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this);
367 context_observer_ = new ContextObserver(context_, weak_factory_.GetWeakPtr()); 217
368 context_observer_->Start(); 218 context_watcher_ = new ServiceWorkerContextWatcher(
219 context_, base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationUpdated,
220 weak_factory_.GetWeakPtr()),
221 base::Bind(&ServiceWorkerHandler::OnWorkerVersionUpdated,
222 weak_factory_.GetWeakPtr()));
223 context_watcher_->Start();
224
369 UpdateHosts(); 225 UpdateHosts();
370 return Response::OK(); 226 return Response::OK();
371 } 227 }
372 228
373 Response ServiceWorkerHandler::Disable() { 229 Response ServiceWorkerHandler::Disable() {
374 if (!enabled_) 230 if (!enabled_)
375 return Response::OK(); 231 return Response::OK();
376 enabled_ = false; 232 enabled_ = false;
377 233
378 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this); 234 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this);
379 for (const auto& pair : attached_hosts_) 235 for (const auto& pair : attached_hosts_)
380 pair.second->DetachClient(); 236 pair.second->DetachClient();
381 attached_hosts_.clear(); 237 attached_hosts_.clear();
382 DCHECK(context_observer_); 238 DCHECK(context_watcher_);
383 context_observer_->Stop(); 239 context_watcher_->Stop();
384 context_observer_ = nullptr; 240 context_watcher_ = nullptr;
385 return Response::OK(); 241 return Response::OK();
386 } 242 }
387 243
388 Response ServiceWorkerHandler::SendMessage( 244 Response ServiceWorkerHandler::SendMessage(
389 const std::string& worker_id, 245 const std::string& worker_id,
390 const std::string& message) { 246 const std::string& message) {
391 auto it = attached_hosts_.find(worker_id); 247 auto it = attached_hosts_.find(worker_id);
392 if (it == attached_hosts_.end()) 248 if (it == attached_hosts_.end())
393 return Response::InternalError("Not connected to the worker"); 249 return Response::InternalError("Not connected to the worker");
394 it->second->DispatchProtocolMessage(message); 250 it->second->DispatchProtocolMessage(message);
(...skipping 24 matching lines...) Expand all
419 void ServiceWorkerHandler::OnWorkerVersionUpdated( 275 void ServiceWorkerHandler::OnWorkerVersionUpdated(
420 const std::vector<ServiceWorkerVersionInfo>& versions) { 276 const std::vector<ServiceWorkerVersionInfo>& versions) {
421 std::vector<scoped_refptr<ServiceWorkerVersion>> version_values; 277 std::vector<scoped_refptr<ServiceWorkerVersion>> version_values;
422 for (const auto& version : versions) { 278 for (const auto& version : versions) {
423 version_values.push_back(CreateVersionDictionaryValue(version)); 279 version_values.push_back(CreateVersionDictionaryValue(version));
424 } 280 }
425 client_->WorkerVersionUpdated( 281 client_->WorkerVersionUpdated(
426 WorkerVersionUpdatedParams::Create()->set_versions(version_values)); 282 WorkerVersionUpdatedParams::Create()->set_versions(version_values));
427 } 283 }
428 284
429 void ServiceWorkerHandler::OnWorkerRegistrationDeleted(int64 registration_id) {
430 client_->WorkerRegistrationDeleted(
431 WorkerRegistrationDeletedParams::Create()->set_registration_id(
432 base::Int64ToString(registration_id)));
433 }
434
435 void ServiceWorkerHandler::DispatchProtocolMessage( 285 void ServiceWorkerHandler::DispatchProtocolMessage(
436 DevToolsAgentHost* host, 286 DevToolsAgentHost* host,
437 const std::string& message) { 287 const std::string& message) {
438 288
439 auto it = attached_hosts_.find(host->GetId()); 289 auto it = attached_hosts_.find(host->GetId());
440 if (it == attached_hosts_.end()) 290 if (it == attached_hosts_.end())
441 return; // Already disconnected. 291 return; // Already disconnected.
442 292
443 client_->DispatchMessage( 293 client_->DispatchMessage(
444 DispatchMessageParams::Create()-> 294 DispatchMessageParams::Create()->
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 return; 337 return;
488 it->second->DetachClient(); 338 it->second->DetachClient();
489 client_->WorkerTerminated(WorkerTerminatedParams::Create()-> 339 client_->WorkerTerminated(WorkerTerminatedParams::Create()->
490 set_worker_id(host->GetId())); 340 set_worker_id(host->GetId()));
491 attached_hosts_.erase(it); 341 attached_hosts_.erase(it);
492 } 342 }
493 343
494 } // namespace service_worker 344 } // namespace service_worker
495 } // namespace devtools 345 } // namespace devtools
496 } // namespace content 346 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/devtools/protocol/service_worker_handler.h ('k') | content/browser/geofencing/geofencing_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698