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

Side by Side Diff: content/child/background_sync/background_sync_provider_thread_proxy.cc

Issue 1104283002: [Background Sync] Add renderer code to interface between JS API and back-end service (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bgsync-mojo
Patch Set: Better scoped_ptr construction (nit) Created 5 years, 7 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/child/background_sync/background_sync_provider_thread_proxy.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/lazy_instance.h"
10 #include "base/location.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/threading/thread_local.h"
14 #include "content/child/background_sync/background_sync_provider.h"
15
16 using base::LazyInstance;
17 using base::ThreadLocalPointer;
18
19 namespace content {
20
21 namespace {
22
23 // CallbackThreadAdapter<S,T> is a wrapper for WebCallbacks<S,T> which
24 // switches to a specific thread before calling the wrapped callback's
25 // onSuccess or onError methods.
26 //
27 // Takes ownership of the WebCallbacks object which it wraps.
28 template <typename S, typename T>
29 class CallbackThreadAdapter : public blink::WebCallbacks<S, T> {
30 public:
31 CallbackThreadAdapter(scoped_ptr<blink::WebCallbacks<S, T>> callbacks,
32 int worker_thread_id)
33 : worker_thread_id_(worker_thread_id) {
34 callbacks_.reset(callbacks.release());
35 }
36
37 virtual void onSuccess(S* results) {
38 // If the worker thread has been destroyed, then this task will be
39 // silently discarded.
40 WorkerTaskRunner::Instance()->PostTask(
41 worker_thread_id_,
42 base::Bind(&blink::WebCallbacks<S, T>::onSuccess,
43 base::Unretained(callbacks_.get()), results));
44 }
45
46 virtual void onError(T* error) {
47 // If the worker thread has been destroyed, then this task will be
48 // silently discarded.
49 WorkerTaskRunner::Instance()->PostTask(
50 worker_thread_id_,
51 base::Bind(&blink::WebCallbacks<S, T>::onError,
52 base::Unretained(callbacks_.get()), error));
53 }
54
55 private:
56 scoped_ptr<blink::WebCallbacks<S, T>> callbacks_;
57 int worker_thread_id_;
58 };
59
60 LazyInstance<ThreadLocalPointer<BackgroundSyncProviderThreadProxy>>::Leaky
61 g_sync_provider_tls = LAZY_INSTANCE_INITIALIZER;
62
63 } // anonymous namespace
64
65 BackgroundSyncProviderThreadProxy*
66 BackgroundSyncProviderThreadProxy::GetThreadInstance(
67 base::SingleThreadTaskRunner* main_thread_task_runner,
68 BackgroundSyncProvider* sync_provider) {
69 if (g_sync_provider_tls.Pointer()->Get())
70 return g_sync_provider_tls.Pointer()->Get();
71
72 BackgroundSyncProviderThreadProxy* instance =
73 new BackgroundSyncProviderThreadProxy(main_thread_task_runner,
74 sync_provider);
75 DCHECK(WorkerTaskRunner::Instance()->CurrentWorkerId());
76 WorkerTaskRunner::Instance()->AddStopObserver(instance);
77 return instance;
78 }
79
80 void BackgroundSyncProviderThreadProxy::registerBackgroundSync(
81 const blink::WebSyncRegistration* options,
82 blink::WebServiceWorkerRegistration* service_worker_registration,
83 blink::WebSyncRegistrationCallbacks* callbacks) {
84 DCHECK(options);
85 DCHECK(service_worker_registration);
86 DCHECK(callbacks);
87 main_thread_task_runner_->PostTask(
88 FROM_HERE,
89 base::Bind(&BackgroundSyncProvider::registerBackgroundSync,
90 base::Unretained(sync_provider_), options,
91 service_worker_registration,
92 new CallbackThreadAdapter<blink::WebSyncRegistration,
93 blink::WebSyncError>(
94 make_scoped_ptr(callbacks),
95 WorkerTaskRunner::Instance()->CurrentWorkerId())));
96 }
97
98 void BackgroundSyncProviderThreadProxy::unregisterBackgroundSync(
99 blink::WebSyncRegistration::Periodicity periodicity,
100 int64_t id,
101 const blink::WebString& tag,
102 blink::WebServiceWorkerRegistration* service_worker_registration,
103 blink::WebSyncUnregistrationCallbacks* callbacks) {
104 DCHECK(service_worker_registration);
105 DCHECK(callbacks);
106 main_thread_task_runner_->PostTask(
107 FROM_HERE,
108 base::Bind(&BackgroundSyncProvider::unregisterBackgroundSync,
109 base::Unretained(sync_provider_), periodicity, id, tag,
110 service_worker_registration,
111 new CallbackThreadAdapter<bool, blink::WebSyncError>(
112 make_scoped_ptr(callbacks),
113 WorkerTaskRunner::Instance()->CurrentWorkerId())));
114 }
115
116 void BackgroundSyncProviderThreadProxy::getRegistration(
117 blink::WebSyncRegistration::Periodicity periodicity,
118 const blink::WebString& tag,
119 blink::WebServiceWorkerRegistration* service_worker_registration,
120 blink::WebSyncRegistrationCallbacks* callbacks) {
121 DCHECK(service_worker_registration);
122 DCHECK(callbacks);
123 main_thread_task_runner_->PostTask(
124 FROM_HERE,
125 base::Bind(&BackgroundSyncProvider::getRegistration,
126 base::Unretained(sync_provider_), periodicity, tag,
127 service_worker_registration,
128 new CallbackThreadAdapter<blink::WebSyncRegistration,
129 blink::WebSyncError>(
130 make_scoped_ptr(callbacks),
131 WorkerTaskRunner::Instance()->CurrentWorkerId())));
132 }
133
134 void BackgroundSyncProviderThreadProxy::getRegistrations(
135 blink::WebSyncRegistration::Periodicity periodicity,
136 blink::WebServiceWorkerRegistration* service_worker_registration,
137 blink::WebSyncGetRegistrationsCallbacks* callbacks) {
138 DCHECK(service_worker_registration);
139 DCHECK(callbacks);
140 main_thread_task_runner_->PostTask(
141 FROM_HERE,
142 base::Bind(&BackgroundSyncProvider::getRegistrations,
143 base::Unretained(sync_provider_), periodicity,
144 service_worker_registration,
145 new CallbackThreadAdapter<
146 blink::WebVector<blink::WebSyncRegistration*>,
147 blink::WebSyncError>(
148 make_scoped_ptr(callbacks),
149 WorkerTaskRunner::Instance()->CurrentWorkerId())));
150 }
151
152 void BackgroundSyncProviderThreadProxy::OnWorkerRunLoopStopped() {
153 delete this;
154 }
155
156 BackgroundSyncProviderThreadProxy::BackgroundSyncProviderThreadProxy(
157 base::SingleThreadTaskRunner* main_thread_task_runner,
158 BackgroundSyncProvider* sync_provider)
159 : main_thread_task_runner_(main_thread_task_runner),
160 sync_provider_(sync_provider) {
161 g_sync_provider_tls.Pointer()->Set(this);
162 }
163
164 BackgroundSyncProviderThreadProxy::~BackgroundSyncProviderThreadProxy() {
165 g_sync_provider_tls.Pointer()->Set(nullptr);
166 }
167
168 } // namespace content
OLDNEW
« no previous file with comments | « content/child/background_sync/background_sync_provider_thread_proxy.h ('k') | content/child/blink_platform_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698