Commit f4a2efba authored by Rayan Kanso's avatar Rayan Kanso Committed by Commit Bot

[Background Fetch] Allow updateUI to change the icon

updateUI accepts a dictionary now, where the developer can change the
title and/or the icon.

TBR=haraken@chromium.org

Bug: 865063
Change-Id: I779c05ee6bdeee83ce94793eddb4409af95395c6
Reviewed-on: https://chromium-review.googlesource.com/1142152
Commit-Queue: Rayan Kanso <rayankans@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Reviewed-by: default avatarMugdha Lakhani <nator@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577524}
parent 740f5567
......@@ -69,7 +69,14 @@ void BackgroundFetchBridge::Abort(const String& developer_id,
void BackgroundFetchBridge::UpdateUI(const String& developer_id,
const String& unique_id,
const String& title,
const SkBitmap& icon,
UpdateUICallback callback) {
if (title.IsNull() && icon.isNull()) {
std::move(callback).Run(
mojom::blink::BackgroundFetchError::INVALID_ARGUMENT);
return;
}
GetService()->UpdateUI(
GetSupplementable()->WebRegistration()->RegistrationId(), developer_id,
unique_id, title, std::move(callback));
......
......@@ -60,11 +60,12 @@ class BackgroundFetchBridge final
void GetIconDisplaySize(GetIconDisplaySizeCallback callback);
// Updates the user interface for the Background Fetch identified by
// |unique_id| with the updated |title|. Will invoke the |callback| when the
// interface has been requested to update.
// |unique_id| with the updated |title| or |icon|. Will invoke the |callback|
// when the interface has been requested to update.
void UpdateUI(const String& developer_id,
const String& unique_id,
const String& title,
const SkBitmap& icon,
UpdateUICallback callback);
// Aborts the active Background Fetch for |unique_id|. Will invoke the
......
......@@ -7,10 +7,13 @@
#include "third_party/blink/public/platform/modules/background_fetch/web_background_fetch_settled_fetch.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/fetch/request.h"
#include "third_party/blink/renderer/core/fetch/response.h"
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_bridge.h"
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_icon_loader.h"
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_settled_fetch.h"
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_update_ui_options.h"
#include "third_party/blink/renderer/modules/event_modules_names.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
......@@ -29,17 +32,20 @@ BackgroundFetchUpdateEvent::BackgroundFetchUpdateEvent(
WaitUntilObserver* observer,
ServiceWorkerRegistration* registration)
: BackgroundFetchSettledEvent(type, initializer, unique_id, observer),
registration_(registration) {}
registration_(registration),
loader_(new BackgroundFetchIconLoader) {}
BackgroundFetchUpdateEvent::~BackgroundFetchUpdateEvent() = default;
void BackgroundFetchUpdateEvent::Trace(blink::Visitor* visitor) {
visitor->Trace(registration_);
visitor->Trace(loader_);
BackgroundFetchSettledEvent::Trace(visitor);
}
ScriptPromise BackgroundFetchUpdateEvent::updateUI(ScriptState* script_state,
const String& title) {
ScriptPromise BackgroundFetchUpdateEvent::updateUI(
ScriptState* script_state,
const BackgroundFetchUpdateUIOptions& ui_options) {
if (!registration_) {
// Return a Promise that will never settle when a developer calls this
// method on a BackgroundFetchedEvent instance they created themselves.
......@@ -47,15 +53,34 @@ ScriptPromise BackgroundFetchUpdateEvent::updateUI(ScriptState* script_state,
}
DCHECK(!unique_id_.IsEmpty());
if (!ui_options.hasTitle() && ui_options.icons().IsEmpty()) {
// Nothing to update, just return a resolved promise.
ScriptPromise::CastUndefined(script_state);
}
ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state);
ScriptPromise promise = resolver->Promise();
if (ui_options.icons().IsEmpty()) {
DidGetIcon(resolver, ui_options.title(), SkBitmap());
} else {
loader_->Start(
BackgroundFetchBridge::From(registration_),
ExecutionContext::From(script_state), ui_options.icons(),
WTF::Bind(&BackgroundFetchUpdateEvent::DidGetIcon, WrapPersistent(this),
WrapPersistent(resolver), ui_options.title()));
}
return promise;
}
void BackgroundFetchUpdateEvent::DidGetIcon(ScriptPromiseResolver* resolver,
const String& title,
const SkBitmap& icon) {
BackgroundFetchBridge::From(registration_)
->UpdateUI(id(), unique_id_, title,
->UpdateUI(id(), unique_id_, title, icon,
WTF::Bind(&BackgroundFetchUpdateEvent::DidUpdateUI,
WrapPersistent(this), WrapPersistent(resolver)));
return promise;
}
void BackgroundFetchUpdateEvent::DidUpdateUI(
......
......@@ -11,6 +11,9 @@
namespace blink {
class BackgroundFetchIconLoader;
class BackgroundFetchUpdateUIOptions;
// Event for interacting with fetch requests that have completed.
class MODULES_EXPORT BackgroundFetchUpdateEvent final
: public BackgroundFetchSettledEvent {
......@@ -37,7 +40,8 @@ class MODULES_EXPORT BackgroundFetchUpdateEvent final
~BackgroundFetchUpdateEvent() override;
// Web Exposed method defined in the IDL file.
ScriptPromise updateUI(ScriptState* script_state, const String& title);
ScriptPromise updateUI(ScriptState* script_state,
const BackgroundFetchUpdateUIOptions& ui_options);
void Trace(blink::Visitor* visitor) override;
......@@ -53,10 +57,15 @@ class MODULES_EXPORT BackgroundFetchUpdateEvent final
WaitUntilObserver* observer,
ServiceWorkerRegistration* registration);
void DidGetIcon(ScriptPromiseResolver* resolver,
const String& title,
const SkBitmap& icon);
void DidUpdateUI(ScriptPromiseResolver* resolver,
mojom::blink::BackgroundFetchError error);
Member<ServiceWorkerRegistration> registration_;
Member<BackgroundFetchIconLoader> loader_;
};
} // namespace blink
......
......@@ -10,5 +10,5 @@
RuntimeEnabled=BackgroundFetch
]
interface BackgroundFetchUpdateEvent : BackgroundFetchSettledEvent {
[CallWith=ScriptState] Promise<void> updateUI(DOMString title);
[CallWith=ScriptState] Promise<void> updateUI(BackgroundFetchUpdateUIOptions title);
};
\ No newline at end of file
// Copyright 2018 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.
// https://wicg.github.io/background-fetch/#backgroundfetchupdateevent
// TODO(crbug.com/865063): Fix names after the standard is updated.
// Spec Bug: https://github.com/WICG/background-fetch/issues/86
dictionary BackgroundFetchUpdateUIOptions {
DOMString? title;
sequence<ImageResource> icons = [];
};
......@@ -468,6 +468,7 @@ modules_dictionary_idl_files =
"background_fetch/background_fetch_fail_event_init.idl",
"background_fetch/background_fetch_options.idl",
"background_fetch/background_fetch_settled_event_init.idl",
"background_fetch/background_fetch_update_ui_options.idl",
"background_sync/sync_event_init.idl",
"bluetooth/bluetooth_le_scan_filter_init.idl",
"bluetooth/request_device_options.idl",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment