Skip to content

Commit

Permalink
Move some WebKit code to WebKit framework
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=265324
rdar://118776213

Reviewed by Brent Fulgham.

Move some WebKit code related to WebKit process extensions to WebKit framework. This change enables
us to use new assertion API to take out an assertion on the Networking process when holding locked
files, which is also included in this patch.

* Source/WebKit/NetworkProcess/NetworkProcess.cpp:
(WebKit::NetworkProcess::setIsHoldingLockedFiles):
* Source/WebKit/NetworkProcess/NetworkProcess.h:
* Source/WebKit/NetworkProcess/cocoa/NetworkProcessCocoa.mm:
(WebKit::NetworkProcess::aqcuireLockedFileGrant):
(WebKit::NetworkProcess::invalidateGrant):
(WebKit::NetworkProcess::hasAcquiredGrant const):
* Source/WebKit/Shared/AuxiliaryProcessExtensions/NetworkingProcessExtension.swift:
(NetworkingProcessExtension.handle(_:)):
* Source/WebKit/Shared/AuxiliaryProcessExtensions/WebKitProcessExtension.swift: Copied from Source/WebKit/Shared/AuxiliaryProcessExtensions/NetworkingProcessExtension.swift.
(WKProcessExtension.sharedInstance):
(WKNetworkingProcessExtension.handle(_:)):
(WKNetworkingProcessExtension.grant(_:name:)):
* Source/WebKit/WebKit.xcodeproj/project.pbxproj:

Canonical link: https://commits.webkit.org/271326@main
  • Loading branch information
