1 | /* |
2 | * Copyright (C) 2010 Google 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 are |
6 | * met: |
7 | * |
8 | * * Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * * Redistributions in binary form must reproduce the above |
11 | * copyright notice, this list of conditions and the following disclaimer |
12 | * in the documentation and/or other materials provided with the |
13 | * distribution. |
14 | * * Neither the name of Google Inc. nor the names of its |
15 | * contributors may be used to endorse or promote products derived from |
16 | * this software without specific prior written permission. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
30 | |
31 | #pragma once |
32 | |
33 | #include "FileStreamClient.h" |
34 | #include "ResourceHandle.h" |
35 | #include <wtf/Vector.h> |
36 | #include <wtf/text/WTFString.h> |
37 | |
38 | namespace WebCore { |
39 | |
40 | class AsyncFileStream; |
41 | class BlobData; |
42 | class FileStream; |
43 | class ResourceHandleClient; |
44 | class ResourceRequest; |
45 | class BlobDataItem; |
46 | |
47 | class BlobResourceHandle final : public FileStreamClient, public ResourceHandle { |
48 | public: |
49 | static Ref<BlobResourceHandle> createAsync(BlobData*, const ResourceRequest&, ResourceHandleClient*); |
50 | |
51 | static void loadResourceSynchronously(BlobData*, const ResourceRequest&, ResourceError&, ResourceResponse&, Vector<char>& data); |
52 | |
53 | void start(); |
54 | int readSync(char*, int); |
55 | |
56 | bool aborted() const { return m_aborted; } |
57 | |
58 | enum class Error { |
59 | NoError = 0, |
60 | NotFoundError = 1, |
61 | SecurityError = 2, |
62 | RangeError = 3, |
63 | NotReadableError = 4, |
64 | MethodNotAllowed = 5 |
65 | }; |
66 | |
67 | private: |
68 | BlobResourceHandle(BlobData*, const ResourceRequest&, ResourceHandleClient*, bool async); |
69 | virtual ~BlobResourceHandle(); |
70 | |
71 | // FileStreamClient methods. |
72 | void didGetSize(long long) override; |
73 | void didOpen(bool) override; |
74 | void didRead(int) override; |
75 | |
76 | // ResourceHandle methods. |
77 | void cancel() override; |
78 | |
79 | void doStart(); |
80 | void getSizeForNext(); |
81 | void seek(); |
82 | void consumeData(const char* data, int bytesRead); |
83 | void failed(Error); |
84 | |
85 | void readAsync(); |
86 | void readDataAsync(const BlobDataItem&); |
87 | void readFileAsync(const BlobDataItem&); |
88 | |
89 | int readDataSync(const BlobDataItem&, char*, int); |
90 | int readFileSync(const BlobDataItem&, char*, int); |
91 | |
92 | void notifyResponse(); |
93 | void notifyResponseOnSuccess(); |
94 | void notifyResponseOnError(); |
95 | void notifyReceiveData(const char*, int); |
96 | void notifyFail(Error); |
97 | void notifyFinish(); |
98 | |
99 | bool erroredOrAborted() const { return m_aborted || m_errorCode != Error::NoError; } |
100 | |
101 | enum { kPositionNotSpecified = -1 }; |
102 | |
103 | RefPtr<BlobData> m_blobData; |
104 | bool m_async; |
105 | std::unique_ptr<AsyncFileStream> m_asyncStream; // For asynchronous loading. |
106 | std::unique_ptr<FileStream> m_stream; // For synchronous loading. |
107 | Vector<char> m_buffer; |
108 | Vector<long long> m_itemLengthList; |
109 | Error m_errorCode { Error::NoError }; |
110 | bool m_aborted { false }; |
111 | long long m_rangeOffset { kPositionNotSpecified }; |
112 | long long m_rangeEnd { kPositionNotSpecified }; |
113 | long long m_rangeSuffixLength { kPositionNotSpecified }; |
114 | long long m_totalSize { 0 }; |
115 | long long m_totalRemainingSize { 0 }; |
116 | long long m_currentItemReadSize { 0 }; |
117 | unsigned m_sizeItemCount { 0 }; |
118 | unsigned m_readItemCount { 0 }; |
119 | bool m_fileOpened { false }; |
120 | }; |
121 | |
122 | } // namespace WebCore |
123 | |