Skip to content

Commit

Permalink
Support using higher priority for a _WKDataTask
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=270112
rdar://121960086

Reviewed by Tim Horton.

* Source/WebKit/NetworkProcess/NetworkProcess.cpp:
(WebKit::NetworkProcess::cancelDataTask):
* Source/WebKit/NetworkProcess/NetworkProcess.h:
* Source/WebKit/NetworkProcess/NetworkProcess.messages.in:
* Source/WebKit/UIProcess/API/APIDataTask.cpp:
(API::DataTask::cancel):
(API::DataTask::networkProcessCrashed):
(API::DataTask::DataTask):
(API::m_client):
(API::DataTask::didCompleteWithError):
* Source/WebKit/UIProcess/API/APIDataTask.h:
* Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView _dataTaskWithRequest:runAtForegroundPriority:completionHandler:]):
(-[WKWebView _dataTaskWithRequest:completionHandler:]):
* Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h:
* Source/WebKit/UIProcess/Network/NetworkProcessProxy.cpp:
(WebKit::NetworkProcessProxy::dataTaskWithRequest):
(WebKit::NetworkProcessProxy::dataTaskDidCompleteWithError):
(WebKit::NetworkProcessProxy::cancelDataTask):
* Source/WebKit/UIProcess/Network/NetworkProcessProxy.h:
* Source/WebKit/UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::dataTaskWithRequest):
* Source/WebKit/UIProcess/WebPageProxy.h:

Canonical link: https://commits.webkit.org/275387@main
  • Loading branch information
cdumez committed Feb 27, 2024
1 parent 606b5ed commit 5b8fdce
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 19 deletions.
3 changes: 2 additions & 1 deletion Source/WebKit/NetworkProcess/NetworkProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2156,10 +2156,11 @@ void NetworkProcess::dataTaskWithRequest(WebPageProxyIdentifier pageID, PAL::Ses
networkSession(sessionID)->dataTaskWithRequest(pageID, WTFMove(request), topOrigin, WTFMove(completionHandler));
}

void NetworkProcess::cancelDataTask(DataTaskIdentifier identifier, PAL::SessionID sessionID)
void NetworkProcess::cancelDataTask(DataTaskIdentifier identifier, PAL::SessionID sessionID, CompletionHandler<void()>&& completionHandler)
{
if (auto* session = networkSession(sessionID))
session->cancelDataTask(identifier);
completionHandler();
}

void NetworkProcess::setCacheModelSynchronouslyForTesting(CacheModel cacheModel, CompletionHandler<void()>&& completionHandler)
Expand Down
2 changes: 1 addition & 1 deletion Source/WebKit/NetworkProcess/NetworkProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ class NetworkProcess : public AuxiliaryProcess, private DownloadManager::Client,
void publishDownloadProgress(DownloadID, const URL&, SandboxExtensionHandle&&);
#endif
void dataTaskWithRequest(WebPageProxyIdentifier, PAL::SessionID, WebCore::ResourceRequest&&, const std::optional<WebCore::SecurityOriginData>& topOrigin, IPC::FormDataReference&&, CompletionHandler<void(DataTaskIdentifier)>&&);
void cancelDataTask(DataTaskIdentifier, PAL::SessionID);
void cancelDataTask(DataTaskIdentifier, PAL::SessionID, CompletionHandler<void()>&&);
void applicationDidEnterBackground();
void applicationWillEnterForeground();

