CacheResult.h revision e95b4a7d9ed67e6d6960e33e794e653aa0bd3887
1/*
2 * Copyright 2011, The Android Open Source Project
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 *  * Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 *  * Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef CacheResult_h
27#define CacheResult_h
28
29#include "ChromiumIncludes.h"
30
31#include <wtf/RefCounted.h>
32#include <wtf/ThreadingPrimitives.h>
33#include <wtf/text/WTFString.h>
34
35namespace android {
36
37// A wrapper around a disk_cache::Entry. Provides fields appropriate for constructing a Java CacheResult object.
38class CacheResult : public base::RefCountedThreadSafe<CacheResult> {
39public:
40    // Takes ownership of the Entry passed to the constructor.
41    CacheResult(disk_cache::Entry*, String url);
42    ~CacheResult();
43
44    int64 contentSize() const;
45    bool firstResponseHeader(const char* name, WTF::String* result, bool allowEmptyString) const;
46    // The Android stack uses the empty string if no Content-Type headers are
47    // found, so we use the same default here.
48    WTF::String mimeType() const;
49    // Returns the value of the expires header as milliseconds since the epoch.
50    int64 expires() const;
51    int responseCode() const;
52    bool writeToFile(const WTF::String& filePath) const;
53private:
54    net::HttpResponseHeaders* responseHeaders() const;
55    void responseHeadersImpl();
56    void onResponseHeadersDone(int size);
57
58    void writeToFileImpl();
59    void readNextChunk();
60    void onReadNextChunkDone(int size);
61    bool writeChunkToFile();
62    void onWriteToFileDone();
63
64    disk_cache::Entry* m_entry;
65
66    scoped_refptr<net::HttpResponseHeaders> m_responseHeaders;
67
68    int m_readOffset;
69    bool m_wasWriteToFileSuccessful;
70    mutable String m_filePath;
71
72    int m_bufferSize;
73    scoped_refptr<net::IOBuffer> m_buffer;
74    mutable bool m_isAsyncOperationInProgress;
75    mutable WTF::Mutex m_mutex;
76    mutable WTF::ThreadCondition m_condition;
77
78    net::CompletionCallbackImpl<CacheResult> m_onResponseHeadersDoneCallback;
79    net::CompletionCallbackImpl<CacheResult> m_onReadNextChunkDoneCallback;
80
81    String m_url;
82};
83
84} // namespace android
85
86#endif
87