Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/background_sync/BackgroundSyncProvider.h" | 5 #include "modules/background_sync/BackgroundSyncProvider.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptPromiseResolver.h" | |
| 8 #include "modules/background_sync/SyncError.h" | |
| 9 #include "platform/heap/Persistent.h" | |
| 7 #include "public/platform/InterfaceProvider.h" | 10 #include "public/platform/InterfaceProvider.h" |
| 8 #include "public/platform/Platform.h" | 11 #include "public/platform/Platform.h" |
| 9 #include "public/platform/modules/background_sync/WebSyncError.h" | |
| 10 #include "public/platform/modules/serviceworker/WebServiceWorkerRegistration.h" | 12 #include "public/platform/modules/serviceworker/WebServiceWorkerRegistration.h" |
| 11 #include "wtf/Functional.h" | 13 #include "wtf/Functional.h" |
| 12 | 14 |
| 13 namespace blink { | 15 namespace blink { |
| 14 | 16 |
| 17 // static | |
| 18 void BackgroundSyncProvider::onRegisterSuccess( | |
| 19 ScriptPromiseResolver* resolver, | |
| 20 mojom::blink::SyncRegistrationPtr syncRegistration) { | |
| 21 if (!resolver->getExecutionContext() || | |
|
jbroman
2016/11/18 16:53:31
Here and below: ScriptPromiseResolver::resolveOrRe
adithyas
2016/11/18 20:05:00
Yeah, it's not necessary, I'll just inline it. The
| |
| 22 resolver->getExecutionContext()->activeDOMObjectsAreStopped()) { | |
| 23 return; | |
| 24 } | |
| 25 | |
| 26 if (!syncRegistration) { | |
| 27 resolver->resolve(v8::Null(resolver->getScriptState()->isolate())); | |
| 28 return; | |
| 29 } | |
| 30 resolver->resolve(); | |
| 31 } | |
| 32 | |
| 33 // static | |
| 34 void BackgroundSyncProvider::onRegisterError(ScriptPromiseResolver* resolver, | |
| 35 const SyncError& error) { | |
| 36 if (!resolver->getExecutionContext() || | |
| 37 resolver->getExecutionContext()->activeDOMObjectsAreStopped()) { | |
| 38 return; | |
| 39 } | |
| 40 resolver->reject(SyncError::take(resolver, error)); | |
| 41 } | |
| 42 | |
| 43 // static | |
| 44 void BackgroundSyncProvider::onGetRegistrationsSuccess( | |
| 45 ScriptPromiseResolver* resolver, | |
| 46 const Vector<mojom::blink::SyncRegistrationPtr>& syncRegistrations) { | |
| 47 if (!resolver->getExecutionContext() || | |
| 48 resolver->getExecutionContext()->activeDOMObjectsAreStopped()) { | |
| 49 return; | |
| 50 } | |
| 51 Vector<String> tags; | |
| 52 for (const mojom::blink::SyncRegistrationPtr& r : syncRegistrations) { | |
| 53 tags.append(r->tag); | |
| 54 } | |
| 55 resolver->resolve(tags); | |
| 56 } | |
| 57 | |
| 58 // static | |
| 59 void BackgroundSyncProvider::onGetRegistrationsError( | |
|
jbroman
2016/11/18 16:53:31
If you don't take my suggestion above and remove t
| |
| 60 ScriptPromiseResolver* resolver, | |
| 61 const SyncError& error) { | |
| 62 onRegisterError(resolver, error); | |
| 63 } | |
| 64 | |
| 15 void BackgroundSyncProvider::registerBackgroundSync( | 65 void BackgroundSyncProvider::registerBackgroundSync( |
| 16 mojom::blink::SyncRegistrationPtr options, | 66 mojom::blink::SyncRegistrationPtr options, |
| 17 WebServiceWorkerRegistration* serviceWorkerRegistration, | 67 WebServiceWorkerRegistration* serviceWorkerRegistration, |
| 18 std::unique_ptr<SyncRegistrationCallbacks> callbacks) { | 68 ScriptPromiseResolver* resolver) { |
| 19 DCHECK(options); | 69 DCHECK(options); |
| 20 DCHECK(serviceWorkerRegistration); | 70 DCHECK(serviceWorkerRegistration); |
| 21 DCHECK(callbacks); | 71 DCHECK(resolver); |
| 22 int64_t serviceWorkerRegistrationId = | 72 int64_t serviceWorkerRegistrationId = |
| 23 serviceWorkerRegistration->registrationId(); | 73 serviceWorkerRegistration->registrationId(); |
| 24 | 74 |
| 25 GetBackgroundSyncServicePtr()->Register( | 75 GetBackgroundSyncServicePtr()->Register( |
| 26 std::move(options), serviceWorkerRegistrationId, | 76 std::move(options), serviceWorkerRegistrationId, |
| 27 convertToBaseCallback(WTF::bind(&BackgroundSyncProvider::registerCallback, | 77 convertToBaseCallback(WTF::bind(&BackgroundSyncProvider::registerCallback, |
| 28 WTF::passed(std::move(callbacks))))); | 78 wrapPersistent(resolver)))); |
| 29 } | 79 } |
| 30 | 80 |
| 31 void BackgroundSyncProvider::getRegistrations( | 81 void BackgroundSyncProvider::getRegistrations( |
| 32 WebServiceWorkerRegistration* serviceWorkerRegistration, | 82 WebServiceWorkerRegistration* serviceWorkerRegistration, |
| 33 std::unique_ptr<SyncGetRegistrationsCallbacks> callbacks) { | 83 ScriptPromiseResolver* resolver) { |
| 34 DCHECK(serviceWorkerRegistration); | 84 DCHECK(serviceWorkerRegistration); |
| 35 DCHECK(callbacks); | 85 DCHECK(resolver); |
| 36 int64_t serviceWorkerRegistrationId = | 86 int64_t serviceWorkerRegistrationId = |
| 37 serviceWorkerRegistration->registrationId(); | 87 serviceWorkerRegistration->registrationId(); |
| 38 | 88 |
| 39 GetBackgroundSyncServicePtr()->GetRegistrations( | 89 GetBackgroundSyncServicePtr()->GetRegistrations( |
| 40 serviceWorkerRegistrationId, | 90 serviceWorkerRegistrationId, |
| 41 convertToBaseCallback( | 91 convertToBaseCallback( |
| 42 WTF::bind(&BackgroundSyncProvider::getRegistrationsCallback, | 92 WTF::bind(&BackgroundSyncProvider::getRegistrationsCallback, |
| 43 WTF::passed(std::move(callbacks))))); | 93 wrapPersistent(resolver)))); |
| 44 } | 94 } |
| 45 | 95 |
| 46 // static | 96 // static |
| 47 void BackgroundSyncProvider::registerCallback( | 97 void BackgroundSyncProvider::registerCallback( |
| 48 std::unique_ptr<SyncRegistrationCallbacks> callbacks, | 98 ScriptPromiseResolver* resolver, |
| 49 mojom::blink::BackgroundSyncError error, | 99 mojom::blink::BackgroundSyncError error, |
| 50 mojom::blink::SyncRegistrationPtr options) { | 100 mojom::blink::SyncRegistrationPtr options) { |
| 51 // TODO(iclelland): Determine the correct error message to return in each case | 101 // TODO(iclelland): Determine the correct error message to return in each case |
| 52 switch (error) { | 102 switch (error) { |
| 53 case mojom::blink::BackgroundSyncError::NONE: | 103 case mojom::blink::BackgroundSyncError::NONE: |
| 54 if (!options.is_null()) | 104 onRegisterSuccess(resolver, std::move(options)); |
| 55 callbacks->onSuccess(std::move(options)); | |
| 56 break; | 105 break; |
| 57 case mojom::blink::BackgroundSyncError::NOT_FOUND: | 106 case mojom::blink::BackgroundSyncError::NOT_FOUND: |
| 58 NOTREACHED(); | 107 NOTREACHED(); |
| 59 break; | 108 break; |
| 60 case mojom::blink::BackgroundSyncError::STORAGE: | 109 case mojom::blink::BackgroundSyncError::STORAGE: |
| 61 callbacks->onError(blink::WebSyncError(WebSyncError::ErrorTypeUnknown, | 110 onRegisterError(resolver, SyncError(SyncError::ErrorTypeUnknown, |
| 62 "Background Sync is disabled.")); | 111 "Background Sync is disabled.")); |
| 63 break; | 112 break; |
| 64 case mojom::blink::BackgroundSyncError::NOT_ALLOWED: | 113 case mojom::blink::BackgroundSyncError::NOT_ALLOWED: |
| 65 callbacks->onError( | 114 onRegisterError(resolver, |
| 66 blink::WebSyncError(WebSyncError::ErrorTypeNoPermission, | 115 SyncError(SyncError::ErrorTypeNoPermission, |
| 67 "Attempted to register a sync event without a " | 116 "Attempted to register a sync event without a " |
| 68 "window or registration tag too long.")); | 117 "window or registration tag too long.")); |
| 69 break; | 118 break; |
| 70 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: | 119 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: |
| 71 callbacks->onError(blink::WebSyncError( | 120 onRegisterError(resolver, SyncError(SyncError::ErrorTypePermissionDenied, |
| 72 WebSyncError::ErrorTypePermissionDenied, "Permission denied.")); | 121 "Permission denied.")); |
| 73 break; | 122 break; |
| 74 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: | 123 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: |
| 75 callbacks->onError(blink::WebSyncError(WebSyncError::ErrorTypeUnknown, | 124 onRegisterError(resolver, SyncError(SyncError::ErrorTypeUnknown, |
| 76 "No service worker is active.")); | 125 "No service worker is active.")); |
| 77 break; | 126 break; |
| 78 } | 127 } |
| 79 } | 128 } |
| 80 | 129 |
| 81 // static | 130 // static |
| 82 void BackgroundSyncProvider::getRegistrationsCallback( | 131 void BackgroundSyncProvider::getRegistrationsCallback( |
| 83 std::unique_ptr<SyncGetRegistrationsCallbacks> syncGetRegistrationCallbacks, | 132 ScriptPromiseResolver* resolver, |
| 84 mojom::blink::BackgroundSyncError error, | 133 mojom::blink::BackgroundSyncError error, |
| 85 mojo::WTFArray<mojom::blink::SyncRegistrationPtr> registrations) { | 134 mojo::WTFArray<mojom::blink::SyncRegistrationPtr> registrations) { |
| 86 // TODO(iclelland): Determine the correct error message to return in each case | 135 // TODO(iclelland): Determine the correct error message to return in each case |
| 87 switch (error) { | 136 switch (error) { |
| 88 case mojom::blink::BackgroundSyncError::NONE: { | 137 case mojom::blink::BackgroundSyncError::NONE: { |
| 89 WebVector<mojom::blink::SyncRegistration*> results(registrations.size()); | 138 onGetRegistrationsSuccess(resolver, registrations.storage()); |
| 90 for (size_t i = 0; i < registrations.size(); ++i) { | |
| 91 results[i] = registrations[i].get(); | |
| 92 } | |
| 93 syncGetRegistrationCallbacks->onSuccess(results); | |
| 94 break; | 139 break; |
| 95 } | 140 } |
| 96 case mojom::blink::BackgroundSyncError::NOT_FOUND: | 141 case mojom::blink::BackgroundSyncError::NOT_FOUND: |
| 97 case mojom::blink::BackgroundSyncError::NOT_ALLOWED: | 142 case mojom::blink::BackgroundSyncError::NOT_ALLOWED: |
| 98 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: | 143 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: |
| 99 // These errors should never be returned from | 144 // These errors should never be returned from |
| 100 // BackgroundSyncManager::GetRegistrations | 145 // BackgroundSyncManager::GetRegistrations |
| 101 NOTREACHED(); | 146 NOTREACHED(); |
| 102 break; | 147 break; |
| 103 case mojom::blink::BackgroundSyncError::STORAGE: | 148 case mojom::blink::BackgroundSyncError::STORAGE: |
| 104 syncGetRegistrationCallbacks->onError(blink::WebSyncError( | 149 onGetRegistrationsError(resolver, |
| 105 WebSyncError::ErrorTypeUnknown, "Background Sync is disabled.")); | 150 SyncError(SyncError::ErrorTypeUnknown, |
| 151 "Background Sync is disabled.")); | |
| 106 break; | 152 break; |
| 107 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: | 153 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: |
| 108 syncGetRegistrationCallbacks->onError(blink::WebSyncError( | 154 onGetRegistrationsError(resolver, |
| 109 WebSyncError::ErrorTypeUnknown, "No service worker is active.")); | 155 SyncError(SyncError::ErrorTypeUnknown, |
| 156 "No service worker is active.")); | |
| 110 break; | 157 break; |
| 111 } | 158 } |
| 112 } | 159 } |
| 113 | 160 |
| 114 mojom::blink::BackgroundSyncServicePtr& | 161 mojom::blink::BackgroundSyncServicePtr& |
| 115 BackgroundSyncProvider::GetBackgroundSyncServicePtr() { | 162 BackgroundSyncProvider::GetBackgroundSyncServicePtr() { |
| 116 if (!m_backgroundSyncService.get()) { | 163 if (!m_backgroundSyncService.get()) { |
| 117 mojo::InterfaceRequest<mojom::blink::BackgroundSyncService> request = | 164 mojo::InterfaceRequest<mojom::blink::BackgroundSyncService> request = |
| 118 mojo::GetProxy(&m_backgroundSyncService); | 165 mojo::GetProxy(&m_backgroundSyncService); |
| 119 Platform::current()->interfaceProvider()->getInterface(std::move(request)); | 166 Platform::current()->interfaceProvider()->getInterface(std::move(request)); |
| 120 } | 167 } |
| 121 return m_backgroundSyncService; | 168 return m_backgroundSyncService; |
| 122 } | 169 } |
| 123 | 170 |
| 124 } // namespace blink | 171 } // namespace blink |
| OLD | NEW |