summaryrefslogtreecommitdiffstats
path: root/netwerk/protocol/about/nsAboutCache.h
blob: 65292b9aaeb01b6ef5c0297f3c103f6ff6141be7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef nsAboutCache_h__
#define nsAboutCache_h__

#include "nsIAboutModule.h"
#include "nsICacheStorageVisitor.h"
#include "nsICacheStorage.h"

#include "nsString.h"
#include "nsIChannel.h"
#include "nsIOutputStream.h"
#include "nsILoadContextInfo.h"

#include "nsCOMPtr.h"
#include "nsTArray.h"

#define NS_FORWARD_SAFE_NSICHANNEL_SUBSET(_to)                                 \
  NS_IMETHOD GetOriginalURI(nsIURI** aOriginalURI) override {                  \
    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
                  : (_to)->GetOriginalURI(aOriginalURI);                       \
  }                                                                            \
  NS_IMETHOD SetOriginalURI(nsIURI* aOriginalURI) override {                   \
    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
                  : (_to)->SetOriginalURI(aOriginalURI);                       \
  }                                                                            \
  NS_IMETHOD GetURI(nsIURI** aURI) override {                                  \
    return !(_to) ? NS_ERROR_NULL_POINTER : (_to)->GetURI(aURI);               \
  }                                                                            \
  NS_IMETHOD GetOwner(nsISupports** aOwner) override {                         \
    return !(_to) ? NS_ERROR_NULL_POINTER : (_to)->GetOwner(aOwner);           \
  }                                                                            \
  NS_IMETHOD SetOwner(nsISupports* aOwner) override {                          \
    return !(_to) ? NS_ERROR_NULL_POINTER : (_to)->SetOwner(aOwner);           \
  }                                                                            \
  NS_IMETHOD GetNotificationCallbacks(                                         \
      nsIInterfaceRequestor** aNotificationCallbacks) override {               \
    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
                  : (_to)->GetNotificationCallbacks(aNotificationCallbacks);   \
  }                                                                            \
  NS_IMETHOD SetNotificationCallbacks(                                         \
      nsIInterfaceRequestor* aNotificationCallbacks) override {                \
    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
                  : (_to)->SetNotificationCallbacks(aNotificationCallbacks);   \
  }                                                                            \
  NS_IMETHOD GetSecurityInfo(nsITransportSecurityInfo** aSecurityInfo)         \
      override {                                                               \
    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
                  : (_to)->GetSecurityInfo(aSecurityInfo);                     \
  }                                                                            \
  NS_IMETHOD GetContentType(nsACString& aContentType) override {               \
    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
                  : (_to)->GetContentType(aContentType);                       \
  }                                                                            \
  NS_IMETHOD SetContentType(const nsACString& aContentType) override {         \
    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
                  : (_to)->SetContentType(aContentType);                       \
  }                                                                            \
  NS_IMETHOD GetContentCharset(nsACString& aContentCharset) override {         \
    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
                  : (_to)->GetContentCharset(aContentCharset);                 \
  }                                                                            \
  NS_IMETHOD SetContentCharset(const nsACString& aContentCharset) override {   \
    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
                  : (_to)->SetContentCharset(aContentCharset);                 \
  }                                                                            \
  NS_IMETHOD GetContentLength(int64_t* aContentLength) override {              \
    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
                  : (_to)->GetContentLength(aContentLength);                   \
  }                                                                            \
  NS_IMETHOD SetContentLength(int64_t aContentLength) override {               \
    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
                  : (_to)->SetContentLength(aContentLength);                   \
  }                                                                            \
  NS_IMETHOD GetContentDisposition(uint32_t* aContentDisposition) override {   \
    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
                  : (_to)->GetContentDisposition(aContentDisposition);         \
  }                                                                            \
  NS_IMETHOD SetContentDisposition(uint32_t aContentDisposition) override {    \
    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
                  : (_to)->SetContentDisposition(aContentDisposition);         \
  }                                                                            \
  NS_IMETHOD GetContentDispositionFilename(                                    \
      nsAString& aContentDispositionFilename) override {                       \
    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
                  : (_to)->GetContentDispositionFilename(                      \
                        aContentDispositionFilename);                          \
  }                                                                            \
  NS_IMETHOD SetContentDispositionFilename(                                    \
      const nsAString& aContentDispositionFilename) override {                 \
    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
                  : (_to)->SetContentDispositionFilename(                      \
                        aContentDispositionFilename);                          \
  }                                                                            \
  NS_IMETHOD GetContentDispositionHeader(                                      \
      nsACString& aContentDispositionHeader) override {                        \
    return !(_to) ? NS_ERROR_NULL_POINTER                                      \
                  : (_to)->GetContentDispositionHeader(                        \
                        aContentDispositionHeader);                            \
  }                                                                            \
  NS_IMETHOD GetLoadInfo(nsILoadInfo** aLoadInfo) override {                   \
    return !(_to) ? NS_ERROR_NULL_POINTER : (_to)->GetLoadInfo(aLoadInfo);     \
  }                                                                            \
  NS_IMETHOD SetLoadInfo(nsILoadInfo* aLoadInfo) override {                    \
    return !(_to) ? NS_ERROR_NULL_POINTER : (_to)->SetLoadInfo(aLoadInfo);     \
  }                                                                            \
  NS_IMETHOD GetIsDocument(bool* aIsDocument) override {                       \
    return !(_to) ? NS_ERROR_NULL_POINTER : (_to)->GetIsDocument(aIsDocument); \
  }                                                                            \
  NS_IMETHOD GetCanceled(bool* aCanceled) override {                           \
    return !(_to) ? NS_ERROR_NULL_POINTER : (_to)->GetCanceled(aCanceled);     \
  };

