Skip to content

Commit

Permalink
Add SPI for connecting to webpushd
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=279142
rdar://135297138

Reviewed by Ben Nham.

This will make it easier to test webpushd functionalities.

Tests: WebPushD.WKWebPushDaemonConnectionRequestPushPermission
       WebPushD.WKWebPushDaemonConnectionPushSubscription

* Source/WebKit/Shared/API/APIObject.h:
* Source/WebKit/Shared/API/Cocoa/WebKitPrivate.h:
* Source/WebKit/Shared/Cocoa/APIObject.mm:
(API::Object::newObject):
* Source/WebKit/Sources.txt:
* Source/WebKit/SourcesCocoa.txt:
* Source/WebKit/UIProcess/API/APIWebPushDaemonConnection.cpp: Added.
(API::WebPushDaemonConnection::WebPushDaemonConnection):
(API::WebPushDaemonConnection::getPushPermissionState):
(API::WebPushDaemonConnection::requestPushPermission):
(API::WebPushDaemonConnection::setAppBadge):
(API::WebPushDaemonConnection::subscribeToPushService):
(API::WebPushDaemonConnection::unsubscribeFromPushService):
(API::WebPushDaemonConnection::getPushSubscription):
(API::WebPushDaemonConnection::getNextPendingPushMessage):
* Source/WebKit/UIProcess/API/APIWebPushDaemonConnection.h: Added.
* Source/WebKit/UIProcess/API/APIWebPushMessage.h: Copied from Source/WebKit/Shared/API/Cocoa/WebKitPrivate.h.
* Source/WebKit/UIProcess/API/APIWebPushSubscriptionData.h: Copied from Source/WebKit/Shared/API/Cocoa/WebKitPrivate.h.
* Source/WebKit/UIProcess/API/Cocoa/_WKWebPushDaemonConnection.h: Added.
* Source/WebKit/UIProcess/API/Cocoa/_WKWebPushDaemonConnection.mm: Added.
(-[_WKWebPushDaemonConnectionConfiguration init]):
(-[_WKWebPushDaemonConnection initWithConfiguration:]):
(toWKPermissionsState):
(-[_WKWebPushDaemonConnection getPushPermissionStateForOrigin:completionHandler:]):
(-[_WKWebPushDaemonConnection requestPushPermissionForOrigin:completionHandler:]):
(-[_WKWebPushDaemonConnection setAppBadge:origin:]):
(-[_WKWebPushDaemonConnection subscribeToPushServiceForScope:applicationServerKey:completionHandler:]):
(-[_WKWebPushDaemonConnection unsubscribeFromPushServiceForScope:completionHandler:]):
(-[_WKWebPushDaemonConnection getSubscriptionForScope:completionHandler:]):
(-[_WKWebPushDaemonConnection getNextPendingPushMessage:]):
(-[_WKWebPushDaemonConnection _apiObject]):
* Source/WebKit/UIProcess/API/Cocoa/_WKWebPushDaemonConnectionInternal.h: Copied from Source/WebKit/Shared/API/Cocoa/WebKitPrivate.h.
* Source/WebKit/UIProcess/API/Cocoa/_WKWebPushMessage.h: Copied from Source/WebKit/Shared/API/Cocoa/WebKitPrivate.h.
* Source/WebKit/UIProcess/API/Cocoa/_WKWebPushMessage.mm: Copied from Source/WebKit/Shared/API/Cocoa/WebKitPrivate.h.
(-[_WKWebPushMessage dealloc]):
(-[_WKWebPushMessage data]):
(-[_WKWebPushMessage scope]):
(-[_WKWebPushMessage partition]):
(-[_WKWebPushMessage _apiObject]):
* Source/WebKit/UIProcess/API/Cocoa/_WKWebPushMessageInternal.h: Copied from Source/WebKit/Shared/API/Cocoa/WebKitPrivate.h.
* Source/WebKit/UIProcess/API/Cocoa/_WKWebPushSubscriptionData.h: Copied from Source/WebKit/Shared/API/Cocoa/WebKitPrivate.h.
* Source/WebKit/UIProcess/API/Cocoa/_WKWebPushSubscriptionData.mm: Copied from Source/WebKit/Shared/API/Cocoa/WebKitPrivate.h.
(-[_WKWebPushSubscriptionData dealloc]):
(-[_WKWebPushSubscriptionData endpoint]):
(-[_WKWebPushSubscriptionData applicationServerKey]):
(-[_WKWebPushSubscriptionData authenticationSecret]):
(-[_WKWebPushSubscriptionData ecdhPublicKey]):
(-[_WKWebPushSubscriptionData _apiObject]):
* Source/WebKit/UIProcess/API/Cocoa/_WKWebPushSubscriptionDataInternal.h: Copied from Source/WebKit/Shared/API/Cocoa/WebKitPrivate.h.
* Source/WebKit/UIProcess/API/ios/WKWebViewTestingIOS.mm:
* Source/WebKit/WebKit.xcodeproj/project.pbxproj:
* Tools/TestWebKitAPI/Tests/WebKitCocoa/WebPushDaemon.mm:
(TestWebKitAPI::(WebPushD, WKWebPushDaemonConnectionRequestPushPermission)):
(TestWebKitAPI::(WebPushD, WKWebPushDaemonConnectionPushSubscription)):

