1/*
2 * Copyright (C) 2013 University of Szeged
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL UNIVERSITY OF SZEGED OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#ifndef CurlCacheManager_h
28#define CurlCacheManager_h
29
30#include "CurlCacheEntry.h"
31#include "ResourceHandle.h"
32#include "ResourceResponse.h"
33#include <wtf/HashMap.h>
34#include <wtf/ListHashSet.h>
35#include <wtf/text/WTFString.h>
36
37namespace WebCore {
38
39class CurlCacheManager {
40
41public:
42    static CurlCacheManager& getInstance();
43
44    void setCacheDirectory(const String&);
45    const String& cacheDirectory() { return m_cacheDir; }
46    void setStorageSizeLimit(size_t);
47
48    bool isCached(const String&) const;
49    HTTPHeaderMap& requestHeaders(const String&); // Load headers
50    bool getCachedResponse(const String& url, ResourceResponse&);
51
52    void didReceiveResponse(ResourceHandle&, ResourceResponse&);
53    void didReceiveData(ResourceHandle&, const char*, size_t); // Save data
54    void didFinishLoading(ResourceHandle&);
55    void didFail(ResourceHandle&);
56
57private:
58    CurlCacheManager();
59    ~CurlCacheManager();
60    CurlCacheManager(CurlCacheManager const&);
61    void operator=(CurlCacheManager const&);
62
63    bool m_disabled;
64    String m_cacheDir;
65    HashMap<String, std::unique_ptr<CurlCacheEntry>> m_index;
66
67    ListHashSet<String> m_LRUEntryList;
68    size_t m_currentStorageSize;
69    size_t m_storageSizeLimit;
70
71    void saveIndex();
72    void loadIndex();
73    void makeRoomForNewEntry();
74
75    void saveResponseHeaders(const String&, ResourceResponse&);
76    void invalidateCacheEntry(const String&);
77    void readCachedData(const String&, ResourceHandle*, ResourceResponse&);
78};
79
80}
81
82#endif // CurlCacheManager_h
83