nsDiskCacheEntry.cpp 4.0 KB

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