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