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 scoped_ptr<const blink::WebSyncRegistration> optionsPtr(options); |
| 48 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacksPtr(callbacks); |
| 49 |
| 50 // base::Unretained is safe here, as the mojo channel will be deleted (and |
| 51 // will wipe its callbacks) before 'this' is deleted. |
| 52 GetBackgroundSyncServicePtr()->Register( |
| 53 mojo::ConvertTo<SyncRegistrationPtr>(*(optionsPtr.get())), |
| 54 service_worker_registration_id, |
| 55 base::Bind(&BackgroundSyncProvider::RegisterCallback, |
| 56 base::Unretained(this), base::Passed(callbacksPtr.Pass()))); |
| 57 } |
| 58 |
| 59 void BackgroundSyncProvider::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 int64 service_worker_registration_id = |
| 68 GetServiceWorkerRegistrationId(service_worker_registration); |
| 69 scoped_ptr<blink::WebSyncUnregistrationCallbacks> callbacksPtr(callbacks); |
| 70 |
| 71 // base::Unretained is safe here, as the mojo channel will be deleted (and |
| 72 // will wipe its callbacks) before 'this' is deleted. |
| 73 GetBackgroundSyncServicePtr()->Unregister( |
| 74 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), id, tag.utf8(), |
| 75 service_worker_registration_id, |
| 76 base::Bind(&BackgroundSyncProvider::UnregisterCallback, |
| 77 base::Unretained(this), base::Passed(callbacksPtr.Pass()))); |
| 78 } |
| 79 |
| 80 void BackgroundSyncProvider::getRegistration( |
| 81 blink::WebSyncRegistration::Periodicity periodicity, |
| 82 const blink::WebString& tag, |
| 83 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 84 blink::WebSyncRegistrationCallbacks* callbacks) { |
| 85 DCHECK(service_worker_registration); |
| 86 DCHECK(callbacks); |
| 87 int64 service_worker_registration_id = |
| 88 GetServiceWorkerRegistrationId(service_worker_registration); |
| 89 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacksPtr(callbacks); |
| 90 |
| 91 // base::Unretained is safe here, as the mojo channel will be deleted (and |
| 92 // will wipe its callbacks) before 'this' is deleted. |
| 93 GetBackgroundSyncServicePtr()->GetRegistration( |
| 94 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), tag.utf8(), |
| 95 service_worker_registration_id, |
| 96 base::Bind(&BackgroundSyncProvider::RegisterCallback, |
| 97 base::Unretained(this), base::Passed(callbacksPtr.Pass()))); |
| 98 } |
| 99 |
| 100 void BackgroundSyncProvider::getRegistrations( |
| 101 blink::WebSyncRegistration::Periodicity periodicity, |
| 102 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 103 blink::WebSyncGetRegistrationsCallbacks* callbacks) { |
| 104 DCHECK(service_worker_registration); |
| 105 DCHECK(callbacks); |
| 106 int64 service_worker_registration_id = |
| 107 GetServiceWorkerRegistrationId(service_worker_registration); |
| 108 scoped_ptr<blink::WebSyncGetRegistrationsCallbacks> callbacksPtr(callbacks); |
| 109 |
| 110 // base::Unretained is safe here, as the mojo channel will be deleted (and |
| 111 // will wipe its callbacks) before 'this' is deleted. |
| 112 GetBackgroundSyncServicePtr()->GetRegistrations( |
| 113 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), |
| 114 service_worker_registration_id, |
| 115 base::Bind(&BackgroundSyncProvider::GetRegistrationsCallback, |
| 116 base::Unretained(this), base::Passed(callbacksPtr.Pass()))); |
| 117 } |
| 118 |
| 119 void BackgroundSyncProvider::RegisterCallback( |
| 120 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks, |
| 121 const SyncRegistrationPtr& options) { |
| 122 // TODO(iclelland): Pass through the various errors from the manager to here |
| 123 // and handle them appropriately. |
| 124 scoped_ptr<blink::WebSyncRegistration> result; |
| 125 if (!options.is_null()) |
| 126 result = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options); |
| 127 |
| 128 callbacks->onSuccess(result.release()); |
| 129 } |
| 130 |
| 131 void BackgroundSyncProvider::UnregisterCallback( |
| 132 scoped_ptr<blink::WebSyncUnregistrationCallbacks> callbacks, |
| 133 bool success) { |
| 134 // TODO(iclelland): Pass through the various errors from the manager to here |
| 135 // and handle them appropriately. |
| 136 if (success) { |
| 137 callbacks->onSuccess(new bool(success)); |
| 138 } else { |
| 139 callbacks->onError( |
| 140 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown, |
| 141 "Sync registration does not exist")); |
| 142 } |
| 143 } |
| 144 |
| 145 void BackgroundSyncProvider::GetRegistrationsCallback( |
| 146 scoped_ptr<blink::WebSyncGetRegistrationsCallbacks> callbacks, |
| 147 const mojo::Array<SyncRegistrationPtr>& registrations) { |
| 148 // TODO(iclelland): Pass through the various errors from the manager to here |
| 149 // and handle them appropriately. |
| 150 blink::WebVector<blink::WebSyncRegistration*>* results = |
| 151 new blink::WebVector<blink::WebSyncRegistration*>(registrations.size()); |
| 152 for (size_t i = 0; i < registrations.size(); ++i) { |
| 153 (*results)[i] = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>( |
| 154 registrations[i]).release(); |
| 155 } |
| 156 callbacks->onSuccess(results); |
| 157 } |
| 158 |
| 159 BackgroundSyncServicePtr& |
| 160 BackgroundSyncProvider::GetBackgroundSyncServicePtr() { |
| 161 if (!background_sync_service_.get()) |
| 162 service_registry_->ConnectToRemoteService(&background_sync_service_); |
| 163 return background_sync_service_; |
| 164 } |
| 165 |
| 166 } // namespace content |
OLD | NEW |