/src/mozilla-central/uriloader/prefetch/nsOfflineCacheUpdate.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 4; 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 | | |
6 | | #ifndef nsOfflineCacheUpdate_h__ |
7 | | #define nsOfflineCacheUpdate_h__ |
8 | | |
9 | | #include "nsIOfflineCacheUpdate.h" |
10 | | |
11 | | #include "nsAutoPtr.h" |
12 | | #include "nsCOMArray.h" |
13 | | #include "nsCOMPtr.h" |
14 | | #include "nsIChannelEventSink.h" |
15 | | #include "nsIInterfaceRequestor.h" |
16 | | #include "nsIMutableArray.h" |
17 | | #include "nsIObserver.h" |
18 | | #include "nsIObserverService.h" |
19 | | #include "nsIApplicationCache.h" |
20 | | #include "nsIRequestObserver.h" |
21 | | #include "nsIRunnable.h" |
22 | | #include "nsIStreamListener.h" |
23 | | #include "nsIURI.h" |
24 | | #include "nsIWebProgressListener.h" |
25 | | #include "nsClassHashtable.h" |
26 | | #include "nsString.h" |
27 | | #include "nsTArray.h" |
28 | | #include "nsWeakReference.h" |
29 | | #include "nsICryptoHash.h" |
30 | | #include "mozilla/Attributes.h" |
31 | | #include "mozilla/WeakPtr.h" |
32 | | #include "nsTHashtable.h" |
33 | | #include "nsHashKeys.h" |
34 | | |
35 | | class nsOfflineCacheUpdate; |
36 | | |
37 | | class nsOfflineCacheUpdateItem : public nsIStreamListener |
38 | | , public nsIRunnable |
39 | | , public nsIInterfaceRequestor |
40 | | , public nsIChannelEventSink |
41 | | { |
42 | | public: |
43 | | NS_DECL_ISUPPORTS |
44 | | NS_DECL_NSIREQUESTOBSERVER |
45 | | NS_DECL_NSISTREAMLISTENER |
46 | | NS_DECL_NSIRUNNABLE |
47 | | NS_DECL_NSIINTERFACEREQUESTOR |
48 | | NS_DECL_NSICHANNELEVENTSINK |
49 | | |
50 | | nsOfflineCacheUpdateItem(nsIURI *aURI, |
51 | | nsIURI *aReferrerURI, |
52 | | nsIPrincipal* aLoadingPrincipal, |
53 | | nsIApplicationCache *aApplicationCache, |
54 | | nsIApplicationCache *aPreviousApplicationCache, |
55 | | uint32_t aType, |
56 | | uint32_t aLoadFlags); |
57 | | |
58 | | nsCOMPtr<nsIURI> mURI; |
59 | | nsCOMPtr<nsIURI> mReferrerURI; |
60 | | nsCOMPtr<nsIPrincipal> mLoadingPrincipal; |
61 | | nsCOMPtr<nsIApplicationCache> mApplicationCache; |
62 | | nsCOMPtr<nsIApplicationCache> mPreviousApplicationCache; |
63 | | nsCString mCacheKey; |
64 | | uint32_t mItemType; |
65 | | uint32_t mLoadFlags; |
66 | | |
67 | | nsresult OpenChannel(nsOfflineCacheUpdate *aUpdate); |
68 | | nsresult Cancel(); |
69 | | nsresult GetRequestSucceeded(bool * succeeded); |
70 | | |
71 | | bool IsInProgress(); |
72 | | bool IsScheduled(); |
73 | | bool IsCompleted(); |
74 | | |
75 | | nsresult GetStatus(uint16_t *aStatus); |
76 | | |
77 | | private: |
78 | | enum LoadStatus : uint16_t { |
79 | | UNINITIALIZED = 0U, |
80 | | REQUESTED = 1U, |
81 | | RECEIVING = 2U, |
82 | | LOADED = 3U |
83 | | }; |
84 | | |
85 | | RefPtr<nsOfflineCacheUpdate> mUpdate; |
86 | | nsCOMPtr<nsIChannel> mChannel; |
87 | | uint16_t mState; |
88 | | |
89 | | protected: |
90 | | virtual ~nsOfflineCacheUpdateItem(); |
91 | | |
92 | | int64_t mBytesRead; |
93 | | }; |
94 | | |
95 | | |
96 | | class nsOfflineManifestItem : public nsOfflineCacheUpdateItem |
97 | | { |
98 | | public: |
99 | | NS_DECL_NSISTREAMLISTENER |
100 | | NS_DECL_NSIREQUESTOBSERVER |
101 | | |
102 | | nsOfflineManifestItem(nsIURI *aURI, |
103 | | nsIURI *aReferrerURI, |
104 | | nsIPrincipal* aLoadingPrincipal, |
105 | | nsIApplicationCache *aApplicationCache, |
106 | | nsIApplicationCache *aPreviousApplicationCache); |
107 | | virtual ~nsOfflineManifestItem(); |
108 | | |
109 | 0 | nsCOMArray<nsIURI> &GetExplicitURIs() { return mExplicitURIs; } |
110 | 0 | nsCOMArray<nsIURI> &GetAnonymousURIs() { return mAnonymousURIs; } |
111 | 0 | nsCOMArray<nsIURI> &GetFallbackURIs() { return mFallbackURIs; } |
112 | | |
113 | | nsTArray<nsCString> &GetOpportunisticNamespaces() |
114 | 0 | { return mOpportunisticNamespaces; } |
115 | | nsIArray *GetNamespaces() |
116 | 0 | { return mNamespaces.get(); } |
117 | | |
118 | | bool ParseSucceeded() |
119 | 0 | { return (mParserState != PARSE_INIT && mParserState != PARSE_ERROR); } |
120 | 0 | bool NeedsUpdate() { return mParserState != PARSE_INIT && mNeedsUpdate; } |
121 | | |
122 | | void GetManifestHash(nsCString &aManifestHash) |
123 | 0 | { aManifestHash = mManifestHashValue; } |
124 | | |
125 | | private: |
126 | | static nsresult ReadManifest(nsIInputStream *aInputStream, |
127 | | void *aClosure, |
128 | | const char *aFromSegment, |
129 | | uint32_t aOffset, |
130 | | uint32_t aCount, |
131 | | uint32_t *aBytesConsumed); |
132 | | |
133 | | nsresult AddNamespace(uint32_t namespaceType, |
134 | | const nsCString &namespaceSpec, |
135 | | const nsCString &data); |
136 | | |
137 | | nsresult HandleManifestLine(const nsCString::const_iterator &aBegin, |
138 | | const nsCString::const_iterator &aEnd); |
139 | | |
140 | | /** |
141 | | * Saves "offline-manifest-hash" meta data from the old offline cache |
142 | | * token to mOldManifestHashValue member to be compared on |
143 | | * successfull load. |
144 | | */ |
145 | | nsresult GetOldManifestContentHash(nsIRequest *aRequest); |
146 | | /** |
147 | | * This method setups the mNeedsUpdate to false when hash value |
148 | | * of the just downloaded manifest file is the same as stored in cache's |
149 | | * "offline-manifest-hash" meta data. Otherwise stores the new value |
150 | | * to this meta data. |
151 | | */ |
152 | | nsresult CheckNewManifestContentHash(nsIRequest *aRequest); |
153 | | |
154 | | void ReadStrictFileOriginPolicyPref(); |
155 | | |
156 | | enum { |
157 | | PARSE_INIT, |
158 | | PARSE_CACHE_ENTRIES, |
159 | | PARSE_FALLBACK_ENTRIES, |
160 | | PARSE_BYPASS_ENTRIES, |
161 | | PARSE_UNKNOWN_SECTION, |
162 | | PARSE_ERROR |
163 | | } mParserState; |
164 | | |
165 | | nsCString mReadBuf; |
166 | | |
167 | | nsCOMArray<nsIURI> mExplicitURIs; |
168 | | nsCOMArray<nsIURI> mAnonymousURIs; |
169 | | nsCOMArray<nsIURI> mFallbackURIs; |
170 | | |
171 | | // All opportunistic caching namespaces. Used to decide whether |
172 | | // to include previously-opportunistically-cached entries. |
173 | | nsTArray<nsCString> mOpportunisticNamespaces; |
174 | | |
175 | | // Array of nsIApplicationCacheNamespace objects specified by the |
176 | | // manifest. |
177 | | nsCOMPtr<nsIMutableArray> mNamespaces; |
178 | | |
179 | | bool mNeedsUpdate; |
180 | | bool mStrictFileOriginPolicy; |
181 | | |
182 | | // manifest hash data |
183 | | nsCOMPtr<nsICryptoHash> mManifestHash; |
184 | | bool mManifestHashInitialized; |
185 | | nsCString mManifestHashValue; |
186 | | nsCString mOldManifestHashValue; |
187 | | }; |
188 | | |
189 | | class nsOfflineCacheUpdateOwner |
190 | | : public mozilla::SupportsWeakPtr<nsOfflineCacheUpdateOwner> |
191 | | { |
192 | | public: |
193 | | MOZ_DECLARE_WEAKREFERENCE_TYPENAME(nsOfflineCacheUpdateOwner) |
194 | 0 | virtual ~nsOfflineCacheUpdateOwner() {} |
195 | | virtual nsresult UpdateFinished(nsOfflineCacheUpdate *aUpdate) = 0; |
196 | | }; |
197 | | |
198 | | class nsOfflineCacheUpdate final : public nsIOfflineCacheUpdate |
199 | | , public nsIOfflineCacheUpdateObserver |
200 | | , public nsIRunnable |
201 | | , public nsOfflineCacheUpdateOwner |
202 | | { |
203 | | public: |
204 | | NS_DECL_ISUPPORTS |
205 | | NS_DECL_NSIOFFLINECACHEUPDATE |
206 | | NS_DECL_NSIOFFLINECACHEUPDATEOBSERVER |
207 | | NS_DECL_NSIRUNNABLE |
208 | | |
209 | | nsOfflineCacheUpdate(); |
210 | | |
211 | | static nsresult GetCacheKey(nsIURI *aURI, nsACString &aKey); |
212 | | |
213 | | nsresult Init(); |
214 | | |
215 | | nsresult Begin(); |
216 | | |
217 | | void LoadCompleted(nsOfflineCacheUpdateItem *aItem); |
218 | | void ManifestCheckCompleted(nsresult aStatus, |
219 | | const nsCString &aManifestHash); |
220 | | void StickDocument(nsIURI *aDocumentURI); |
221 | | |
222 | | void SetOwner(nsOfflineCacheUpdateOwner *aOwner); |
223 | | |
224 | | bool IsForGroupID(const nsACString& groupID); |
225 | | bool IsForProfile(nsIFile* aCustomProfileDir); |
226 | | |
227 | | virtual nsresult UpdateFinished(nsOfflineCacheUpdate *aUpdate) override; |
228 | | |
229 | | protected: |
230 | | ~nsOfflineCacheUpdate(); |
231 | | |
232 | | friend class nsOfflineCacheUpdateItem; |
233 | | void OnByteProgress(uint64_t byteIncrement); |
234 | | |
235 | | private: |
236 | | nsresult InitInternal(nsIURI *aManifestURI, nsIPrincipal* aPrincipal); |
237 | | nsresult HandleManifest(bool *aDoUpdate); |
238 | | nsresult AddURI(nsIURI *aURI, uint32_t aItemType, uint32_t aLoadFlags = 0); |
239 | | |
240 | | nsresult ProcessNextURI(); |
241 | | |
242 | | // Adds items from the previous cache witha type matching aType. |
243 | | // If namespaceFilter is non-null, only items matching the |
244 | | // specified namespaces will be added. |
245 | | nsresult AddExistingItems(uint32_t aType, |
246 | | nsTArray<nsCString>* namespaceFilter = nullptr); |
247 | | nsresult ScheduleImplicit(); |
248 | | void AssociateDocuments(nsIApplicationCache* cache); |
249 | | bool CheckUpdateAvailability(); |
250 | | void NotifyUpdateAvailability(bool updateAvailable); |
251 | | |
252 | | void GatherObservers(nsCOMArray<nsIOfflineCacheUpdateObserver> &aObservers); |
253 | | void NotifyState(uint32_t state); |
254 | | nsresult Finish(); |
255 | | nsresult FinishNoNotify(); |
256 | | |
257 | | void AsyncFinishWithError(); |
258 | | |
259 | | // Find one non-pinned cache group and evict it. |
260 | | nsresult EvictOneNonPinned(); |
261 | | |
262 | | enum { |
263 | | STATE_UNINITIALIZED, |
264 | | STATE_INITIALIZED, |
265 | | STATE_CHECKING, |
266 | | STATE_DOWNLOADING, |
267 | | STATE_CANCELLED, |
268 | | STATE_FINISHED |
269 | | } mState; |
270 | | |
271 | | mozilla::WeakPtr<nsOfflineCacheUpdateOwner> mOwner; |
272 | | |
273 | | bool mAddedItems; |
274 | | bool mPartialUpdate; |
275 | | bool mOnlyCheckUpdate; |
276 | | bool mSucceeded; |
277 | | bool mObsolete; |
278 | | |
279 | | nsCString mUpdateDomain; |
280 | | nsCString mGroupID; |
281 | | nsCOMPtr<nsIURI> mManifestURI; |
282 | | nsCOMPtr<nsIURI> mDocumentURI; |
283 | | nsCOMPtr<nsIPrincipal> mLoadingPrincipal; |
284 | | nsCOMPtr<nsIFile> mCustomProfileDir; |
285 | | |
286 | | nsCOMPtr<nsIObserver> mUpdateAvailableObserver; |
287 | | |
288 | | nsCOMPtr<nsIApplicationCache> mApplicationCache; |
289 | | nsCOMPtr<nsIApplicationCache> mPreviousApplicationCache; |
290 | | |
291 | | nsCOMPtr<nsIObserverService> mObserverService; |
292 | | |
293 | | RefPtr<nsOfflineManifestItem> mManifestItem; |
294 | | |
295 | | /* Items being updated */ |
296 | | uint32_t mItemsInProgress; |
297 | | nsTArray<RefPtr<nsOfflineCacheUpdateItem> > mItems; |
298 | | |
299 | | /* Clients watching this update for changes */ |
300 | | nsCOMArray<nsIWeakReference> mWeakObservers; |
301 | | nsCOMArray<nsIOfflineCacheUpdateObserver> mObservers; |
302 | | |
303 | | /* Documents that requested this update */ |
304 | | nsCOMArray<nsIURI> mDocumentURIs; |
305 | | |
306 | | /* Reschedule count. When an update is rescheduled due to |
307 | | * mismatched manifests, the reschedule count will be increased. */ |
308 | | uint32_t mRescheduleCount; |
309 | | |
310 | | /* Whena an entry for a pinned app is retried, retries count is |
311 | | * increaded. */ |
312 | | uint32_t mPinnedEntryRetriesCount; |
313 | | |
314 | | RefPtr<nsOfflineCacheUpdate> mImplicitUpdate; |
315 | | |
316 | | bool mPinned; |
317 | | |
318 | | uint64_t mByteProgress; |
319 | | }; |
320 | | |
321 | | class nsOfflineCacheUpdateService final : public nsIOfflineCacheUpdateService |
322 | | , public nsIObserver |
323 | | , public nsOfflineCacheUpdateOwner |
324 | | , public nsSupportsWeakReference |
325 | | { |
326 | | public: |
327 | | NS_DECL_ISUPPORTS |
328 | | NS_DECL_NSIOFFLINECACHEUPDATESERVICE |
329 | | NS_DECL_NSIOBSERVER |
330 | | |
331 | | nsOfflineCacheUpdateService(); |
332 | | |
333 | | nsresult Init(); |
334 | | |
335 | | nsresult ScheduleUpdate(nsOfflineCacheUpdate *aUpdate); |
336 | | nsresult FindUpdate(nsIURI *aManifestURI, |
337 | | nsACString const &aOriginSuffix, |
338 | | nsIFile *aCustomProfileDir, |
339 | | nsOfflineCacheUpdate **aUpdate); |
340 | | |
341 | | nsresult Schedule(nsIURI *aManifestURI, |
342 | | nsIURI *aDocumentURI, |
343 | | nsIPrincipal* aLoadingPrincipal, |
344 | | nsIDocument *aDocument, |
345 | | nsPIDOMWindowInner* aWindow, |
346 | | nsIFile* aCustomProfileDir, |
347 | | nsIOfflineCacheUpdate **aUpdate); |
348 | | |
349 | | virtual nsresult UpdateFinished(nsOfflineCacheUpdate *aUpdate) override; |
350 | | |
351 | | /** |
352 | | * Returns the singleton nsOfflineCacheUpdateService without an addref, or |
353 | | * nullptr if the service couldn't be created. |
354 | | */ |
355 | | static nsOfflineCacheUpdateService *EnsureService(); |
356 | | |
357 | | static already_AddRefed<nsOfflineCacheUpdateService> GetInstance(); |
358 | | |
359 | | static nsresult OfflineAppPinnedForURI(nsIURI *aDocumentURI, |
360 | | nsIPrefBranch *aPrefBranch, |
361 | | bool *aPinned); |
362 | | |
363 | | static nsTHashtable<nsCStringHashKey>* AllowedDomains(); |
364 | | |
365 | | private: |
366 | | ~nsOfflineCacheUpdateService(); |
367 | | |
368 | | nsresult ProcessNextUpdate(); |
369 | | |
370 | | nsTArray<RefPtr<nsOfflineCacheUpdate> > mUpdates; |
371 | | static nsTHashtable<nsCStringHashKey>* mAllowedDomains; |
372 | | |
373 | | bool mDisabled; |
374 | | bool mUpdateRunning; |
375 | | }; |
376 | | |
377 | | #endif |