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.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "content/child/background_sync/background_sync_type_converters.h" |
| 10 #include "content/child/service_worker/web_service_worker_registration_impl.h" |
| 11 #include "content/child/worker_task_runner.h" |
| 12 #include "content/public/common/service_registry.h" |
| 13 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncErro
r.h" |
| 14 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncRegi
stration.h" |
| 15 |
| 16 namespace content { |
| 17 namespace { |
| 18 |
| 19 // Returns the id of the given |service_worker_registration|, which |
| 20 // is only available on the implementation of the interface. |
| 21 int64 GetServiceWorkerRegistrationId( |
| 22 blink::WebServiceWorkerRegistration* service_worker_registration) { |
| 23 return static_cast<WebServiceWorkerRegistrationImpl*>( |
| 24 service_worker_registration)->registration_id(); |
| 25 } |
| 26 |
| 27 } // namespace |
| 28 |
| 29 BackgroundSyncProvider::BackgroundSyncProvider( |
| 30 ServiceRegistry* service_registry) |
| 31 : service_registry_(service_registry) { |
| 32 DCHECK(service_registry); |
| 33 } |
| 34 |
| 35 BackgroundSyncProvider::~BackgroundSyncProvider() { |
| 36 } |
| 37 |
| 38 void BackgroundSyncProvider::registerBackgroundSync( |
| 39 const blink::WebSyncRegistration* options, |
| 40 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 41 blink::WebSyncRegistrationCallbacks* callbacks) { |
| 42 DCHECK(options); |
| 43 DCHECK(service_worker_registration); |
| 44 DCHECK(callbacks); |
| 45 int64 service_worker_registration_id = |
| 46 GetServiceWorkerRegistrationId(service_worker_registration); |
| 47 |
| 48 GetBackgroundSyncServicePtr()->Register( |
| 49 mojo::ConvertTo<SyncRegistrationPtr>(*options), |
| 50 service_worker_registration_id, |
| 51 base::Bind(&BackgroundSyncProvider::RegisterCallback, |
| 52 base::Unretained(this),callbacks)); |
| 53 } |
| 54 |
| 55 void BackgroundSyncProvider::unregisterBackgroundSync( |
| 56 blink::WebSyncRegistration::Periodicity periodicity, |
| 57 int64_t id, const blink::WebString& tag, |
| 58 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 59 blink::WebSyncUnregistrationCallbacks* callbacks) { |
| 60 DCHECK(service_worker_registration); |
| 61 DCHECK(callbacks); |
| 62 int64 service_worker_registration_id = |
| 63 GetServiceWorkerRegistrationId(service_worker_registration); |
| 64 |
| 65 GetBackgroundSyncServicePtr()->Unregister( |
| 66 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), id, |
| 67 tag.utf8(), service_worker_registration_id, |
| 68 base::Bind(&BackgroundSyncProvider::UnregisterCallback, |
| 69 base::Unretained(this),callbacks)); |
| 70 } |
| 71 |
| 72 void BackgroundSyncProvider::getRegistration( |
| 73 blink::WebSyncRegistration::Periodicity periodicity, |
| 74 const blink::WebString& tag, |
| 75 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 76 blink::WebSyncRegistrationCallbacks* callbacks) { |
| 77 DCHECK(service_worker_registration); |
| 78 DCHECK(callbacks); |
| 79 int64 service_worker_registration_id = |
| 80 GetServiceWorkerRegistrationId(service_worker_registration); |
| 81 |
| 82 GetBackgroundSyncServicePtr()->GetRegistration( |
| 83 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), |
| 84 tag.utf8(), service_worker_registration_id, |
| 85 base::Bind(&BackgroundSyncProvider::RegisterCallback, |
| 86 base::Unretained(this),callbacks)); |
| 87 } |
| 88 |
| 89 void BackgroundSyncProvider::getRegistrations( |
| 90 blink::WebSyncRegistration::Periodicity periodicity, |
| 91 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 92 blink::WebSyncGetRegistrationsCallbacks* callbacks) { |
| 93 DCHECK(service_worker_registration); |
| 94 DCHECK(callbacks); |
| 95 int64 service_worker_registration_id = |
| 96 GetServiceWorkerRegistrationId(service_worker_registration); |
| 97 |
| 98 GetBackgroundSyncServicePtr()->GetRegistrations( |
| 99 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), |
| 100 service_worker_registration_id, |
| 101 base::Bind(&BackgroundSyncProvider::GetRegistrationsCallback, |
| 102 base::Unretained(this), |
| 103 callbacks)); |
| 104 } |
| 105 |
| 106 void BackgroundSyncProvider::RegisterCallback( |
| 107 blink::WebSyncRegistrationCallbacks* callbacks, |
| 108 const SyncRegistrationPtr &options) { |
| 109 // TODO(iclelland): Pass through the various errors from the manager to here |
| 110 // and handle them appropriately. |
| 111 scoped_ptr<blink::WebSyncRegistration> result; |
| 112 if (!options.is_null()) |
| 113 result = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options); |
| 114 |
| 115 callbacks->onSuccess(result.release()); |
| 116 } |
| 117 |
| 118 void BackgroundSyncProvider::UnregisterCallback( |
| 119 blink::WebSyncUnregistrationCallbacks *callbacks, |
| 120 bool success) { |
| 121 // TODO(iclelland): Pass through the various errors from the manager to here |
| 122 // and handle them appropriately. |
| 123 if (success) { |
| 124 callbacks->onSuccess(new bool(success)); |
| 125 } else { |
| 126 callbacks->onError(new blink::WebSyncError( |
| 127 blink::WebSyncError::ErrorTypeUnknown, |
| 128 "Sync registration does not exist")); |
| 129 } |
| 130 } |
| 131 |
| 132 void BackgroundSyncProvider::GetRegistrationsCallback( |
| 133 blink::WebSyncGetRegistrationsCallbacks* callbacks, |
| 134 const mojo::Array<SyncRegistrationPtr> ®istrations) { |
| 135 // TODO(iclelland): Pass through the various errors from the manager to here |
| 136 // and handle them appropriately. |
| 137 blink::WebVector<blink::WebSyncRegistration*>* results = |
| 138 new blink::WebVector<blink::WebSyncRegistration*>(registrations.size()); |
| 139 for (size_t i=0; i < registrations.size(); ++i) { |
| 140 (*results)[i] = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>( |
| 141 registrations[i]).release(); |
| 142 } |
| 143 callbacks->onSuccess(results); |
| 144 } |
| 145 |
| 146 BackgroundSyncServicePtr& |
| 147 BackgroundSyncProvider::GetBackgroundSyncServicePtr() { |
| 148 if (!background_sync_service_.get()) |
| 149 service_registry_->ConnectToRemoteService(&background_sync_service_); |
| 150 return background_sync_service_; |
| 151 } |
| 152 |
| 153 } // namespace content |
OLD | NEW |