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