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#pragma once
27
28#if ENABLE(SERVICE_WORKER)
29
30#include "SecurityOriginData.h"
31#include "ServiceWorkerClientIdentifier.h"
32#include "ServiceWorkerJobDataIdentifier.h"
33#include "ServiceWorkerJobType.h"
34#include "ServiceWorkerRegistrationKey.h"
35#include "ServiceWorkerRegistrationOptions.h"
36#include "ServiceWorkerTypes.h"
37#include <wtf/URL.h>
38
39namespace WebCore {
40
41struct ServiceWorkerJobData {
42 using Identifier = ServiceWorkerJobDataIdentifier;
43 ServiceWorkerJobData(SWServerConnectionIdentifier, const DocumentOrWorkerIdentifier& sourceContext);
44 ServiceWorkerJobData(const ServiceWorkerJobData&) = default;
45 ServiceWorkerJobData() = default;
46
47 SWServerConnectionIdentifier connectionIdentifier() const { return m_identifier.connectionIdentifier; }
48
49 URL scriptURL;
50 URL clientCreationURL;
51 SecurityOriginData topOrigin;
52 URL scopeURL;
53 ServiceWorkerOrClientIdentifier sourceContext;
54 ServiceWorkerJobType type;
55
56 ServiceWorkerRegistrationOptions registrationOptions;
57
58 Identifier identifier() const { return m_identifier; }
59 ServiceWorkerRegistrationKey registrationKey() const;
60 ServiceWorkerJobData isolatedCopy() const;
61
62 template<class Encoder> void encode(Encoder&) const;
63 template<class Decoder> static Optional<ServiceWorkerJobData> decode(Decoder&);
64
65private:
66 WEBCORE_EXPORT explicit ServiceWorkerJobData(const Identifier&);
67
68 Identifier m_identifier;
69};
70
71template<class Encoder>
72void ServiceWorkerJobData::encode(Encoder& encoder) const
73{
74 encoder << identifier() << scriptURL << clientCreationURL << topOrigin << scopeURL << sourceContext;
75 encoder.encodeEnum(type);
76 switch (type) {
77 case ServiceWorkerJobType::Register:
78 encoder << registrationOptions;
79 break;
80 case ServiceWorkerJobType::Unregister:
81 case ServiceWorkerJobType::Update:
82 break;
83 }
84}
85
86template<class Decoder>
87Optional<ServiceWorkerJobData> ServiceWorkerJobData::decode(Decoder& decoder)
88{
89 Optional<ServiceWorkerJobDataIdentifier> identifier;
90 decoder >> identifier;
91 if (!identifier)
92 return WTF::nullopt;
93
94 ServiceWorkerJobData jobData { WTFMove(*identifier) };
95
96 if (!decoder.decode(jobData.scriptURL))
97 return WTF::nullopt;
98 if (!decoder.decode(jobData.clientCreationURL))
99 return WTF::nullopt;
100
101 Optional<SecurityOriginData> topOrigin;
102 decoder >> topOrigin;
103 if (!topOrigin)
104 return WTF::nullopt;
105 jobData.topOrigin = WTFMove(*topOrigin);
106
107 if (!decoder.decode(jobData.scopeURL))
108 return WTF::nullopt;
109 if (!decoder.decode(jobData.sourceContext))
110 return WTF::nullopt;
111 if (!decoder.decodeEnum(jobData.type))
112 return WTF::nullopt;
113
114 switch (jobData.type) {
115 case ServiceWorkerJobType::Register: {
116 Optional<ServiceWorkerRegistrationOptions> registrationOptions;
117 decoder >> registrationOptions;
118 if (!registrationOptions)
119 return WTF::nullopt;
120 jobData.registrationOptions = WTFMove(*registrationOptions);
121 break;
122 }
123 case ServiceWorkerJobType::Unregister:
124 case ServiceWorkerJobType::Update:
125 break;
126 }
127
128 return jobData;
129}
130
131} // namespace WebCore
132
133#endif // ENABLE(SERVICE_WORKER)
134