Canonical link: https://commits.webkit.org/283218@main
  • Loading branch information
szewai committed Sep 5, 2024
1 parent dbcb33d commit 9f0d6c1
Show file tree
Hide file tree
Showing 21 changed files with 983 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Source/WebKit/Shared/API/APIObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ class Object
#endif
#endif
WebResourceLoadStatisticsManager,
WebPushDaemonConnection,
WebPushMessage,
WebPushSubscriptionData,
WebsiteDataRecord,
WebsiteDataStore,
WebsiteDataStoreConfiguration,
Expand Down
3 changes: 3 additions & 0 deletions Source/WebKit/Shared/API/Cocoa/WebKitPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@
#import <WebKit/_WKThumbnailView.h>
#import <WebKit/_WKVisitedLinkStore.h>
#import <WebKit/_WKWebPushAction.h>
#import <WebKit/_WKWebPushDaemonConnection.h>
#import <WebKit/_WKWebPushMessage.h>
#import <WebKit/_WKWebPushSubscriptionData.h>
15 changes: 15 additions & 0 deletions Source/WebKit/Shared/Cocoa/APIObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
#import "_WKVisitedLinkStoreInternal.h"
#import "_WKWebAuthenticationAssertionResponseInternal.h"
#import "_WKWebAuthenticationPanelInternal.h"
#import "_WKWebPushDaemonConnectionInternal.h"
#import "_WKWebPushMessageInternal.h"
#import "_WKWebPushSubscriptionDataInternal.h"
#import "_WKWebsiteDataStoreConfigurationInternal.h"

#if ENABLE(INSPECTOR_EXTENSIONS)
Expand Down Expand Up @@ -446,6 +449,18 @@
#endif
#endif // ENABLE(WK_WEB_EXTENSIONS)

case Type::WebPushDaemonConnection:
wrapper = [_WKWebPushDaemonConnection alloc];
break;

case Type::WebPushMessage:
wrapper = [_WKWebPushMessage alloc];
break;

case Type::WebPushSubscriptionData:
wrapper = [_WKWebPushSubscriptionData alloc];
break;

case Type::WebsiteDataRecord:
wrapper = [WKWebsiteDataRecord alloc];
break;
Expand Down
1 change: 1 addition & 0 deletions Source/WebKit/Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ UIProcess/API/APIUserScript.cpp
UIProcess/API/APIUserStyleSheet.cpp
UIProcess/API/APIWebAuthenticationAssertionResponse.cpp
UIProcess/API/APIWebAuthenticationPanel.cpp
UIProcess/API/APIWebPushDaemonConnection.cpp
UIProcess/API/APIWebsitePolicies.cpp
UIProcess/API/APIWindowFeatures.cpp

Expand Down
3 changes: 3 additions & 0 deletions Source/WebKit/SourcesCocoa.txt
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ UIProcess/API/Cocoa/_WKUserStyleSheet.mm
UIProcess/API/Cocoa/_WKVisitedLinkStore.mm
UIProcess/API/Cocoa/_WKWebAuthenticationAssertionResponse.mm
UIProcess/API/Cocoa/_WKWebAuthenticationPanel.mm
UIProcess/API/Cocoa/_WKWebPushDaemonConnection.mm
UIProcess/API/Cocoa/_WKWebPushMessage.mm
UIProcess/API/Cocoa/_WKWebPushSubscriptionData.mm
UIProcess/API/Cocoa/_WKWebExtension.mm
UIProcess/API/Cocoa/_WKWebPushAction.mm
UIProcess/API/Cocoa/_WKWebsiteDataSize.mm
Expand Down
106 changes: 106 additions & 0 deletions Source/WebKit/UIProcess/API/APIWebPushDaemonConnection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (C) 2024 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 "APIWebPushDaemonConnection.h"

