2 * Copyright (C) 2017 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "SWClientConnection.h"
29 #if ENABLE(SERVICE_WORKER)
31 #include "ExceptionData.h"
32 #include "ServiceWorkerFetchResult.h"
33 #include "ServiceWorkerJobData.h"
37 SWClientConnection::SWClientConnection() = default;
39 SWClientConnection::~SWClientConnection() = default;
41 void SWClientConnection::scheduleJob(ServiceWorkerJob& job)
43 auto addResult = m_scheduledJobs.add(job.data().identifier(), &job);
44 ASSERT_UNUSED(addResult, addResult.isNewEntry);
46 scheduleJobInServer(job.data());
49 void SWClientConnection::finishedFetchingScript(ServiceWorkerJob& job, const String& script)
51 ASSERT(m_scheduledJobs.get(job.data().identifier()) == &job);
53 finishFetchingScriptInServer({ job.data().identifier(), job.data().connectionIdentifier(), job.data().registrationKey(), script, { } });
56 void SWClientConnection::failedFetchingScript(ServiceWorkerJob& job, const ResourceError& error)
58 ASSERT(m_scheduledJobs.get(job.data().identifier()) == &job);
60 finishFetchingScriptInServer({ job.data().identifier(), job.data().connectionIdentifier(), job.data().registrationKey(), { }, error });
63 void SWClientConnection::jobRejectedInServer(uint64_t jobIdentifier, const ExceptionData& exceptionData)
65 auto job = m_scheduledJobs.take(jobIdentifier);
67 LOG_ERROR("Job %" PRIu64 " rejected from server, but was not found", jobIdentifier);
71 job->failedWithException(exceptionData.toException());
74 void SWClientConnection::jobResolvedInServer(uint64_t jobIdentifier, ServiceWorkerRegistrationData&& registrationData)
76 auto job = m_scheduledJobs.take(jobIdentifier);
78 LOG_ERROR("Job %" PRIu64 " resolved in server, but was not found", jobIdentifier);
82 job->resolvedWithRegistration(WTFMove(registrationData));
85 void SWClientConnection::startScriptFetchForServer(uint64_t jobIdentifier)
87 auto job = m_scheduledJobs.get(jobIdentifier);
89 LOG_ERROR("Job %" PRIu64 " instructed to start fetch from server, but job was not found", jobIdentifier);
91 // FIXME: Should message back to the server here to signal failure to fetch,
92 // but we currently need the registration key to do so, and don't have it here.
93 // In the future we'll refactor to have a global, cross-process job identifier that can be used to overcome this.
98 job->startScriptFetch();
101 } // namespace WebCore
103 #endif // ENABLE(SERVICE_WORKER)