1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
|
/*
* Copyright (C) 2017 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 "SWServerJobQueue.h"
#include "ExceptionData.h"
#include "Logging.h"
#include "SWServer.h"
#include "SWServerRegistration.h"
#include "SWServerWorker.h"
#include "SecurityOrigin.h"
#include "ServiceWorkerRegistrationData.h"
#include "ServiceWorkerUpdateViaCache.h"
#include "WorkerFetchResult.h"
#include "WorkerType.h"
#include <wtf/TZoneMallocInlines.h>
#include <wtf/text/MakeString.h>
namespace WebCore {
WTF_MAKE_TZONE_ALLOCATED_IMPL(SWServerJobQueue);
SWServerJobQueue::SWServerJobQueue(SWServer& server, const ServiceWorkerRegistrationKey& key)
: m_jobTimer(*this, &SWServerJobQueue::runNextJobSynchronously)
, m_server(server)
, m_registrationKey(key)
{
}
SWServerJobQueue::~SWServerJobQueue()
{
}
bool SWServerJobQueue::isCurrentlyProcessingJob(const ServiceWorkerJobDataIdentifier& jobDataIdentifier) const
{
return !m_jobQueue.isEmpty() && firstJob().identifier() == jobDataIdentifier;
}
static bool doCertificatesMatch(const CertificateInfo& first, const CertificateInfo& second)
{
#if PLATFORM(COCOA)
return first.trust() == second.trust() || certificatesMatch(first.trust().get(), second.trust().get());
#else
// FIXME: Add support for certificate matching in CertificateInfo.
UNUSED_PARAM(first);
UNUSED_PARAM(second);
return true;
#endif
}
void SWServerJobQueue::scriptFetchFinished(const ServiceWorkerJobDataIdentifier& jobDataIdentifier, const std::optional<ProcessIdentifier>& requestingProcessIdentifier, WorkerFetchResult&& result)
{
if (!isCurrentlyProcessingJob(jobDataIdentifier))
return;
auto& job = firstJob();
Ref server = m_server.get();
RefPtr registration = server->getRegistration(m_registrationKey);
if (!registration)
return;
RefPtr newestWorker = registration->getNewestWorker();
if (!result.error.isNull()) {
// Invoke Reject Job Promise with job and TypeError.
server->rejectJob(job, ExceptionData { ExceptionCode::TypeError, makeString("Script URL "_s, job.scriptURL.string(), " fetch resulted in error: "_s, result.error.localizedDescription()) });
// If newestWorker is null, invoke Clear Registration algorithm passing registration as its argument.
if (!newestWorker)
registration->clear(); // Will destroy the registration.
// Invoke Finish Job with job and abort these steps.
finishCurrentJob();
return;
}
registration->setLastUpdateTime(WallTime::now());
// If newestWorker is not null, newestWorker's script url equals job's script url with the exclude fragments
// flag set, and script's source text is a byte-for-byte match with newestWorker's script resource's source
// text, then:
if (newestWorker && equalIgnoringFragmentIdentifier(newestWorker->scriptURL(), job.scriptURL) && newestWorker->type() == job.workerType && result.script == newestWorker->script() && doCertificatesMatch(result.certificateInfo, newestWorker->certificateInfo())) {
auto scriptURLs = newestWorker->importedScriptURLs();
if (!scriptURLs.isEmpty()) {
m_workerFetchResult = WTFMove(result);
protectedServer()->refreshImportedScripts(job, *registration, scriptURLs, requestingProcessIdentifier);
return;
}
// FIXME: for non classic scripts, check the script’s module record's [[ECMAScriptCode]].
RELEASE_LOG(ServiceWorker, "%p - SWServerJobQueue::scriptFetchFinished, script, certificate and imported scripts are matching for registrationID=%" PRIu64, this, registration->identifier().toUInt64());
scriptAndImportedScriptsFetchFinished(job, *registration);
return;
}
protectedServer()->updateWorker(job.identifier(), requestingProcessIdentifier, *registration, job.scriptURL, result.script, result.certificateInfo, result.contentSecurityPolicy, result.crossOriginEmbedderPolicy, result.referrerPolicy, job.workerType, { }, job.serviceWorkerPageIdentifier());
}
void SWServerJobQueue::importedScriptsFetchFinished(const ServiceWorkerJobDataIdentifier& jobDataIdentifier, const Vector<std::pair<URL, ScriptBuffer>>& importedScripts, const std::optional<ProcessIdentifier>& requestingProcessIdentifier)
{
if (!isCurrentlyProcessingJob(jobDataIdentifier))
return;
auto& job = firstJob();
RefPtr registration = protectedServer()->getRegistration(m_registrationKey);
if (!registration)
return;
RefPtr newestWorker = registration->getNewestWorker();
if (newestWorker && newestWorker->matchingImportedScripts(importedScripts)) {
RELEASE_LOG(ServiceWorker, "%p - SWServerJobQueue::importedScriptsFetchFinished, script, certificate and imported scripts are matching for registrationID=%" PRIu64, this, registration->identifier().toUInt64());
scriptAndImportedScriptsFetchFinished(job, *registration);
return;
}
protectedServer()->updateWorker(job.identifier(), requestingProcessIdentifier, *registration, job.scriptURL, m_workerFetchResult.script, m_workerFetchResult.certificateInfo, m_workerFetchResult.contentSecurityPolicy, m_workerFetchResult.crossOriginEmbedderPolicy, m_workerFetchResult.referrerPolicy, job.workerType, { }, job.serviceWorkerPageIdentifier());
}
void SWServerJobQueue::scriptAndImportedScriptsFetchFinished(const ServiceWorkerJobData& job, SWServerRegistration& registration)
{
// Invoke Resolve Job Promise with job and registration.
protectedServer()->resolveRegistrationJob(job, registration.data(), ShouldNotifyWhenResolved::No);
// Invoke Finish Job with job and abort these steps.
finishCurrentJob();
}
// https://w3c.github.io/ServiceWorker/#update-algorithm
void SWServerJobQueue::scriptContextFailedToStart(const ServiceWorkerJobDataIdentifier& jobDataIdentifier, ServiceWorkerIdentifier, const String& message)
{
if (!isCurrentlyProcessingJob(jobDataIdentifier))
return;
// If an uncaught runtime script error occurs during the above step, then:
Ref server = m_server.get();
RefPtr registration = server->getRegistration(m_registrationKey);
ASSERT(registration);
if (!registration || !registration->preInstallationWorker()) {
RELEASE_LOG_ERROR(ServiceWorker, "SWServerJobQueue::scriptContextFailedToStart registration is null (%d) or pre installation worker is null", !registration);
return;
}
ASSERT(registration->preInstallationWorker());
registration->protectedPreInstallationWorker()->terminate();
registration->setPreInstallationWorker(nullptr);
// Invoke Reject Job Promise with job and TypeError.
server->rejectJob(firstJob(), { ExceptionCode::TypeError, message });
// If newestWorker is null, invoke Clear Registration algorithm passing registration as its argument.
if (!registration->getNewestWorker())
registration->clear(); // Will destroy the registation.
// Invoke Finish Job with job and abort these steps.
finishCurrentJob();
}
void SWServerJobQueue::scriptContextStarted(const ServiceWorkerJobDataIdentifier& jobDataIdentifier, ServiceWorkerIdentifier identifier)
{
if (!isCurrentlyProcessingJob(jobDataIdentifier))
return;
RefPtr registration = protectedServer()->getRegistration(m_registrationKey);
ASSERT(registration);
if (!registration) {
RELEASE_LOG_ERROR(ServiceWorker, "SWServerJobQueue::scriptContextStarted registration is null");
return;
}
install(*registration, identifier);
}
// https://w3c.github.io/ServiceWorker/#install
void SWServerJobQueue::install(SWServerRegistration& registration, ServiceWorkerIdentifier installingWorker)
{
// The Install algorithm should never be invoked with a null worker.
Ref server = m_server.get();
RefPtr worker = server->workerByID(installingWorker);
RELEASE_ASSERT(worker);
ASSERT(registration.preInstallationWorker() == worker.get());
registration.setPreInstallationWorker(nullptr);
registration.updateRegistrationState(ServiceWorkerRegistrationState::Installing, worker.get());
registration.updateWorkerState(*worker, ServiceWorkerState::Installing);
// Invoke Resolve Job Promise with job and registration.
server->resolveRegistrationJob(firstJob(), registration.data(), ShouldNotifyWhenResolved::Yes);
// FIXME: https://bugs.webkit.org/show_bug.cgi?id=215122. We do not need to wait for the registration promise to resolve to continue the install steps.
}
// https://w3c.github.io/ServiceWorker/#install (after resolving promise).
void SWServerJobQueue::didResolveRegistrationPromise()
{
Ref server = m_server.get();
RefPtr registration = server->getRegistration(m_registrationKey);
ASSERT(registration);
ASSERT(registration->installingWorker());
if (!registration || !registration->installingWorker()) {
RELEASE_LOG_ERROR(ServiceWorker, "%p - SWServerJobQueue::didResolveRegistrationPromise with null registration (%d) or null worker", this, !!registration);
return;
}
RELEASE_LOG(ServiceWorker, "%p - SWServerJobQueue::didResolveRegistrationPromise: RegistrationID=%" PRIu64 ". Now proceeding with install", this, registration->identifier().toUInt64());
// Queue a task to fire an event named updatefound at all the ServiceWorkerRegistration objects
// for all the service worker clients whose creation URL matches registration's scope url and
// all the service workers whose containing service worker registration is registration.
registration->fireUpdateFoundEvent();
// Queue a task to fire the InstallEvent.
ASSERT(registration->installingWorker());
server->fireInstallEvent(*registration->protectedInstallingWorker());
}
// https://w3c.github.io/ServiceWorker/#install
void SWServerJobQueue::didFinishInstall(const ServiceWorkerJobDataIdentifier& jobDataIdentifier, SWServerWorker& worker, bool wasSuccessful)
{
if (!isCurrentlyProcessingJob(jobDataIdentifier))
return;
RefPtr registration = worker.registration();
ASSERT(registration);
ASSERT(registration->installingWorker() == &worker);
if (!wasSuccessful) {
worker.terminate();
// Run the Update Registration State algorithm passing registration, "installing" and null as the arguments.
registration->updateRegistrationState(ServiceWorkerRegistrationState::Installing, nullptr);
// Run the Update Worker State algorithm passing registration's installing worker and redundant as the arguments.
registration->updateWorkerState(worker, ServiceWorkerState::Redundant);
// If newestWorker is null, invoke Clear Registration algorithm passing registration as its argument.
if (!registration->getNewestWorker())
registration->clear(); // Will destroy the registration.
// Invoke Finish Job with job and abort these steps.
finishCurrentJob();
return;
}
if (RefPtr waitingWorker = registration->waitingWorker()) {
waitingWorker->terminate();
registration->updateWorkerState(*waitingWorker, ServiceWorkerState::Redundant);
}
registration->updateRegistrationState(ServiceWorkerRegistrationState::Waiting, &worker);
registration->updateRegistrationState(ServiceWorkerRegistrationState::Installing, nullptr);
registration->updateWorkerState(worker, ServiceWorkerState::Installed);
finishCurrentJob();
// FIXME: Wait for all the tasks queued by Update Worker State invoked in this algorithm have executed.
registration->tryActivate();
}
// https://w3c.github.io/ServiceWorker/#run-job
void SWServerJobQueue::runNextJob()
{
ASSERT(!m_jobQueue.isEmpty());
ASSERT(!m_jobTimer.isActive());
m_jobTimer.startOneShot(0_s);
}
void SWServerJobQueue::runNextJobSynchronously()
{
ASSERT(!m_jobQueue.isEmpty());
if (m_jobQueue.isEmpty())
return;
auto& job = firstJob();
switch (job.type) {
case ServiceWorkerJobType::Register:
runRegisterJob(job);
return;
case ServiceWorkerJobType::Unregister:
runUnregisterJob(job);
return;
case ServiceWorkerJobType::Update:
runUpdateJob(job);
return;
}
ASSERT_NOT_REACHED();
}
// https://w3c.github.io/ServiceWorker/#register-algorithm
void SWServerJobQueue::runRegisterJob(const ServiceWorkerJobData& job)
{
ASSERT(job.type == ServiceWorkerJobType::Register);
ASSERT(job.registrationOptions);
Ref server = m_server.get();
if (!job.isFromServiceWorkerPage && !shouldTreatAsPotentiallyTrustworthy(job.scriptURL) && !server->canHandleScheme(job.scriptURL.protocol()))
return rejectCurrentJob(ExceptionData { ExceptionCode::SecurityError, "Script URL is not potentially trustworthy"_s });
// If the origin of job's script url is not job's referrer's origin, then:
if (!protocolHostAndPortAreEqual(job.scriptURL, job.clientCreationURL))
return rejectCurrentJob(ExceptionData { ExceptionCode::SecurityError, "Script origin does not match the registering client's origin"_s });
// If the origin of job's scope url is not job's referrer's origin, then:
if (!protocolHostAndPortAreEqual(job.scopeURL, job.clientCreationURL))
return rejectCurrentJob(ExceptionData { ExceptionCode::SecurityError, "Scope origin does not match the registering client's origin"_s });
// If registration is not null (in our parlance "empty"), then:
if (RefPtr registration = server->getRegistration(m_registrationKey)) {
RefPtr newestWorker = registration->getNewestWorker();
if (newestWorker && equalIgnoringFragmentIdentifier(job.scriptURL, newestWorker->scriptURL()) && job.workerType == newestWorker->type() && job.registrationOptions->updateViaCache == registration->updateViaCache()) {
RELEASE_LOG(ServiceWorker, "%p - SWServerJobQueue::runRegisterJob: Found directly reusable registration %" PRIu64 " for job %s (DONE)", this, registration->identifier().toUInt64(), job.identifier().loggingString().utf8().data());
server->resolveRegistrationJob(job, registration->data(), ShouldNotifyWhenResolved::No);
finishCurrentJob();
return;
}
// This is not specified yet (https://github.com/w3c/ServiceWorker/issues/1189).
if (registration->updateViaCache() != job.registrationOptions->updateViaCache)
registration->setUpdateViaCache(job.registrationOptions->updateViaCache);
RELEASE_LOG(ServiceWorker, "%p - SWServerJobQueue::runRegisterJob: Found registration %" PRIu64 " for job %s but it needs updating", this, registration->identifier().toUInt64(), job.identifier().loggingString().utf8().data());
} else {
Ref newRegistration = SWServerRegistration::create(server.get(), m_registrationKey, job.registrationOptions->updateViaCache, job.scopeURL, job.scriptURL, job.serviceWorkerPageIdentifier(), NavigationPreloadState::defaultValue());
server->addRegistration(WTFMove(newRegistration));
RELEASE_LOG(ServiceWorker, "%p - SWServerJobQueue::runRegisterJob: No existing registration for job %s, constructing a new one.", this, job.identifier().loggingString().utf8().data());
}
runUpdateJob(job);
}
// https://w3c.github.io/ServiceWorker/#unregister-algorithm
void SWServerJobQueue::runUnregisterJob(const ServiceWorkerJobData& job)
{
// If the origin of job's scope url is not job's client's origin, then:
if (!protocolHostAndPortAreEqual(job.scopeURL, job.clientCreationURL))
return rejectCurrentJob(ExceptionData { ExceptionCode::SecurityError, "Origin of scope URL does not match the client's origin"_s });
// Let registration be the result of running "Get Registration" algorithm passing job's scope url as the argument.
Ref server = m_server.get();
RefPtr registration = server->getRegistration(m_registrationKey);
// If registration is null, then:
if (!registration) {
// Invoke Resolve Job Promise with job and false.
server->resolveUnregistrationJob(job, m_registrationKey, false);
finishCurrentJob();
return;
}
// Remove scope to registration map[job’s scope url].
server->removeFromScopeToRegistrationMap(m_registrationKey);
// Invoke Resolve Job Promise with job and true.
server->resolveUnregistrationJob(job, m_registrationKey, true);
// Invoke Try Clear Registration with registration.
registration->tryClear(); // This may destroy the registration.
finishCurrentJob();
}
// https://w3c.github.io/ServiceWorker/#update-algorithm
void SWServerJobQueue::runUpdateJob(const ServiceWorkerJobData& job)
{
// Let registration be the result of running the Get Registration algorithm passing job's scope url as the argument.
Ref server = m_server.get();
RefPtr registration = server->getRegistration(m_registrationKey);
// If registration is null (in our parlance "empty") or registration's uninstalling flag is set, then:
if (!registration)
return rejectCurrentJob(ExceptionData { ExceptionCode::TypeError, "Cannot update a null/nonexistent service worker registration"_s });
// Let newestWorker be the result of running Get Newest Worker algorithm passing registration as the argument.
RefPtr newestWorker = registration->getNewestWorker();
// If job’s type is update, and newestWorker is not null and its script url does not equal job’s script url, then:
if (job.type == ServiceWorkerJobType::Update && newestWorker && !equalIgnoringFragmentIdentifier(job.scriptURL, newestWorker->scriptURL()))
return rejectCurrentJob(ExceptionData { ExceptionCode::TypeError, "Cannot update a service worker with a requested script URL whose newest worker has a different script URL"_s });
server->startScriptFetch(job, *registration);
}
void SWServerJobQueue::rejectCurrentJob(const ExceptionData& exceptionData)
{
protectedServer()->rejectJob(firstJob(), exceptionData);
finishCurrentJob();
}
// https://w3c.github.io/ServiceWorker/#finish-job
void SWServerJobQueue::finishCurrentJob()
{
ASSERT(!m_jobTimer.isActive());
m_jobQueue.removeFirst();
if (!m_jobQueue.isEmpty())
runNextJob();
}
void SWServerJobQueue::removeAllJobsMatching(NOESCAPE const Function<bool(ServiceWorkerJobData&)>& matches)
{
bool isFirst = true;
bool didRemoveFirstJob = false;
m_jobQueue.removeAllMatching([&](auto& job) {
bool shouldRemove = matches(job);
if (isFirst) {
isFirst = false;
if (shouldRemove)
didRemoveFirstJob = true;
}
return shouldRemove;
});
if (m_jobTimer.isActive()) {
if (m_jobQueue.isEmpty())
m_jobTimer.stop();
} else if (didRemoveFirstJob && !m_jobQueue.isEmpty())
runNextJob();
}
void SWServerJobQueue::cancelJobsFromConnection(SWServerConnectionIdentifier connectionIdentifier)
{
removeAllJobsMatching([connectionIdentifier](auto& job) {
return job.identifier().connectionIdentifier == connectionIdentifier;
});
}
void SWServerJobQueue::cancelJobsFromServiceWorker(ServiceWorkerIdentifier serviceWorkerIdentifier)
{
removeAllJobsMatching([serviceWorkerIdentifier](auto& job) {
return std::holds_alternative<ServiceWorkerIdentifier>(job.sourceContext) && std::get<ServiceWorkerIdentifier>(job.sourceContext) == serviceWorkerIdentifier;
});
}
} // namespace WebCore
|