#include "MessageSenderInlines.h"
#include "PushClientConnectionMessages.h"
#include "WebPushMessage.h"
#include <WebCore/ExceptionData.h>
#include <WebCore/PushPermissionState.h>
#include <WebCore/PushSubscriptionData.h>

namespace API {

WebPushDaemonConnection::WebPushDaemonConnection(const WTF::String& machServiceName, const WTF::String& partition, const WTF::String& bundleIdentifier)
#if ENABLE(WEB_PUSH_NOTIFICATIONS)
: m_connection(makeUniqueRef<WebKit::WebPushD::Connection>(machServiceName.utf8(), WebKit::WebPushD::WebPushDaemonConnectionConfiguration { { }, bundleIdentifier, partition, { } }))
#endif
{
}

void WebPushDaemonConnection::getPushPermissionState(const WTF::URL& scopeURL, CompletionHandler<void(WebCore::PushPermissionState)>&& completionHandler)
{
#if ENABLE(WEB_PUSH_NOTIFICATIONS)
m_connection->sendWithAsyncReplyWithoutUsingIPCConnection(Messages::PushClientConnection::GetPushPermissionState(SecurityOriginData::fromURL(scopeURL)), WTFMove(completionHandler));
#else
completionHandler(WebCore::PushPermissionState::Denied);
#endif
}

void WebPushDaemonConnection::requestPushPermission(const WTF::URL& scopeURL, CompletionHandler<void(bool)>&& completionHandler)
{
#if ENABLE(WEB_PUSH_NOTIFICATIONS)
m_connection->sendWithAsyncReplyWithoutUsingIPCConnection(Messages::PushClientConnection::RequestPushPermission(SecurityOriginData::fromURL(scopeURL)), WTFMove(completionHandler));
#else
completionHandler(false);
#endif
}

void WebPushDaemonConnection::setAppBadge(const WTF::URL& scopeURL, std::optional<uint64_t> badge)
{
#if ENABLE(WEB_PUSH_NOTIFICATIONS)
m_connection->sendWithoutUsingIPCConnection(Messages::PushClientConnection::SetAppBadge(SecurityOriginData::fromURL(scopeURL), badge));
#endif
}

void WebPushDaemonConnection::subscribeToPushService(const WTF::URL& scopeURL, const Vector<uint8_t>& applicationServerKey, CompletionHandler<void(const Expected<WebCore::PushSubscriptionData, WebCore::ExceptionData>&)>&& completionHandler)
{
#if ENABLE(WEB_PUSH_NOTIFICATIONS)
m_connection->sendWithAsyncReplyWithoutUsingIPCConnection(Messages::PushClientConnection::SubscribeToPushService(scopeURL, applicationServerKey), WTFMove(completionHandler));
#else
completionHandler(makeUnexpected(WebCore::ExceptionData { WebCore::ExceptionCode::UnknownError, "Cannot subscribe to push service"_s }));
#endif
}

void WebPushDaemonConnection::unsubscribeFromPushService(const WTF::URL& scopeURL, CompletionHandler<void(const Expected<bool, WebCore::ExceptionData>&)>&& completionHandler)
{
#if ENABLE(WEB_PUSH_NOTIFICATIONS)
m_connection->sendWithAsyncReplyWithoutUsingIPCConnection(Messages::PushClientConnection::UnsubscribeFromPushService(scopeURL, std::nullopt), WTFMove(completionHandler));
#else
completionHandler(false);
#endif
}

void WebPushDaemonConnection::getPushSubscription(const WTF::URL& scopeURL, CompletionHandler<void(const Expected<std::optional<WebCore::PushSubscriptionData>, WebCore::ExceptionData>&)>&& completionHandler)
{
#if ENABLE(WEB_PUSH_NOTIFICATIONS)
m_connection->sendWithAsyncReplyWithoutUsingIPCConnection(Messages::PushClientConnection::GetPushSubscription(WTFMove(scopeURL)), WTFMove(completionHandler));
#else
completionHandler(std::optional<WebCore::PushSubscriptionData> { });
#endif
}

void WebPushDaemonConnection::getNextPendingPushMessage(CompletionHandler<void(const std::optional<WebKit::WebPushMessage>&)>&& completionHandler)
{
#if ENABLE(WEB_PUSH_NOTIFICATIONS)
m_connection->sendWithAsyncReplyWithoutUsingIPCConnection(Messages::PushClientConnection::GetPendingPushMessage(), WTFMove(completionHandler));
#else
completionHandler(std::nullopt);
#endif
}

};
64 changes: 64 additions & 0 deletions Source/WebKit/UIProcess/API/APIWebPushDaemonConnection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2024 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 "APIObject.h"
#include "WebPushDaemonConnection.h"

