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/lazy_instance.h" |
| 8 #include "base/location.h" |
| 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/threading/thread_local.h" |
| 11 #include "content/child/background_sync/background_sync_provider.h" |
| 12 |
| 13 using base::LazyInstance; |
| 14 using base::ThreadLocalPointer; |
| 15 |
| 16 namespace content { |
| 17 |
| 18 namespace { |
| 19 |
| 20 LazyInstance<ThreadLocalPointer<BackgroundSyncProviderThreadProxy>>::Leaky |
| 21 g_sync_provider_tls = LAZY_INSTANCE_INITIALIZER; |
| 22 |
| 23 } // anonymous namespace |
| 24 |
| 25 BackgroundSyncProviderThreadProxy* |
| 26 BackgroundSyncProviderThreadProxy::GetThreadInstance( |
| 27 base::SingleThreadTaskRunner* main_thread_task_runner, |
| 28 BackgroundSyncProvider* sync_provider) { |
| 29 if (g_sync_provider_tls.Pointer()->Get()) |
| 30 return g_sync_provider_tls.Pointer()->Get(); |
| 31 |
| 32 BackgroundSyncProviderThreadProxy* instance = |
| 33 new BackgroundSyncProviderThreadProxy(main_thread_task_runner, |
| 34 sync_provider); |
| 35 DCHECK(WorkerTaskRunner::Instance()->CurrentWorkerId()); |
| 36 WorkerTaskRunner::Instance()->AddStopObserver(instance); |
| 37 return instance; |
| 38 } |
| 39 |
| 40 void BackgroundSyncProviderThreadProxy::registerBackgroundSync( |
| 41 const blink::WebSyncRegistration* options, |
| 42 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 43 blink::WebSyncRegistrationCallbacks* callbacks) { |
| 44 DCHECK(options); |
| 45 DCHECK(service_worker_registration); |
| 46 DCHECK(callbacks); |
| 47 main_thread_task_runner_->PostTask( |
| 48 FROM_HERE, |
| 49 base::Bind(&BackgroundSyncProvider::registerBackgroundSync, |
| 50 base::Unretained(sync_provider_), |
| 51 options, |
| 52 service_worker_registration, |
| 53 // TODO(iclelland): Ensure this gets deleted, and doesn't leak |
| 54 new CallbackThreadAdapter<blink::WebSyncRegistration, |
| 55 blink::WebSyncError>( |
| 56 make_scoped_ptr(callbacks), |
| 57 WorkerTaskRunner::Instance()->CurrentWorkerId()))); |
| 58 } |
| 59 |
| 60 void BackgroundSyncProviderThreadProxy::unregisterBackgroundSync( |
| 61 blink::WebSyncRegistration::Periodicity periodicity, |
| 62 int64_t id, |
| 63 const blink::WebString& tag, |
| 64 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 65 blink::WebSyncUnregistrationCallbacks* callbacks) { |
| 66 DCHECK(service_worker_registration); |
| 67 DCHECK(callbacks); |
| 68 main_thread_task_runner_->PostTask( |
| 69 FROM_HERE, |
| 70 base::Bind(&BackgroundSyncProvider::unregisterBackgroundSync, |
| 71 base::Unretained(sync_provider_), |
| 72 periodicity, id, tag, service_worker_registration, |
| 73 // TODO(iclelland): Ensure this gets deleted, and doesn't leak |
| 74 new CallbackThreadAdapter<bool, blink::WebSyncError>( |
| 75 make_scoped_ptr(callbacks), |
| 76 WorkerTaskRunner::Instance()->CurrentWorkerId()))); |
| 77 } |
| 78 |
| 79 void BackgroundSyncProviderThreadProxy::getRegistration( |
| 80 blink::WebSyncRegistration::Periodicity periodicity, |
| 81 const blink::WebString& tag, |
| 82 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 83 blink::WebSyncRegistrationCallbacks* callbacks) { |
| 84 DCHECK(service_worker_registration); |
| 85 DCHECK(callbacks); |
| 86 main_thread_task_runner_->PostTask( |
| 87 FROM_HERE, |
| 88 base::Bind(&BackgroundSyncProvider::getRegistration, |
| 89 base::Unretained(sync_provider_), |
| 90 periodicity, tag, service_worker_registration, |
| 91 // TODO(iclelland): Ensure this gets deleted, and doesn't leak |
| 92 new CallbackThreadAdapter<blink::WebSyncRegistration, |
| 93 blink::WebSyncError>( |
| 94 make_scoped_ptr(callbacks), |
| 95 WorkerTaskRunner::Instance()->CurrentWorkerId()))); |
| 96 } |
| 97 |
| 98 void BackgroundSyncProviderThreadProxy::getRegistrations( |
| 99 blink::WebSyncRegistration::Periodicity periodicity, |
| 100 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 101 blink::WebSyncGetRegistrationsCallbacks* callbacks) { |
| 102 DCHECK(service_worker_registration); |
| 103 DCHECK(callbacks); |
| 104 main_thread_task_runner_->PostTask( |
| 105 FROM_HERE, |
| 106 base::Bind(&BackgroundSyncProvider::getRegistrations, |
| 107 base::Unretained(sync_provider_), |
| 108 periodicity, |
| 109 service_worker_registration, |
| 110 // TODO(iclelland): Ensure this gets deleted, and doesn't leak |
| 111 new CallbackThreadAdapter< |
| 112 blink::WebVector<blink::WebSyncRegistration*>, |
| 113 blink::WebSyncError>( |
| 114 make_scoped_ptr(callbacks), |
| 115 WorkerTaskRunner::Instance()->CurrentWorkerId()))); |
| 116 } |
| 117 |
| 118 void BackgroundSyncProviderThreadProxy::OnWorkerRunLoopStopped() { |
| 119 delete this; |
| 120 } |
| 121 |
| 122 BackgroundSyncProviderThreadProxy::BackgroundSyncProviderThreadProxy( |
| 123 base::SingleThreadTaskRunner* main_thread_task_runner, |
| 124 BackgroundSyncProvider* sync_provider) |
| 125 : main_thread_task_runner_(main_thread_task_runner), |
| 126 sync_provider_(sync_provider) { |
| 127 g_sync_provider_tls.Pointer()->Set(this); |
| 128 } |
| 129 |
| 130 BackgroundSyncProviderThreadProxy::~BackgroundSyncProviderThreadProxy() { |
| 131 g_sync_provider_tls.Pointer()->Set(nullptr); |
| 132 } |
| 133 |
| 134 } // namespace content |
OLD | NEW |