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