Remove INJECT_EVENTS permissions from test APKs.
[chromium-blink-merge.git] / content / child / background_sync / background_sync_provider_thread_proxy.cc
blob6e40365f50427c11537fd6791fca87f43d0181d1
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.
5 #include "content/child/background_sync/background_sync_provider_thread_proxy.h"
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"
16 using base::LazyInstance;
17 using base::ThreadLocalPointer;
19 namespace content {
21 namespace {
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.
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());
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::Owned(callbacks_.release()), results));
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::Owned(callbacks_.release()), error));
55 private:
56 scoped_ptr<blink::WebCallbacks<S, T>> callbacks_;
57 int worker_thread_id_;
60 LazyInstance<ThreadLocalPointer<BackgroundSyncProviderThreadProxy>>::Leaky
61 g_sync_provider_tls = LAZY_INSTANCE_INITIALIZER;
63 } // anonymous namespace
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();
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;
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())));
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())));
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())));
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())));
152 void BackgroundSyncProviderThreadProxy::getPermissionStatus(
153 blink::WebSyncRegistration::Periodicity periodicity,
154 blink::WebServiceWorkerRegistration* service_worker_registration,
155 blink::WebSyncGetPermissionStatusCallbacks* callbacks) {
156 DCHECK(service_worker_registration);
157 DCHECK(callbacks);
158 main_thread_task_runner_->PostTask(
159 FROM_HERE,
160 base::Bind(&BackgroundSyncProvider::getPermissionStatus,
161 base::Unretained(sync_provider_), periodicity,
162 service_worker_registration,
163 new CallbackThreadAdapter<blink::WebSyncPermissionStatus,
164 blink::WebSyncError>(
165 make_scoped_ptr(callbacks),
166 WorkerTaskRunner::Instance()->CurrentWorkerId())));
169 void BackgroundSyncProviderThreadProxy::OnWorkerRunLoopStopped() {
170 delete this;
173 BackgroundSyncProviderThreadProxy::BackgroundSyncProviderThreadProxy(
174 base::SingleThreadTaskRunner* main_thread_task_runner,
175 BackgroundSyncProvider* sync_provider)
176 : main_thread_task_runner_(main_thread_task_runner),
177 sync_provider_(sync_provider) {
178 g_sync_provider_tls.Pointer()->Set(this);
181 BackgroundSyncProviderThreadProxy::~BackgroundSyncProviderThreadProxy() {
182 g_sync_provider_tls.Pointer()->Set(nullptr);
185 } // namespace content