tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsAboutCache.h (11232B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      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 
      6 #ifndef nsAboutCache_h__
      7 #define nsAboutCache_h__
      8 
      9 #include "nsIAboutModule.h"
     10 #include "nsICacheStorageVisitor.h"
     11 #include "nsICacheStorage.h"
     12 
     13 #include "nsString.h"
     14 #include "nsIChannel.h"
     15 #include "nsIOutputStream.h"
     16 #include "nsILoadContextInfo.h"
     17 
     18 #include "nsCOMPtr.h"
     19 #include "nsTArray.h"
     20 
     21 #define NS_FORWARD_SAFE_NSICHANNEL_SUBSET(_to)                                 \
     22  NS_IMETHOD GetOriginalURI(nsIURI** aOriginalURI) override {                  \
     23    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
     24                  : (_to)->GetOriginalURI(aOriginalURI);                       \
     25  }                                                                            \
     26  NS_IMETHOD SetOriginalURI(nsIURI* aOriginalURI) override {                   \
     27    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
     28                  : (_to)->SetOriginalURI(aOriginalURI);                       \
     29  }                                                                            \
     30  NS_IMETHOD GetURI(nsIURI** aURI) override {                                  \
     31    return !(_to) ? NS_ERROR_NULL_POINTER : (_to)->GetURI(aURI);               \
     32  }                                                                            \
     33  NS_IMETHOD GetOwner(nsISupports** aOwner) override {                         \
     34    return !(_to) ? NS_ERROR_NULL_POINTER : (_to)->GetOwner(aOwner);           \
     35  }                                                                            \
     36  NS_IMETHOD SetOwner(nsISupports* aOwner) override {                          \
     37    return !(_to) ? NS_ERROR_NULL_POINTER : (_to)->SetOwner(aOwner);           \
     38  }                                                                            \
     39  NS_IMETHOD GetNotificationCallbacks(                                         \
     40      nsIInterfaceRequestor** aNotificationCallbacks) override {               \
     41    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
     42                  : (_to)->GetNotificationCallbacks(aNotificationCallbacks);   \
     43  }                                                                            \
     44  NS_IMETHOD SetNotificationCallbacks(                                         \
     45      nsIInterfaceRequestor* aNotificationCallbacks) override {                \
     46    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
     47                  : (_to)->SetNotificationCallbacks(aNotificationCallbacks);   \
     48  }                                                                            \
     49  NS_IMETHOD GetSecurityInfo(nsITransportSecurityInfo** aSecurityInfo)         \
     50      override {                                                               \
     51    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
     52                  : (_to)->GetSecurityInfo(aSecurityInfo);                     \
     53  }                                                                            \
     54  NS_IMETHOD GetContentType(nsACString& aContentType) override {               \
     55    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
     56                  : (_to)->GetContentType(aContentType);                       \
     57  }                                                                            \
     58  NS_IMETHOD SetContentType(const nsACString& aContentType) override {         \
     59    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
     60                  : (_to)->SetContentType(aContentType);                       \
     61  }                                                                            \
     62  NS_IMETHOD GetContentCharset(nsACString& aContentCharset) override {         \
     63    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
     64                  : (_to)->GetContentCharset(aContentCharset);                 \
     65  }                                                                            \
     66  NS_IMETHOD SetContentCharset(const nsACString& aContentCharset) override {   \
     67    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
     68                  : (_to)->SetContentCharset(aContentCharset);                 \
     69  }                                                                            \
     70  NS_IMETHOD GetContentLength(int64_t* aContentLength) override {              \
     71    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
     72                  : (_to)->GetContentLength(aContentLength);                   \
     73  }                                                                            \
     74  NS_IMETHOD SetContentLength(int64_t aContentLength) override {               \
     75    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
     76                  : (_to)->SetContentLength(aContentLength);                   \
     77  }                                                                            \
     78  NS_IMETHOD GetContentDisposition(uint32_t* aContentDisposition) override {   \
     79    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
     80                  : (_to)->GetContentDisposition(aContentDisposition);         \
     81  }                                                                            \
     82  NS_IMETHOD SetContentDisposition(uint32_t aContentDisposition) override {    \
     83    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
     84                  : (_to)->SetContentDisposition(aContentDisposition);         \
     85  }                                                                            \
     86  NS_IMETHOD GetContentDispositionFilename(                                    \
     87      nsAString& aContentDispositionFilename) override {                       \
     88    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
     89                  : (_to)->GetContentDispositionFilename(                      \
     90                        aContentDispositionFilename);                          \
     91  }                                                                            \
     92  NS_IMETHOD SetContentDispositionFilename(                                    \
     93      const nsAString& aContentDispositionFilename) override {                 \
     94    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
     95                  : (_to)->SetContentDispositionFilename(                      \
     96                        aContentDispositionFilename);                          \
     97  }                                                                            \
     98  NS_IMETHOD GetContentDispositionHeader(                                      \
     99      nsACString& aContentDispositionHeader) override {                        \
    100    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
    101                  : (_to)->GetContentDispositionHeader(                        \
    102                        aContentDispositionHeader);                            \
    103  }                                                                            \
    104  NS_IMETHOD GetLoadInfo(nsILoadInfo** aLoadInfo) override {                   \
    105    return !(_to) ? NS_ERROR_NULL_POINTER : (_to)->GetLoadInfo(aLoadInfo);     \
    106  }                                                                            \
    107  NS_IMETHOD SetLoadInfo(nsILoadInfo* aLoadInfo) override {                    \
    108    return !(_to) ? NS_ERROR_NULL_POINTER : (_to)->SetLoadInfo(aLoadInfo);     \
    109  }                                                                            \
    110  NS_IMETHOD GetIsDocument(bool* aIsDocument) override {                       \
    111    return !(_to) ? NS_ERROR_NULL_POINTER : (_to)->GetIsDocument(aIsDocument); \
    112  }                                                                            \
    113  NS_IMETHOD GetCanceled(bool* aCanceled) override {                           \
    114    return !(_to) ? NS_ERROR_NULL_POINTER : (_to)->GetCanceled(aCanceled);     \
    115  };
    116 
    117 class nsAboutCache final : public nsIAboutModule {
    118 public:
    119  NS_DECL_ISUPPORTS
    120  NS_DECL_NSIABOUTMODULE
    121 
    122  nsAboutCache() = default;
    123 
    124  [[nodiscard]] static nsresult Create(REFNSIID aIID, void** aResult);
    125 
    126  [[nodiscard]] static nsresult GetStorage(nsACString const& storageName,
    127                                           nsILoadContextInfo* loadInfo,
    128                                           nsICacheStorage** storage);
    129 
    130 protected:
    131  virtual ~nsAboutCache() = default;
    132 
    133  class Channel final : public nsIChannel, public nsICacheStorageVisitor {
    134    NS_DECL_ISUPPORTS
    135    NS_DECL_NSICACHESTORAGEVISITOR
    136    NS_FORWARD_SAFE_NSIREQUEST(mChannel)
    137    NS_FORWARD_SAFE_NSICHANNEL_SUBSET(mChannel)
    138    NS_IMETHOD AsyncOpen(nsIStreamListener* aListener) override;
    139    NS_IMETHOD Open(nsIInputStream** _retval) override;
    140 
    141   private:
    142    virtual ~Channel() = default;
    143 
    144   public:
    145    [[nodiscard]] nsresult Init(nsIURI* aURI, nsILoadInfo* aLoadInfo);
    146    [[nodiscard]] nsresult ParseURI(nsIURI* uri, nsACString& storage);
    147 
    148    // Finds a next storage we wish to visit (we use this method
    149    // even there is a specified storage name, which is the only
    150    // one in the list then.)  Posts FireVisitStorage() when found.
    151    [[nodiscard]] nsresult VisitNextStorage();
    152    // Helper method that calls VisitStorage() for the current storage.
    153    // When it fails, OnCacheEntryVisitCompleted is simulated to close
    154    // the output stream and thus the about:cache channel.
    155    void FireVisitStorage();
    156    // Kiks the visit cycle for the given storage, names can be:
    157    // "disk", "memory", "appcache"
    158    // Note: any newly added storage type has to be manually handled here.
    159    [[nodiscard]] nsresult VisitStorage(nsACString const& storageName);
    160 
    161    // Writes content of mBuffer to mStream and truncates
    162    // the buffer.  It may fail when the input stream is closed by canceling
    163    // the input stream channel.  It can be used to stop the cache iteration
    164    // process.
    165    [[nodiscard]] nsresult FlushBuffer();
    166 
    167    // Whether we are showing overview status of all available
    168    // storages.
    169    bool mOverview = false;
    170 
    171    // Flag initially false, that indicates the entries header has
    172    // been added to the output HTML.
    173    bool mEntriesHeaderAdded = false;
    174 
    175    // Cancelation flag
    176    bool mCancel = false;
    177 
    178    // The list of all storage names we want to visit
    179    nsTArray<nsCString> mStorageList;
    180    nsCString mStorageName;
    181    nsCOMPtr<nsICacheStorage> mStorage;
    182 
    183    // Output data buffering and streaming output
    184    nsCString mBuffer;
    185    nsCOMPtr<nsIOutputStream> mStream;
    186 
    187    // The input stream channel, the one that actually does the job
    188    nsCOMPtr<nsIChannel> mChannel;
    189  };
    190 };
    191 
    192 #define NS_ABOUT_CACHE_MODULE_CID             \
    193  {/* 9158c470-86e4-11d4-9be2-00e09872a416 */ \
    194   0x9158c470,                                \
    195   0x86e4,                                    \
    196   0x11d4,                                    \
    197   {0x9b, 0xe2, 0x00, 0xe0, 0x98, 0x72, 0xa4, 0x16}}
    198 
    199 #endif  // nsAboutCache_h__