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/service_worker/service_worker_internals_ui.h" | 5 #include "content/browser/service_worker/service_worker_internals_ui.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 : internals_(internals), original_args_(original_args.Pass()) {} | 42 : internals_(internals), original_args_(original_args.Pass()) {} |
43 | 43 |
44 void GetRegistrationsOnIOThread(ServiceWorkerContextWrapper* context, | 44 void GetRegistrationsOnIOThread(ServiceWorkerContextWrapper* context, |
45 const base::FilePath& context_path); | 45 const base::FilePath& context_path); |
46 void UnregisterOnIOThread(scoped_refptr<ServiceWorkerContextWrapper> context, | 46 void UnregisterOnIOThread(scoped_refptr<ServiceWorkerContextWrapper> context, |
47 const GURL& scope); | 47 const GURL& scope); |
48 void StartWorkerOnIOThread(scoped_refptr<ServiceWorkerContextWrapper> context, | 48 void StartWorkerOnIOThread(scoped_refptr<ServiceWorkerContextWrapper> context, |
49 const GURL& scope); | 49 const GURL& scope); |
50 void StopWorkerOnIOThread(scoped_refptr<ServiceWorkerContextWrapper> context, | 50 void StopWorkerOnIOThread(scoped_refptr<ServiceWorkerContextWrapper> context, |
51 const GURL& scope); | 51 const GURL& scope); |
52 void DispatchSyncEventToWorkerOnIOThread( | |
53 scoped_refptr<ServiceWorkerContextWrapper> context, | |
54 const GURL& scope); | |
52 | 55 |
53 private: | 56 private: |
54 friend class base::RefCountedThreadSafe<OperationProxy>; | 57 friend class base::RefCountedThreadSafe<OperationProxy>; |
55 ~OperationProxy() {} | 58 ~OperationProxy() {} |
56 void OnHaveRegistrations( | 59 void OnHaveRegistrations( |
57 const base::FilePath& context_path, | 60 const base::FilePath& context_path, |
58 const std::vector<ServiceWorkerRegistrationInfo>& registrations); | 61 const std::vector<ServiceWorkerRegistrationInfo>& registrations); |
59 | 62 |
60 void OperationComplete(ServiceWorkerStatusCode status); | 63 void OperationComplete(ServiceWorkerStatusCode status); |
61 | 64 |
62 void StartActiveWorker( | 65 void StartActiveWorker( |
63 ServiceWorkerStatusCode status, | 66 ServiceWorkerStatusCode status, |
64 const scoped_refptr<ServiceWorkerRegistration>& registration); | 67 const scoped_refptr<ServiceWorkerRegistration>& registration); |
65 | 68 |
66 void StopActiveWorker( | 69 void StopActiveWorker( |
67 ServiceWorkerStatusCode status, | 70 ServiceWorkerStatusCode status, |
68 const scoped_refptr<ServiceWorkerRegistration>& registration); | 71 const scoped_refptr<ServiceWorkerRegistration>& registration); |
69 | 72 |
73 void DispatchSyncEventToActiveWorker( | |
74 ServiceWorkerStatusCode status, | |
75 const scoped_refptr<ServiceWorkerRegistration>& registration); | |
76 | |
70 WeakPtr<ServiceWorkerInternalsUI> internals_; | 77 WeakPtr<ServiceWorkerInternalsUI> internals_; |
71 scoped_ptr<ListValue> original_args_; | 78 scoped_ptr<ListValue> original_args_; |
72 }; | 79 }; |
73 | 80 |
74 ServiceWorkerInternalsUI::ServiceWorkerInternalsUI(WebUI* web_ui) | 81 ServiceWorkerInternalsUI::ServiceWorkerInternalsUI(WebUI* web_ui) |
75 : WebUIController(web_ui) { | 82 : WebUIController(web_ui) { |
76 WebUIDataSource* source = | 83 WebUIDataSource* source = |
77 WebUIDataSource::Create(kChromeUIServiceWorkerInternalsHost); | 84 WebUIDataSource::Create(kChromeUIServiceWorkerInternalsHost); |
78 source->SetUseJsonJSFormatV2(); | 85 source->SetUseJsonJSFormatV2(); |
79 source->SetJsonPath("strings.js"); | 86 source->SetJsonPath("strings.js"); |
(...skipping 16 matching lines...) Expand all Loading... | |
96 base::Bind(&ServiceWorkerInternalsUI::StartWorker, | 103 base::Bind(&ServiceWorkerInternalsUI::StartWorker, |
97 base::Unretained(this))); | 104 base::Unretained(this))); |
98 web_ui->RegisterMessageCallback( | 105 web_ui->RegisterMessageCallback( |
99 "stop", | 106 "stop", |
100 base::Bind(&ServiceWorkerInternalsUI::StopWorker, | 107 base::Bind(&ServiceWorkerInternalsUI::StopWorker, |
101 base::Unretained(this))); | 108 base::Unretained(this))); |
102 web_ui->RegisterMessageCallback( | 109 web_ui->RegisterMessageCallback( |
103 "unregister", | 110 "unregister", |
104 base::Bind(&ServiceWorkerInternalsUI::Unregister, | 111 base::Bind(&ServiceWorkerInternalsUI::Unregister, |
105 base::Unretained(this))); | 112 base::Unretained(this))); |
113 web_ui->RegisterMessageCallback( | |
114 "sync", | |
115 base::Bind(&ServiceWorkerInternalsUI::DispatchSyncEventToWorker, | |
116 base::Unretained(this))); | |
106 } | 117 } |
107 | 118 |
108 ServiceWorkerInternalsUI::~ServiceWorkerInternalsUI() {} | 119 ServiceWorkerInternalsUI::~ServiceWorkerInternalsUI() {} |
109 | 120 |
110 void ServiceWorkerInternalsUI::GetAllRegistrations(const ListValue* args) { | 121 void ServiceWorkerInternalsUI::GetAllRegistrations(const ListValue* args) { |
111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
112 | 123 |
113 BrowserContext* browser_context = | 124 BrowserContext* browser_context = |
114 web_ui()->GetWebContents()->GetBrowserContext(); | 125 web_ui()->GetWebContents()->GetBrowserContext(); |
115 | 126 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 BrowserContext::StoragePartitionCallback cb = | 182 BrowserContext::StoragePartitionCallback cb = |
172 base::Bind(&FindContext, *partition_path, &result_partition, context); | 183 base::Bind(&FindContext, *partition_path, &result_partition, context); |
173 BrowserContext::ForEachStoragePartition(browser_context, cb); | 184 BrowserContext::ForEachStoragePartition(browser_context, cb); |
174 | 185 |
175 if (!result_partition || !(*context)) | 186 if (!result_partition || !(*context)) |
176 return false; | 187 return false; |
177 | 188 |
178 return true; | 189 return true; |
179 } | 190 } |
180 | 191 |
192 void ServiceWorkerInternalsUI::DispatchSyncEventToWorker( | |
193 const ListValue* args) { | |
194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
195 base::FilePath partition_path; | |
196 GURL scope; | |
197 scoped_refptr<ServiceWorkerContextWrapper> context; | |
198 if (!GetRegistrationInfo(args, &partition_path, &scope, &context)) | |
199 return; | |
200 | |
201 scoped_ptr<ListValue> args_copy(args->DeepCopy()); | |
202 BrowserThread::PostTask( | |
203 BrowserThread::IO, | |
204 FROM_HERE, | |
205 base::Bind(&ServiceWorkerInternalsUI::OperationProxy:: | |
206 DispatchSyncEventToWorkerOnIOThread, | |
207 new OperationProxy(AsWeakPtr(), args_copy.Pass()), | |
208 context, | |
209 scope)); | |
210 } | |
211 | |
181 void ServiceWorkerInternalsUI::Unregister(const ListValue* args) { | 212 void ServiceWorkerInternalsUI::Unregister(const ListValue* args) { |
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
183 base::FilePath partition_path; | 214 base::FilePath partition_path; |
184 GURL scope; | 215 GURL scope; |
185 scoped_refptr<ServiceWorkerContextWrapper> context; | 216 scoped_refptr<ServiceWorkerContextWrapper> context; |
186 if (!GetRegistrationInfo(args, &partition_path, &scope, &context)) | 217 if (!GetRegistrationInfo(args, &partition_path, &scope, &context)) |
187 return; | 218 return; |
188 | 219 |
189 scoped_ptr<ListValue> args_copy(args->DeepCopy()); | 220 scoped_ptr<ListValue> args_copy(args->DeepCopy()); |
190 BrowserThread::PostTask( | 221 BrowserThread::PostTask( |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
274 const GURL& scope) { | 305 const GURL& scope) { |
275 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 306 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
276 // TODO(alecflett): Add support for starting/stopping workers for | 307 // TODO(alecflett): Add support for starting/stopping workers for |
277 // pending versions too. | 308 // pending versions too. |
278 context->context()->storage()->FindRegistrationForPattern( | 309 context->context()->storage()->FindRegistrationForPattern( |
279 scope, | 310 scope, |
280 base::Bind(&ServiceWorkerInternalsUI::OperationProxy::StopActiveWorker, | 311 base::Bind(&ServiceWorkerInternalsUI::OperationProxy::StopActiveWorker, |
281 this)); | 312 this)); |
282 } | 313 } |
283 | 314 |
315 void | |
316 ServiceWorkerInternalsUI::OperationProxy::DispatchSyncEventToWorkerOnIOThread( | |
317 scoped_refptr<ServiceWorkerContextWrapper> context, | |
318 const GURL& scope) { | |
319 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
320 context->context()->storage()->FindRegistrationForPattern( | |
321 scope, | |
322 base::Bind(&ServiceWorkerInternalsUI::OperationProxy:: | |
323 DispatchSyncEventToActiveWorker, | |
324 this)); | |
325 } | |
326 | |
284 namespace { | 327 namespace { |
285 void UpdateVersionInfo(const ServiceWorkerVersionInfo& version, | 328 void UpdateVersionInfo(const ServiceWorkerVersionInfo& version, |
286 DictionaryValue* info) { | 329 DictionaryValue* info) { |
287 switch (version.running_status) { | 330 switch (version.running_status) { |
288 case ServiceWorkerVersion::STOPPED: | 331 case ServiceWorkerVersion::STOPPED: |
289 info->SetString("running_status", "STOPPED"); | 332 info->SetString("running_status", "STOPPED"); |
290 break; | 333 break; |
291 case ServiceWorkerVersion::STARTING: | 334 case ServiceWorkerVersion::STARTING: |
292 info->SetString("running_status", "STARTING"); | 335 info->SetString("running_status", "STARTING"); |
293 break; | 336 break; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
413 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 456 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
414 if (status == SERVICE_WORKER_OK) { | 457 if (status == SERVICE_WORKER_OK) { |
415 registration->active_version()->StopWorker(base::Bind( | 458 registration->active_version()->StopWorker(base::Bind( |
416 &ServiceWorkerInternalsUI::OperationProxy::OperationComplete, this)); | 459 &ServiceWorkerInternalsUI::OperationProxy::OperationComplete, this)); |
417 return; | 460 return; |
418 } | 461 } |
419 | 462 |
420 OperationComplete(status); | 463 OperationComplete(status); |
421 } | 464 } |
422 | 465 |
466 void ServiceWorkerInternalsUI::OperationProxy::DispatchSyncEventToActiveWorker( | |
467 ServiceWorkerStatusCode status, | |
468 const scoped_refptr<ServiceWorkerRegistration>& registration) { | |
469 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
470 if (status == SERVICE_WORKER_OK) { | |
kinuko
2014/04/01 14:40:49
Shouldn't we check if registration->active_version
jkarlin
2014/04/01 15:06:06
Done.
| |
471 registration->active_version()->DispatchSyncEvent(base::Bind( | |
472 &ServiceWorkerInternalsUI::OperationProxy::OperationComplete, this)); | |
473 return; | |
474 } | |
475 | |
476 OperationComplete(status); | |
477 } | |
478 | |
423 } // namespace content | 479 } // namespace content |
OLD | NEW |