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/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()), | |
44 results)); | |
45 } | |
46 | |
47 virtual void onError(T* error) { | |
48 // If the worker thread has been destroyed, then this task will be | |
49 // silently discarded. | |
50 WorkerTaskRunner::Instance()->PostTask( | |
51 worker_thread_id_, | |
52 base::Bind(&blink::WebCallbacks<S,T>::onError, | |
53 base::Unretained(callbacks_.get()), | |
54 error)); | |
55 } | |
56 | |
57 private: | |
58 scoped_ptr<blink::WebCallbacks<S,T>> callbacks_; | |
59 int worker_thread_id_; | |
60 }; | |
61 | |
62 LazyInstance<ThreadLocalPointer<BackgroundSyncProviderThreadProxy>>::Leaky | |
63 g_sync_provider_tls = LAZY_INSTANCE_INITIALIZER; | |
64 | |
65 } // anonymous namespace | |
66 | |
67 BackgroundSyncProviderThreadProxy* | |
68 BackgroundSyncProviderThreadProxy::GetThreadInstance( | |
69 base::SingleThreadTaskRunner* main_thread_task_runner, | |
70 BackgroundSyncProvider* sync_provider) { | |
71 if (g_sync_provider_tls.Pointer()->Get()) | |
72 return g_sync_provider_tls.Pointer()->Get(); | |
73 | |
74 BackgroundSyncProviderThreadProxy* instance = | |
75 new BackgroundSyncProviderThreadProxy(main_thread_task_runner, | |
76 sync_provider); | |
77 DCHECK(WorkerTaskRunner::Instance()->CurrentWorkerId()); | |
78 WorkerTaskRunner::Instance()->AddStopObserver(instance); | |
79 return instance; | |
80 } | |
81 | |
82 void BackgroundSyncProviderThreadProxy::registerBackgroundSync( | |
83 const blink::WebSyncRegistration* options, | |
84 blink::WebServiceWorkerRegistration* service_worker_registration, | |
85 blink::WebSyncRegistrationCallbacks* callbacks) { | |
86 DCHECK(options); | |
87 DCHECK(service_worker_registration); | |
88 DCHECK(callbacks); | |
89 main_thread_task_runner_->PostTask( | |
90 FROM_HERE, | |
91 base::Bind(&BackgroundSyncProvider::registerBackgroundSync, | |
92 base::Unretained(sync_provider_), | |
93 options, | |
94 service_worker_registration, | |
95 // TODO(iclelland): Ensure this gets deleted, and doesn't leak | |
jkarlin
2015/05/12 13:19:51
Ah, looks like you weren't quite finished. Anyway,
iclelland
2015/05/12 14:40:05
Done.
| |
96 new CallbackThreadAdapter<blink::WebSyncRegistration, | |
97 blink::WebSyncError>( | |
98 make_scoped_ptr(callbacks), | |
99 WorkerTaskRunner::Instance()->CurrentWorkerId()))); | |
100 } | |
101 | |
102 void BackgroundSyncProviderThreadProxy::unregisterBackgroundSync( | |
103 blink::WebSyncRegistration::Periodicity periodicity, | |
104 int64_t id, | |
105 const blink::WebString& tag, | |
106 blink::WebServiceWorkerRegistration* service_worker_registration, | |
107 blink::WebSyncUnregistrationCallbacks* callbacks) { | |
108 DCHECK(service_worker_registration); | |
109 DCHECK(callbacks); | |
110 main_thread_task_runner_->PostTask( | |
111 FROM_HERE, | |
112 base::Bind(&BackgroundSyncProvider::unregisterBackgroundSync, | |
113 base::Unretained(sync_provider_), | |
114 periodicity, id, tag, service_worker_registration, | |
115 // TODO(iclelland): Ensure this gets deleted, and doesn't leak | |
116 new CallbackThreadAdapter<bool, blink::WebSyncError>( | |
117 make_scoped_ptr(callbacks), | |
118 WorkerTaskRunner::Instance()->CurrentWorkerId()))); | |
119 } | |
120 | |
121 void BackgroundSyncProviderThreadProxy::getRegistration( | |
122 blink::WebSyncRegistration::Periodicity periodicity, | |
123 const blink::WebString& tag, | |
124 blink::WebServiceWorkerRegistration* service_worker_registration, | |
125 blink::WebSyncRegistrationCallbacks* callbacks) { | |
126 DCHECK(service_worker_registration); | |
127 DCHECK(callbacks); | |
128 main_thread_task_runner_->PostTask( | |
129 FROM_HERE, | |
130 base::Bind(&BackgroundSyncProvider::getRegistration, | |
131 base::Unretained(sync_provider_), | |
132 periodicity, tag, service_worker_registration, | |
133 // TODO(iclelland): Ensure this gets deleted, and doesn't leak | |
134 new CallbackThreadAdapter<blink::WebSyncRegistration, | |
135 blink::WebSyncError>( | |
136 make_scoped_ptr(callbacks), | |
137 WorkerTaskRunner::Instance()->CurrentWorkerId()))); | |
138 } | |
139 | |
140 void BackgroundSyncProviderThreadProxy::getRegistrations( | |
141 blink::WebSyncRegistration::Periodicity periodicity, | |
142 blink::WebServiceWorkerRegistration* service_worker_registration, | |
143 blink::WebSyncGetRegistrationsCallbacks* callbacks) { | |
144 DCHECK(service_worker_registration); | |
145 DCHECK(callbacks); | |
146 main_thread_task_runner_->PostTask( | |
147 FROM_HERE, | |
148 base::Bind(&BackgroundSyncProvider::getRegistrations, | |
149 base::Unretained(sync_provider_), | |
150 periodicity, | |
151 service_worker_registration, | |
152 // TODO(iclelland): Ensure this gets deleted, and doesn't leak | |
153 new CallbackThreadAdapter< | |
154 blink::WebVector<blink::WebSyncRegistration*>, | |
155 blink::WebSyncError>( | |
156 make_scoped_ptr(callbacks), | |
157 WorkerTaskRunner::Instance()->CurrentWorkerId()))); | |
158 } | |
159 | |
160 void BackgroundSyncProviderThreadProxy::OnWorkerRunLoopStopped() { | |
161 delete this; | |
162 } | |
163 | |
164 BackgroundSyncProviderThreadProxy::BackgroundSyncProviderThreadProxy( | |
165 base::SingleThreadTaskRunner* main_thread_task_runner, | |
166 BackgroundSyncProvider* sync_provider) | |
167 : main_thread_task_runner_(main_thread_task_runner), | |
168 sync_provider_(sync_provider) { | |
169 g_sync_provider_tls.Pointer()->Set(this); | |
170 } | |
171 | |
172 BackgroundSyncProviderThreadProxy::~BackgroundSyncProviderThreadProxy() { | |
173 g_sync_provider_tls.Pointer()->Set(nullptr); | |
174 } | |
175 | |
176 } // namespace content | |
OLD | NEW |