OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "extensions/browser/service_worker_task_queue.h" | |
6 | |
7 #include "content/public/browser/browser_context.h" | |
8 #include "content/public/browser/browser_thread.h" | |
9 #include "content/public/browser/render_process_host.h" | |
10 #include "content/public/browser/service_worker_context.h" | |
11 #include "content/public/browser/storage_partition.h" | |
12 #include "extensions/browser/event_router.h" | |
13 #include "extensions/browser/lazy_context_id.h" | |
14 #include "extensions/browser/service_worker_task_queue_factory.h" | |
15 #include "extensions/common/constants.h" | |
16 | |
17 using content::BrowserContext; | |
18 using content::BrowserThread; | |
19 | |
20 namespace extensions { | |
21 | |
22 namespace { | |
23 | |
24 void FinishTask(const LazyContextTaskQueue::PendingTask& task, | |
25 const ExtensionId& extension_id, | |
26 int process_id, | |
27 int thread_id) { | |
28 auto params = base::MakeUnique<LazyContextTaskQueue::ContextInfo>( | |
29 extension_id, content::RenderProcessHost::FromID(process_id), thread_id, | |
30 GURL()); | |
31 task.Run(std::move(params)); | |
32 } | |
33 | |
34 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.
| |
35 const ExtensionId& extension_id, | |
36 int process_id, | |
37 int thread_id) { | |
38 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
39 content::BrowserThread::PostTask( | |
40 content::BrowserThread::UI, FROM_HERE, | |
41 base::Bind(FinishTask, task, extension_id, process_id, thread_id)); | |
42 } | |
43 | |
44 void GetWorkerInfoFailed() { | |
45 DCHECK(false) << "GetWorkerInfoFailed"; | |
46 // TODO(lazyboy): Handle failure case. | |
47 } | |
48 | |
49 void GetServiceWorkerInfoOnIO( | |
50 const GURL& origin, | |
51 const ExtensionId& extension_id, | |
52 content::ServiceWorkerContext* service_worker_context, | |
53 const LazyContextTaskQueue::PendingTask& task) { | |
54 service_worker_context->GetWorkerInfoAfterStartWorker( | |
55 origin, base::BindOnce(&GotWorkerInfoOnIO, task, extension_id), | |
56 base::BindOnce(&GetWorkerInfoFailed)); | |
57 } | |
58 | |
59 } // namespace | |
60 | |
61 ServiceWorkerTaskQueue::ServiceWorkerTaskQueue( | |
62 content::BrowserContext* browser_context) {} | |
63 | |
64 ServiceWorkerTaskQueue::~ServiceWorkerTaskQueue() {} | |
65 | |
66 // static | |
67 ServiceWorkerTaskQueue* ServiceWorkerTaskQueue::Get( | |
68 content::BrowserContext* context) { | |
69 return ServiceWorkerTaskQueueFactory::GetForBrowserContext(context); | |
70 } | |
71 | |
72 bool ServiceWorkerTaskQueue::ShouldEnqueueTask(content::BrowserContext* context, | |
73 const Extension* extension) { | |
74 // We call StartWorker every time we want to dispatch an event to an extension | |
75 // Service worker. | |
76 // TODO(lazyboy): Is that a problem? | |
77 return true; | |
78 } | |
79 | |
80 void ServiceWorkerTaskQueue::AddPendingTaskToDispatchEvent( | |
81 LazyContextId* context_id, | |
82 const LazyContextTaskQueue::PendingTask& task) { | |
83 DCHECK(context_id->is_for_service_worker()); | |
84 content::StoragePartition* partition = | |
85 BrowserContext::GetStoragePartitionForSite( | |
86 context_id->browser_context(), context_id->service_worker_scope()); | |
87 content::ServiceWorkerContext* service_worker_context = | |
88 partition->GetServiceWorkerContext(); | |
89 | |
90 BrowserThread::PostTask( | |
91 BrowserThread::IO, FROM_HERE, | |
92 base::Bind(&GetServiceWorkerInfoOnIO, context_id->service_worker_scope(), | |
93 context_id->extension_id(), service_worker_context, task)); | |
94 } | |
95 | |
96 } // namespace extensions | |
OLD | NEW |