Roll ANGLE e754fb8..6ffeb74
[chromium-blink-merge.git] / content / browser / appcache / appcache_disk_cache.h
blob6107f5b6b26f767d2a775ce8b1aa1a5d679ce888
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 CONTENT_BROWSER_APPCACHE_APPCACHE_DISK_CACHE_H_
6 #define CONTENT_BROWSER_APPCACHE_APPCACHE_DISK_CACHE_H_
8 #include <set>
9 #include <vector>
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "content/browser/appcache/appcache_response.h"
14 #include "content/common/content_export.h"
15 #include "net/disk_cache/disk_cache.h"
17 namespace base {
18 class SingleThreadTaskRunner;
19 } // namespace base
21 namespace content {
23 // An implementation of AppCacheDiskCacheInterface that
24 // uses net::DiskCache as the backing store.
25 class CONTENT_EXPORT AppCacheDiskCache
26 : public AppCacheDiskCacheInterface {
27 public:
28 AppCacheDiskCache();
29 ~AppCacheDiskCache() override;
31 // Initializes the object to use disk backed storage.
32 int InitWithDiskBackend(
33 const base::FilePath& disk_cache_directory,
34 int disk_cache_size,
35 bool force,
36 const scoped_refptr<base::SingleThreadTaskRunner>& cache_thread,
37 const net::CompletionCallback& callback);
39 // Initializes the object to use memory only storage.
40 // This is used for Chrome's incognito browsing.
41 int InitWithMemBackend(int disk_cache_size,
42 const net::CompletionCallback& callback);
44 void Disable();
45 bool is_disabled() const { return is_disabled_; }
47 int CreateEntry(int64 key,
48 Entry** entry,
49 const net::CompletionCallback& callback) override;
50 int OpenEntry(int64 key,
51 Entry** entry,
52 const net::CompletionCallback& callback) override;
53 int DoomEntry(int64 key, const net::CompletionCallback& callback) override;
55 protected:
56 explicit AppCacheDiskCache(bool use_simple_cache);
57 disk_cache::Backend* disk_cache() { return disk_cache_.get(); }
59 private:
60 class CreateBackendCallbackShim;
61 class EntryImpl;
63 // PendingCalls allow CreateEntry, OpenEntry, and DoomEntry to be called
64 // immediately after construction, without waiting for the
65 // underlying disk_cache::Backend to be fully constructed. Early
66 // calls are queued up and serviced once the disk_cache::Backend is
67 // really ready to go.
68 enum PendingCallType {
69 CREATE,
70 OPEN,
71 DOOM
73 struct PendingCall {
74 PendingCallType call_type;
75 int64 key;
76 Entry** entry;
77 net::CompletionCallback callback;
79 PendingCall();
81 PendingCall(PendingCallType call_type, int64 key,
82 Entry** entry, const net::CompletionCallback& callback);
84 ~PendingCall();
86 typedef std::vector<PendingCall> PendingCalls;
88 class ActiveCall;
89 typedef std::set<ActiveCall*> ActiveCalls;
90 typedef std::set<EntryImpl*> OpenEntries;
92 bool is_initializing() const {
93 return create_backend_callback_.get() != NULL;
95 int Init(net::CacheType cache_type,
96 const base::FilePath& directory,
97 int cache_size,
98 bool force,
99 const scoped_refptr<base::SingleThreadTaskRunner>& cache_thread,
100 const net::CompletionCallback& callback);
101 void OnCreateBackendComplete(int rv);
102 void AddOpenEntry(EntryImpl* entry) { open_entries_.insert(entry); }
103 void RemoveOpenEntry(EntryImpl* entry) { open_entries_.erase(entry); }
105 bool use_simple_cache_;
106 bool is_disabled_;
107 net::CompletionCallback init_callback_;
108 scoped_refptr<CreateBackendCallbackShim> create_backend_callback_;
109 PendingCalls pending_calls_;
110 OpenEntries open_entries_;
111 scoped_ptr<disk_cache::Backend> disk_cache_;
113 base::WeakPtrFactory<AppCacheDiskCache> weak_factory_;
116 } // namespace content
118 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_DISK_CACHE_H_