1/*
2 * Copyright (C) 2009 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2009, 2011 Google Inc. All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include "ContentSecurityPolicyResponseHeaders.h"
30#include "FetchOptions.h"
31#include "ResourceError.h"
32#include "ResourceRequest.h"
33#include "ThreadableLoader.h"
34#include "ThreadableLoaderClient.h"
35#include <memory>
36#include <wtf/FastMalloc.h>
37#include <wtf/RefCounted.h>
38#include <wtf/RefPtr.h>
39#include <wtf/URL.h>
40#include <wtf/text/StringBuilder.h>
41
42namespace WebCore {
43
44class Exception;
45class ResourceResponse;
46class ScriptExecutionContext;
47class TextResourceDecoder;
48class WorkerScriptLoaderClient;
49
50class WorkerScriptLoader : public RefCounted<WorkerScriptLoader>, public ThreadableLoaderClient {
51 WTF_MAKE_FAST_ALLOCATED;
52public:
53 static Ref<WorkerScriptLoader> create()
54 {
55 return adoptRef(*new WorkerScriptLoader);
56 }
57
58 Optional<Exception> loadSynchronously(ScriptExecutionContext*, const URL&, FetchOptions::Mode, FetchOptions::Cache, ContentSecurityPolicyEnforcement, const String& initiatorIdentifier);
59 void loadAsynchronously(ScriptExecutionContext&, ResourceRequest&&, FetchOptions&&, ContentSecurityPolicyEnforcement, ServiceWorkersMode, WorkerScriptLoaderClient&);
60
61 void notifyError();
62
63 String script();
64 const ContentSecurityPolicyResponseHeaders& contentSecurityPolicy() const { return m_contentSecurityPolicy; }
65 const String& referrerPolicy() const { return m_referrerPolicy; }
66 const URL& url() const { return m_url; }
67 const URL& responseURL() const;
68 const String& responseMIMEType() const { return m_responseMIMEType; }
69 bool failed() const { return m_failed; }
70 unsigned long identifier() const { return m_identifier; }
71 const ResourceError& error() const { return m_error; }
72
73 void didReceiveResponse(unsigned long identifier, const ResourceResponse&) override;
74 void didReceiveData(const char* data, int dataLength) override;
75 void didFinishLoading(unsigned long identifier) override;
76 void didFail(const ResourceError&) override;
77
78 void cancel();
79
80private:
81 friend class WTF::RefCounted<WorkerScriptLoader>;
82
83 WorkerScriptLoader();
84 ~WorkerScriptLoader();
85
86 std::unique_ptr<ResourceRequest> createResourceRequest(const String& initiatorIdentifier);
87 void notifyFinished();
88
89 WorkerScriptLoaderClient* m_client { nullptr };
90 RefPtr<ThreadableLoader> m_threadableLoader;
91 String m_responseEncoding;
92 RefPtr<TextResourceDecoder> m_decoder;
93 StringBuilder m_script;
94 URL m_url;
95 URL m_responseURL;
96 String m_responseMIMEType;
97 FetchOptions::Destination m_destination;
98 ContentSecurityPolicyResponseHeaders m_contentSecurityPolicy;
99 String m_referrerPolicy;
100 unsigned long m_identifier { 0 };
101 bool m_failed { false };
102 bool m_finishing { false };
103 ResourceError m_error;
104};
105
106} // namespace WebCore
107