Skip to content

Commit

Permalink
Initial Stubs for Compression Streams API
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=241964

Reviewed by Alex Christensen and Brent Fulgham.

Implement initial stubs for Compression Streams API.

* Source/WTF/Scripts/Preferences/WebPreferencesExperimental.yaml:
* Source/WebCore/CMakeLists.txt:
* Source/WebCore/DerivedSources-input.xcfilelist:
* Source/WebCore/DerivedSources-output.xcfilelist:
* Source/WebCore/DerivedSources.make:
* Source/WebCore/Headers.cmake:
* Source/WebCore/Modules/compression/CompressionStream.cpp: Added.
(WebCore::CompressionStream::createStreams):
(WebCore::CompressionStream::readable):
(WebCore::CompressionStream::writable):
* Source/WebCore/Modules/compression/CompressionStream.h: Added.
(WebCore::CompressionStream::~CompressionStream):
(WebCore::CompressionStream::create):
(WebCore::CompressionStream::getCompressionFormat):
(WebCore::CompressionStream::CompressionStream):
* Source/WebCore/Modules/compression/CompressionStream.idl: Added.
* Source/WebCore/Modules/compression/DecompressionStream.cpp: Added.
(WebCore::DecompressionStream::createStreams):
(WebCore::DecompressionStream::readable):
(WebCore::DecompressionStream::writable):
* Source/WebCore/Modules/compression/DecompressionStream.h: Added.
(WebCore::DecompressionStream::~DecompressionStream):
(WebCore::DecompressionStream::create):
(WebCore::DecompressionStream::getCompressionFormat):
(WebCore::DecompressionStream::DecompressionStream):
* Source/WebCore/Modules/compression/DecompressionStream.idl: Added.
* Source/WebCore/Modules/compression/Formats.h: Added.
* Source/WebCore/Modules/streams/GenericTransformStream.idl: Added.
* Source/WebCore/Modules/websockets/WebSocket.cpp:
* Source/WebCore/Sources.txt:
* Source/WebCore/WebCore.xcodeproj/project.pbxproj:
* Source/WebCore/bindings/js/WebCoreBuiltinNames.h:
* Source/WebCore/dom/EventTargetFactory.in:
* Source/WebCore/features.json:

Canonical link: https://commits.webkit.org/252071@main
  • Loading branch information
stwrt committed Jul 1, 2022
1 parent c1a2e7a commit 9e3619b
Show file tree
Hide file tree
Showing 20 changed files with 643 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Source/WTF/Scripts/Preferences/WebPreferencesExperimental.yaml
Expand Up @@ -457,6 +457,18 @@ CanvasColorSpaceEnabled:
WebCore:
default: false

CompressionStreamEnabled:
type: bool
humanReadableName: "Compression Stream API"
humanReadableDescription: "Enable Compression Stream API"
defaultValue:
WebKitLegacy:
default: false
WebKit:
default: false
WebCore:
default: false

