OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/renderer/background_sync/background_sync_client_impl.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/location.h" | |
9 #include "base/memory/weak_ptr.h" | |
10 #include "content/child/worker_task_runner.h" | |
11 #include "content/renderer/service_worker/service_worker_context_client.h" | |
12 | |
13 namespace content { | |
14 | |
15 // static | |
16 void BackgroundSyncClientImpl::Create( | |
17 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner, | |
18 mojo::InterfaceRequest<BackgroundSyncServiceClient> request) { | |
19 new BackgroundSyncClientImpl(main_thread_task_runner, request.Pass()); | |
20 } | |
21 | |
22 BackgroundSyncClientImpl::~BackgroundSyncClientImpl() { | |
23 } | |
24 | |
25 BackgroundSyncClientImpl::BackgroundSyncClientImpl( | |
26 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner, | |
27 mojo::InterfaceRequest<BackgroundSyncServiceClient> request) | |
28 : binding_(this, request.Pass()), | |
29 main_thread_task_runner_(main_thread_task_runner), | |
30 weak_ptr_factory_(this) { | |
jkarlin
2015/06/12 21:08:15
DCHECK that we're on main_thread_task_runner's thr
iclelland
2015/06/16 15:59:16
Done.
| |
31 } | |
32 | |
33 void BackgroundSyncClientImpl::Sync(content::SyncRegistrationPtr registration, | |
34 int thread_id, | |
35 const SyncCallback& callback) { | |
jkarlin
2015/06/12 21:08:14
DCHECK that this is called on the main thread task
iclelland
2015/06/16 15:59:16
Done.
| |
36 WorkerTaskRunner* worker_task_runner = WorkerTaskRunner::Instance(); | |
37 if (!worker_task_runner) { | |
38 callback.Run(SERVICE_WORKER_EVENT_STATUS_ABORT); | |
39 return; | |
40 } | |
41 // TODO(iclelland): If the worker is dead, GetTaskRunnerFor(thread_id) will | |
42 // still return a task runner. Detect that state and call callback with an | |
43 // abort status. | |
44 worker_task_runner->GetTaskRunnerFor(thread_id)->PostTask( | |
45 FROM_HERE, base::Bind(&BackgroundSyncClientImpl::DispatchSyncForWorker, | |
46 base::Unretained(this), | |
jkarlin
2015/06/12 21:08:14
This base::Unretained makes me nervous. Can you gu
iclelland
2015/06/16 15:59:16
OnSyncComplete needs to have access to at least ma
| |
47 base::Passed(registration.Pass()), callback)); | |
48 } | |
49 | |
50 void BackgroundSyncClientImpl::DispatchSyncForWorker( | |
51 content::SyncRegistrationPtr registration, | |
52 const SyncCallback& callback) { | |
53 ServiceWorkerContextClient* client = | |
54 ServiceWorkerContextClient::ThreadSpecificInstance(); | |
55 if (!client) { | |
56 callback.Run(SERVICE_WORKER_EVENT_STATUS_ABORT); | |
jkarlin
2015/06/12 21:08:15
Isn't callback supposed to be run on the main thre
iclelland
2015/06/16 15:59:16
Yes. Fixed, thanks.
| |
57 return; | |
58 } | |
59 client->DispatchSyncEvent( | |
60 base::Bind(&BackgroundSyncClientImpl::OnSyncComplete, | |
61 weak_ptr_factory_.GetWeakPtr(), callback)); | |
jkarlin
2015/06/12 21:08:14
You can't use weak_ptr_factory_ here as you're on
iclelland
2015/06/16 15:59:16
Done.
| |
62 } | |
63 | |
64 void BackgroundSyncClientImpl::OnSyncComplete(const SyncCallback& callback, | |
65 ServiceWorkerEventStatus status) { | |
66 if (!main_thread_task_runner_->RunsTasksOnCurrentThread()) { | |
jkarlin
2015/06/12 21:08:15
This pattern is being phased out in Chromium. Bett
iclelland
2015/06/16 15:59:16
At this point, all I know is that I'm on *a* worke
| |
67 // Re-run on main thread | |
68 main_thread_task_runner_->PostTask( | |
69 FROM_HERE, base::Bind(&BackgroundSyncClientImpl::OnSyncComplete, | |
70 base::Unretained(this), callback, status)); | |
71 return; | |
72 } | |
73 callback.Run(status); | |
74 } | |
75 | |
76 } // namespace content | |
OLD | NEW |