2007-07-24 22:35:39 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Mozilla Corporation
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Dave Camp <dcamp@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#ifndef nsOfflineCacheUpdate_h__
|
|
|
|
#define nsOfflineCacheUpdate_h__
|
|
|
|
|
|
|
|
#include "nsIOfflineCacheUpdate.h"
|
|
|
|
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsICacheService.h"
|
|
|
|
#include "nsIChannelEventSink.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIDOMLoadStatus.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
2008-09-30 11:34:23 -07:00
|
|
|
#include "nsIMutableArray.h"
|
2007-07-24 22:35:39 -07:00
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIObserverService.h"
|
2008-08-26 16:09:02 -07:00
|
|
|
#include "nsIApplicationCache.h"
|
2007-07-24 22:35:39 -07:00
|
|
|
#include "nsIRequestObserver.h"
|
2007-07-24 23:31:27 -07:00
|
|
|
#include "nsIRunnable.h"
|
2007-07-24 22:35:39 -07:00
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIWebProgressListener.h"
|
2008-01-16 13:54:33 -08:00
|
|
|
#include "nsClassHashtable.h"
|
2007-07-24 22:35:39 -07:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsWeakReference.h"
|
2008-01-28 23:54:54 -08:00
|
|
|
#include "nsICryptoHash.h"
|
2007-07-24 22:35:39 -07:00
|
|
|
|
|
|
|
class nsOfflineCacheUpdate;
|
|
|
|
|
2008-01-28 23:54:54 -08:00
|
|
|
class nsICacheEntryDescriptor;
|
2008-09-30 11:34:23 -07:00
|
|
|
class nsIUTF8StringEnumerator;
|
2008-01-28 23:54:54 -08:00
|
|
|
|
2007-07-24 22:35:39 -07:00
|
|
|
class nsOfflineCacheUpdateItem : public nsIDOMLoadStatus
|
|
|
|
, public nsIStreamListener
|
2007-07-24 23:31:27 -07:00
|
|
|
, public nsIRunnable
|
2007-07-24 22:35:39 -07:00
|
|
|
, public nsIInterfaceRequestor
|
|
|
|
, public nsIChannelEventSink
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIDOMLOADSTATUS
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
2007-07-24 23:31:27 -07:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2007-07-24 22:35:39 -07:00
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
NS_DECL_NSICHANNELEVENTSINK
|
|
|
|
|
|
|
|
nsOfflineCacheUpdateItem(nsOfflineCacheUpdate *aUpdate,
|
|
|
|
nsIURI *aURI,
|
|
|
|
nsIURI *aReferrerURI,
|
2008-08-26 16:09:02 -07:00
|
|
|
nsIApplicationCache *aPreviousApplicationCache,
|
|
|
|
const nsACString &aClientID,
|
|
|
|
PRUint32 aType);
|
2008-01-16 13:54:33 -08:00
|
|
|
virtual ~nsOfflineCacheUpdateItem();
|
2007-07-24 22:35:39 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> mURI;
|
|
|
|
nsCOMPtr<nsIURI> mReferrerURI;
|
2008-08-26 16:09:02 -07:00
|
|
|
nsCOMPtr<nsIApplicationCache> mPreviousApplicationCache;
|
2007-07-24 23:31:27 -07:00
|
|
|
nsCString mClientID;
|
2008-08-26 16:09:02 -07:00
|
|
|
nsCString mCacheKey;
|
|
|
|
PRUint32 mItemType;
|
2007-07-24 22:35:39 -07:00
|
|
|
|
|
|
|
nsresult OpenChannel();
|
|
|
|
nsresult Cancel();
|
2009-02-27 06:09:06 -08:00
|
|
|
nsresult GetRequestSucceeded(PRBool * succeeded);
|
2007-07-24 22:35:39 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsOfflineCacheUpdate* mUpdate;
|
|
|
|
nsCOMPtr<nsIChannel> mChannel;
|
|
|
|
PRUint16 mState;
|
2008-01-16 13:54:33 -08:00
|
|
|
|
|
|
|
protected:
|
2007-07-24 22:35:39 -07:00
|
|
|
PRInt32 mBytesRead;
|
|
|
|
};
|
|
|
|
|
2008-01-16 13:54:33 -08:00
|
|
|
|
|
|
|
class nsOfflineManifestItem : public nsOfflineCacheUpdateItem
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
|
|
|
|
nsOfflineManifestItem(nsOfflineCacheUpdate *aUpdate,
|
|
|
|
nsIURI *aURI,
|
|
|
|
nsIURI *aReferrerURI,
|
2008-08-26 16:09:02 -07:00
|
|
|
nsIApplicationCache *aPreviousApplicationCache,
|
2008-01-16 13:54:33 -08:00
|
|
|
const nsACString &aClientID);
|
|
|
|
virtual ~nsOfflineManifestItem();
|
|
|
|
|
|
|
|
nsCOMArray<nsIURI> &GetExplicitURIs() { return mExplicitURIs; }
|
2008-09-30 11:34:23 -07:00
|
|
|
nsCOMArray<nsIURI> &GetFallbackURIs() { return mFallbackURIs; }
|
|
|
|
|
|
|
|
nsTArray<nsCString> &GetOpportunisticNamespaces()
|
|
|
|
{ return mOpportunisticNamespaces; }
|
|
|
|
nsIArray *GetNamespaces()
|
|
|
|
{ return mNamespaces.get(); }
|
2008-01-16 13:54:33 -08:00
|
|
|
|
|
|
|
PRBool ParseSucceeded()
|
|
|
|
{ return (mParserState != PARSE_INIT && mParserState != PARSE_ERROR); }
|
|
|
|
PRBool NeedsUpdate() { return mParserState != PARSE_INIT && mNeedsUpdate; }
|
|
|
|
|
2008-11-05 16:01:07 -08:00
|
|
|
void GetManifestHash(nsCString &aManifestHash)
|
|
|
|
{ aManifestHash = mManifestHashValue; }
|
|
|
|
|
2008-01-16 13:54:33 -08:00
|
|
|
private:
|
|
|
|
static NS_METHOD ReadManifest(nsIInputStream *aInputStream,
|
|
|
|
void *aClosure,
|
|
|
|
const char *aFromSegment,
|
|
|
|
PRUint32 aOffset,
|
|
|
|
PRUint32 aCount,
|
|
|
|
PRUint32 *aBytesConsumed);
|
|
|
|
|
2008-09-30 11:34:23 -07:00
|
|
|
nsresult AddNamespace(PRUint32 namespaceType,
|
|
|
|
const nsCString &namespaceSpec,
|
|
|
|
const nsCString &data);
|
|
|
|
|
2008-01-16 13:54:33 -08:00
|
|
|
nsresult HandleManifestLine(const nsCString::const_iterator &aBegin,
|
|
|
|
const nsCString::const_iterator &aEnd);
|
|
|
|
|
2008-01-28 23:54:54 -08:00
|
|
|
/**
|
|
|
|
* Saves "offline-manifest-hash" meta data from the old offline cache
|
|
|
|
* token to mOldManifestHashValue member to be compared on
|
|
|
|
* successfull load.
|
|
|
|
*/
|
|
|
|
nsresult GetOldManifestContentHash(nsIRequest *aRequest);
|
|
|
|
/**
|
|
|
|
* This method setups the mNeedsUpdate to PR_FALSE when hash value
|
|
|
|
* of the just downloaded manifest file is the same as stored in cache's
|
|
|
|
* "offline-manifest-hash" meta data. Otherwise stores the new value
|
|
|
|
* to this meta data.
|
|
|
|
*/
|
|
|
|
nsresult CheckNewManifestContentHash(nsIRequest *aRequest);
|
|
|
|
|
2008-09-30 11:34:23 -07:00
|
|
|
void ReadStrictFileOriginPolicyPref();
|
|
|
|
|
2008-01-16 13:54:33 -08:00
|
|
|
enum {
|
|
|
|
PARSE_INIT,
|
|
|
|
PARSE_CACHE_ENTRIES,
|
|
|
|
PARSE_FALLBACK_ENTRIES,
|
2008-09-30 11:34:23 -07:00
|
|
|
PARSE_BYPASS_ENTRIES,
|
2008-01-16 13:54:33 -08:00
|
|
|
PARSE_ERROR
|
|
|
|
} mParserState;
|
|
|
|
|
|
|
|
nsCString mReadBuf;
|
2008-09-30 11:34:23 -07:00
|
|
|
|
2008-01-16 13:54:33 -08:00
|
|
|
nsCOMArray<nsIURI> mExplicitURIs;
|
2008-09-30 11:34:23 -07:00
|
|
|
nsCOMArray<nsIURI> mFallbackURIs;
|
|
|
|
|
|
|
|
// All opportunistic caching namespaces. Used to decide whether
|
|
|
|
// to include previously-opportunistically-cached entries.
|
|
|
|
nsTArray<nsCString> mOpportunisticNamespaces;
|
|
|
|
|
|
|
|
// Array of nsIApplicationCacheNamespace objects specified by the
|
|
|
|
// manifest.
|
|
|
|
nsCOMPtr<nsIMutableArray> mNamespaces;
|
|
|
|
|
2008-01-16 13:54:33 -08:00
|
|
|
PRBool mNeedsUpdate;
|
2008-09-30 11:34:23 -07:00
|
|
|
PRBool mStrictFileOriginPolicy;
|
2008-01-28 23:54:54 -08:00
|
|
|
|
|
|
|
// manifest hash data
|
|
|
|
nsCOMPtr<nsICryptoHash> mManifestHash;
|
|
|
|
PRBool mManifestHashInitialized;
|
2008-11-05 16:01:07 -08:00
|
|
|
nsCString mManifestHashValue;
|
2008-01-28 23:54:54 -08:00
|
|
|
nsCString mOldManifestHashValue;
|
2008-01-16 13:54:33 -08:00
|
|
|
};
|
|
|
|
|
2009-02-22 11:09:10 -08:00
|
|
|
class nsOfflineCacheUpdateOwner
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual nsresult UpdateFinished(nsOfflineCacheUpdate *aUpdate) = 0;
|
|
|
|
};
|
|
|
|
|
2007-07-24 22:35:39 -07:00
|
|
|
class nsOfflineCacheUpdate : public nsIOfflineCacheUpdate
|
2009-02-22 11:09:10 -08:00
|
|
|
, public nsOfflineCacheUpdateOwner
|
2007-07-24 22:35:39 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOFFLINECACHEUPDATE
|
|
|
|
|
|
|
|
nsOfflineCacheUpdate();
|
|
|
|
~nsOfflineCacheUpdate();
|
|
|
|
|
2008-08-26 16:09:02 -07:00
|
|
|
static nsresult GetCacheKey(nsIURI *aURI, nsACString &aKey);
|
|
|
|
|
2007-07-24 22:35:39 -07:00
|
|
|
nsresult Init();
|
|
|
|
|
|
|
|
nsresult Begin();
|
|
|
|
nsresult Cancel();
|
|
|
|
|
|
|
|
void LoadCompleted();
|
2008-11-05 16:01:07 -08:00
|
|
|
void ManifestCheckCompleted(nsresult aStatus,
|
|
|
|
const nsCString &aManifestHash);
|
2008-11-05 16:01:08 -08:00
|
|
|
void AddDocument(nsIDOMDocument *aDocument);
|
2008-11-04 02:20:27 -08:00
|
|
|
|
2009-02-22 11:09:10 -08:00
|
|
|
void SetOwner(nsOfflineCacheUpdateOwner *aOwner);
|
|
|
|
|
|
|
|
virtual nsresult UpdateFinished(nsOfflineCacheUpdate *aUpdate);
|
|
|
|
|
2007-07-24 22:35:39 -07:00
|
|
|
private:
|
2008-01-16 13:54:33 -08:00
|
|
|
nsresult HandleManifest(PRBool *aDoUpdate);
|
2008-08-26 16:09:02 -07:00
|
|
|
nsresult AddURI(nsIURI *aURI, PRUint32 aItemType);
|
2008-01-16 13:54:33 -08:00
|
|
|
|
2007-07-24 22:35:39 -07:00
|
|
|
nsresult ProcessNextURI();
|
2008-01-16 13:54:33 -08:00
|
|
|
|
2008-09-30 11:34:23 -07:00
|
|
|
// Adds items from the previous cache witha type matching aType.
|
|
|
|
// If namespaceFilter is non-null, only items matching the
|
|
|
|
// specified namespaces will be added.
|
|
|
|
nsresult AddExistingItems(PRUint32 aType,
|
|
|
|
nsTArray<nsCString>* namespaceFilter = nsnull);
|
2008-01-16 13:54:33 -08:00
|
|
|
|
|
|
|
nsresult GatherObservers(nsCOMArray<nsIOfflineCacheUpdateObserver> &aObservers);
|
|
|
|
nsresult NotifyError();
|
|
|
|
nsresult NotifyChecking();
|
|
|
|
nsresult NotifyNoUpdate();
|
2008-11-04 10:59:12 -08:00
|
|
|
nsresult NotifyObsolete();
|
2008-01-16 13:54:33 -08:00
|
|
|
nsresult NotifyDownloading();
|
|
|
|
nsresult NotifyStarted(nsOfflineCacheUpdateItem *aItem);
|
2007-07-24 22:35:39 -07:00
|
|
|
nsresult NotifyCompleted(nsOfflineCacheUpdateItem *aItem);
|
2009-03-10 14:58:00 -07:00
|
|
|
nsresult AssociateDocument(nsIDOMDocument *aDocument,
|
|
|
|
nsIApplicationCache *aApplicationCache);
|
2008-11-05 16:01:08 -08:00
|
|
|
nsresult ScheduleImplicit();
|
2007-07-24 22:35:39 -07:00
|
|
|
nsresult Finish();
|
|
|
|
|
|
|
|
enum {
|
|
|
|
STATE_UNINITIALIZED,
|
|
|
|
STATE_INITIALIZED,
|
2008-01-16 13:54:33 -08:00
|
|
|
STATE_CHECKING,
|
|
|
|
STATE_DOWNLOADING,
|
2007-07-24 22:35:39 -07:00
|
|
|
STATE_CANCELLED,
|
|
|
|
STATE_FINISHED
|
|
|
|
} mState;
|
|
|
|
|
2009-02-22 11:09:10 -08:00
|
|
|
nsOfflineCacheUpdateOwner *mOwner;
|
|
|
|
|
2008-11-04 10:59:12 -08:00
|
|
|
PRPackedBool mAddedItems;
|
|
|
|
PRPackedBool mPartialUpdate;
|
|
|
|
PRPackedBool mSucceeded;
|
|
|
|
PRPackedBool mObsolete;
|
|
|
|
|
2007-07-24 22:35:39 -07:00
|
|
|
nsCString mUpdateDomain;
|
2008-01-16 13:54:33 -08:00
|
|
|
nsCOMPtr<nsIURI> mManifestURI;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> mDocumentURI;
|
2007-07-24 22:35:39 -07:00
|
|
|
|
2007-07-24 23:31:27 -07:00
|
|
|
nsCString mClientID;
|
2008-08-26 16:09:02 -07:00
|
|
|
nsCOMPtr<nsIApplicationCache> mApplicationCache;
|
|
|
|
nsCOMPtr<nsIApplicationCache> mPreviousApplicationCache;
|
2007-07-24 23:31:27 -07:00
|
|
|
|
2007-07-24 22:35:39 -07:00
|
|
|
nsCOMPtr<nsIObserverService> mObserverService;
|
|
|
|
|
2008-01-16 13:54:33 -08:00
|
|
|
nsRefPtr<nsOfflineManifestItem> mManifestItem;
|
|
|
|
|
2007-07-24 22:35:39 -07:00
|
|
|
/* Items being updated */
|
2007-07-24 23:31:27 -07:00
|
|
|
PRInt32 mCurrentItem;
|
2007-07-24 22:35:39 -07:00
|
|
|
nsTArray<nsRefPtr<nsOfflineCacheUpdateItem> > mItems;
|
|
|
|
|
|
|
|
/* Clients watching this update for changes */
|
|
|
|
nsCOMArray<nsIWeakReference> mWeakObservers;
|
|
|
|
nsCOMArray<nsIOfflineCacheUpdateObserver> mObservers;
|
2008-11-04 02:20:27 -08:00
|
|
|
|
|
|
|
/* Documents that requested this update */
|
|
|
|
nsCOMArray<nsIDOMDocument> mDocuments;
|
2008-11-05 16:01:07 -08:00
|
|
|
|
|
|
|
/* Reschedule count. When an update is rescheduled due to
|
|
|
|
* mismatched manifests, the reschedule count will be increased. */
|
|
|
|
PRUint32 mRescheduleCount;
|
2009-02-22 11:09:10 -08:00
|
|
|
|
|
|
|
nsRefPtr<nsOfflineCacheUpdate> mImplicitUpdate;
|
2007-07-24 22:35:39 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class nsOfflineCacheUpdateService : public nsIOfflineCacheUpdateService
|
|
|
|
, public nsIObserver
|
2009-02-22 11:09:10 -08:00
|
|
|
, public nsOfflineCacheUpdateOwner
|
2009-03-04 13:37:29 -08:00
|
|
|
, public nsSupportsWeakReference
|
2007-07-24 22:35:39 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOFFLINECACHEUPDATESERVICE
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
nsOfflineCacheUpdateService();
|
|
|
|
~nsOfflineCacheUpdateService();
|
|
|
|
|
|
|
|
nsresult Init();
|
|
|
|
|
|
|
|
nsresult Schedule(nsOfflineCacheUpdate *aUpdate);
|
2008-11-04 02:20:27 -08:00
|
|
|
nsresult Schedule(nsIURI *aManifestURI,
|
|
|
|
nsIURI *aDocumentURI,
|
|
|
|
nsIDOMDocument *aDocument,
|
|
|
|
nsIOfflineCacheUpdate **aUpdate);
|
|
|
|
|
2009-02-22 11:09:10 -08:00
|
|
|
virtual nsresult UpdateFinished(nsOfflineCacheUpdate *aUpdate);
|
2007-07-24 22:35:39 -07:00
|
|
|
|
2007-11-08 21:12:59 -08:00
|
|
|
/**
|
|
|
|
* Returns the singleton nsOfflineCacheUpdateService without an addref, or
|
|
|
|
* nsnull if the service couldn't be created.
|
|
|
|
*/
|
|
|
|
static nsOfflineCacheUpdateService *EnsureService();
|
2007-07-24 22:35:39 -07:00
|
|
|
|
2007-11-08 21:12:59 -08:00
|
|
|
/** Addrefs and returns the singleton nsOfflineCacheUpdateService. */
|
|
|
|
static nsOfflineCacheUpdateService *GetInstance();
|
2008-11-04 02:20:27 -08:00
|
|
|
|
2007-07-24 22:35:39 -07:00
|
|
|
private:
|
|
|
|
nsresult ProcessNextUpdate();
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<nsOfflineCacheUpdate> > mUpdates;
|
2008-01-16 13:54:33 -08:00
|
|
|
|
2007-07-24 22:35:39 -07:00
|
|
|
PRBool mDisabled;
|
|
|
|
PRBool mUpdateRunning;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|