1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2007
20 * the Initial Developer. All Rights Reserved.
23 * Dave Camp <dcamp@mozilla.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef nsOfflineCacheUpdate_h__
40 #define nsOfflineCacheUpdate_h__
42 #include "nsIOfflineCacheUpdate.h"
44 #include "nsAutoPtr.h"
45 #include "nsCOMArray.h"
47 #include "nsICacheService.h"
48 #include "nsIChannelEventSink.h"
49 #include "nsIDOMDocument.h"
50 #include "nsIDOMNode.h"
51 #include "nsIDOMLoadStatus.h"
52 #include "nsIInterfaceRequestor.h"
53 #include "nsIMutableArray.h"
54 #include "nsIObserver.h"
55 #include "nsIObserverService.h"
56 #include "nsIApplicationCache.h"
57 #include "nsIRequestObserver.h"
58 #include "nsIRunnable.h"
59 #include "nsIStreamListener.h"
61 #include "nsIWebProgressListener.h"
62 #include "nsClassHashtable.h"
65 #include "nsWeakReference.h"
66 #include "nsICryptoHash.h"
68 class nsOfflineCacheUpdate
;
70 class nsICacheEntryDescriptor
;
71 class nsIUTF8StringEnumerator
;
73 class nsOfflineCacheUpdateItem
: public nsIDOMLoadStatus
74 , public nsIStreamListener
76 , public nsIInterfaceRequestor
77 , public nsIChannelEventSink
81 NS_DECL_NSIDOMLOADSTATUS
82 NS_DECL_NSIREQUESTOBSERVER
83 NS_DECL_NSISTREAMLISTENER
85 NS_DECL_NSIINTERFACEREQUESTOR
86 NS_DECL_NSICHANNELEVENTSINK
88 nsOfflineCacheUpdateItem(nsOfflineCacheUpdate
*aUpdate
,
91 nsIApplicationCache
*aPreviousApplicationCache
,
92 const nsACString
&aClientID
,
94 virtual ~nsOfflineCacheUpdateItem();
96 nsCOMPtr
<nsIURI
> mURI
;
97 nsCOMPtr
<nsIURI
> mReferrerURI
;
98 nsCOMPtr
<nsIApplicationCache
> mPreviousApplicationCache
;
103 nsresult
OpenChannel();
107 nsOfflineCacheUpdate
* mUpdate
;
108 nsCOMPtr
<nsIChannel
> mChannel
;
116 class nsOfflineManifestItem
: public nsOfflineCacheUpdateItem
119 NS_DECL_NSISTREAMLISTENER
120 NS_DECL_NSIREQUESTOBSERVER
122 nsOfflineManifestItem(nsOfflineCacheUpdate
*aUpdate
,
124 nsIURI
*aReferrerURI
,
125 nsIApplicationCache
*aPreviousApplicationCache
,
126 const nsACString
&aClientID
);
127 virtual ~nsOfflineManifestItem();
129 nsCOMArray
<nsIURI
> &GetExplicitURIs() { return mExplicitURIs
; }
130 nsCOMArray
<nsIURI
> &GetFallbackURIs() { return mFallbackURIs
; }
132 nsTArray
<nsCString
> &GetOpportunisticNamespaces()
133 { return mOpportunisticNamespaces
; }
134 nsIArray
*GetNamespaces()
135 { return mNamespaces
.get(); }
137 PRBool
ParseSucceeded()
138 { return (mParserState
!= PARSE_INIT
&& mParserState
!= PARSE_ERROR
); }
139 PRBool
NeedsUpdate() { return mParserState
!= PARSE_INIT
&& mNeedsUpdate
; }
141 void GetManifestHash(nsCString
&aManifestHash
)
142 { aManifestHash
= mManifestHashValue
; }
145 static NS_METHOD
ReadManifest(nsIInputStream
*aInputStream
,
147 const char *aFromSegment
,
150 PRUint32
*aBytesConsumed
);
152 nsresult
AddNamespace(PRUint32 namespaceType
,
153 const nsCString
&namespaceSpec
,
154 const nsCString
&data
);
156 nsresult
HandleManifestLine(const nsCString::const_iterator
&aBegin
,
157 const nsCString::const_iterator
&aEnd
);
160 * Saves "offline-manifest-hash" meta data from the old offline cache
161 * token to mOldManifestHashValue member to be compared on
164 nsresult
GetOldManifestContentHash(nsIRequest
*aRequest
);
166 * This method setups the mNeedsUpdate to PR_FALSE when hash value
167 * of the just downloaded manifest file is the same as stored in cache's
168 * "offline-manifest-hash" meta data. Otherwise stores the new value
171 nsresult
CheckNewManifestContentHash(nsIRequest
*aRequest
);
173 void ReadStrictFileOriginPolicyPref();
178 PARSE_FALLBACK_ENTRIES
,
179 PARSE_BYPASS_ENTRIES
,
185 nsCOMArray
<nsIURI
> mExplicitURIs
;
186 nsCOMArray
<nsIURI
> mFallbackURIs
;
188 // All opportunistic caching namespaces. Used to decide whether
189 // to include previously-opportunistically-cached entries.
190 nsTArray
<nsCString
> mOpportunisticNamespaces
;
192 // Array of nsIApplicationCacheNamespace objects specified by the
194 nsCOMPtr
<nsIMutableArray
> mNamespaces
;
197 PRBool mStrictFileOriginPolicy
;
199 // manifest hash data
200 nsCOMPtr
<nsICryptoHash
> mManifestHash
;
201 PRBool mManifestHashInitialized
;
202 nsCString mManifestHashValue
;
203 nsCString mOldManifestHashValue
;
206 class nsOfflineCacheUpdate
: public nsIOfflineCacheUpdate
210 NS_DECL_NSIOFFLINECACHEUPDATE
212 nsOfflineCacheUpdate();
213 ~nsOfflineCacheUpdate();
215 static nsresult
GetCacheKey(nsIURI
*aURI
, nsACString
&aKey
);
222 void LoadCompleted();
223 void ManifestCheckCompleted(nsresult aStatus
,
224 const nsCString
&aManifestHash
);
225 void AddDocument(nsIDOMDocument
*aDocument
);
228 nsresult
HandleManifest(PRBool
*aDoUpdate
);
229 nsresult
AddURI(nsIURI
*aURI
, PRUint32 aItemType
);
231 nsresult
ProcessNextURI();
233 // Adds items from the previous cache witha type matching aType.
234 // If namespaceFilter is non-null, only items matching the
235 // specified namespaces will be added.
236 nsresult
AddExistingItems(PRUint32 aType
,
237 nsTArray
<nsCString
>* namespaceFilter
= nsnull
);
239 nsresult
GatherObservers(nsCOMArray
<nsIOfflineCacheUpdateObserver
> &aObservers
);
240 nsresult
NotifyError();
241 nsresult
NotifyChecking();
242 nsresult
NotifyNoUpdate();
243 nsresult
NotifyObsolete();
244 nsresult
NotifyDownloading();
245 nsresult
NotifyStarted(nsOfflineCacheUpdateItem
*aItem
);
246 nsresult
NotifyCompleted(nsOfflineCacheUpdateItem
*aItem
);
247 nsresult
AssociateDocument(nsIDOMDocument
*aDocument
);
248 nsresult
ScheduleImplicit();
260 PRPackedBool mAddedItems
;
261 PRPackedBool mPartialUpdate
;
262 PRPackedBool mSucceeded
;
263 PRPackedBool mObsolete
;
265 nsCString mUpdateDomain
;
266 nsCOMPtr
<nsIURI
> mManifestURI
;
268 nsCOMPtr
<nsIURI
> mDocumentURI
;
271 nsCOMPtr
<nsIApplicationCache
> mApplicationCache
;
272 nsCOMPtr
<nsIApplicationCache
> mPreviousApplicationCache
;
274 nsCOMPtr
<nsIObserverService
> mObserverService
;
276 nsRefPtr
<nsOfflineManifestItem
> mManifestItem
;
278 /* Items being updated */
279 PRInt32 mCurrentItem
;
280 nsTArray
<nsRefPtr
<nsOfflineCacheUpdateItem
> > mItems
;
282 /* Clients watching this update for changes */
283 nsCOMArray
<nsIWeakReference
> mWeakObservers
;
284 nsCOMArray
<nsIOfflineCacheUpdateObserver
> mObservers
;
286 /* Documents that requested this update */
287 nsCOMArray
<nsIDOMDocument
> mDocuments
;
289 /* Reschedule count. When an update is rescheduled due to
290 * mismatched manifests, the reschedule count will be increased. */
291 PRUint32 mRescheduleCount
;
294 class nsOfflineCacheUpdateService
: public nsIOfflineCacheUpdateService
295 , public nsIWebProgressListener
297 , public nsSupportsWeakReference
301 NS_DECL_NSIOFFLINECACHEUPDATESERVICE
302 NS_DECL_NSIWEBPROGRESSLISTENER
305 nsOfflineCacheUpdateService();
306 ~nsOfflineCacheUpdateService();
310 nsresult
Schedule(nsOfflineCacheUpdate
*aUpdate
);
311 nsresult
Schedule(nsIURI
*aManifestURI
,
312 nsIURI
*aDocumentURI
,
313 nsIDOMDocument
*aDocument
,
314 nsIOfflineCacheUpdate
**aUpdate
);
316 nsresult
UpdateFinished(nsOfflineCacheUpdate
*aUpdate
);
319 * Returns the singleton nsOfflineCacheUpdateService without an addref, or
320 * nsnull if the service couldn't be created.
322 static nsOfflineCacheUpdateService
*EnsureService();
324 /** Addrefs and returns the singleton nsOfflineCacheUpdateService. */
325 static nsOfflineCacheUpdateService
*GetInstance();
328 nsresult
ProcessNextUpdate();
330 nsTArray
<nsRefPtr
<nsOfflineCacheUpdate
> > mUpdates
;
332 struct PendingUpdate
{
333 nsCOMPtr
<nsIURI
> mManifestURI
;
334 nsCOMPtr
<nsIURI
> mDocumentURI
;
336 nsClassHashtable
<nsVoidPtrHashKey
, PendingUpdate
> mDocUpdates
;
339 PRBool mUpdateRunning
;