nsCacheSession.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "nsCacheSession.h"
  7. #include "nsCacheService.h"
  8. #include "nsCRT.h"
  9. #include "nsThreadUtils.h"
  10. NS_IMPL_ISUPPORTS(nsCacheSession, nsICacheSession)
  11. nsCacheSession::nsCacheSession(const char * clientID,
  12. nsCacheStoragePolicy storagePolicy,
  13. bool streamBased)
  14. : mClientID(clientID),
  15. mInfo(0)
  16. {
  17. SetStoragePolicy(storagePolicy);
  18. if (streamBased) MarkStreamBased();
  19. else SetStoragePolicy(nsICache::STORE_IN_MEMORY);
  20. MarkPublic();
  21. MarkDoomEntriesIfExpired();
  22. }
  23. nsCacheSession::~nsCacheSession()
  24. {
  25. /* destructor code */
  26. // notify service we are going away?
  27. }
  28. NS_IMETHODIMP nsCacheSession::GetDoomEntriesIfExpired(bool *result)
  29. {
  30. NS_ENSURE_ARG_POINTER(result);
  31. *result = WillDoomEntriesIfExpired();
  32. return NS_OK;
  33. }
  34. NS_IMETHODIMP nsCacheSession::SetProfileDirectory(nsIFile *profileDir)
  35. {
  36. if (StoragePolicy() != nsICache::STORE_OFFLINE && profileDir) {
  37. // Profile directory override is currently implemented only for
  38. // offline cache. This is an early failure to prevent the request
  39. // being processed before it would fail later because of inability
  40. // to assign a cache base dir.
  41. return NS_ERROR_UNEXPECTED;
  42. }
  43. mProfileDir = profileDir;
  44. return NS_OK;
  45. }
  46. NS_IMETHODIMP nsCacheSession::GetProfileDirectory(nsIFile **profileDir)
  47. {
  48. if (mProfileDir)
  49. NS_ADDREF(*profileDir = mProfileDir);
  50. else
  51. *profileDir = nullptr;
  52. return NS_OK;
  53. }
  54. NS_IMETHODIMP nsCacheSession::SetDoomEntriesIfExpired(bool doomEntriesIfExpired)
  55. {
  56. if (doomEntriesIfExpired) MarkDoomEntriesIfExpired();
  57. else ClearDoomEntriesIfExpired();
  58. return NS_OK;
  59. }
  60. NS_IMETHODIMP
  61. nsCacheSession::OpenCacheEntry(const nsACString & key,
  62. nsCacheAccessMode accessRequested,
  63. bool blockingMode,
  64. nsICacheEntryDescriptor ** result)
  65. {
  66. nsresult rv;
  67. if (NS_IsMainThread())
  68. rv = NS_ERROR_NOT_AVAILABLE;
  69. else
  70. rv = nsCacheService::OpenCacheEntry(this,
  71. key,
  72. accessRequested,
  73. blockingMode,
  74. nullptr, // no listener
  75. result);
  76. return rv;
  77. }
  78. NS_IMETHODIMP nsCacheSession::AsyncOpenCacheEntry(const nsACString & key,
  79. nsCacheAccessMode accessRequested,
  80. nsICacheListener *listener,
  81. bool noWait)
  82. {
  83. nsresult rv;
  84. rv = nsCacheService::OpenCacheEntry(this,
  85. key,
  86. accessRequested,
  87. !noWait,
  88. listener,
  89. nullptr); // no result
  90. if (rv == NS_ERROR_CACHE_WAIT_FOR_VALIDATION) rv = NS_OK;
  91. return rv;
  92. }
  93. NS_IMETHODIMP nsCacheSession::EvictEntries()
  94. {
  95. return nsCacheService::EvictEntriesForSession(this);
  96. }
  97. NS_IMETHODIMP nsCacheSession::IsStorageEnabled(bool *result)
  98. {
  99. return nsCacheService::IsStorageEnabledForPolicy(StoragePolicy(), result);
  100. }
  101. NS_IMETHODIMP nsCacheSession::DoomEntry(const nsACString &key,
  102. nsICacheListener *listener)
  103. {
  104. return nsCacheService::DoomEntry(this, key, listener);
  105. }
  106. NS_IMETHODIMP nsCacheSession::GetIsPrivate(bool* aPrivate)
  107. {
  108. *aPrivate = IsPrivate();
  109. return NS_OK;
  110. }
  111. NS_IMETHODIMP nsCacheSession::SetIsPrivate(bool aPrivate)
  112. {
  113. if (aPrivate)
  114. MarkPrivate();
  115. else
  116. MarkPublic();
  117. return NS_OK;
  118. }