Add support for unregistering a service worker
[WebKit-https.git] / Source / WebCore / workers / service / ServiceWorkerJob.cpp
1 /*
2  * Copyright (C) 2017 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #include "config.h"
27 #include "ServiceWorkerJob.h"
28
29 #if ENABLE(SERVICE_WORKER)
30
31 #include "JSDOMPromiseDeferred.h"
32 #include "ResourceError.h"
33 #include "ResourceResponse.h"
34 #include "ServiceWorkerJobData.h"
35 #include "ServiceWorkerRegistration.h"
36
37 namespace WebCore {
38
39 ServiceWorkerJob::ServiceWorkerJob(ServiceWorkerJobClient& client, Ref<DeferredPromise>&& promise, ServiceWorkerJobData&& jobData)
40     : m_client(client)
41     , m_jobData(WTFMove(jobData))
42     , m_promise(WTFMove(promise))
43 {
44 }
45
46 ServiceWorkerJob::~ServiceWorkerJob()
47 {
48     ASSERT(currentThread() == m_creationThread);
49 }
50
51 void ServiceWorkerJob::failedWithException(const Exception& exception)
52 {
53     ASSERT(currentThread() == m_creationThread);
54     ASSERT(!m_completed);
55
56     m_completed = true;
57     m_client->jobFailedWithException(*this, exception);
58 }
59
60 void ServiceWorkerJob::resolvedWithRegistration(ServiceWorkerRegistrationData&& data)
61 {
62     ASSERT(currentThread() == m_creationThread);
63     ASSERT(!m_completed);
64
65     m_completed = true;
66     m_client->jobResolvedWithRegistration(*this, WTFMove(data));
67 }
68
69 void ServiceWorkerJob::resolvedWithUnregistrationResult(bool unregistrationResult)
70 {
71     ASSERT(currentThread() == m_creationThread);
72     ASSERT(!m_completed);
73
74     m_completed = true;
75     m_client->jobResolvedWithUnregistrationResult(*this, unregistrationResult);
76 }
77
78 void ServiceWorkerJob::startScriptFetch()
79 {
80     ASSERT(currentThread() == m_creationThread);
81     ASSERT(!m_completed);
82
83     m_client->startScriptFetchForJob(*this);
84 }
85
86 void ServiceWorkerJob::fetchScriptWithContext(ScriptExecutionContext& context)
87 {
88     ASSERT(currentThread() == m_creationThread);
89     ASSERT(!m_completed);
90
91     // FIXME: WorkerScriptLoader is the wrong loader class to use here, but there's nothing else better right now.
92     m_scriptLoader = WorkerScriptLoader::create();
93     m_scriptLoader->loadAsynchronously(&context, m_jobData.scriptURL, FetchOptions::Mode::SameOrigin, ContentSecurityPolicyEnforcement::DoNotEnforce, "serviceWorkerScriptLoad:", this);
94 }
95
96 void ServiceWorkerJob::didReceiveResponse(unsigned long, const ResourceResponse& response)
97 {
98     ASSERT(currentThread() == m_creationThread);
99     ASSERT(!m_completed);
100     ASSERT(m_scriptLoader);
101
102     m_lastResponse = response;
103 }
104
105 void ServiceWorkerJob::notifyFinished()
106 {
107     ASSERT(currentThread() == m_creationThread);
108     ASSERT(m_scriptLoader);
109     
110     if (!m_scriptLoader->failed())
111         m_client->jobFinishedLoadingScript(*this, m_scriptLoader->script());
112     else
113         m_client->jobFailedLoadingScript(*this, m_scriptLoader->error());
114
115     m_scriptLoader = nullptr;
116 }
117
118 } // namespace WebCore
119
120 #endif // ENABLE(SERVICE_WORKER)