Expand Down
2 changes: 1 addition & 1 deletion Source/WebKit/NetworkProcess/NetworkProcess.messages.in
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ messages -> NetworkProcess LegacyReceiver {
PublishDownloadProgress(WebKit::DownloadID downloadID, URL url, WebKit::SandboxExtension::Handle sandboxExtensionHandle)
#endif
DataTaskWithRequest(WebKit::WebPageProxyIdentifier pageID, PAL::SessionID sessionID, WebCore::ResourceRequest request, std::optional<WebCore::SecurityOriginData> topOrigin, IPC::FormDataReference requestBody) -> (WebKit::DataTaskIdentifier taskIdentifier)
CancelDataTask(WebKit::DataTaskIdentifier taskIdentifier, PAL::SessionID sessionID)
CancelDataTask(WebKit::DataTaskIdentifier taskIdentifier, PAL::SessionID sessionID) -> ()
ApplicationDidEnterBackground()
ApplicationWillEnterForeground()

Expand Down
16 changes: 14 additions & 2 deletions Source/WebKit/UIProcess/API/APIDataTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,31 @@ void DataTask::cancel()
{
if (m_networkProcess && m_sessionID)
m_networkProcess->cancelDataTask(m_identifier, *m_sessionID);
m_activity = nullptr;
}

void DataTask::networkProcessCrashed()
{
m_activity = nullptr;
m_client->didCompleteWithError(*this, WebCore::internalError(m_originalURL));
}

DataTask::DataTask(WebKit::DataTaskIdentifier identifier, WeakPtr<WebKit::WebPageProxy>&& page, WTF::URL&& originalURL)
DataTask::DataTask(WebKit::DataTaskIdentifier identifier, WeakPtr<WebKit::WebPageProxy>&& page, WTF::URL&& originalURL, bool shouldRunAtForegroundPriority)
: m_identifier(identifier)
, m_page(WTFMove(page))
, m_originalURL(WTFMove(originalURL))
, m_networkProcess(m_page ? WeakPtr { m_page->websiteDataStore().networkProcess() } : nullptr)
, m_sessionID(m_page ? std::optional<PAL::SessionID> { m_page->sessionID() } : std::nullopt)
, m_client(DataTaskClient::create()) { }
, m_client(DataTaskClient::create())
{
if (RefPtr networkProcess = m_networkProcess.get())
m_activity = shouldRunAtForegroundPriority ? networkProcess->throttler().foregroundActivity("WKDataTask"_s).moveToUniquePtr() : networkProcess->throttler().backgroundActivity("WKDataTask"_s).moveToUniquePtr();
}

void DataTask::didCompleteWithError(WebCore::ResourceError&& error)
{
m_activity = nullptr;
m_client->didCompleteWithError(*this, WTFMove(error));
}

} // namespace API
9 changes: 8 additions & 1 deletion Source/WebKit/UIProcess/API/APIDataTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@
#include <wtf/URL.h>
#include <wtf/WeakPtr.h>

namespace WebCore {
class ResourceError;
}

namespace WebKit {
class NetworkProcessProxy;
class ProcessThrottlerActivity;
class WebPageProxy;
}

Expand All @@ -56,16 +61,18 @@ class DataTask : public API::ObjectImpl<API::Object::Type::DataTask> {
const DataTaskClient& client() const { return m_client.get(); }
void setClient(Ref<DataTaskClient>&&);
void networkProcessCrashed();
void didCompleteWithError(WebCore::ResourceError&&);

private:
DataTask(WebKit::DataTaskIdentifier, WeakPtr<WebKit::WebPageProxy>&&, WTF::URL&&);
DataTask(WebKit::DataTaskIdentifier, WeakPtr<WebKit::WebPageProxy>&&, WTF::URL&&, bool shouldRunAtForegroundPriority);

WebKit::DataTaskIdentifier m_identifier;
WeakPtr<WebKit::WebPageProxy> m_page;
WTF::URL m_originalURL;
WeakPtr<WebKit::NetworkProcessProxy> m_networkProcess;
std::optional<PAL::SessionID> m_sessionID;
Ref<DataTaskClient> m_client;
std::unique_ptr<WebKit::ProcessThrottlerActivity> m_activity;
};

} // namespace API
9 changes: 7 additions & 2 deletions Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2470,13 +2470,18 @@ - (void)_startImageAnalysis:(NSString *)sourceLanguageIdentifier target:(NSStrin
#endif
}

- (void)_dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void(^)(_WKDataTask *))completionHandler
- (void)_dataTaskWithRequest:(NSURLRequest *)request runAtForegroundPriority:(BOOL)runAtForegroundPriority completionHandler:(void(^)(_WKDataTask *))completionHandler
{
_page->dataTaskWithRequest(request, std::nullopt, [completionHandler = makeBlockPtr(completionHandler)] (Ref<API::DataTask>&& task) {
_page->dataTaskWithRequest(request, std::nullopt, !!runAtForegroundPriority, [completionHandler = makeBlockPtr(completionHandler)] (Ref<API::DataTask>&& task) {
completionHandler(wrapper(task));
});
}

- (void)_dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void(^)(_WKDataTask *))completionHandler
{
[self _dataTaskWithRequest:request runAtForegroundPriority:NO completionHandler:completionHandler];
}

- (void)_takeFindStringFromSelection:(id)sender
{
THROW_IF_SUSPENDED;
Expand Down
1 change: 1 addition & 0 deletions Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ for this property.
- (void)_startImageAnalysis:(NSString *)identifier target:(NSString *)targetIdentifier WK_API_AVAILABLE(macos(13.0), ios(16.0));

- (void)_dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void(^)(_WKDataTask *))completionHandler WK_API_AVAILABLE(macos(13.0), ios(16.0));
- (void)_dataTaskWithRequest:(NSURLRequest *)request runAtForegroundPriority:(BOOL)runAtForegroundPriority completionHandler:(void(^)(_WKDataTask *))completionHandler WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));