pvollan committed Nov 30, 2023
1 parent 0769c44 commit c0b5c33
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 18 deletions.
19 changes: 16 additions & 3 deletions Source/WebKit/Configurations/BaseExtension.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,29 @@ CLANG_ENABLE_OBJC_ARC = YES;
CLANG_ENABLE_MODULES = YES;
CLANG_INSTRUMENT_FOR_OPTIMIZATION_PROFILING = NO;
INSTALL_PATH = $(WEBKIT_FRAMEWORKS_DIR)/WebKit.framework/$(WK_FRAMEWORK_VERSION_PREFIX)Extensions;
SWIFT_OBJC_BRIDGING_HEADER[sdk=iphoneos17.4.internal] = Shared/AuxiliaryProcessExtensions/AuxiliaryProcessExtensionBridge.h;
SWIFT_OBJC_BRIDGING_HEADER = $(SWIFT_OBJC_BRIDGING_HEADER_$(USE_INTERNAL_SDK));
SWIFT_OBJC_BRIDGING_HEADER_ = ;
SWIFT_OBJC_BRIDGING_HEADER_YES = ;
SWIFT_OBJC_BRIDGING_HEADER_YES[sdk=iphone*] = Shared/AuxiliaryProcessExtensions/AuxiliaryProcessExtensionBridge.h;
SWIFT_OBJC_BRIDGING_HEADER_YES[sdk=iphone*17.0*] = ;
SWIFT_OBJC_BRIDGING_HEADER_YES[sdk=iphone*17.1*] = ;
SWIFT_OBJC_BRIDGING_HEADER_YES[sdk=iphone*17.2*] = ;
SWIFT_OBJC_BRIDGING_HEADER_YES[sdk=iphone*17.3*] = ;
SWIFT_VERSION = 5.0;
SWIFT_OPTIMIZATION_LEVEL = -O;
SWIFT_OPTIMIZATION_LEVEL[config=Debug] = -Onone;
PRODUCT_NAME = $(TARGET_NAME);
SUPPORTS_MACCATALYST = NO;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
TARGETED_DEVICE_FAMILY = 1,2,7;
EXCLUDED_SOURCE_FILE_NAMES = *;
EXCLUDED_SOURCE_FILE_NAMES[sdk=iphoneos17.4.internal] = ;
EXCLUDED_SOURCE_FILE_NAMES = $(EXCLUDED_SOURCE_FILE_NAMES_$(USE_INTERNAL_SDK));
EXCLUDED_SOURCE_FILE_NAMES_ = *;
EXCLUDED_SOURCE_FILE_NAMES_YES = *;
EXCLUDED_SOURCE_FILE_NAMES_YES[sdk=iphone*] = ;
EXCLUDED_SOURCE_FILE_NAMES_YES[sdk=iphone*17.0*] = *;
EXCLUDED_SOURCE_FILE_NAMES_YES[sdk=iphone*17.1*] = *;
EXCLUDED_SOURCE_FILE_NAMES_YES[sdk=iphone*17.2*] = *;
EXCLUDED_SOURCE_FILE_NAMES_YES[sdk=iphone*17.3*] = *;
CODE_SIGN_ENTITLEMENTS[sdk=*simulator] = Shared/AuxiliaryProcessExtensions/AuxiliaryProcessExtension.entitlements;
CODE_SIGN_INJECT_BASE_ENTITLEMENTS = NO;
WK_PROCESSED_XCENT_FILE=$(TEMP_FILE_DIR)/$(FULL_PRODUCT_NAME).entitlements
Expand Down
13 changes: 11 additions & 2 deletions Source/WebKit/Configurations/WebKit.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,15 @@ WK_EXCLUDED_WEBPUSHD_SANDBOX = $(WK_EXCLUDED_WEBPUSHD_SANDBOX_$(WK_RELOCATABLE_W
WK_EXCLUDED_WEBPUSHD_SANDBOX_NO = com.apple.WebKit.webpushd.relocatable.mac.sb
WK_EXCLUDED_WEBPUSHD_SANDBOX_YES = com.apple.WebKit.webpushd.mac.sb

EXCLUDED_SOURCE_FILE_NAMES = $(EXCLUDED_PRODUCT_DEPENDENCY_NAMES) $(EXCLUDED_IOS_RESOURCE_FILE_NAMES) $(EXCLUDED_MACOS_PLUGIN_FILE_NAMES) $(EXCLUDED_MIGRATED_HEADERS_COCOA_TOUCH_$(WK_IS_COCOA_TOUCH)) $(WK_EXCLUDED_WEBPUSHD_SANDBOX);
EXCLUDED_EXTENSION_SOURCE_FILE_NAMES = Shared/AuxiliaryProcessExtensions/*;

INCLUDED_EXTENSION_SOURCE_FILE_NAMES_YES[sdk=iphone*] = Shared/AuxiliaryProcessExtensions/*;
INCLUDED_EXTENSION_SOURCE_FILE_NAMES_YES[sdk=iphone*17.0.internal] = ;
INCLUDED_EXTENSION_SOURCE_FILE_NAMES_YES[sdk=iphone*17.1.internal] = ;
INCLUDED_EXTENSION_SOURCE_FILE_NAMES_YES[sdk=iphone*17.2.internal] = ;
INCLUDED_EXTENSION_SOURCE_FILE_NAMES_YES[sdk=iphone*17.3.internal] = ;

EXCLUDED_SOURCE_FILE_NAMES = $(EXCLUDED_PRODUCT_DEPENDENCY_NAMES) $(EXCLUDED_IOS_RESOURCE_FILE_NAMES) $(EXCLUDED_MACOS_PLUGIN_FILE_NAMES) $(EXCLUDED_MIGRATED_HEADERS_COCOA_TOUCH_$(WK_IS_COCOA_TOUCH)) $(WK_EXCLUDED_WEBPUSHD_SANDBOX) $(EXCLUDED_EXTENSION_SOURCE_FILE_NAMES);

// These are only available in internal builds, so they must be explicitly ignored for the open-source build to succeed.
EXCLUDED_PRODUCT_DEPENDENCY_NAMES = libWebKitAdditions.a AuthenticationServicesCore.framework;
Expand All @@ -245,7 +253,8 @@ EXCLUDED_IOS_RESOURCE_FILE_NAMES[sdk=iphone*] = ;
EXCLUDED_MIGRATED_HEADERS_COCOA_TOUCH_YES = WebDynamicScrollBarsView.h WebEventRegion.h WebIconDatabase.h WebJavaScriptTextInputPanel.h WebNSEventExtras.h WebNSPasteboardExtras.h WebNSWindowExtras.h WebPanelAuthenticationHandler.h WebStringTruncator.h;
EXCLUDED_MIGRATED_HEADERS_COCOA_TOUCH_NO = AbstractPasteboard.h DOMHTMLTextAreaElementPrivate.h DOMUIKitExtensions.h KeyEventCodesIOS.h WAKAppKitStubs.h WAKResponder.h WAKView.h WAKWindow.h WebCaretChangeListener.h WebCoreThread.h WebCoreThreadMessage.h WebCoreThreadRun.h WebEvent.h WebEventRegion.h WebFixedPositionContent.h WebFrameIOS.h WebFrameIPhone.h WebGeolocationCoreLocationProvider.h WebGeolocationPrivate.h WebGeolocationProviderIOS.h WebItemProviderPasteboard.h WebMIMETypeRegistry.h WebNSStringExtrasIOS.h WebNSStringExtrasIPhone.h WebPDFViewIOS.h WebPDFViewIPhone.h WebPDFViewPlaceholder.h WebSelectionRect.h WebUIKitDelegate.h WebUIKitSupport.h WebVisiblePosition.h WKContentObservation.h WKGraphics.h WKTypes.h;

INCLUDED_SOURCE_FILE_NAMES = $(INCLUDED_MIGRATED_HEADERS_$(USE_INTERNAL_SDK)_$(WK_COCOA_TOUCH)) $(INCLUDED_MIGRATED_HEADERS_$(ENABLE_IOS_TOUCH_EVENTS));
INCLUDED_SOURCE_FILE_NAMES = $(INCLUDED_MIGRATED_HEADERS_$(USE_INTERNAL_SDK)_$(WK_COCOA_TOUCH)) $(INCLUDED_MIGRATED_HEADERS_$(ENABLE_IOS_TOUCH_EVENTS)) $(INCLUDED_EXTENSION_SOURCE_FILE_NAMES_$(USE_INTERNAL_SDK));

// WebEventRegion.h is migrated when building for an iOS family target with an internal SDK...
INCLUDED_MIGRATED_HEADERS_YES_cocoatouch = WebEventRegion.h;
// ...or when --ios-touch-events was passed to build-webkit manually.
Expand Down
10 changes: 10 additions & 0 deletions Source/WebKit/NetworkProcess/NetworkProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2941,12 +2941,22 @@ void NetworkProcess::setIsHoldingLockedFiles(bool isHoldingLockedFiles)
#else
if (!isHoldingLockedFiles) {
m_holdingLockedFileAssertion = nullptr;
#if USE(EXTENSIONKIT)
invalidateGrant();
#endif
return;
}

if (m_holdingLockedFileAssertion && m_holdingLockedFileAssertion->isValid())
return;

#if USE(EXTENSIONKIT)
if (hasAcquiredGrant())
return;
if (aqcuireLockedFileGrant())
return;
#endif

// We synchronously take a process assertion when beginning a SQLite transaction so that we don't get suspended
// while holding a locked file. We would get killed if suspended while holding locked files.
m_holdingLockedFileAssertion = ProcessAssertion::create(getCurrentProcessID(), "Network Process is holding locked files"_s, ProcessAssertionType::FinishTaskInterruptable, ProcessAssertion::Mode::Sync);
Expand Down
12 changes: 12 additions & 0 deletions Source/WebKit/NetworkProcess/NetworkProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
typedef struct OpaqueCFHTTPCookieStorage* CFHTTPCookieStorageRef;
#endif

#if USE(EXTENSIONKIT)
OBJC_CLASS Grant;
#endif

namespace IPC {
class FormDataReference;
}
Expand Down Expand Up @@ -523,6 +527,11 @@ class NetworkProcess : public AuxiliaryProcess, private DownloadManager::Client,

#if USE(RUNNINGBOARD)
void setIsHoldingLockedFiles(bool);
#endif
#if USE(EXTENSIONKIT)
bool aqcuireLockedFileGrant();
void invalidateGrant();
bool hasAcquiredGrant() const;
#endif
void stopRunLoopIfNecessary();

Expand Down Expand Up @@ -559,6 +568,9 @@ class NetworkProcess : public AuxiliaryProcess, private DownloadManager::Client,

#if USE(RUNNINGBOARD)
WebSQLiteDatabaseTracker m_webSQLiteDatabaseTracker;
#if USE(EXTENSIONKIT)
RetainPtr<Grant> m_holdingLockedFileGrant;
#endif
RefPtr<ProcessAssertion> m_holdingLockedFileAssertion;
#endif

Expand Down
35 changes: 35 additions & 0 deletions Source/WebKit/NetworkProcess/cocoa/NetworkProcessCocoa.mm
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
#import <pal/spi/cocoa/NEFilterSourceSPI.h>
#endif

#if USE(EXTENSIONKIT)
#import "ExtensionKitSPI.h"
#import <WebKit-Swift.h>
#endif

namespace WebKit {

static void initializeNetworkSettings()
Expand Down Expand Up @@ -251,4 +256,34 @@ void saveCookies(NSHTTPCookieStorage *cookieStorage, CompletionHandler<void()>&&
}
#endif // HAVE(NW_PROXY_CONFIG)

#if USE(RUNNINGBOARD) && USE(EXTENSIONKIT)
bool NetworkProcess::aqcuireLockedFileGrant()
{
NSError* error = nil;
m_holdingLockedFileGrant = [WKNetworkingProcessExtension.sharedInstance grant:@"com.apple.common" name:@"FinishTaskInterruptable" error:&error];
if (m_holdingLockedFileGrant)
RELEASE_LOG(Process, "Successfully took assertion on Network process for locked file");
else
RELEASE_LOG_ERROR(Process, "Unable to take assertion on Network process for locked file, error = %@", error);
return !!m_holdingLockedFileGrant;
}

void NetworkProcess::invalidateGrant()
{
if (!hasAcquiredGrant())
return;

NSError* error = nil;
[m_holdingLockedFileGrant invalidateWithError:&error];
if (error)
RELEASE_LOG_ERROR(Process, "Unable to invalidate grant, error = %@", error);
m_holdingLockedFileGrant = nil;
}

bool NetworkProcess::hasAcquiredGrant() const
{
return !!m_holdingLockedFileGrant;
}
#endif

} // namespace WebKit
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@

import ServiceExtensions
@_spi(Private) import ServiceExtensions
@_spi(Private) import WebKit

@main
class NetworkingProcessExtension {
required init() {}
class NetworkingProcessExtension : WKNetworkingProcessExtension {
required init() {
super.init()
}
}

extension NetworkingProcessExtension: NetworkingServiceExtension {
func handle(xpcConnection: xpc_connection_t) {
extension NetworkingProcessExtension {
@objc(handle:)
override open func handle(xpcConnection: xpc_connection_t) {
handleNewConnection(xpcConnection)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2023 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.
*/

import ServiceExtensions
@_spi(Private) import ServiceExtensions

@objc
@_spi(Private)
open class Grant: NSObject {
let inner : _Capabilities.Grant

init(inner: _Capabilities.Grant) {
self.inner = inner
}

@objc(invalidateWithError:)
open func invalidate() throws {
try self.inner.invalidate()
}
}

@_spi(Private)
@objc(WKNetworkingProcessExtension)
open class WKNetworkingProcessExtension : NSObject {
@objc(sharedInstance)
static public var sharedInstance: WKNetworkingProcessExtension? = nil
required public override init() {
super.init()
WKNetworkingProcessExtension.sharedInstance = self
}
}

extension WKNetworkingProcessExtension: NetworkingServiceExtension {
@objc(handle:)
open func handle(xpcConnection: xpc_connection_t) {
}

@objc(grant:name:error:)
open func grant(domain: String, name: String) throws -> Any {
let grant = try self._request(capabilities: _Capabilities.assertion(domain, name))
return Grant(inner: grant)
}
}
Loading

0 comments on commit c0b5c33

Please sign in to comment.