Chromium Code Reviews| Index: extensions/browser/service_worker_task_queue.cc |
| diff --git a/extensions/browser/service_worker_task_queue.cc b/extensions/browser/service_worker_task_queue.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..52e889ac56a61ca28da1d0715e70e2d159cd6788 |
| --- /dev/null |
| +++ b/extensions/browser/service_worker_task_queue.cc |
| @@ -0,0 +1,96 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "extensions/browser/service_worker_task_queue.h" |
| + |
| +#include "content/public/browser/browser_context.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "content/public/browser/service_worker_context.h" |
| +#include "content/public/browser/storage_partition.h" |
| +#include "extensions/browser/event_router.h" |
| +#include "extensions/browser/lazy_context_id.h" |
| +#include "extensions/browser/service_worker_task_queue_factory.h" |
| +#include "extensions/common/constants.h" |
| + |
| +using content::BrowserContext; |
| +using content::BrowserThread; |
| + |
| +namespace extensions { |
| + |
| +namespace { |
| + |
| +void FinishTask(const LazyContextTaskQueue::PendingTask& task, |
| + const ExtensionId& extension_id, |
| + int process_id, |
| + int thread_id) { |
| + auto params = base::MakeUnique<LazyContextTaskQueue::ContextInfo>( |
| + extension_id, content::RenderProcessHost::FromID(process_id), thread_id, |
| + GURL()); |
| + task.Run(std::move(params)); |
| +} |
| + |
| +void GotWorkerInfoOnIO(const LazyContextTaskQueue::PendingTask& task, |
|
Devlin
2017/06/20 20:31:20
nit: Get and Got are pretty similar for both readi
lazyboy
2017/06/22 02:46:57
Done.
|
| + const ExtensionId& extension_id, |
| + int process_id, |
| + int thread_id) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(FinishTask, task, extension_id, process_id, thread_id)); |
| +} |
| + |
| +void GetWorkerInfoFailed() { |
| + DCHECK(false) << "GetWorkerInfoFailed"; |
| + // TODO(lazyboy): Handle failure case. |
| +} |
| + |
| +void GetServiceWorkerInfoOnIO( |
| + const GURL& origin, |
| + const ExtensionId& extension_id, |
| + content::ServiceWorkerContext* service_worker_context, |
| + const LazyContextTaskQueue::PendingTask& task) { |
| + service_worker_context->GetWorkerInfoAfterStartWorker( |
| + origin, base::BindOnce(&GotWorkerInfoOnIO, task, extension_id), |
| + base::BindOnce(&GetWorkerInfoFailed)); |
| +} |
| + |
| +} // namespace |
| + |
| +ServiceWorkerTaskQueue::ServiceWorkerTaskQueue( |
| + content::BrowserContext* browser_context) {} |
| + |
| +ServiceWorkerTaskQueue::~ServiceWorkerTaskQueue() {} |
| + |
| +// static |
| +ServiceWorkerTaskQueue* ServiceWorkerTaskQueue::Get( |
| + content::BrowserContext* context) { |
| + return ServiceWorkerTaskQueueFactory::GetForBrowserContext(context); |
| +} |
| + |
| +bool ServiceWorkerTaskQueue::ShouldEnqueueTask(content::BrowserContext* context, |
| + const Extension* extension) { |
| + // We call StartWorker every time we want to dispatch an event to an extension |
| + // Service worker. |
| + // TODO(lazyboy): Is that a problem? |
| + return true; |
| +} |
| + |
| +void ServiceWorkerTaskQueue::AddPendingTaskToDispatchEvent( |
| + LazyContextId* context_id, |
| + const LazyContextTaskQueue::PendingTask& task) { |
| + DCHECK(context_id->is_for_service_worker()); |
| + content::StoragePartition* partition = |
| + BrowserContext::GetStoragePartitionForSite( |
| + context_id->browser_context(), context_id->service_worker_scope()); |
| + content::ServiceWorkerContext* service_worker_context = |
| + partition->GetServiceWorkerContext(); |
| + |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(&GetServiceWorkerInfoOnIO, context_id->service_worker_scope(), |
| + context_id->extension_id(), service_worker_context, task)); |
| +} |
| + |
| +} // namespace extensions |