nsCacheEntry.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. #include "nsCache.h"
  7. #include "nspr.h"
  8. #include "nsCacheEntry.h"
  9. #include "nsCacheEntryDescriptor.h"
  10. #include "nsCacheMetaData.h"
  11. #include "nsCacheRequest.h"
  12. #include "nsThreadUtils.h"
  13. #include "nsError.h"
  14. #include "nsICacheService.h"
  15. #include "nsCacheService.h"
  16. #include "nsCacheDevice.h"
  17. #include "nsHashKeys.h"
  18. using namespace mozilla;
  19. nsCacheEntry::nsCacheEntry(const nsACString & key,
  20. bool streamBased,
  21. nsCacheStoragePolicy storagePolicy)
  22. : mKey(key),
  23. mFetchCount(0),
  24. mLastFetched(0),
  25. mLastModified(0),
  26. mExpirationTime(nsICache::NO_EXPIRATION_TIME),
  27. mFlags(0),
  28. mPredictedDataSize(-1),
  29. mDataSize(0),
  30. mCacheDevice(nullptr),
  31. mCustomDevice(nullptr),
  32. mData(nullptr)
  33. {
  34. MOZ_COUNT_CTOR(nsCacheEntry);
  35. PR_INIT_CLIST(this);
  36. PR_INIT_CLIST(&mRequestQ);
  37. PR_INIT_CLIST(&mDescriptorQ);
  38. if (streamBased) MarkStreamBased();
  39. SetStoragePolicy(storagePolicy);
  40. MarkPublic();
  41. }
  42. nsCacheEntry::~nsCacheEntry()
  43. {
  44. MOZ_COUNT_DTOR(nsCacheEntry);
  45. if (mData)
  46. nsCacheService::ReleaseObject_Locked(mData, mThread);
  47. }
  48. nsresult
  49. nsCacheEntry::Create( const char * key,
  50. bool streamBased,
  51. nsCacheStoragePolicy storagePolicy,
  52. nsCacheDevice * device,
  53. nsCacheEntry ** result)
  54. {
  55. nsCacheEntry* entry = new nsCacheEntry(nsCString(key),
  56. streamBased,
  57. storagePolicy);
  58. entry->SetCacheDevice(device);
  59. *result = entry;
  60. return NS_OK;
  61. }
  62. void
  63. nsCacheEntry::Fetched()
  64. {
  65. mLastFetched = SecondsFromPRTime(PR_Now());
  66. ++mFetchCount;
  67. MarkEntryDirty();
  68. }
  69. const char *
  70. nsCacheEntry::GetDeviceID()
  71. {
  72. if (mCacheDevice) return mCacheDevice->GetDeviceID();
  73. return nullptr;
  74. }
  75. void
  76. nsCacheEntry::TouchData()
  77. {
  78. mLastModified = SecondsFromPRTime(PR_Now());
  79. MarkDataDirty();
  80. }
  81. void
  82. nsCacheEntry::SetData(nsISupports * data)
  83. {
  84. if (mData) {
  85. nsCacheService::ReleaseObject_Locked(mData, mThread);
  86. mData = nullptr;
  87. }
  88. if (data) {
  89. NS_ADDREF(mData = data);
  90. mThread = do_GetCurrentThread();
  91. }
  92. }
  93. void
  94. nsCacheEntry::TouchMetaData()
  95. {
  96. mLastModified = SecondsFromPRTime(PR_Now());
  97. MarkMetaDataDirty();
  98. }
  99. /**
  100. * cache entry states
  101. * 0 descriptors (new entry)
  102. * 0 descriptors (existing, bound entry)
  103. * n descriptors (existing, bound entry) valid
  104. * n descriptors (existing, bound entry) not valid (wait until valid or doomed)
  105. */
  106. nsresult
  107. nsCacheEntry::RequestAccess(nsCacheRequest * request, nsCacheAccessMode *accessGranted)
  108. {
  109. nsresult rv = NS_OK;
  110. if (IsDoomed()) return NS_ERROR_CACHE_ENTRY_DOOMED;
  111. if (!IsInitialized()) {
  112. // brand new, unbound entry
  113. if (request->IsStreamBased()) MarkStreamBased();
  114. MarkInitialized();
  115. *accessGranted = request->AccessRequested() & nsICache::ACCESS_WRITE;
  116. NS_ASSERTION(*accessGranted, "new cache entry for READ-ONLY request");
  117. PR_APPEND_LINK(request, &mRequestQ);
  118. return rv;
  119. }
  120. if (IsStreamData() != request->IsStreamBased()) {
  121. *accessGranted = nsICache::ACCESS_NONE;
  122. return request->IsStreamBased() ?
  123. NS_ERROR_CACHE_DATA_IS_NOT_STREAM : NS_ERROR_CACHE_DATA_IS_STREAM;
  124. }
  125. if (PR_CLIST_IS_EMPTY(&mDescriptorQ)) {
  126. // 1st descriptor for existing bound entry
  127. *accessGranted = request->AccessRequested();
  128. if (*accessGranted & nsICache::ACCESS_WRITE) {
  129. MarkInvalid();
  130. } else {
  131. MarkValid();
  132. }
  133. } else {
  134. // nth request for existing, bound entry
  135. *accessGranted = request->AccessRequested() & ~nsICache::ACCESS_WRITE;
  136. if (!IsValid())
  137. rv = NS_ERROR_CACHE_WAIT_FOR_VALIDATION;
  138. }
  139. PR_APPEND_LINK(request,&mRequestQ);
  140. return rv;
  141. }
  142. nsresult
  143. nsCacheEntry::CreateDescriptor(nsCacheRequest * request,
  144. nsCacheAccessMode accessGranted,
  145. nsICacheEntryDescriptor ** result)
  146. {
  147. NS_ENSURE_ARG_POINTER(request && result);
  148. nsCacheEntryDescriptor * descriptor =
  149. new nsCacheEntryDescriptor(this, accessGranted);
  150. // XXX check request is on q
  151. PR_REMOVE_AND_INIT_LINK(request); // remove request regardless of success
  152. if (descriptor == nullptr)
  153. return NS_ERROR_OUT_OF_MEMORY;
  154. PR_APPEND_LINK(descriptor, &mDescriptorQ);
  155. CACHE_LOG_DEBUG((" descriptor %p created for request %p on entry %p\n",
  156. descriptor, request, this));
  157. NS_ADDREF(*result = descriptor);
  158. return NS_OK;
  159. }
  160. bool
  161. nsCacheEntry::RemoveRequest(nsCacheRequest * request)
  162. {
  163. // XXX if debug: verify this request belongs to this entry
  164. PR_REMOVE_AND_INIT_LINK(request);
  165. // return true if this entry should stay active
  166. return !((PR_CLIST_IS_EMPTY(&mRequestQ)) &&
  167. (PR_CLIST_IS_EMPTY(&mDescriptorQ)));
  168. }
  169. bool
  170. nsCacheEntry::RemoveDescriptor(nsCacheEntryDescriptor * descriptor,
  171. bool * doomEntry)
  172. {
  173. NS_ASSERTION(descriptor->CacheEntry() == this, "### Wrong cache entry!!");
  174. *doomEntry = descriptor->ClearCacheEntry();
  175. PR_REMOVE_AND_INIT_LINK(descriptor);
  176. if (!PR_CLIST_IS_EMPTY(&mDescriptorQ))
  177. return true; // stay active if we still have open descriptors
  178. if (PR_CLIST_IS_EMPTY(&mRequestQ))
  179. return false; // no descriptors or requests, we can deactivate
  180. return true; // find next best request to give a descriptor to
  181. }
  182. void
  183. nsCacheEntry::DetachDescriptors()
  184. {
  185. nsCacheEntryDescriptor * descriptor =
  186. (nsCacheEntryDescriptor *)PR_LIST_HEAD(&mDescriptorQ);
  187. while (descriptor != &mDescriptorQ) {
  188. nsCacheEntryDescriptor * nextDescriptor =
  189. (nsCacheEntryDescriptor *)PR_NEXT_LINK(descriptor);
  190. descriptor->ClearCacheEntry();
  191. PR_REMOVE_AND_INIT_LINK(descriptor);
  192. descriptor = nextDescriptor;
  193. }
  194. }
  195. void
  196. nsCacheEntry::GetDescriptors(
  197. nsTArray<RefPtr<nsCacheEntryDescriptor> > &outDescriptors)
  198. {
  199. nsCacheEntryDescriptor * descriptor =
  200. (nsCacheEntryDescriptor *)PR_LIST_HEAD(&mDescriptorQ);
  201. while (descriptor != &mDescriptorQ) {
  202. nsCacheEntryDescriptor * nextDescriptor =
  203. (nsCacheEntryDescriptor *)PR_NEXT_LINK(descriptor);
  204. outDescriptors.AppendElement(descriptor);
  205. descriptor = nextDescriptor;
  206. }
  207. }
  208. /******************************************************************************
  209. * nsCacheEntryInfo - for implementing about:cache
  210. *****************************************************************************/
  211. NS_IMPL_ISUPPORTS(nsCacheEntryInfo, nsICacheEntryInfo)
  212. NS_IMETHODIMP
  213. nsCacheEntryInfo::GetClientID(char ** clientID)
  214. {
  215. NS_ENSURE_ARG_POINTER(clientID);
  216. if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
  217. return ClientIDFromCacheKey(*mCacheEntry->Key(), clientID);
  218. }
  219. NS_IMETHODIMP
  220. nsCacheEntryInfo::GetDeviceID(char ** deviceID)
  221. {
  222. NS_ENSURE_ARG_POINTER(deviceID);
  223. if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
  224. *deviceID = NS_strdup(mCacheEntry->GetDeviceID());
  225. return *deviceID ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
  226. }
  227. NS_IMETHODIMP
  228. nsCacheEntryInfo::GetKey(nsACString &key)
  229. {
  230. if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
  231. return ClientKeyFromCacheKey(*mCacheEntry->Key(), key);
  232. }
  233. NS_IMETHODIMP
  234. nsCacheEntryInfo::GetFetchCount(int32_t * fetchCount)
  235. {
  236. NS_ENSURE_ARG_POINTER(fetchCount);
  237. if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
  238. *fetchCount = mCacheEntry->FetchCount();
  239. return NS_OK;
  240. }
  241. NS_IMETHODIMP
  242. nsCacheEntryInfo::GetLastFetched(uint32_t * lastFetched)
  243. {
  244. NS_ENSURE_ARG_POINTER(lastFetched);
  245. if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
  246. *lastFetched = mCacheEntry->LastFetched();
  247. return NS_OK;
  248. }
  249. NS_IMETHODIMP
  250. nsCacheEntryInfo::GetLastModified(uint32_t * lastModified)
  251. {
  252. NS_ENSURE_ARG_POINTER(lastModified);
  253. if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
  254. *lastModified = mCacheEntry->LastModified();
  255. return NS_OK;
  256. }
  257. NS_IMETHODIMP
  258. nsCacheEntryInfo::GetExpirationTime(uint32_t * expirationTime)
  259. {
  260. NS_ENSURE_ARG_POINTER(expirationTime);
  261. if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
  262. *expirationTime = mCacheEntry->ExpirationTime();
  263. return NS_OK;
  264. }
  265. NS_IMETHODIMP
  266. nsCacheEntryInfo::GetDataSize(uint32_t * dataSize)
  267. {
  268. NS_ENSURE_ARG_POINTER(dataSize);
  269. if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
  270. *dataSize = mCacheEntry->DataSize();
  271. return NS_OK;
  272. }
  273. NS_IMETHODIMP
  274. nsCacheEntryInfo::IsStreamBased(bool * result)
  275. {
  276. NS_ENSURE_ARG_POINTER(result);
  277. if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
  278. *result = mCacheEntry->IsStreamData();
  279. return NS_OK;
  280. }
  281. /******************************************************************************
  282. * nsCacheEntryHashTable
  283. *****************************************************************************/
  284. const PLDHashTableOps
  285. nsCacheEntryHashTable::ops =
  286. {
  287. HashKey,
  288. MatchEntry,
  289. MoveEntry,
  290. ClearEntry
  291. };
  292. nsCacheEntryHashTable::nsCacheEntryHashTable()
  293. : table(&ops, sizeof(nsCacheEntryHashTableEntry), kInitialTableLength)
  294. , initialized(false)
  295. {
  296. MOZ_COUNT_CTOR(nsCacheEntryHashTable);
  297. }
  298. nsCacheEntryHashTable::~nsCacheEntryHashTable()
  299. {
  300. MOZ_COUNT_DTOR(nsCacheEntryHashTable);
  301. if (initialized)
  302. Shutdown();
  303. }
  304. void
  305. nsCacheEntryHashTable::Init()
  306. {
  307. table.ClearAndPrepareForLength(kInitialTableLength);
  308. initialized = true;
  309. }
  310. void
  311. nsCacheEntryHashTable::Shutdown()
  312. {
  313. if (initialized) {
  314. table.ClearAndPrepareForLength(kInitialTableLength);
  315. initialized = false;
  316. }
  317. }
  318. nsCacheEntry *
  319. nsCacheEntryHashTable::GetEntry( const nsCString * key)
  320. {
  321. NS_ASSERTION(initialized, "nsCacheEntryHashTable not initialized");
  322. if (!initialized) return nullptr;
  323. PLDHashEntryHdr *hashEntry = table.Search(key);
  324. return hashEntry ? ((nsCacheEntryHashTableEntry *)hashEntry)->cacheEntry
  325. : nullptr;
  326. }
  327. nsresult
  328. nsCacheEntryHashTable::AddEntry( nsCacheEntry *cacheEntry)
  329. {
  330. PLDHashEntryHdr *hashEntry;
  331. NS_ASSERTION(initialized, "nsCacheEntryHashTable not initialized");
  332. if (!initialized) return NS_ERROR_NOT_INITIALIZED;
  333. if (!cacheEntry) return NS_ERROR_NULL_POINTER;
  334. hashEntry = table.Add(&(cacheEntry->mKey), fallible);
  335. if (!hashEntry)
  336. return NS_ERROR_FAILURE;
  337. NS_ASSERTION(((nsCacheEntryHashTableEntry *)hashEntry)->cacheEntry == 0,
  338. "### nsCacheEntryHashTable::AddEntry - entry already used");
  339. ((nsCacheEntryHashTableEntry *)hashEntry)->cacheEntry = cacheEntry;
  340. return NS_OK;
  341. }
  342. void
  343. nsCacheEntryHashTable::RemoveEntry( nsCacheEntry *cacheEntry)
  344. {
  345. NS_ASSERTION(initialized, "nsCacheEntryHashTable not initialized");
  346. NS_ASSERTION(cacheEntry, "### cacheEntry == nullptr");
  347. if (!initialized) return; // NS_ERROR_NOT_INITIALIZED
  348. #if DEBUG
  349. // XXX debug code to make sure we have the entry we're trying to remove
  350. nsCacheEntry *check = GetEntry(&(cacheEntry->mKey));
  351. NS_ASSERTION(check == cacheEntry, "### Attempting to remove unknown cache entry!!!");
  352. #endif
  353. table.Remove(&(cacheEntry->mKey));
  354. }
  355. PLDHashTable::Iterator
  356. nsCacheEntryHashTable::Iter()
  357. {
  358. return PLDHashTable::Iterator(&table);
  359. }
  360. /**
  361. * hash table operation callback functions
  362. */
  363. PLDHashNumber
  364. nsCacheEntryHashTable::HashKey(const void *key)
  365. {
  366. return HashString(*static_cast<const nsCString *>(key));
  367. }
  368. bool
  369. nsCacheEntryHashTable::MatchEntry(const PLDHashEntryHdr * hashEntry,
  370. const void * key)
  371. {
  372. NS_ASSERTION(key != nullptr, "### nsCacheEntryHashTable::MatchEntry : null key");
  373. nsCacheEntry *cacheEntry = ((nsCacheEntryHashTableEntry *)hashEntry)->cacheEntry;
  374. return cacheEntry->mKey.Equals(*(nsCString *)key);
  375. }
  376. void
  377. nsCacheEntryHashTable::MoveEntry(PLDHashTable * /* table */,
  378. const PLDHashEntryHdr *from,
  379. PLDHashEntryHdr *to)
  380. {
  381. ((nsCacheEntryHashTableEntry *)to)->cacheEntry =
  382. ((nsCacheEntryHashTableEntry *)from)->cacheEntry;
  383. }
  384. void
  385. nsCacheEntryHashTable::ClearEntry(PLDHashTable * /* table */,
  386. PLDHashEntryHdr * hashEntry)
  387. {
  388. ((nsCacheEntryHashTableEntry *)hashEntry)->cacheEntry = 0;
  389. }