Replace WTF::move with WTFMove
[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, AllowStoredCredentials, AskClientForAllCredentials, SkipSecurityCheck, UseDefaultOriginRestrictionsForType, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading))
47     , m_client(client)
48 {
49 #if ENABLE(CONTENT_EXTENSIONS)
50     m_resourceType = ResourceType::PlugInStream;
51 #endif
52 }
53
54 NetscapePlugInStreamLoader::~NetscapePlugInStreamLoader()
55 {
56 }
57
58 PassRefPtr<NetscapePlugInStreamLoader> NetscapePlugInStreamLoader::create(Frame* frame, NetscapePlugInStreamLoaderClient* client, const ResourceRequest& request)
59 {
60     RefPtr<NetscapePlugInStreamLoader> loader(adoptRef(new NetscapePlugInStreamLoader(frame, client)));
61     if (!loader->init(request))
62         return nullptr;
63
64     return loader.release();
65 }
66
67 bool NetscapePlugInStreamLoader::isDone() const
68 {
69     return !m_client;
70 }
71
72 void NetscapePlugInStreamLoader::releaseResources()
73 {
74     m_client = nullptr;
75     ResourceLoader::releaseResources();
76 }
77
78 bool NetscapePlugInStreamLoader::init(const ResourceRequest& request)
79 {
80     if (!ResourceLoader::init(request))
81         return false;
82
83     ASSERT(!reachedTerminalState());
84
85     m_documentLoader->addPlugInStreamLoader(*this);
86     m_isInitialized = true;
87
88     return true;
89 }
90
91 void NetscapePlugInStreamLoader::willSendRequest(ResourceRequest&& request, const ResourceResponse& redirectResponse, std::function<void(ResourceRequest&&)>&& callback)
92 {
93     RefPtr<NetscapePlugInStreamLoader> protect(this);
94
95     m_client->willSendRequest(this, WTFMove(request), redirectResponse, [protect, redirectResponse, callback](ResourceRequest request) {
96         if (!request.isNull())
97             protect->willSendRequestInternal(request, redirectResponse);
98
99         callback(WTFMove(request));
100     });
101 }
102
103 void NetscapePlugInStreamLoader::didReceiveResponse(const ResourceResponse& response)
104 {
105     Ref<NetscapePlugInStreamLoader> protect(*this);
106
107     m_client->didReceiveResponse(this, response);
108
109     // Don't continue if the stream is cancelled
110     if (!m_client)
111         return;
112
113     ResourceLoader::didReceiveResponse(response);
114
115     // Don't continue if the stream is cancelled
116     if (!m_client)
117         return;
118
119     if (!response.isHTTP())
120         return;
121     
122     if (m_client->wantsAllStreams())
123         return;
124
125     // Status code can be null when serving from a Web archive.
126     if (response.httpStatusCode() && (response.httpStatusCode() < 100 || response.httpStatusCode() >= 400))
127         cancel(frameLoader()->client().fileDoesNotExistError(response));
128 }
129
130 void NetscapePlugInStreamLoader::didReceiveData(const char* data, unsigned length, long long encodedDataLength, DataPayloadType dataPayloadType)
131 {
132     didReceiveDataOrBuffer(data, length, 0, encodedDataLength, dataPayloadType);
133 }
134
135 void NetscapePlugInStreamLoader::didReceiveBuffer(PassRefPtr<SharedBuffer> buffer, long long encodedDataLength, DataPayloadType dataPayloadType)
136 {
137     didReceiveDataOrBuffer(0, 0, buffer, encodedDataLength, dataPayloadType);
138 }
139
140 void NetscapePlugInStreamLoader::didReceiveDataOrBuffer(const char* data, int length, PassRefPtr<SharedBuffer> buffer, long long encodedDataLength, DataPayloadType dataPayloadType)
141 {
142     Ref<NetscapePlugInStreamLoader> protect(*this);
143     
144     m_client->didReceiveData(this, buffer ? buffer->data() : data, buffer ? buffer->size() : length);
145
146     ResourceLoader::didReceiveDataOrBuffer(data, length, buffer, encodedDataLength, dataPayloadType);
147 }
148
149 void NetscapePlugInStreamLoader::didFinishLoading(double finishTime)
150 {
151     Ref<NetscapePlugInStreamLoader> protect(*this);
152
153     notifyDone();
154
155     m_client->didFinishLoading(this);
156     ResourceLoader::didFinishLoading(finishTime);
157 }
158
159 void NetscapePlugInStreamLoader::didFail(const ResourceError& error)
160 {
161     Ref<NetscapePlugInStreamLoader> protect(*this);
162
163     notifyDone();
164
165     m_client->didFail(this, error);
166     ResourceLoader::didFail(error);
167 }
168
169 void NetscapePlugInStreamLoader::willCancel(const ResourceError& error)
170 {
171     m_client->didFail(this, error);
172 }
173
174 void NetscapePlugInStreamLoader::didCancel(const ResourceError&)
175 {
176     notifyDone();
177 }
178
179 void NetscapePlugInStreamLoader::notifyDone()
180 {
181     if (!m_isInitialized)
182         return;
183
184     m_documentLoader->removePlugInStreamLoader(*this);
185 }
186
187
188 }