1 | /* |
2 | * Copyright (C) 2010 Google Inc. All rights reserved. |
3 | * Copyright (C) 2016 Igalia S.L. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are |
7 | * met: |
8 | * |
9 | * * Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * * Redistributions in binary form must reproduce the above |
12 | * copyright notice, this list of conditions and the following disclaimer |
13 | * in the documentation and/or other materials provided with the |
14 | * distribution. |
15 | * * Neither the name of Google Inc. nor the names of its |
16 | * contributors may be used to endorse or promote products derived from |
17 | * this software without specific prior written permission. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
31 | |
32 | #pragma once |
33 | |
34 | #include "NetworkDataTask.h" |
35 | #include <WebCore/FileStreamClient.h> |
36 | #include <wtf/FileSystem.h> |
37 | |
38 | namespace WebCore { |
39 | class AsyncFileStream; |
40 | class BlobDataFileReference; |
41 | class BlobData; |
42 | class BlobDataItem; |
43 | class BlobRegistryImpl; |
44 | } |
45 | |
46 | namespace WebKit { |
47 | |
48 | class NetworkProcess; |
49 | |
50 | class NetworkDataTaskBlob final : public NetworkDataTask, public WebCore::FileStreamClient { |
51 | public: |
52 | static Ref<NetworkDataTask> create(NetworkSession& session, WebCore::BlobRegistryImpl& blobRegistry, NetworkDataTaskClient& client, const WebCore::ResourceRequest& request, WebCore::ContentSniffingPolicy shouldContentSniff, const Vector<RefPtr<WebCore::BlobDataFileReference>>& fileReferences) |
53 | { |
54 | return adoptRef(*new NetworkDataTaskBlob(session, blobRegistry, client, request, shouldContentSniff, fileReferences)); |
55 | } |
56 | |
57 | ~NetworkDataTaskBlob(); |
58 | |
59 | private: |
60 | NetworkDataTaskBlob(NetworkSession&, WebCore::BlobRegistryImpl&, NetworkDataTaskClient&, const WebCore::ResourceRequest&, WebCore::ContentSniffingPolicy, const Vector<RefPtr<WebCore::BlobDataFileReference>>&); |
61 | |
62 | void cancel() override; |
63 | void resume() override; |
64 | void invalidateAndCancel() override; |
65 | NetworkDataTask::State state() const override { return m_state; } |
66 | |
67 | void setPendingDownloadLocation(const String&, SandboxExtension::Handle&&, bool /*allowOverwrite*/) override; |
68 | String suggestedFilename() const override; |
69 | |
70 | // FileStreamClient methods. |
71 | void didGetSize(long long) override; |
72 | void didOpen(bool) override; |
73 | void didRead(int) override; |
74 | |
75 | enum class Error { |
76 | NoError = 0, |
77 | NotFoundError = 1, |
78 | SecurityError = 2, |
79 | RangeError = 3, |
80 | NotReadableError = 4, |
81 | MethodNotAllowed = 5 |
82 | }; |
83 | |
84 | void clearStream(); |
85 | void getSizeForNext(); |
86 | void dispatchDidReceiveResponse(Error = Error::NoError); |
87 | void seek(); |
88 | void consumeData(const char* data, int bytesRead); |
89 | void read(); |
90 | void readData(const WebCore::BlobDataItem&); |
91 | void readFile(const WebCore::BlobDataItem&); |
92 | void download(); |
93 | bool writeDownload(const char* data, int bytesRead); |
94 | void cleanDownloadFiles(); |
95 | void didFailDownload(const WebCore::ResourceError&); |
96 | void didFinishDownload(); |
97 | void didFail(Error); |
98 | void didFinish(); |
99 | |
100 | enum { kPositionNotSpecified = -1 }; |
101 | |
102 | RefPtr<WebCore::BlobData> m_blobData; |
103 | std::unique_ptr<WebCore::AsyncFileStream> m_stream; // For asynchronous loading. |
104 | Vector<char> m_buffer; |
105 | Vector<long long> m_itemLengthList; |
106 | State m_state { State::Suspended }; |
107 | long long m_rangeOffset { kPositionNotSpecified }; |
108 | long long m_rangeEnd { kPositionNotSpecified }; |
109 | long long m_rangeSuffixLength { kPositionNotSpecified }; |
110 | long long m_totalSize { 0 }; |
111 | long long m_totalRemainingSize { 0 }; |
112 | long long m_currentItemReadSize { 0 }; |
113 | unsigned m_sizeItemCount { 0 }; |
114 | unsigned m_readItemCount { 0 }; |
115 | bool m_fileOpened { false }; |
116 | FileSystem::PlatformFileHandle m_downloadFile { FileSystem::invalidPlatformFileHandle }; |
117 | |
118 | Vector<RefPtr<WebCore::BlobDataFileReference>> m_fileReferences; |
119 | RefPtr<SandboxExtension> m_sandboxExtension; |
120 | Ref<NetworkProcess> m_networkProcess; |
121 | }; |
122 | |
123 | } // namespace WebKit |
124 | |