// Default value is 0. A value of 0 means the window's backing scale factor will be used and automatically update when the window moves screens.
@property (nonatomic, setter=_setOverrideDeviceScaleFactor:) CGFloat _overrideDeviceScaleFactor WK_API_AVAILABLE(macos(10.11), ios(16.4));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,8 @@ - (void)completeLoad

auto request = WebCore::ResourceRequest(url);
WeakPtr weakThis { *this };
m_webPageProxy.dataTaskWithRequest(WTFMove(request), topOrigin, [weakThis, completionHandler = WTFMove(completionHandler)] (Ref<API::DataTask>&& task) mutable {
bool shouldRunAtForegroundPriority = false;
m_webPageProxy.dataTaskWithRequest(WTFMove(request), topOrigin, shouldRunAtForegroundPriority, [weakThis, completionHandler = WTFMove(completionHandler)] (Ref<API::DataTask>&& task) mutable {
if (!weakThis)
return completionHandler();

Expand Down
13 changes: 7 additions & 6 deletions Source/WebKit/UIProcess/Network/NetworkProcessProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,17 @@ Ref<DownloadProxy> NetworkProcessProxy::createDownloadProxy(WebsiteDataStore& da
return m_downloadProxyMap->createDownloadProxy(dataStore, WTFMove(client), resourceRequest, frameInfo, originatingPage);
}

void NetworkProcessProxy::dataTaskWithRequest(WebPageProxy& page, PAL::SessionID sessionID, WebCore::ResourceRequest&& request, const std::optional<SecurityOriginData>& topOrigin, CompletionHandler<void(API::DataTask&)>&& completionHandler)
void NetworkProcessProxy::dataTaskWithRequest(WebPageProxy& page, PAL::SessionID sessionID, WebCore::ResourceRequest&& request, const std::optional<SecurityOriginData>& topOrigin, bool shouldRunAtForegroundPriority, CompletionHandler<void(API::DataTask&)>&& completionHandler)
{
sendWithAsyncReply(Messages::NetworkProcess::DataTaskWithRequest(page.identifier(), sessionID, request, topOrigin, IPC::FormDataReference(request.httpBody())), [this, protectedThis = Ref { *this }, weakPage = WeakPtr { page }, completionHandler = WTFMove(completionHandler), originalURL = request.url()] (DataTaskIdentifier identifier) mutable {
auto dataTask = API::DataTask::create(identifier, WTFMove(weakPage), WTFMove(originalURL));
auto activity = shouldRunAtForegroundPriority ? throttler().foregroundActivity("WKDataTask initialization"_s) : throttler().backgroundActivity("WKDataTask initialization"_s);
sendWithAsyncReply(Messages::NetworkProcess::DataTaskWithRequest(page.identifier(), sessionID, request, topOrigin, IPC::FormDataReference(request.httpBody())), [this, protectedThis = Ref { *this }, weakPage = WeakPtr { page }, activity = WTFMove(activity), shouldRunAtForegroundPriority, completionHandler = WTFMove(completionHandler), originalURL = request.url()] (DataTaskIdentifier identifier) mutable {
auto dataTask = API::DataTask::create(identifier, WTFMove(weakPage), WTFMove(originalURL), shouldRunAtForegroundPriority);
completionHandler(dataTask);
if (decltype(m_dataTasks)::isValidKey(identifier))
m_dataTasks.add(identifier, WTFMove(dataTask));
else
dataTask->networkProcessCrashed();
});
}, 0, { }, ShouldStartProcessThrottlerActivity::No);
}

void NetworkProcessProxy::dataTaskReceivedChallenge(DataTaskIdentifier identifier, WebCore::AuthenticationChallenge&& challenge, CompletionHandler<void(AuthenticationChallengeDisposition, WebCore::Credential&&)>&& completionHandler)
Expand Down Expand Up @@ -396,13 +397,13 @@ void NetworkProcessProxy::dataTaskDidCompleteWithError(DataTaskIdentifier identi
{
MESSAGE_CHECK(decltype(m_dataTasks)::isValidKey(identifier));
if (auto task = m_dataTasks.take(identifier))
task->client().didCompleteWithError(*task, WTFMove(error));
task->didCompleteWithError(WTFMove(error));
}

void NetworkProcessProxy::cancelDataTask(DataTaskIdentifier identifier, PAL::SessionID sessionID)
{
m_dataTasks.remove(identifier);
send(Messages::NetworkProcess::CancelDataTask(identifier, sessionID), 0);
sendWithAsyncReply(Messages::NetworkProcess::CancelDataTask(identifier, sessionID), [] { });
}

void NetworkProcessProxy::fetchWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> dataTypes, OptionSet<WebsiteDataFetchOption> fetchOptions, CompletionHandler<void(WebsiteData)>&& completionHandler)
Expand Down
2 changes: 1 addition & 1 deletion Source/WebKit/UIProcess/Network/NetworkProcessProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class NetworkProcessProxy final : public AuxiliaryProcessProxy {
void getNetworkProcessConnection(WebProcessProxy&, CompletionHandler<void(NetworkProcessConnectionInfo&&)>&&);

Ref<DownloadProxy> createDownloadProxy(WebsiteDataStore&, Ref<API::DownloadClient>&&, const WebCore::ResourceRequest&, const FrameInfoData&, WebPageProxy* originatingPage);
void dataTaskWithRequest(WebPageProxy&, PAL::SessionID, WebCore::ResourceRequest&&, const std::optional<WebCore::SecurityOriginData>& topOrigin, CompletionHandler<void(API::DataTask&)>&&);
void dataTaskWithRequest(WebPageProxy&, PAL::SessionID, WebCore::ResourceRequest&&, const std::optional<WebCore::SecurityOriginData>& topOrigin, bool shouldRunAtForegroundPriority, CompletionHandler<void(API::DataTask&)>&&);

void addAllowedFirstPartyForCookies(WebProcessProxy&, const WebCore::RegistrableDomain& firstPartyForCookies, LoadedWebArchive, CompletionHandler<void()>&&);

Expand Down
4 changes: 2 additions & 2 deletions Source/WebKit/UIProcess/WebPageProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7999,9 +7999,9 @@ void WebPageProxy::downloadRequest(WebCore::ResourceRequest&& request, Completio
download->setDidStartCallback(WTFMove(completionHandler));
}

void WebPageProxy::dataTaskWithRequest(WebCore::ResourceRequest&& request, const std::optional<WebCore::SecurityOriginData>& topOrigin, CompletionHandler<void(API::DataTask&)>&& completionHandler)
void WebPageProxy::dataTaskWithRequest(WebCore::ResourceRequest&& request, const std::optional<WebCore::SecurityOriginData>& topOrigin, bool shouldRunAtForegroundPriority, CompletionHandler<void(API::DataTask&)>&& completionHandler)
{
websiteDataStore().protectedNetworkProcess()->dataTaskWithRequest(*this, sessionID(), WTFMove(request), topOrigin, WTFMove(completionHandler));
websiteDataStore().protectedNetworkProcess()->dataTaskWithRequest(*this, sessionID(), WTFMove(request), topOrigin, shouldRunAtForegroundPriority, WTFMove(completionHandler));
}

void WebPageProxy::didChangeContentSize(const IntSize& size)
Expand Down
2 changes: 1 addition & 1 deletion Source/WebKit/UIProcess/WebPageProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -1581,7 +1581,7 @@ class WebPageProxy final : public API::ObjectImpl<API::Object::Type::Page>, publ

void resumeDownload(const API::Data& resumeData, const String& path, CompletionHandler<void(DownloadProxy*)>&&);
void downloadRequest(WebCore::ResourceRequest&&, CompletionHandler<void(DownloadProxy*)>&&);
void dataTaskWithRequest(WebCore::ResourceRequest&&, const std::optional<WebCore::SecurityOriginData>& topOrigin, CompletionHandler<void(API::DataTask&)>&&);
void dataTaskWithRequest(WebCore::ResourceRequest&&, const std::optional<WebCore::SecurityOriginData>& topOrigin, bool shouldRunAtForegroundPriority, CompletionHandler<void(API::DataTask&)>&&);

void advanceToNextMisspelling(bool startBeforeSelection);
void changeSpellingToWord(const String& word);
Expand Down

0 comments on commit 5b8fdce

Please sign in to comment.