WebCache.h revision d7712e41feaa225f656b1a41d58dd57d5fae884f
1/*
2 * Copyright 2010, 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 WebCache_h
27#define WebCache_h
28
29#include "CacheResult.h"
30#include "ChromiumIncludes.h"
31
32#include <OwnPtr.h>
33#include <platform/text/PlatformString.h>
34#include <wtf/ThreadingPrimitives.h>
35
36namespace android {
37
38// This class is not generally threadsafe. However, get() and cleanup() are
39// threadsafe.
40class WebCache : public base::RefCountedThreadSafe<WebCache> {
41public:
42    static WebCache* get(bool isPrivateBrowsing);
43    static void cleanup(bool isPrivateBrowsing);
44
45    void clear();
46    scoped_refptr<CacheResult> getCacheResult(WTF::String url);
47    net::HostResolver* hostResolver() { return m_hostResolver.get(); }
48    net::HttpCache* cache() { return m_cache.get(); }
49    net::ProxyConfigServiceAndroid* proxy() { return m_proxyConfigService; }
50    void closeIdleConnections();
51
52
53private:
54    WebCache(bool isPrivateBrowsing);
55
56    // For clear()
57    void clearImpl();
58    void doomAllEntries(int);
59    void onClearDone(int);
60
61    // For closeIdleConnections
62    void closeIdleImpl();
63
64    // For getEntry()
65    void getEntryImpl();
66    void openEntry(int);
67    void onGetEntryDone(int);
68
69    OwnPtr<net::HostResolver> m_hostResolver;
70    OwnPtr<net::HttpCache> m_cache;
71    // This is owned by the ProxyService, which is owned by the HttpNetworkLayer,
72    // which is owned by the HttpCache, which is owned by this class.
73    net::ProxyConfigServiceAndroid* m_proxyConfigService;
74
75    // For clear()
76    net::CompletionCallbackImpl<WebCache> m_doomAllEntriesCallback;
77    net::CompletionCallbackImpl<WebCache> m_onClearDoneCallback;
78    bool m_isClearInProgress;
79    // For getEntry()
80    net::CompletionCallbackImpl<WebCache> m_openEntryCallback;
81    net::CompletionCallbackImpl<WebCache> m_onGetEntryDoneCallback;
82    bool m_isGetEntryInProgress;
83    String m_entryUrl;
84    disk_cache::Entry* m_entry;
85    WTF::Mutex m_getEntryMutex;
86    WTF::ThreadCondition m_getEntryCondition;
87
88    disk_cache::Backend* m_cacheBackend;
89};
90
91} // namespace android
92
93#endif
94