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#ifndef BlobResourceHandle_h
32#define BlobResourceHandle_h
33
34#include "FileStreamClient.h"
35#include "ResourceHandle.h"
36#include <wtf/PassRefPtr.h>
37#include <wtf/Vector.h>
38#include <wtf/text/WTFString.h>
39
40namespace WebCore {
41
42class AsyncFileStream;
43class BlobData;
44class FileStream;
45class ResourceHandleClient;
46class ResourceRequest;
47struct BlobDataItem;
48
49class BlobResourceHandle final : public FileStreamClient, public ResourceHandle  {
50public:
51    static PassRefPtr<BlobResourceHandle> createAsync(BlobData*, const ResourceRequest&, ResourceHandleClient*);
52
53    static void loadResourceSynchronously(BlobData*, const ResourceRequest&, ResourceError&, ResourceResponse&, Vector<char>& data);
54
55    void start();
56    int readSync(char*, int);
57
58    bool aborted() const { return m_aborted; }
59
60private:
61    BlobResourceHandle(BlobData*, const ResourceRequest&, ResourceHandleClient*, bool async);
62    virtual ~BlobResourceHandle();
63
64    // FileStreamClient methods.
65    virtual void didGetSize(long long) override;
66    virtual void didOpen(bool) override;
67    virtual void didRead(int) override;
68
69    // ResourceHandle methods.
70    virtual void cancel() override;
71    virtual void continueDidReceiveResponse() override;
72
73    void doStart();
74    void getSizeForNext();
75    void seek();
76    void consumeData(const char* data, int bytesRead);
77    void failed(int errorCode);
78
79    void readAsync();
80    void readDataAsync(const BlobDataItem&);
81    void readFileAsync(const BlobDataItem&);
82
83    int readDataSync(const BlobDataItem&, char*, int);
84    int readFileSync(const BlobDataItem&, char*, int);
85
86    void notifyResponse();
87    void notifyResponseOnSuccess();
88    void notifyResponseOnError();
89    void notifyReceiveData(const char*, int);
90    void notifyFail(int errorCode);
91    void notifyFinish();
92
93    RefPtr<BlobData> m_blobData;
94    bool m_async;
95    RefPtr<AsyncFileStream> m_asyncStream; // For asynchronous loading.
96    RefPtr<FileStream> m_stream; // For synchronous loading.
97    Vector<char> m_buffer;
98    Vector<long long> m_itemLengthList;
99    int m_errorCode;
100    bool m_aborted;
101    long long m_rangeOffset;
102    long long m_rangeEnd;
103    long long m_rangeSuffixLength;
104    long long m_totalRemainingSize;
105    long long m_currentItemReadSize;
106    unsigned m_sizeItemCount;
107    unsigned m_readItemCount;
108    bool m_fileOpened;
109};
110
111} // namespace WebCore
112
113#endif // BlobResourceHandle_h
114