|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #ifndef _nsDiskCacheDevice_h_ |
|
8 #define _nsDiskCacheDevice_h_ |
|
9 |
|
10 #include "mozilla/MemoryReporting.h" |
|
11 #include "nsCacheDevice.h" |
|
12 #include "nsDiskCacheBinding.h" |
|
13 #include "nsDiskCacheBlockFile.h" |
|
14 #include "nsDiskCacheEntry.h" |
|
15 |
|
16 #include "nsIFile.h" |
|
17 #include "nsIObserver.h" |
|
18 #include "nsCOMArray.h" |
|
19 |
|
20 class nsDiskCacheMap; |
|
21 |
|
22 |
|
23 class nsDiskCacheDevice : public nsCacheDevice { |
|
24 public: |
|
25 nsDiskCacheDevice(); |
|
26 virtual ~nsDiskCacheDevice(); |
|
27 |
|
28 virtual nsresult Init(); |
|
29 virtual nsresult Shutdown(); |
|
30 |
|
31 virtual const char * GetDeviceID(void); |
|
32 virtual nsCacheEntry * FindEntry(nsCString * key, bool *collision); |
|
33 virtual nsresult DeactivateEntry(nsCacheEntry * entry); |
|
34 virtual nsresult BindEntry(nsCacheEntry * entry); |
|
35 virtual void DoomEntry( nsCacheEntry * entry ); |
|
36 |
|
37 virtual nsresult OpenInputStreamForEntry(nsCacheEntry * entry, |
|
38 nsCacheAccessMode mode, |
|
39 uint32_t offset, |
|
40 nsIInputStream ** result); |
|
41 |
|
42 virtual nsresult OpenOutputStreamForEntry(nsCacheEntry * entry, |
|
43 nsCacheAccessMode mode, |
|
44 uint32_t offset, |
|
45 nsIOutputStream ** result); |
|
46 |
|
47 virtual nsresult GetFileForEntry(nsCacheEntry * entry, |
|
48 nsIFile ** result); |
|
49 |
|
50 virtual nsresult OnDataSizeChange(nsCacheEntry * entry, int32_t deltaSize); |
|
51 |
|
52 virtual nsresult Visit(nsICacheVisitor * visitor); |
|
53 |
|
54 virtual nsresult EvictEntries(const char * clientID); |
|
55 |
|
56 bool EntryIsTooBig(int64_t entrySize); |
|
57 |
|
58 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf); |
|
59 |
|
60 /** |
|
61 * Preference accessors |
|
62 */ |
|
63 void SetCacheParentDirectory(nsIFile * parentDir); |
|
64 void SetCapacity(uint32_t capacity); |
|
65 void SetMaxEntrySize(int32_t maxSizeInKilobytes); |
|
66 |
|
67 /* private: */ |
|
68 |
|
69 void getCacheDirectory(nsIFile ** result); |
|
70 uint32_t getCacheCapacity(); |
|
71 uint32_t getCacheSize(); |
|
72 uint32_t getEntryCount(); |
|
73 |
|
74 nsDiskCacheMap * CacheMap() { return &mCacheMap; } |
|
75 |
|
76 private: |
|
77 friend class nsDiskCacheDeviceDeactivateEntryEvent; |
|
78 friend class nsEvictDiskCacheEntriesEvent; |
|
79 friend class nsDiskCacheMap; |
|
80 /** |
|
81 * Private methods |
|
82 */ |
|
83 |
|
84 inline bool IsValidBinding(nsDiskCacheBinding *binding) |
|
85 { |
|
86 NS_ASSERTION(binding, " binding == nullptr"); |
|
87 NS_ASSERTION(binding->mDeactivateEvent == nullptr, |
|
88 " entry in process of deactivation"); |
|
89 return (binding && !binding->mDeactivateEvent); |
|
90 } |
|
91 |
|
92 bool Initialized() { return mInitialized; } |
|
93 |
|
94 nsresult Shutdown_Private(bool flush); |
|
95 nsresult DeactivateEntry_Private(nsCacheEntry * entry, |
|
96 nsDiskCacheBinding * binding); |
|
97 |
|
98 nsresult OpenDiskCache(); |
|
99 nsresult ClearDiskCache(); |
|
100 |
|
101 nsresult EvictDiskCacheEntries(uint32_t targetCapacity); |
|
102 |
|
103 /** |
|
104 * Member variables |
|
105 */ |
|
106 nsCOMPtr<nsIFile> mCacheDirectory; |
|
107 nsDiskCacheBindery mBindery; |
|
108 uint32_t mCacheCapacity; // Unit is KiB's |
|
109 int32_t mMaxEntrySize; // Unit is bytes internally |
|
110 // XXX need soft/hard limits, currentTotal |
|
111 nsDiskCacheMap mCacheMap; |
|
112 bool mInitialized; |
|
113 bool mClearingDiskCache; |
|
114 }; |
|
115 |
|
116 #endif // _nsDiskCacheDevice_h_ |