ContactPickerAPIEnabled:
type: bool
humanReadableName: "Contact Picker API"
Expand Down
6 changes: 6 additions & 0 deletions Source/WebCore/CMakeLists.txt
Expand Up @@ -26,6 +26,7 @@ set(WebCore_PRIVATE_INCLUDE_DIRECTORIES
"${WEBCORE_DIR}/Modules/async-clipboard"
"${WEBCORE_DIR}/Modules/beacon"
"${WEBCORE_DIR}/Modules/cache"
"${WEBCORE_DIR}/Modules/compression"
"${WEBCORE_DIR}/Modules/contact-picker"
"${WEBCORE_DIR}/Modules/cookie-consent"
"${WEBCORE_DIR}/Modules/credentialmanagement"
Expand Down Expand Up @@ -205,6 +206,7 @@ set(WebCore_IDL_INCLUDES
Modules/WebGPU
Modules/airplay
Modules/cache
Modules/compression
Modules/credentialmanagement
Modules/encryptedmedia/legacy
Modules/entriesapi
Expand Down Expand Up @@ -258,6 +260,9 @@ set(WebCore_NON_SVG_IDL_FILES
Modules/cache/DOMCacheStorage.idl
Modules/cache/WindowOrWorkerGlobalScope+Caches.idl

Modules/compression/CompressionStream.idl
Modules/compression/DecompressionStream.idl

Modules/contact-picker/ContactInfo.idl
Modules/contact-picker/ContactProperty.idl
Modules/contact-picker/ContactsManager.idl
Expand Down Expand Up @@ -560,6 +565,7 @@ set(WebCore_NON_SVG_IDL_FILES

Modules/streams/ByteLengthQueuingStrategy.idl
Modules/streams/CountQueuingStrategy.idl
Modules/streams/GenericTransformStream.idl
Modules/streams/ReadableByteStreamController.idl
Modules/streams/ReadableStream.idl
Modules/streams/ReadableStreamBYOBReader.idl
Expand Down
3 changes: 3 additions & 0 deletions Source/WebCore/DerivedSources-input.xcfilelist
Expand Up @@ -233,6 +233,8 @@ $(PROJECT_DIR)/Modules/cache/CacheQueryOptions.idl
$(PROJECT_DIR)/Modules/cache/DOMCache.idl
$(PROJECT_DIR)/Modules/cache/DOMCacheStorage.idl
$(PROJECT_DIR)/Modules/cache/WindowOrWorkerGlobalScope+Caches.idl
$(PROJECT_DIR)/Modules/compression/CompressionStream.idl
$(PROJECT_DIR)/Modules/compression/DecompressionStream.idl
$(PROJECT_DIR)/Modules/contact-picker/ContactInfo.idl
$(PROJECT_DIR)/Modules/contact-picker/ContactProperty.idl
$(PROJECT_DIR)/Modules/contact-picker/ContactsManager.idl
Expand Down Expand Up @@ -619,6 +621,7 @@ $(PROJECT_DIR)/Modules/streams/ByteLengthQueuingStrategy.idl
$(PROJECT_DIR)/Modules/streams/ByteLengthQueuingStrategy.js
$(PROJECT_DIR)/Modules/streams/CountQueuingStrategy.idl
$(PROJECT_DIR)/Modules/streams/CountQueuingStrategy.js
$(PROJECT_DIR)/Modules/streams/GenericTransformStream.idl
$(PROJECT_DIR)/Modules/streams/ReadableByteStreamController.idl
$(PROJECT_DIR)/Modules/streams/ReadableByteStreamController.js
$(PROJECT_DIR)/Modules/streams/ReadableByteStreamInternals.js
Expand Down
6 changes: 6 additions & 0 deletions Source/WebCore/DerivedSources-output.xcfilelist
Expand Up @@ -520,6 +520,8 @@ $(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSCompositeOperationOrAuto.cpp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSCompositeOperationOrAuto.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSCompositionEvent.cpp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSCompositionEvent.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSCompressionStream.cpp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSCompressionStream.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSComputedEffectTiming.cpp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSComputedEffectTiming.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSConstantSourceNode.cpp
Expand Down Expand Up @@ -688,6 +690,8 @@ $(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSDatabase.cpp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSDatabase.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSDatabaseCallback.cpp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSDatabaseCallback.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSDecompressionStream.cpp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSDecompressionStream.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSDedicatedWorkerGlobalScope.cpp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSDedicatedWorkerGlobalScope.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSDelayNode.cpp
Expand Down Expand Up @@ -1204,6 +1208,8 @@ $(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSGamepadButton.cpp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSGamepadButton.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSGamepadEvent.cpp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSGamepadEvent.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSGenericTransformStream.cpp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSGenericTransformStream.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSGeolocation.cpp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSGeolocation.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore/JSGeolocationCoordinates.cpp
Expand Down
3 changes: 3 additions & 0 deletions Source/WebCore/DerivedSources.make
Expand Up @@ -255,6 +255,8 @@ JS_BINDING_IDLS := \
$(WebCore)/Modules/cache/DOMCache.idl \
$(WebCore)/Modules/cache/DOMCacheStorage.idl \
$(WebCore)/Modules/cache/WindowOrWorkerGlobalScope+Caches.idl \
$(WebCore)/Modules/compression/CompressionStream.idl \
$(WebCore)/Modules/compression/DecompressionStream.idl \
$(WebCore)/Modules/contact-picker/ContactInfo.idl \
$(WebCore)/Modules/contact-picker/ContactProperty.idl \
$(WebCore)/Modules/contact-picker/ContactsManager.idl \
Expand Down Expand Up @@ -540,6 +542,7 @@ JS_BINDING_IDLS := \
$(WebCore)/Modules/speech/SpeechRecognitionResultList.idl \
$(WebCore)/Modules/streams/ByteLengthQueuingStrategy.idl \
$(WebCore)/Modules/streams/CountQueuingStrategy.idl \
$(WebCore)/Modules/streams/GenericTransformStream.idl \
$(WebCore)/Modules/streams/ReadableByteStreamController.idl \
$(WebCore)/Modules/streams/ReadableStream.idl \
$(WebCore)/Modules/streams/ReadableStreamBYOBReader.idl \
Expand Down
4 changes: 4 additions & 0 deletions Source/WebCore/Headers.cmake
Expand Up @@ -150,6 +150,10 @@ set(WebCore_PRIVATE_FRAMEWORK_HEADERS
Modules/cache/DOMCacheEngine.h
Modules/cache/RetrieveRecordsOptions.h

Modules/compression/CompressionStream.h
Modules/compression/DecompressionStream.h
Modules/compression/Formats.h

Modules/contact-picker/ContactInfo.h
Modules/contact-picker/ContactProperty.h
Modules/contact-picker/ContactsRequestData.h
Expand Down
92 changes: 92 additions & 0 deletions Source/WebCore/Modules/compression/CompressionStream.cpp
@@ -0,0 +1,92 @@
/*
* Copyright (C) 2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/


#include "config.h"
#include "CompressionStream.h"

#include "ScriptExecutionContext.h"
#include "WritableStreamSink.h"
#include <wtf/IsoMalloc.h>
#include <wtf/IsoMallocInlines.h>
#include <wtf/WeakPtr.h>

namespace WebCore {

WTF_MAKE_ISO_ALLOCATED_IMPL(CompressionStream);

ExceptionOr<void> CompressionStream::createStreams()
{
// get global object
auto* globalObject = scriptExecutionContext() ? scriptExecutionContext()->globalObject() : nullptr;
if (!globalObject)
return Exception { InvalidStateError };

// setup readable stream
m_readableStreamSource = SimpleReadableStreamSource::create();
auto readable = ReadableStream::create(*globalObject, m_readableStreamSource.copyRef());
if (readable.hasException())
return readable.releaseException();

m_readable = readable.releaseReturnValue();

// setup writable stream
auto simpleWritableStreamSink = SimpleWritableStreamSink::create([readableStreamSource = m_readableStreamSource, weakThis = WeakPtr { *this }](auto& context, auto value) -> ExceptionOr<void> {
if (!context.globalObject())
return Exception { InvalidStateError };

UNUSED_PARAM(value);
return { };
});
auto writable = WritableStream::create(*JSC::jsCast<JSDOMGlobalObject*>(globalObject), simpleWritableStreamSink);

m_writable = writable.releaseReturnValue();

return { };
}

ExceptionOr<RefPtr<ReadableStream>> CompressionStream::readable()
{
if (!m_readable) {
auto result = createStreams();
if (result.hasException())
return result.releaseException();
}

return m_readable.copyRef();
}

ExceptionOr<RefPtr<WritableStream>> CompressionStream::writable()
{
if (!m_writable) {
auto result = createStreams();
if (result.hasException())
return result.releaseException();
}

return m_writable.copyRef();
}

}
116 changes: 116 additions & 0 deletions Source/WebCore/Modules/compression/CompressionStream.h
@@ -0,0 +1,116 @@
/*
* Copyright (C) 2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/


#pragma once

#include "ActiveDOMObject.h"
#include "Document.h"
#include "Event.h"
#include "EventTarget.h"
#include "ExceptionOr.h"
#include "Formats.h"
#include "ReadableStream.h"
#include "ReadableStreamSource.h"
#include "ScriptExecutionContext.h"
#include "WritableStream.h"

#include <wtf/IsoMalloc.h>
#include <wtf/IsoMallocInlines.h>
#include <wtf/RefCounted.h>
#include <wtf/text/WTFString.h>

namespace JSC {
class CallFrame;
class JSGlobalObject;
class JSValue;
}

namespace WebCore {

class CompressionStream : public RefCounted<CompressionStream>,
public ActiveDOMObject,
public EventTargetWithInlineData {
WTF_MAKE_ISO_ALLOCATED(CompressionStream);

public:
~CompressionStream() { }

using RefCounted<CompressionStream>::ref;
using RefCounted<CompressionStream>::deref;

// Initialize readable and writable streams
ExceptionOr<void> createStreams();

// Initialize Compression Streams and perform initial basic error handling
static ExceptionOr<Ref<CompressionStream>> create(Document& document, String string)
{
// gzip, deflate, and deflate-raw are the supported compression formats.
if (string == String("gzip", 4))
return adoptRef(*new CompressionStream(document, string, Formats::Gzip));
if (string == String("deflate", 7))
return adoptRef(*new CompressionStream(document, string, Formats::Deflate));
if (string == String("deflate-raw", 11))
return adoptRef(*new CompressionStream(document, string, Formats::Deflate_raw));

// Section 5
// 1. If format is unsupported in CompressionStream, then throw a TypeError.
return Exception(TypeError, "Please provide a supported compression algorithm ('gzip', 'deflate', or 'deflate-raw')."_s);
}

ExceptionOr<RefPtr<ReadableStream>> readable();
ExceptionOr<RefPtr<WritableStream>> writable();

Formats::CompressionFormat getCompressionFormat() { return m_format; }

private:
CompressionStream(Document& document, String string, Formats::CompressionFormat format)
: ActiveDOMObject(document), m_string(string), m_format(format)
{
createStreams();
}

// Compression Format
const String m_string;
const Formats::CompressionFormat m_format;

// Streams
RefPtr<ReadableStream> m_readable;
RefPtr<WritableStream> m_writable;
RefPtr<SimpleReadableStreamSource> m_readableStreamSource;

// ActiveDOMObject
const char* activeDOMObjectName() const override { return "CompressionStream"; }
void stop() override { }
void suspend(ReasonForSuspension) override { }
bool virtualHasPendingActivity() const final { return false; }

// EventTargetWithInlineData
EventTargetInterface eventTargetInterface() const override { return CompressionStreamEventTargetInterfaceType; }
ScriptExecutionContext* scriptExecutionContext() const override { return ActiveDOMObject::scriptExecutionContext(); }
void refEventTarget() override { ref(); }
void derefEventTarget() override { deref(); }
};
}
36 changes: 36 additions & 0 deletions Source/WebCore/Modules/compression/CompressionStream.idl
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

// https://wicg.github.io/compression/#compression-stream

[
ActiveDOMObject,
EnabledBySetting=CompressionStreamEnabled,
Exposed=*
] interface CompressionStream : EventTarget {
[CallWith=CurrentDocument] constructor(DOMString format);
};

CompressionStream includes GenericTransformStream;

0 comments on commit 9e3619b

Please sign in to comment.