Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
|
haraken
2016/11/05 13:00:18
2016
| |
| 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 "modules/background_sync/BackgroundSyncProvider.h" | |
| 6 | |
| 7 #include "platform/WebTaskRunner.h" | |
| 8 #include "public/platform/InterfaceProvider.h" | |
| 9 #include "public/platform/Platform.h" | |
| 10 #include "public/platform/modules/background_sync/WebSyncError.h" | |
| 11 #include "wtf/Functional.h" | |
| 12 #include <memory> | |
| 13 #include <stddef.h> | |
| 14 #include <utility> | |
| 15 | |
| 16 namespace blink { | |
| 17 namespace { | |
| 18 | |
| 19 void ConnectToServiceOnMainThread( | |
| 20 mojom::blink::BackgroundSyncServiceRequest request) { | |
| 21 DCHECK(Platform::current()); | |
|
jbroman
2016/11/05 22:51:09
No need to check that Platform exists. If it doesn
adithyas
2016/11/07 19:22:55
Done.
| |
| 22 Platform::current()->interfaceProvider()->getInterface(std::move(request)); | |
|
jbroman
2016/11/05 22:51:09
public/platform/InterfaceProvider.h promises to be
adithyas
2016/11/07 19:22:55
Ah I see, did not know that, thanks!
| |
| 23 } | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 void BackgroundSyncProvider::registerBackgroundSync( | |
| 28 mojom::blink::SyncRegistrationPtr& options, | |
|
jbroman
2016/11/05 22:51:09
This is a reference-to-pointer; you should just us
adithyas
2016/11/07 19:22:55
Done.
| |
| 29 WebServiceWorkerRegistration* serviceWorkerRegistration, | |
|
jbroman
2016/11/05 22:51:08
For legacy reasons, owning pointers were sometimes
adithyas
2016/11/07 19:22:54
Done.
| |
| 30 SyncRegistrationCallbacks* callbacks) { | |
| 31 DCHECK(options); | |
| 32 DCHECK(serviceWorkerRegistration); | |
| 33 DCHECK(callbacks); | |
| 34 int64_t serviceWorkerRegistrationId = | |
| 35 serviceWorkerRegistration->registrationId(); | |
| 36 std::unique_ptr<SyncRegistrationCallbacks> callbacksPtr(callbacks); | |
| 37 | |
| 38 // WTF::unretained is safe here, as the mojo channel will be deleted (and | |
| 39 // will wipe its callbacks) before 'this' is deleted. | |
|
haraken
2016/11/05 13:00:18
It may be safe but is not nice. See the below comm
adithyas
2016/11/07 19:22:55
Acknowledged.
| |
| 40 GetBackgroundSyncServicePtr()->Register( | |
| 41 std::move(options), serviceWorkerRegistrationId, | |
| 42 convertToBaseCallback(WTF::bind(&BackgroundSyncProvider::RegisterCallback, | |
| 43 WTF::unretained(this), | |
|
haraken
2016/11/05 13:00:19
We normally make the object GarbageCollected, but
jbroman
2016/11/05 22:51:09
Here and below, it doesn't look like any members a
adithyas
2016/11/07 19:22:55
Done.
| |
| 44 WTF::passed(std::move(callbacksPtr))))); | |
| 45 } | |
| 46 | |
| 47 void BackgroundSyncProvider::getRegistrations( | |
| 48 WebServiceWorkerRegistration* serviceWorkerRegistration, | |
| 49 SyncGetRegistrationsCallbacks* callbacks) { | |
| 50 DCHECK(serviceWorkerRegistration); | |
| 51 DCHECK(callbacks); | |
| 52 int64_t serviceWorkerRegistrationId = | |
| 53 serviceWorkerRegistration->registrationId(); | |
| 54 std::unique_ptr<SyncGetRegistrationsCallbacks> callbacksPtr(callbacks); | |
| 55 | |
| 56 // WTF::unretained is safe here, as the mojo channel will be deleted (and | |
| 57 // will wipe its callbacks) before 'this' is deleted. | |
| 58 GetBackgroundSyncServicePtr()->GetRegistrations( | |
| 59 serviceWorkerRegistrationId, | |
| 60 convertToBaseCallback(WTF::bind( | |
| 61 &BackgroundSyncProvider::GetRegistrationsCallback, | |
| 62 WTF::unretained(this), WTF::passed(std::move(callbacksPtr))))); | |
|
haraken
2016/11/05 13:00:19
Ditto.
| |
| 63 } | |
| 64 | |
| 65 void BackgroundSyncProvider::RegisterCallback( | |
|
jbroman
2016/11/05 22:51:09
(here and elsewhere) Blink method names should beg
adithyas
2016/11/07 19:22:55
Done.
| |
| 66 std::unique_ptr<SyncRegistrationCallbacks> callbacks, | |
| 67 mojom::blink::BackgroundSyncError error, | |
| 68 mojom::blink::SyncRegistrationPtr options) { | |
| 69 // TODO(iclelland): Determine the correct error message to return in each case | |
| 70 switch (error) { | |
| 71 case mojom::blink::BackgroundSyncError::NONE: | |
| 72 if (!options.is_null()) | |
| 73 callbacks->onSuccess(std::move(options)); | |
| 74 break; | |
| 75 case mojom::blink::BackgroundSyncError::NOT_FOUND: | |
| 76 NOTREACHED(); | |
| 77 break; | |
| 78 case mojom::blink::BackgroundSyncError::STORAGE: | |
| 79 callbacks->onError(blink::WebSyncError(WebSyncError::ErrorTypeUnknown, | |
| 80 "Background Sync is disabled.")); | |
| 81 break; | |
| 82 case mojom::blink::BackgroundSyncError::NOT_ALLOWED: | |
| 83 callbacks->onError( | |
| 84 blink::WebSyncError(WebSyncError::ErrorTypeNoPermission, | |
| 85 "Attempted to register a sync event without a " | |
| 86 "window or registration tag too long.")); | |
| 87 break; | |
| 88 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: | |
| 89 callbacks->onError(blink::WebSyncError( | |
| 90 WebSyncError::ErrorTypePermissionDenied, "Permission denied.")); | |
| 91 break; | |
| 92 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: | |
| 93 callbacks->onError(blink::WebSyncError(WebSyncError::ErrorTypeUnknown, | |
| 94 "No service worker is active.")); | |
| 95 break; | |
|
haraken
2016/11/05 13:00:19
Shall we add:
default:
NOTREACHED();
?
jbroman
2016/11/05 22:51:09
Doesn't doing so turn a compile-time error (not ex
| |
| 96 } | |
| 97 } | |
| 98 | |
| 99 void BackgroundSyncProvider::GetRegistrationsCallback( | |
| 100 std::unique_ptr<SyncGetRegistrationsCallbacks> syncGetRegistrationCallbacks, | |
| 101 mojom::blink::BackgroundSyncError error, | |
| 102 mojo::WTFArray<mojom::blink::SyncRegistrationPtr> registrations) { | |
| 103 // TODO(iclelland): Determine the correct error message to return in each case | |
| 104 switch (error) { | |
| 105 case mojom::blink::BackgroundSyncError::NONE: { | |
| 106 WebVector<mojom::blink::SyncRegistration*> results(registrations.size()); | |
|
jbroman
2016/11/05 22:51:08
In a followup CL, you can switch from WebVector to
adithyas
2016/11/07 19:22:55
Acknowledged.
| |
| 107 for (size_t i = 0; i < registrations.size(); ++i) { | |
| 108 results[i] = registrations[i].get(); | |
| 109 } | |
| 110 syncGetRegistrationCallbacks->onSuccess(results); | |
| 111 break; | |
| 112 } | |
| 113 case mojom::blink::BackgroundSyncError::NOT_FOUND: | |
| 114 case mojom::blink::BackgroundSyncError::NOT_ALLOWED: | |
| 115 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: | |
| 116 // These errors should never be returned from | |
| 117 // BackgroundSyncManager::GetRegistrations | |
| 118 NOTREACHED(); | |
| 119 break; | |
| 120 case mojom::blink::BackgroundSyncError::STORAGE: | |
| 121 syncGetRegistrationCallbacks->onError(blink::WebSyncError( | |
|
jbroman
2016/11/05 22:51:08
Similarly, it looks like WebSyncError can move to
| |
| 122 WebSyncError::ErrorTypeUnknown, "Background Sync is disabled.")); | |
| 123 break; | |
| 124 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: | |
| 125 syncGetRegistrationCallbacks->onError(blink::WebSyncError( | |
| 126 WebSyncError::ErrorTypeUnknown, "No service worker is active.")); | |
| 127 break; | |
|
haraken
2016/11/05 13:00:19
Ditto.
| |
| 128 } | |
| 129 } | |
| 130 | |
| 131 mojom::blink::BackgroundSyncServicePtr& | |
| 132 BackgroundSyncProvider::GetBackgroundSyncServicePtr() { | |
| 133 if (!backgroundSyncService.get()) { | |
| 134 mojo::InterfaceRequest<mojom::blink::BackgroundSyncService> request = | |
| 135 mojo::GetProxy(&backgroundSyncService); | |
| 136 Platform::current()->mainThread()->getWebTaskRunner()->postTask( | |
|
haraken
2016/11/05 13:00:18
Why do we need to post a task?
jbroman
2016/11/05 22:51:08
I commented on this above. The content-side equiva
| |
| 137 BLINK_FROM_HERE, WTF::bind(&ConnectToServiceOnMainThread, | |
| 138 WTF::passed(std::move(request)))); | |
| 139 } | |
| 140 return backgroundSyncService; | |
| 141 } | |
| 142 | |
| 143 } // namespace blink | |
| OLD | NEW |