Chromium Code Reviews| Index: content/child/background_sync/background_sync_provider.cc |
| diff --git a/content/child/background_sync/background_sync_provider.cc b/content/child/background_sync/background_sync_provider.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..570e236346fbf5fcd421dfcc8696ea26026a53be |
| --- /dev/null |
| +++ b/content/child/background_sync/background_sync_provider.cc |
| @@ -0,0 +1,233 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/child/background_sync/background_sync_provider.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "content/child/background_sync/background_sync_type_converters.h" |
| +#include "content/child/service_worker/web_service_worker_registration_impl.h" |
| +#include "content/child/worker_task_runner.h" |
| +#include "content/public/common/service_registry.h" |
| +#include "third_party/WebKit/public/platform/modules/background_sync/WebSyncError.h" |
| +#include "third_party/WebKit/public/platform/modules/background_sync/WebSyncRegistration.h" |
| + |
| +namespace content { |
| +namespace { |
| + |
| +// Returns the id of the given |service_worker_registration|, which |
| +// is only available on the implementation of the interface. |
| +int64 GetServiceWorkerRegistrationId( |
| + blink::WebServiceWorkerRegistration* service_worker_registration) { |
| + return static_cast<WebServiceWorkerRegistrationImpl*>( |
| + service_worker_registration)->registration_id(); |
| +} |
| + |
| +const int kNoWorkerThread = 0; |
| + |
| +} // namespace |
| + |
| +BackgroundSyncProvider::BackgroundSyncProvider( |
| + ServiceRegistry* service_registry) |
| + : service_registry_(service_registry) { |
|
jkarlin
2015/05/01 16:13:51
DCHECK(service_registry_)?
iclelland
2015/05/05 14:52:01
Done.
|
| +} |
| + |
| +BackgroundSyncProvider::~BackgroundSyncProvider() { |
| +} |
| + |
| +void BackgroundSyncProvider::registerBackgroundSync( |
| + const blink::WebSyncRegistration* options, |
| + blink::WebServiceWorkerRegistration* service_worker_registration, |
| + blink::WebSyncRegistrationCallbacks* callbacks) { |
| + DCHECK(options); |
| + DCHECK(service_worker_registration); |
| + DCHECK(callbacks); |
| + RegisterBackgroundSyncForWorker(options, service_worker_registration, |
| + callbacks, kNoWorkerThread); |
| +} |
| + |
| +void BackgroundSyncProvider::unregisterBackgroundSync( |
| + blink::WebSyncRegistration::Periodicity periodicity, |
| + int64_t id, const blink::WebString& tag, |
| + blink::WebServiceWorkerRegistration* service_worker_registration, |
| + blink::WebSyncUnregistrationCallbacks* callbacks) { |
| + DCHECK(service_worker_registration); |
| + DCHECK(callbacks); |
| + UnregisterBackgroundSyncForWorker(periodicity, id, tag, |
| + service_worker_registration, callbacks, |
| + kNoWorkerThread); |
| +} |
| + |
| +void BackgroundSyncProvider::getRegistration( |
| + blink::WebSyncRegistration::Periodicity periodicity, |
| + const blink::WebString& tag, |
| + blink::WebServiceWorkerRegistration* service_worker_registration, |
| + blink::WebSyncRegistrationCallbacks* callbacks) { |
| + DCHECK(service_worker_registration); |
| + DCHECK(callbacks); |
| + GetRegistrationForWorker(periodicity, tag, service_worker_registration, |
| + callbacks, kNoWorkerThread); |
| +} |
| + |
| +void BackgroundSyncProvider::getRegistrations( |
| + blink::WebSyncRegistration::Periodicity periodicity, |
| + blink::WebServiceWorkerRegistration* service_worker_registration, |
| + blink::WebSyncGetRegistrationsCallbacks* callbacks) { |
| + DCHECK(service_worker_registration); |
| + DCHECK(callbacks); |
| + GetRegistrationsForWorker(periodicity, service_worker_registration, callbacks, |
| + kNoWorkerThread); |
| +} |
| + |
| +void BackgroundSyncProvider::OnWorkerRunLoopStopped() { |
| + delete this; |
|
jkarlin
2015/05/01 16:13:51
This object is owned by BlinkPlatformImpl right? S
iclelland
2015/05/07 06:03:50
Right. I've removed this method, as it was part of
|
| +} |
| + |
| +void BackgroundSyncProvider::RegisterBackgroundSyncForWorker( |
| + const blink::WebSyncRegistration* options, |
| + blink::WebServiceWorkerRegistration* service_worker_registration, |
| + blink::WebSyncRegistrationCallbacks* callbacks, |
| + int worker_thread_id) { |
| + int64 service_worker_registration_id = |
| + GetServiceWorkerRegistrationId(service_worker_registration); |
| + |
| + GetBackgroundSyncServicePtr()->Register( |
| + mojo::ConvertTo<SyncRegistrationPtr>(*options), |
| + service_worker_registration_id, |
| + base::Bind(&BackgroundSyncProvider::registerCallback, |
| + base::Unretained(this),callbacks, |
| + worker_thread_id)); |
| + delete options; |
| +} |
| + |
| +void BackgroundSyncProvider::UnregisterBackgroundSyncForWorker( |
| + blink::WebSyncRegistration::Periodicity periodicity, |
| + int64_t id, |
| + const blink::WebString& tag, |
| + blink::WebServiceWorkerRegistration* service_worker_registration, |
| + blink::WebSyncUnregistrationCallbacks* callbacks, |
| + int worker_thread_id) { |
| + int64 service_worker_registration_id = |
| + GetServiceWorkerRegistrationId(service_worker_registration); |
| + |
| + GetBackgroundSyncServicePtr()->Unregister( |
| + mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), id, |
| + tag.utf8(), service_worker_registration_id, |
| + base::Bind(&BackgroundSyncProvider::unregisterCallback, |
| + base::Unretained(this),callbacks, |
| + worker_thread_id)); |
| +} |
| + |
| +void BackgroundSyncProvider::GetRegistrationForWorker( |
| + blink::WebSyncRegistration::Periodicity periodicity, |
| + const blink::WebString& tag, |
| + blink::WebServiceWorkerRegistration* service_worker_registration, |
| + blink::WebSyncRegistrationCallbacks* callbacks, |
| + int worker_thread_id) { |
| + int64 service_worker_registration_id = |
| + GetServiceWorkerRegistrationId(service_worker_registration); |
| + |
| + GetBackgroundSyncServicePtr()->GetRegistration( |
| + mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), |
| + tag.utf8(), service_worker_registration_id, |
| + base::Bind(&BackgroundSyncProvider::registerCallback, |
| + base::Unretained(this),callbacks, |
| + worker_thread_id)); |
| +} |
| + |
| +void BackgroundSyncProvider::GetRegistrationsForWorker( |
| + blink::WebSyncRegistration::Periodicity periodicity, |
| + blink::WebServiceWorkerRegistration* service_worker_registration, |
| + blink::WebSyncGetRegistrationsCallbacks* callbacks, |
| + int worker_thread_id) { |
| + int64 service_worker_registration_id = |
| + GetServiceWorkerRegistrationId(service_worker_registration); |
| + |
| + GetBackgroundSyncServicePtr()->GetRegistrations( |
| + mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), |
| + service_worker_registration_id, |
| + base::Bind(&BackgroundSyncProvider::getRegistrationsCallback, |
| + base::Unretained(this), |
| + callbacks, |
| + worker_thread_id)); |
| +} |
| + |
| +void BackgroundSyncProvider::registerCallback( |
| + blink::WebSyncRegistrationCallbacks* callbacks, |
| + int worker_thread_id, |
| + const SyncRegistrationPtr &options) { |
| + scoped_ptr<blink::WebSyncRegistration> result; |
| + if (!options.is_null()) |
| + result = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options); |
| + |
| + if (worker_thread_id == kNoWorkerThread) { |
| + callbacks->onSuccess(result.release()); |
| + } else { |
| + WorkerTaskRunner::Instance()->PostTask( |
| + worker_thread_id, |
| + base::Bind(&blink::WebSyncRegistrationCallbacks::onSuccess, |
| + base::Unretained(callbacks), |
| + result.release())); |
| + } |
| +} |
| + |
| +void BackgroundSyncProvider::unregisterCallback( |
| + blink::WebSyncUnregistrationCallbacks *callbacks, int worker_thread_id, |
| + bool success) { |
| + if (worker_thread_id == kNoWorkerThread) { |
| + if (success) { |
| + callbacks->onSuccess(new bool(success)); |
|
jkarlin
2015/05/01 16:13:51
Why does onSuccess take a bool* instead of just a
iclelland
2015/05/05 14:52:01
The template for WebCallbacks makes pointers out o
|
| + } else { |
| + callbacks->onError(new blink::WebSyncError( |
| + blink::WebSyncError::ErrorTypeUnknown, |
| + "Sync registration does not exist")); |
|
jkarlin
2015/05/01 16:13:51
Please create a bug (and a TODO) to handle the var
iclelland
2015/05/05 14:52:01
Done. crbug.com/484306
|
| + } |
| + } else { |
| + if (success) { |
| + WorkerTaskRunner::Instance()->PostTask( |
|
jkarlin
2015/05/01 16:13:51
Add a comment that it's safe to call PostTask() on
iclelland
2015/05/05 14:52:01
Done.
|
| + worker_thread_id, |
| + base::Bind(&blink::WebSyncUnregistrationCallbacks::onSuccess, |
| + base::Unretained(callbacks), |
| + new bool(success))); |
| + } else { |
| + WorkerTaskRunner::Instance()->PostTask( |
| + worker_thread_id, |
| + base::Bind(&blink::WebSyncUnregistrationCallbacks::onError, |
| + base::Unretained(callbacks), |
| + new blink::WebSyncError( |
| + blink::WebSyncError::ErrorTypeUnknown, |
| + "Sync registration does not exist"))); |
| + } |
| + } |
| +} |
| + |
| +void BackgroundSyncProvider::getRegistrationsCallback( |
| + blink::WebSyncGetRegistrationsCallbacks* callbacks, |
| + int worker_thread_id, |
| + const mojo::Array<SyncRegistrationPtr> ®istrations) { |
| + blink::WebVector<blink::WebSyncRegistration*>* results = |
| + new blink::WebVector<blink::WebSyncRegistration*>(registrations.size()); |
| + for (size_t i=0; i < registrations.size(); ++i) { |
| + (*results)[i] = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>( |
| + registrations[i]).release(); |
| + } |
| + if (worker_thread_id == kNoWorkerThread) { |
| + callbacks->onSuccess(results); |
| + } else { |
| + WorkerTaskRunner::Instance()->PostTask( |
| + worker_thread_id, |
| + base::Bind(&blink::WebSyncGetRegistrationsCallbacks::onSuccess, |
| + base::Unretained(callbacks), |
| + results)); |
| + } |
| +} |
| + |
| +BackgroundSyncServicePtr& |
| + BackgroundSyncProvider::GetBackgroundSyncServicePtr() { |
| + if (!background_sync_service_.get()) |
| + service_registry_->ConnectToRemoteService(&background_sync_service_); |
| + return background_sync_service_; |
| +} |
| + |
| +} // namespace content |