namespace WebCore {
enum class PushPermissionState : uint8_t;
struct ExceptionData;
struct PushSubscriptionData;
}

namespace WebKit {
namespace WebPushD {
class Connection;
}
struct WebPushMessage;
}

namespace API {

class WebPushDaemonConnection final : public ObjectImpl<Object::Type::WebPushDaemonConnection> {
public:
WebPushDaemonConnection(const WTF::String& machServiceName, const WTF::String& partition, const WTF::String& bundleIdentifier);

void getPushPermissionState(const WTF::URL&, CompletionHandler<void(WebCore::PushPermissionState)>&&);
void requestPushPermission(const WTF::URL&, CompletionHandler<void(bool)>&&);
void setAppBadge(const WTF::URL&, std::optional<uint64_t>);
void subscribeToPushService(const WTF::URL&, const Vector<uint8_t>& applicationServerKey, CompletionHandler<void(const Expected<WebCore::PushSubscriptionData, WebCore::ExceptionData>&)>&&);
void unsubscribeFromPushService(const WTF::URL&, CompletionHandler<void(const Expected<bool, WebCore::ExceptionData>&)>&&);
void getPushSubscription(const WTF::URL&, CompletionHandler<void(const Expected<std::optional<WebCore::PushSubscriptionData>, WebCore::ExceptionData>&)>&&);
void getNextPendingPushMessage(CompletionHandler<void(const std::optional<WebKit::WebPushMessage>&)>&&);

private:
#if ENABLE(WEB_PUSH_NOTIFICATIONS)
UniqueRef<WebKit::WebPushD::Connection> m_connection;
#endif
};

} // namespace API
51 changes: 51 additions & 0 deletions Source/WebKit/UIProcess/API/APIWebPushMessage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2024 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 "APIObject.h"
#include "WebPushMessage.h"

namespace API {

class WebPushMessage final : public ObjectImpl<Object::Type::WebPushMessage> {
public:
static Ref<WebPushMessage> create(WebKit::WebPushMessage&& message)
{
return adoptRef(*new WebPushMessage(WTFMove(message)));
}

explicit WebPushMessage(WebKit::WebPushMessage&& message)
: m_message(WTFMove(message)) { }

std::optional<Vector<uint8_t>> data() const { return m_message.pushData; };
WTF::URL scope() const { return m_message.registrationURL; }
WTF::String partition() const { return m_message.pushPartitionString; }

private:
WebKit::WebPushMessage m_message;
};

} // namespace API
52 changes: 52 additions & 0 deletions Source/WebKit/UIProcess/API/APIWebPushSubscriptionData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2024 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 "APIObject.h"
#include <WebCore/PushSubscriptionData.h>

namespace API {

class WebPushSubscriptionData final : public ObjectImpl<Object::Type::WebPushSubscriptionData> {
public:
static Ref<WebPushSubscriptionData> create(WebCore::PushSubscriptionData&& data)
{
return adoptRef(*new WebPushSubscriptionData(WTFMove(data)));
}

explicit WebPushSubscriptionData(WebCore::PushSubscriptionData&& data)
: m_data(WTFMove(data)) { }

WTF::URL endpoint() const { return WTF::URL { m_data.endpoint }; }
Vector<uint8_t> applicationServerKey() const { return m_data.serverVAPIDPublicKey; }
Vector<uint8_t> clientECDHPublicKey() const { return m_data.clientECDHPublicKey; }
Vector<uint8_t> sharedAuthenticationSecret() const { return m_data.sharedAuthenticationSecret; }

private:
const WebCore::PushSubscriptionData m_data;
};

} // namespace API
Loading

0 comments on commit 9f0d6c1

Please sign in to comment.