nsAboutCache.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef nsAboutCache_h__
  6. #define nsAboutCache_h__
  7. #include "nsIAboutModule.h"
  8. #include "nsICacheStorageVisitor.h"
  9. #include "nsICacheStorage.h"
  10. #include "nsIChannel.h"
  11. #include "nsString.h"
  12. #include "nsIOutputStream.h"
  13. #include "nsILoadContextInfo.h"
  14. #include "nsCOMPtr.h"
  15. #include "nsTArray.h"
  16. #define NS_FORWARD_SAFE_NSICHANNEL_SUBSET(_to) \
  17. NS_IMETHOD GetOriginalURI(nsIURI * *aOriginalURI) override { return !_to ? NS_ERROR_NULL_POINTER : _to->GetOriginalURI(aOriginalURI); } \
  18. NS_IMETHOD SetOriginalURI(nsIURI *aOriginalURI) override { return !_to ? NS_ERROR_NULL_POINTER : _to->SetOriginalURI(aOriginalURI); } \
  19. NS_IMETHOD GetURI(nsIURI * *aURI) override { return !_to ? NS_ERROR_NULL_POINTER : _to->GetURI(aURI); } \
  20. NS_IMETHOD GetOwner(nsISupports * *aOwner) override { return !_to ? NS_ERROR_NULL_POINTER : _to->GetOwner(aOwner); } \
  21. NS_IMETHOD SetOwner(nsISupports *aOwner) override { return !_to ? NS_ERROR_NULL_POINTER : _to->SetOwner(aOwner); } \
  22. NS_IMETHOD GetNotificationCallbacks(nsIInterfaceRequestor * *aNotificationCallbacks) override { return !_to ? NS_ERROR_NULL_POINTER : _to->GetNotificationCallbacks(aNotificationCallbacks); } \
  23. NS_IMETHOD SetNotificationCallbacks(nsIInterfaceRequestor *aNotificationCallbacks) override { return !_to ? NS_ERROR_NULL_POINTER : _to->SetNotificationCallbacks(aNotificationCallbacks); } \
  24. NS_IMETHOD GetSecurityInfo(nsISupports * *aSecurityInfo) override { return !_to ? NS_ERROR_NULL_POINTER : _to->GetSecurityInfo(aSecurityInfo); } \
  25. NS_IMETHOD GetContentType(nsACString & aContentType) override { return !_to ? NS_ERROR_NULL_POINTER : _to->GetContentType(aContentType); } \
  26. NS_IMETHOD SetContentType(const nsACString & aContentType) override { return !_to ? NS_ERROR_NULL_POINTER : _to->SetContentType(aContentType); } \
  27. NS_IMETHOD GetContentCharset(nsACString & aContentCharset) override { return !_to ? NS_ERROR_NULL_POINTER : _to->GetContentCharset(aContentCharset); } \
  28. NS_IMETHOD SetContentCharset(const nsACString & aContentCharset) override { return !_to ? NS_ERROR_NULL_POINTER : _to->SetContentCharset(aContentCharset); } \
  29. NS_IMETHOD GetContentLength(int64_t *aContentLength) override { return !_to ? NS_ERROR_NULL_POINTER : _to->GetContentLength(aContentLength); } \
  30. NS_IMETHOD SetContentLength(int64_t aContentLength) override { return !_to ? NS_ERROR_NULL_POINTER : _to->SetContentLength(aContentLength); } \
  31. NS_IMETHOD GetContentDisposition(uint32_t *aContentDisposition) override { return !_to ? NS_ERROR_NULL_POINTER : _to->GetContentDisposition(aContentDisposition); } \
  32. NS_IMETHOD SetContentDisposition(uint32_t aContentDisposition) override { return !_to ? NS_ERROR_NULL_POINTER : _to->SetContentDisposition(aContentDisposition); } \
  33. NS_IMETHOD GetContentDispositionFilename(nsAString & aContentDispositionFilename) override { return !_to ? NS_ERROR_NULL_POINTER : _to->GetContentDispositionFilename(aContentDispositionFilename); } \
  34. NS_IMETHOD SetContentDispositionFilename(const nsAString & aContentDispositionFilename) override { return !_to ? NS_ERROR_NULL_POINTER : _to->SetContentDispositionFilename(aContentDispositionFilename); } \
  35. NS_IMETHOD GetContentDispositionHeader(nsACString & aContentDispositionHeader) override { return !_to ? NS_ERROR_NULL_POINTER : _to->GetContentDispositionHeader(aContentDispositionHeader); } \
  36. NS_IMETHOD GetLoadInfo(nsILoadInfo * *aLoadInfo) override { return !_to ? NS_ERROR_NULL_POINTER : _to->GetLoadInfo(aLoadInfo); } \
  37. NS_IMETHOD SetLoadInfo(nsILoadInfo *aLoadInfo) override { return !_to ? NS_ERROR_NULL_POINTER : _to->SetLoadInfo(aLoadInfo); } \
  38. class nsAboutCache final : public nsIAboutModule
  39. {
  40. public:
  41. NS_DECL_ISUPPORTS
  42. NS_DECL_NSIABOUTMODULE
  43. nsAboutCache() {}
  44. static MOZ_MUST_USE nsresult
  45. Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
  46. static MOZ_MUST_USE nsresult
  47. GetStorage(nsACString const & storageName, nsILoadContextInfo* loadInfo,
  48. nsICacheStorage **storage);
  49. protected:
  50. virtual ~nsAboutCache() {}
  51. class Channel final : public nsIChannel
  52. , public nsICacheStorageVisitor
  53. {
  54. NS_DECL_ISUPPORTS
  55. NS_DECL_NSICACHESTORAGEVISITOR
  56. NS_FORWARD_SAFE_NSIREQUEST(mChannel)
  57. NS_FORWARD_SAFE_NSICHANNEL_SUBSET(mChannel)
  58. NS_IMETHOD AsyncOpen(nsIStreamListener *aListener, nsISupports *aContext) override;
  59. NS_IMETHOD AsyncOpen2(nsIStreamListener *aListener) override;
  60. NS_IMETHOD Open(nsIInputStream * *_retval) override;
  61. NS_IMETHOD Open2(nsIInputStream * *_retval) override;
  62. private:
  63. virtual ~Channel() {}
  64. public:
  65. MOZ_MUST_USE nsresult Init(nsIURI* aURI, nsILoadInfo* aLoadInfo);
  66. MOZ_MUST_USE nsresult ParseURI(nsIURI * uri, nsACString & storage);
  67. // Finds a next storage we wish to visit (we use this method
  68. // even there is a specified storage name, which is the only
  69. // one in the list then.) Posts FireVisitStorage() when found.
  70. MOZ_MUST_USE nsresult VisitNextStorage();
  71. // Helper method that calls VisitStorage() for the current storage.
  72. // When it fails, OnCacheEntryVisitCompleted is simulated to close
  73. // the output stream and thus the about:cache channel.
  74. void FireVisitStorage();
  75. // Kiks the visit cycle for the given storage, names can be:
  76. // "disk", "memory", "appcache"
  77. // Note: any newly added storage type has to be manually handled here.
  78. MOZ_MUST_USE nsresult VisitStorage(nsACString const & storageName);
  79. // Writes content of mBuffer to mStream and truncates
  80. // the buffer. It may fail when the input stream is closed by canceling
  81. // the input stream channel. It can be used to stop the cache iteration
  82. // process.
  83. MOZ_MUST_USE nsresult FlushBuffer();
  84. // Whether we are showing overview status of all available
  85. // storages.
  86. bool mOverview;
  87. // Flag initially false, that indicates the entries header has
  88. // been added to the output HTML.
  89. bool mEntriesHeaderAdded;
  90. // Cancelation flag
  91. bool mCancel;
  92. // The context we are working with.
  93. nsCOMPtr<nsILoadContextInfo> mLoadInfo;
  94. nsCString mContextString;
  95. // The list of all storage names we want to visit
  96. nsTArray<nsCString> mStorageList;
  97. nsCString mStorageName;
  98. nsCOMPtr<nsICacheStorage> mStorage;
  99. // Output data buffering and streaming output
  100. nsCString mBuffer;
  101. nsCOMPtr<nsIOutputStream> mStream;
  102. // The input stream channel, the one that actually does the job
  103. nsCOMPtr<nsIChannel> mChannel;
  104. };
  105. };
  106. #define NS_ABOUT_CACHE_MODULE_CID \
  107. { /* 9158c470-86e4-11d4-9be2-00e09872a416 */ \
  108. 0x9158c470, \
  109. 0x86e4, \
  110. 0x11d4, \
  111. {0x9b, 0xe2, 0x00, 0xe0, 0x98, 0x72, 0xa4, 0x16} \
  112. }
  113. #endif // nsAboutCache_h__