class nsAboutCache final : public nsIAboutModule {
 public:
  NS_DECL_ISUPPORTS
  NS_DECL_NSIABOUTMODULE

  nsAboutCache() = default;

  [[nodiscard]] static nsresult Create(REFNSIID aIID, void** aResult);

  [[nodiscard]] static nsresult GetStorage(nsACString const& storageName,
                                           nsILoadContextInfo* loadInfo,
                                           nsICacheStorage** storage);

 protected:
  virtual ~nsAboutCache() = default;

  class Channel final : public nsIChannel, public nsICacheStorageVisitor {
    NS_DECL_ISUPPORTS
    NS_DECL_NSICACHESTORAGEVISITOR
    NS_FORWARD_SAFE_NSIREQUEST(mChannel)
    NS_FORWARD_SAFE_NSICHANNEL_SUBSET(mChannel)
    NS_IMETHOD AsyncOpen(nsIStreamListener* aListener) override;
    NS_IMETHOD Open(nsIInputStream** _retval) override;

   private:
    virtual ~Channel() = default;

   public:
    [[nodiscard]] nsresult Init(nsIURI* aURI, nsILoadInfo* aLoadInfo);
    [[nodiscard]] nsresult ParseURI(nsIURI* uri, nsACString& storage);

    // Finds a next storage we wish to visit (we use this method
    // even there is a specified storage name, which is the only
    // one in the list then.)  Posts FireVisitStorage() when found.
    [[nodiscard]] nsresult VisitNextStorage();
    // Helper method that calls VisitStorage() for the current storage.
    // When it fails, OnCacheEntryVisitCompleted is simulated to close
    // the output stream and thus the about:cache channel.
    void FireVisitStorage();
    // Kiks the visit cycle for the given storage, names can be:
    // "disk", "memory", "appcache"
    // Note: any newly added storage type has to be manually handled here.
    [[nodiscard]] nsresult VisitStorage(nsACString const& storageName);

    // Writes content of mBuffer to mStream and truncates
    // the buffer.  It may fail when the input stream is closed by canceling
    // the input stream channel.  It can be used to stop the cache iteration
    // process.
    [[nodiscard]] nsresult FlushBuffer();

    // Whether we are showing overview status of all available
    // storages.
    bool mOverview = false;

    // Flag initially false, that indicates the entries header has
    // been added to the output HTML.
    bool mEntriesHeaderAdded = false;

    // Cancelation flag
    bool mCancel = false;

    // The list of all storage names we want to visit
    nsTArray<nsCString> mStorageList;
    nsCString mStorageName;
    nsCOMPtr<nsICacheStorage> mStorage;

    // Output data buffering and streaming output
    nsCString mBuffer;
    nsCOMPtr<nsIOutputStream> mStream;

    // The input stream channel, the one that actually does the job
    nsCOMPtr<nsIChannel> mChannel;
  };
};

#define NS_ABOUT_CACHE_MODULE_CID                    \
  { /* 9158c470-86e4-11d4-9be2-00e09872a416 */       \
    0x9158c470, 0x86e4, 0x11d4, {                    \
      0x9b, 0xe2, 0x00, 0xe0, 0x98, 0x72, 0xa4, 0x16 \
    }                                                \
  }

#endif  // nsAboutCache_h__