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

Side by Side Diff: content/renderer/background_sync/background_sync_client_impl.cc

Issue 1171173002: [Background Sync] Use Mojo IPC to fire background sync events (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixing style nits Created 5 years, 6 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
(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) {
31 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread());
32 }
33
34 void BackgroundSyncClientImpl::Sync(content::SyncRegistrationPtr registration,
35 int thread_id,
36 const SyncCallback& callback) {
37 WorkerTaskRunner* worker_task_runner = WorkerTaskRunner::Instance();
38 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread());
jkarlin 2015/06/18 18:13:56 DCHECK should be first line, ideally with a newlin
iclelland 2015/06/19 13:44:32 Done.
39 if (!worker_task_runner) {
40 callback.Run(SERVICE_WORKER_EVENT_STATUS_ABORTED);
41 return;
42 }
43 // TODO(iclelland): If the worker is dead, GetTaskRunnerFor(thread_id) will
44 // still return a task runner. Detect that state and call callback with an
45 // abort status.
jkarlin 2015/06/18 18:13:56 PostTask will return false on a deleted task runne
iclelland 2015/06/19 13:44:32 Done. Thanks.
46 worker_task_runner->GetTaskRunnerFor(thread_id)->PostTask(
47 FROM_HERE,
48 base::Bind(&BackgroundSyncClientImpl::DispatchSyncOnWorkerThread,
49 main_thread_task_runner_, base::Passed(registration.Pass()),
50 callback));
51 }
52
53 // static
54 void BackgroundSyncClientImpl::DispatchSyncOnWorkerThread(
55 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner,
56 content::SyncRegistrationPtr registration,
57 const SyncCallback& callback) {
58 ServiceWorkerContextClient* client =
59 ServiceWorkerContextClient::ThreadSpecificInstance();
60 if (!client) {
61 OnSyncCompleteOnWorkerThread(main_thread_task_runner, callback,
62 SERVICE_WORKER_EVENT_STATUS_ABORTED);
63 return;
64 }
65 client->DispatchSyncEvent(
66 base::Bind(&BackgroundSyncClientImpl::OnSyncCompleteOnWorkerThread,
67 main_thread_task_runner, callback));
68 }
69
70 // static
71 void BackgroundSyncClientImpl::OnSyncCompleteOnWorkerThread(
72 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner,
73 const SyncCallback& callback,
74 ServiceWorkerEventStatus status) {
75 // Run callback on main thread
76 main_thread_task_runner->PostTask(
77 FROM_HERE,
78 base::Bind(&BackgroundSyncClientImpl::OnSyncCompleteOnMainThread,
jkarlin 2015/06/18 18:13:57 base::Bind(callback, status) and then delete OnSyn
iclelland 2015/06/19 13:44:32 This doesn't appear to be possible; callback is a
79 callback, status));
80 }
81
82 // static
83 void BackgroundSyncClientImpl::OnSyncCompleteOnMainThread(
84 const SyncCallback& callback,
85 ServiceWorkerEventStatus status) {
86 callback.Run(status);
87 }
88
89 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698