Chromium Code Reviews| Index: third_party/WebKit/Source/modules/background_fetch/BackgroundFetchBridge.cpp |
| diff --git a/third_party/WebKit/Source/modules/background_fetch/BackgroundFetchBridge.cpp b/third_party/WebKit/Source/modules/background_fetch/BackgroundFetchBridge.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..28c2084ad4aafb8811cdf462b9ad48a337eb6ac5 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/modules/background_fetch/BackgroundFetchBridge.cpp |
| @@ -0,0 +1,132 @@ |
| +// Copyright 2017 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 "modules/background_fetch/BackgroundFetchBridge.h" |
| + |
| +#include "modules/background_fetch/BackgroundFetchOptions.h" |
| +#include "modules/background_fetch/BackgroundFetchRegistration.h" |
| +#include "modules/background_fetch/IconDefinition.h" |
| +#include "public/platform/InterfaceProvider.h" |
| +#include "public/platform/Platform.h" |
| +#include "public/platform/modules/serviceworker/WebServiceWorkerRegistration.h" |
| + |
| +namespace blink { |
| + |
| +namespace { |
| + |
| +// Creates a new BackgroundFetchRegistration instance given a Service Worker |
| +// Registration and a Mojo BackgroundFetchRegistrationPtr instance. |
| +BackgroundFetchRegistration* CreateBackgroundFetchRegistration( |
| + ServiceWorkerRegistration* serviceWorkerRegistration, |
| + mojom::blink::BackgroundFetchRegistrationPtr registrationPtr) { |
| + HeapVector<IconDefinition> icons; |
| + |
| + for (const auto& iconPtr : registrationPtr->icons) { |
| + IconDefinition icon; |
| + icon.setSrc(iconPtr->src); |
| + icon.setSizes(iconPtr->sizes); |
| + icon.setType(iconPtr->type); |
|
haraken
2017/03/14 09:13:13
Can we probably use a mojo's type-mapping to avoid
Peter Beverloo
2017/03/14 17:28:15
Since IconDefinition is a dictionary and not garba
|
| + |
| + icons.push_back(icon); |
| + } |
| + |
| + return new BackgroundFetchRegistration( |
| + serviceWorkerRegistration, registrationPtr->tag, std::move(icons), |
| + registrationPtr->total_download_size, registrationPtr->title); |
| +} |
| + |
| +} // namespace |
| + |
| +// static |
| +BackgroundFetchBridge* BackgroundFetchBridge::from( |
| + ServiceWorkerRegistration* serviceWorkerRegistration) { |
| + DCHECK(serviceWorkerRegistration); |
| + |
| + BackgroundFetchBridge* bridge = static_cast<BackgroundFetchBridge*>( |
| + Supplement<ServiceWorkerRegistration>::from(serviceWorkerRegistration, |
| + supplementName())); |
| + |
| + if (!bridge) { |
| + bridge = new BackgroundFetchBridge(*serviceWorkerRegistration); |
| + Supplement<ServiceWorkerRegistration>::provideTo(*serviceWorkerRegistration, |
| + supplementName(), bridge); |
| + } |
| + |
| + return bridge; |
| +} |
| + |
| +// static |
| +const char* BackgroundFetchBridge::supplementName() { |
| + return "BackgroundFetchBridge"; |
| +} |
| + |
| +BackgroundFetchBridge::BackgroundFetchBridge( |
| + ServiceWorkerRegistration& registration) |
| + : Supplement<ServiceWorkerRegistration>(registration) {} |
| + |
| +BackgroundFetchBridge::~BackgroundFetchBridge() = default; |
| + |
| +void BackgroundFetchBridge::abort(const String& tag) { |
| + GetService()->Abort(supplementable()->webRegistration()->registrationId(), |
| + tag); |
| +} |
| + |
| +void BackgroundFetchBridge::updateUI( |
| + const String& tag, |
| + const String& title, |
| + std::unique_ptr<UpdateUICallback> callback) { |
| + GetService()->UpdateUI(supplementable()->webRegistration()->registrationId(), |
| + tag, title, |
| + convertToBaseCallback(std::move(callback))); |
| +} |
| + |
| +void BackgroundFetchBridge::getRegistration( |
| + const String& tag, |
| + std::unique_ptr<GetRegistrationCallback> callback) { |
| + GetService()->GetRegistration( |
| + supplementable()->webRegistration()->registrationId(), tag, |
| + convertToBaseCallback( |
| + WTF::bind(&BackgroundFetchBridge::didGetRegistration, |
| + wrapPersistent(this), WTF::passed(std::move(callback))))); |
| +} |
| + |
| +void BackgroundFetchBridge::didGetRegistration( |
| + std::unique_ptr<GetRegistrationCallback> callback, |
| + mojom::blink::BackgroundFetchError error, |
| + mojom::blink::BackgroundFetchRegistrationPtr registrationPtr) { |
| + switch (error) { |
| + case mojom::blink::BackgroundFetchError::NONE: |
| + DCHECK(registrationPtr); |
| + |
| + // Invoke the |callback| after creating the resulting Background Fetch |
| + // Registration object representing the fetched registration. |
| + (*callback)(error, CreateBackgroundFetchRegistration( |
| + supplementable(), std::move(registrationPtr))); |
| + return; |
| + case mojom::blink::BackgroundFetchError::DUPLICATED_TAG: |
| + DCHECK(!registrationPtr); |
| + |
| + // Invoke the |callback| without a Background Fetch registration. |
| + (*callback)(error, nullptr /* registration */); |
| + return; |
| + } |
| + |
| + NOTREACHED(); |
| +} |
| + |
| +void BackgroundFetchBridge::getTags(std::unique_ptr<GetTagsCallback> callback) { |
| + GetService()->GetTags(supplementable()->webRegistration()->registrationId(), |
| + convertToBaseCallback(std::move(callback))); |
| +} |
| + |
| +const mojom::blink::BackgroundFetchServicePtr& |
| +BackgroundFetchBridge::GetService() { |
|
haraken
2017/03/14 09:13:14
Nit: getService.
Peter Beverloo
2017/03/14 15:17:21
Done.
|
| + if (!m_backgroundFetchService.get()) { |
| + Platform::current()->interfaceProvider()->getInterface( |
| + mojo::MakeRequest(&m_backgroundFetchService)); |
| + } |
| + return m_backgroundFetchService; |
| +} |
| + |
| +} // namespace blink |