Use "= default" to denote default constructor or destructor
[WebKit-https.git] / Source / WebCore / loader / NetscapePlugInStreamLoader.cpp
1 /*
2  * Copyright (C) 2006 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  *
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  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission. 
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "config.h"
30 #include "NetscapePlugInStreamLoader.h"
31
32 #include "DocumentLoader.h"
33 #include "FrameLoader.h"
34 #include "FrameLoaderClient.h"
35 #include <wtf/Ref.h>
36
37 #if ENABLE(CONTENT_EXTENSIONS)
38 #include "ResourceLoadInfo.h"
39 #endif
40
41 namespace WebCore {
42
43 // FIXME: Skip Content Security Policy check when associated plugin element is in a user agent shadow tree.
44 // See <https://bugs.webkit.org/show_bug.cgi?id=146663>.
45 NetscapePlugInStreamLoader::NetscapePlugInStreamLoader(Frame& frame, NetscapePlugInStreamLoaderClient& client)
46     : ResourceLoader(frame, ResourceLoaderOptions(SendCallbacks, SniffContent, DoNotBufferData, StoredCredentialsPolicy::Use, ClientCredentialPolicy::MayAskClientForCredentials, FetchOptions::Credentials::Include, SkipSecurityCheck, FetchOptions::Mode::NoCors, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, CachingPolicy::AllowCaching))
47     , m_client(&client)
48 {
49 #if ENABLE(CONTENT_EXTENSIONS)
50     m_resourceType = ResourceType::PlugInStream;
51 #endif
52 }
53
54 NetscapePlugInStreamLoader::~NetscapePlugInStreamLoader() = default;
55
56 RefPtr<NetscapePlugInStreamLoader> NetscapePlugInStreamLoader::create(Frame& frame, NetscapePlugInStreamLoaderClient& client, const ResourceRequest& request)
57 {
58     auto loader(adoptRef(new NetscapePlugInStreamLoader(frame, client)));
59     if (!loader->init(request))
60         return nullptr;
61
62     return loader;
63 }
64
65 bool NetscapePlugInStreamLoader::isDone() const
66 {
67     return !m_client;
68 }
69
70 void NetscapePlugInStreamLoader::releaseResources()
71 {
72     m_client = nullptr;
73     ResourceLoader::releaseResources();
74 }
75
76 bool NetscapePlugInStreamLoader::init(const ResourceRequest& request)
77 {
78     if (!ResourceLoader::init(request))
79         return false;
80
81     ASSERT(!reachedTerminalState());
82
83     m_documentLoader->addPlugInStreamLoader(*this);
84     m_isInitialized = true;
85
86     return true;
87 }
88
89 void NetscapePlugInStreamLoader::willSendRequest(ResourceRequest&& request, const ResourceResponse& redirectResponse, WTF::Function<void(ResourceRequest&&)>&& callback)
90 {
91     RefPtr<NetscapePlugInStreamLoader> protectedThis(this);
92
93     m_client->willSendRequest(this, WTFMove(request), redirectResponse, [protectedThis, redirectResponse, callback = WTFMove(callback)](ResourceRequest request) {
94         if (!request.isNull())
95             protectedThis->willSendRequestInternal(request, redirectResponse);
96
97         callback(WTFMove(request));
98     });
99 }
100
101 void NetscapePlugInStreamLoader::didReceiveResponse(const ResourceResponse& response)
102 {
103     Ref<NetscapePlugInStreamLoader> protectedThis(*this);
104
105     m_client->didReceiveResponse(this, response);
106
107     // Don't continue if the stream is cancelled
108     if (!m_client)
109         return;
110
111     ResourceLoader::didReceiveResponse(response);
112
113     // Don't continue if the stream is cancelled
114     if (!m_client)
115         return;
116
117     if (!response.isHTTP())
118         return;
119     
120     if (m_client->wantsAllStreams())
121         return;
122
123     // Status code can be null when serving from a Web archive.
124     if (response.httpStatusCode() && (response.httpStatusCode() < 100 || response.httpStatusCode() >= 400))
125         cancel(frameLoader()->client().fileDoesNotExistError(response));
126 }
127
128 void NetscapePlugInStreamLoader::didReceiveData(const char* data, unsigned length, long long encodedDataLength, DataPayloadType dataPayloadType)
129 {
130     didReceiveDataOrBuffer(data, length, nullptr, encodedDataLength, dataPayloadType);
131 }
132
133 void NetscapePlugInStreamLoader::didReceiveBuffer(Ref<SharedBuffer>&& buffer, long long encodedDataLength, DataPayloadType dataPayloadType)
134 {
135     didReceiveDataOrBuffer(nullptr, 0, WTFMove(buffer), encodedDataLength, dataPayloadType);
136 }
137
138 void NetscapePlugInStreamLoader::didReceiveDataOrBuffer(const char* data, int length, RefPtr<SharedBuffer>&& buffer, long long encodedDataLength, DataPayloadType dataPayloadType)
139 {
140     Ref<NetscapePlugInStreamLoader> protectedThis(*this);
141     
142     m_client->didReceiveData(this, buffer ? buffer->data() : data, buffer ? buffer->size() : length);
143
144     ResourceLoader::didReceiveDataOrBuffer(data, length, WTFMove(buffer), encodedDataLength, dataPayloadType);
145 }
146
147 void NetscapePlugInStreamLoader::didFinishLoading(const NetworkLoadMetrics& networkLoadMetrics)
148 {
149     Ref<NetscapePlugInStreamLoader> protectedThis(*this);
150
151     notifyDone();
152
153     m_client->didFinishLoading(this);
154     ResourceLoader::didFinishLoading(networkLoadMetrics);
155 }
156
157 void NetscapePlugInStreamLoader::didFail(const ResourceError& error)
158 {
159     Ref<NetscapePlugInStreamLoader> protectedThis(*this);
160
161     notifyDone();
162
163     m_client->didFail(this, error);
164     ResourceLoader::didFail(error);
165 }
166
167 void NetscapePlugInStreamLoader::willCancel(const ResourceError& error)
168 {
169     m_client->didFail(this, error);
170 }
171
172 void NetscapePlugInStreamLoader::didCancel(const ResourceError&)
173 {
174     notifyDone();
175 }
176
177 void NetscapePlugInStreamLoader::notifyDone()
178 {
179     if (!m_isInitialized)
180         return;
181
182     m_documentLoader->removePlugInStreamLoader(*this);
183 }
184
185
186 }