47831a89cff69f4b163785095d8c38f293acec83
[WebKit-https.git] / Source / WebKit / WebProcess / Storage / ServiceWorkerContextManager.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 "ServiceWorkerContextManager.h"
28
29 #if ENABLE(SERVICE_WORKER)
30
31 #include "Logging.h"
32 #include "StorageProcessMessages.h"
33 #include "WebCoreArgumentCoders.h"
34 #include <WebCore/ResourceRequest.h>
35 #include <WebCore/ResourceResponse.h>
36 #include <pal/SessionID.h>
37
38 using namespace PAL;
39 using namespace WebCore;
40
41 namespace WebKit {
42
43 void ServiceWorkerContextManager::startServiceWorker(uint64_t serverConnectionIdentifier, const ServiceWorkerContextData& data)
44 {
45     // FIXME: Provide a sensical session ID.
46     auto thread = ServiceWorkerThread::create(serverConnectionIdentifier, data, SessionID::defaultSessionID());
47     auto threadIdentifier = thread->identifier();
48     auto result = m_workerThreadMap.add(threadIdentifier, WTFMove(thread));
49     ASSERT(result.isNewEntry);
50
51     result.iterator->value->start();
52
53     LOG(ServiceWorker, "Context process PID: %i started worker thread %s\n", getpid(), data.workerID.utf8().data());
54
55     m_connectionToStorageProcess->send(Messages::StorageProcess::ServiceWorkerContextStarted(serverConnectionIdentifier, data.registrationKey, threadIdentifier, data.workerID), 0);
56 }
57
58 void ServiceWorkerContextManager::startFetch(uint64_t serverConnectionIdentifier, uint64_t fetchIdentifier, uint64_t serviceWorkerIdentifier, const ResourceRequest& request, const FetchOptions& options)
59 {
60     UNUSED_PARAM(serviceWorkerIdentifier);
61
62     // FIXME: Hard coding some fetches for testing purpose until we implement the creation of fetch event.
63     if (request.url().string().contains("test1")) {
64         ResourceResponse response;
65         response.setURL(request.url());
66         response.setHTTPStatusCode(200);
67         response.setHTTPStatusText(ASCIILiteral("Hello from service worker"));
68         m_connectionToStorageProcess->send(Messages::StorageProcess::DidReceiveFetchResponse(serverConnectionIdentifier, fetchIdentifier, response), 0);
69         m_connectionToStorageProcess->send(Messages::StorageProcess::DidFinishFetch(serverConnectionIdentifier, fetchIdentifier), 0);
70         return;
71     }
72     if (request.url().string().contains("test2")) {
73         ResourceResponse response;
74         response.setURL(request.url());
75         response.setHTTPStatusCode(500);
76         response.setHTTPStatusText(ASCIILiteral("Error from service worker"));
77         m_connectionToStorageProcess->send(Messages::StorageProcess::DidReceiveFetchResponse(serverConnectionIdentifier, fetchIdentifier, response), 0);
78         m_connectionToStorageProcess->send(Messages::StorageProcess::DidFinishFetch(serverConnectionIdentifier, fetchIdentifier), 0);
79         return;
80     }
81     if (request.url().string().contains("test3")) {
82         ResourceResponse response;
83         response.setURL(request.url());
84         response.setHTTPStatusCode(500);
85         response.setHTTPStatusText(ASCIILiteral("Error from service worker"));
86         response.setType(ResourceResponse::Type::Error);
87         m_connectionToStorageProcess->send(Messages::StorageProcess::DidReceiveFetchResponse(serverConnectionIdentifier, fetchIdentifier, response), 0);
88         m_connectionToStorageProcess->send(Messages::StorageProcess::DidFinishFetch(serverConnectionIdentifier, fetchIdentifier), 0);
89         return;
90     }
91
92     m_connectionToStorageProcess->send(Messages::StorageProcess::DidFailFetch(serverConnectionIdentifier, fetchIdentifier), 0);
93 }
94
95 } // namespace WebCore
96
97 #endif // ENABLE(SERVICE_WORKER)