1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef WEBKIT_BROWSER_APPCACHE_APPCACHE_DISK_CACHE_H_
6 #define WEBKIT_BROWSER_APPCACHE_APPCACHE_DISK_CACHE_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "net/disk_cache/disk_cache.h"
13 #include "webkit/browser/appcache/appcache_response.h"
14 #include "webkit/browser/webkit_storage_browser_export.h"
18 // An implementation of AppCacheDiskCacheInterface that
19 // uses net::DiskCache as the backing store.
20 class WEBKIT_STORAGE_BROWSER_EXPORT AppCacheDiskCache
21 : public AppCacheDiskCacheInterface
{
24 virtual ~AppCacheDiskCache();
26 // Initializes the object to use disk backed storage.
27 int InitWithDiskBackend(const base::FilePath
& disk_cache_directory
,
28 int disk_cache_size
, bool force
,
29 base::MessageLoopProxy
* cache_thread
,
30 const net::CompletionCallback
& callback
);
32 // Initializes the object to use memory only storage.
33 // This is used for Chrome's incognito browsing.
34 int InitWithMemBackend(int disk_cache_size
,
35 const net::CompletionCallback
& callback
);
38 bool is_disabled() const { return is_disabled_
; }
40 virtual int CreateEntry(int64 key
, Entry
** entry
,
41 const net::CompletionCallback
& callback
) OVERRIDE
;
42 virtual int OpenEntry(int64 key
, Entry
** entry
,
43 const net::CompletionCallback
& callback
) OVERRIDE
;
44 virtual int DoomEntry(int64 key
,
45 const net::CompletionCallback
& callback
) OVERRIDE
;
48 class CreateBackendCallbackShim
;
51 // PendingCalls allow CreateEntry, OpenEntry, and DoomEntry to be called
52 // immediately after construction, without waiting for the
53 // underlying disk_cache::Backend to be fully constructed. Early
54 // calls are queued up and serviced once the disk_cache::Backend is
55 // really ready to go.
56 enum PendingCallType
{
62 PendingCallType call_type
;
65 net::CompletionCallback callback
;
69 PendingCall(PendingCallType call_type
, int64 key
,
70 Entry
** entry
, const net::CompletionCallback
& callback
);
74 typedef std::vector
<PendingCall
> PendingCalls
;
77 typedef std::set
<ActiveCall
*> ActiveCalls
;
79 bool is_initializing() const {
80 return create_backend_callback_
.get() != NULL
;
82 disk_cache::Backend
* disk_cache() { return disk_cache_
.get(); }
83 int Init(net::CacheType cache_type
, const base::FilePath
& directory
,
84 int cache_size
, bool force
, base::MessageLoopProxy
* cache_thread
,
85 const net::CompletionCallback
& callback
);
86 void OnCreateBackendComplete(int rv
);
87 void AddActiveCall(ActiveCall
* call
) { active_calls_
.insert(call
); }
88 void RemoveActiveCall(ActiveCall
* call
) { active_calls_
.erase(call
); }
91 net::CompletionCallback init_callback_
;
92 scoped_refptr
<CreateBackendCallbackShim
> create_backend_callback_
;
93 PendingCalls pending_calls_
;
94 ActiveCalls active_calls_
;
95 scoped_ptr
<disk_cache::Backend
> disk_cache_
;
98 } // namespace appcache
100 #endif // WEBKIT_BROWSER_APPCACHE_APPCACHE_DISK_CACHE_H_