Skip to content

Commit

Permalink
Bug 1897871 - Implement PushMessageData .bytes() method. r=saschana…
Browse files Browse the repository at this point in the history
…z,webidl

Implements w3c/push-api#370

Differential Revision: https://phabricator.services.mozilla.com/D212319
  • Loading branch information
evilpie committed Jun 3, 2024
1 parent 2645d76 commit 6518590
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions dom/push/test/test_data.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
);
isDeeply(new Uint8Array(message.data.arrayBuffer), typedArray,
"Wrong array buffer message data");
isDeeply(message.data.bytes, typedArray, "Wrong bytes message data");

message = await waitForMessage(
pushSubscription,
Expand Down
1 change: 1 addition & 0 deletions dom/push/test/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function handlePush(event) {
arrayBuffer: event.data.arrayBuffer(),
json: getJSON(event.data),
blob: event.data.blob(),
bytes: event.data.bytes(),
};
}
broadcast(event, message);
Expand Down
10 changes: 10 additions & 0 deletions dom/serviceworkers/ServiceWorkerEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,16 @@ already_AddRefed<mozilla::dom::Blob> PushMessageData::Blob(ErrorResult& aRv) {
return nullptr;
}

void PushMessageData::Bytes(JSContext* cx, JS::MutableHandle<JSObject*> aRetval,
ErrorResult& aRv) {
uint8_t* data = GetContentsCopy();
if (data) {
UniquePtr<uint8_t[], JS::FreePolicy> dataPtr(data);
BodyUtil::ConsumeBytes(cx, aRetval, mBytes.Length(), std::move(dataPtr),
aRv);
}
}

nsresult PushMessageData::EnsureDecodedText() {
if (mBytes.IsEmpty() || !mDecodedText.IsEmpty()) {
return NS_OK;
Expand Down
2 changes: 2 additions & 0 deletions dom/serviceworkers/ServiceWorkerEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ class PushMessageData final : public nsISupports, public nsWrapperCache {
void ArrayBuffer(JSContext* cx, JS::MutableHandle<JSObject*> aRetval,
ErrorResult& aRv);
already_AddRefed<mozilla::dom::Blob> Blob(ErrorResult& aRv);
void Bytes(JSContext* cx, JS::MutableHandle<JSObject*> aRetval,
ErrorResult& aRv);

PushMessageData(nsIGlobalObject* aOwner, nsTArray<uint8_t>&& aBytes);

Expand Down
2 changes: 2 additions & 0 deletions dom/webidl/PushMessageData.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ interface PushMessageData
[Throws]
Blob blob();
[Throws]
Uint8Array bytes();
[Throws]
any json();
USVString text();
};

0 comments on commit 6518590

Please sign in to comment.