|
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 #include "nsCache.h" |
|
8 #include "nsDiskCache.h" |
|
9 #include "nsDiskCacheEntry.h" |
|
10 #include "nsDiskCacheBinding.h" |
|
11 #include "nsCRT.h" |
|
12 |
|
13 #include "nsISerializable.h" |
|
14 #include "nsSerializationHelper.h" |
|
15 |
|
16 /****************************************************************************** |
|
17 * nsDiskCacheEntry |
|
18 *****************************************************************************/ |
|
19 |
|
20 /** |
|
21 * CreateCacheEntry() |
|
22 * |
|
23 * Creates an nsCacheEntry and sets all fields except for the binding. |
|
24 */ |
|
25 nsCacheEntry * |
|
26 nsDiskCacheEntry::CreateCacheEntry(nsCacheDevice * device) |
|
27 { |
|
28 nsCacheEntry * entry = nullptr; |
|
29 nsresult rv = nsCacheEntry::Create(Key(), |
|
30 nsICache::STREAM_BASED, |
|
31 nsICache::STORE_ON_DISK, |
|
32 device, |
|
33 &entry); |
|
34 if (NS_FAILED(rv) || !entry) return nullptr; |
|
35 |
|
36 entry->SetFetchCount(mFetchCount); |
|
37 entry->SetLastFetched(mLastFetched); |
|
38 entry->SetLastModified(mLastModified); |
|
39 entry->SetExpirationTime(mExpirationTime); |
|
40 entry->SetCacheDevice(device); |
|
41 // XXX why does nsCacheService have to fill out device in BindEntry()? |
|
42 entry->SetDataSize(mDataSize); |
|
43 |
|
44 rv = entry->UnflattenMetaData(MetaData(), mMetaDataSize); |
|
45 if (NS_FAILED(rv)) { |
|
46 delete entry; |
|
47 return nullptr; |
|
48 } |
|
49 |
|
50 // Restore security info, if present |
|
51 const char* info = entry->GetMetaDataElement("security-info"); |
|
52 if (info) { |
|
53 nsCOMPtr<nsISupports> infoObj; |
|
54 rv = NS_DeserializeObject(nsDependentCString(info), |
|
55 getter_AddRefs(infoObj)); |
|
56 if (NS_FAILED(rv)) { |
|
57 delete entry; |
|
58 return nullptr; |
|
59 } |
|
60 entry->SetSecurityInfo(infoObj); |
|
61 } |
|
62 |
|
63 return entry; |
|
64 } |
|
65 |
|
66 |
|
67 /****************************************************************************** |
|
68 * nsDiskCacheEntryInfo |
|
69 *****************************************************************************/ |
|
70 |
|
71 NS_IMPL_ISUPPORTS(nsDiskCacheEntryInfo, nsICacheEntryInfo) |
|
72 |
|
73 NS_IMETHODIMP nsDiskCacheEntryInfo::GetClientID(char ** clientID) |
|
74 { |
|
75 NS_ENSURE_ARG_POINTER(clientID); |
|
76 return ClientIDFromCacheKey(nsDependentCString(mDiskEntry->Key()), clientID); |
|
77 } |
|
78 |
|
79 extern const char DISK_CACHE_DEVICE_ID[]; |
|
80 NS_IMETHODIMP nsDiskCacheEntryInfo::GetDeviceID(char ** deviceID) |
|
81 { |
|
82 NS_ENSURE_ARG_POINTER(deviceID); |
|
83 *deviceID = NS_strdup(mDeviceID); |
|
84 return *deviceID ? NS_OK : NS_ERROR_OUT_OF_MEMORY; |
|
85 } |
|
86 |
|
87 |
|
88 NS_IMETHODIMP nsDiskCacheEntryInfo::GetKey(nsACString &clientKey) |
|
89 { |
|
90 return ClientKeyFromCacheKey(nsDependentCString(mDiskEntry->Key()), clientKey); |
|
91 } |
|
92 |
|
93 NS_IMETHODIMP nsDiskCacheEntryInfo::GetFetchCount(int32_t *aFetchCount) |
|
94 { |
|
95 NS_ENSURE_ARG_POINTER(aFetchCount); |
|
96 *aFetchCount = mDiskEntry->mFetchCount; |
|
97 return NS_OK; |
|
98 } |
|
99 |
|
100 NS_IMETHODIMP nsDiskCacheEntryInfo::GetLastFetched(uint32_t *aLastFetched) |
|
101 { |
|
102 NS_ENSURE_ARG_POINTER(aLastFetched); |
|
103 *aLastFetched = mDiskEntry->mLastFetched; |
|
104 return NS_OK; |
|
105 } |
|
106 |
|
107 NS_IMETHODIMP nsDiskCacheEntryInfo::GetLastModified(uint32_t *aLastModified) |
|
108 { |
|
109 NS_ENSURE_ARG_POINTER(aLastModified); |
|
110 *aLastModified = mDiskEntry->mLastModified; |
|
111 return NS_OK; |
|
112 } |
|
113 |
|
114 NS_IMETHODIMP nsDiskCacheEntryInfo::GetExpirationTime(uint32_t *aExpirationTime) |
|
115 { |
|
116 NS_ENSURE_ARG_POINTER(aExpirationTime); |
|
117 *aExpirationTime = mDiskEntry->mExpirationTime; |
|
118 return NS_OK; |
|
119 } |
|
120 |
|
121 NS_IMETHODIMP nsDiskCacheEntryInfo::IsStreamBased(bool *aStreamBased) |
|
122 { |
|
123 NS_ENSURE_ARG_POINTER(aStreamBased); |
|
124 *aStreamBased = true; |
|
125 return NS_OK; |
|
126 } |
|
127 |
|
128 NS_IMETHODIMP nsDiskCacheEntryInfo::GetDataSize(uint32_t *aDataSize) |
|
129 { |
|
130 NS_ENSURE_ARG_POINTER(aDataSize); |
|
131 *aDataSize = mDiskEntry->mDataSize; |
|
132 return NS_OK; |
|
133 } |