2007-07-25 09:35:39 +04: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 22:34:23 +04:00
|
|
|
#include "nsIMutableArray.h"
|
2007-07-25 09:35:39 +04:00
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIObserverService.h"
|
2008-08-27 03:09:02 +04:00
|
|
|
#include "nsIApplicationCache.h"
|
2007-07-25 09:35:39 +04:00
|
|
|
#include "nsIRequestObserver.h"
|
2007-07-25 10:31:27 +04:00
|
|
|
#include "nsIRunnable.h"
|
2007-07-25 09:35:39 +04:00
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIWebProgressListener.h"
|
2008-01-17 00:54:33 +03:00
|
|
|
#include "nsClassHashtable.h"
|
2007-07-25 09:35:39 +04:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsWeakReference.h"
|
2008-01-29 10:54:54 +03:00
|
|
|
#include "nsICryptoHash.h"
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
class nsOfflineCacheUpdate;
|
|
|
|
|
2008-01-29 10:54:54 +03:00
|
|
|
class nsICacheEntryDescriptor;
|
2008-09-30 22:34:23 +04:00
|
|
|
class nsIUTF8StringEnumerator;
|
2008-01-29 10:54:54 +03:00
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
class nsOfflineCacheUpdateItem : public nsIDOMLoadStatus
|
|
|
|
, public nsIStreamListener
|
2007-07-25 10:31:27 +04:00
|
|
|
, public nsIRunnable
|
2007-07-25 09:35:39 +04:00
|
|
|
, public nsIInterfaceRequestor
|
|
|
|
, public nsIChannelEventSink
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIDOMLOADSTATUS
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
2007-07-25 10:31:27 +04:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2007-07-25 09:35:39 +04:00
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
NS_DECL_NSICHANNELEVENTSINK
|
|
|
|
|
|
|
|
nsOfflineCacheUpdateItem(nsOfflineCacheUpdate *aUpdate,
|
|
|
|
nsIURI *aURI,
|
|
|
|
nsIURI *aReferrerURI,
|
2008-08-27 03:09:02 +04:00
|
|
|
nsIApplicationCache *aPreviousApplicationCache,
|
|
|
|
const nsACString &aClientID,
|
|
|
|
PRUint32 aType);
|
2008-01-17 00:54:33 +03:00
|
|
|
virtual ~nsOfflineCacheUpdateItem();
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> mURI;
|
|
|
|
nsCOMPtr<nsIURI> mReferrerURI;
|
2008-08-27 03:09:02 +04:00
|
|
|
nsCOMPtr<nsIApplicationCache> mPreviousApplicationCache;
|
2007-07-25 10:31:27 +04:00
|
|
|
nsCString mClientID;
|
2008-08-27 03:09:02 +04:00
|
|
|
nsCString mCacheKey;
|
|
|
|
PRUint32 mItemType;
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
nsresult OpenChannel();
|
|
|
|
nsresult Cancel();
|
2009-02-27 17:09:06 +03:00
|
|
|
nsresult GetRequestSucceeded(PRBool * succeeded);
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsOfflineCacheUpdate* mUpdate;
|
|
|
|
nsCOMPtr<nsIChannel> mChannel;
|
|
|
|
PRUint16 mState;
|
2008-01-17 00:54:33 +03:00
|
|
|
|
|
|
|
protected:
|
2010-08-31 00:20:39 +04:00
|
|
|
PRInt32 mBytesRead;
|
2007-07-25 09:35:39 +04:00
|
|
|
};
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
|
|
|
|
class nsOfflineManifestItem : public nsOfflineCacheUpdateItem
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
|
|
|
|
nsOfflineManifestItem(nsOfflineCacheUpdate *aUpdate,
|
|
|
|
nsIURI *aURI,
|
|
|
|
nsIURI *aReferrerURI,
|
2008-08-27 03:09:02 +04:00
|
|
|
nsIApplicationCache *aPreviousApplicationCache,
|
2008-01-17 00:54:33 +03:00
|
|
|
const nsACString &aClientID);
|
|
|
|
virtual ~nsOfflineManifestItem();
|
|
|
|
|
|
|
|
nsCOMArray<nsIURI> &GetExplicitURIs() { return mExplicitURIs; }
|
2008-09-30 22:34:23 +04:00
|
|
|
nsCOMArray<nsIURI> &GetFallbackURIs() { return mFallbackURIs; }
|
|
|
|
|
|
|
|
nsTArray<nsCString> &GetOpportunisticNamespaces()
|
|
|
|
{ return mOpportunisticNamespaces; }
|
|
|
|
nsIArray *GetNamespaces()
|
|
|
|
{ return mNamespaces.get(); }
|
2008-01-17 00:54:33 +03:00
|
|
|
|
|
|
|
PRBool ParseSucceeded()
|
|
|
|
{ return (mParserState != PARSE_INIT && mParserState != PARSE_ERROR); }
|
|
|
|
PRBool NeedsUpdate() { return mParserState != PARSE_INIT && mNeedsUpdate; }
|
|
|
|
|
2008-11-06 03:01:07 +03:00
|
|
|
void GetManifestHash(nsCString &aManifestHash)
|
|
|
|
{ aManifestHash = mManifestHashValue; }
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
private:
|
|
|
|
static NS_METHOD ReadManifest(nsIInputStream *aInputStream,
|
|
|
|
void *aClosure,
|
|
|
|
const char *aFromSegment,
|
|
|
|
PRUint32 aOffset,
|
|
|
|
PRUint32 aCount,
|
|
|
|
PRUint32 *aBytesConsumed);
|
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
nsresult AddNamespace(PRUint32 namespaceType,
|
|
|
|
const nsCString &namespaceSpec,
|
|
|
|
const nsCString &data);
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
nsresult HandleManifestLine(const nsCString::const_iterator &aBegin,
|
|
|
|
const nsCString::const_iterator &aEnd);
|
|
|
|
|
2008-01-29 10:54:54 +03: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 22:34:23 +04:00
|
|
|
void ReadStrictFileOriginPolicyPref();
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
enum {
|
|
|
|
PARSE_INIT,
|
|
|
|
PARSE_CACHE_ENTRIES,
|
|
|
|
PARSE_FALLBACK_ENTRIES,
|
2008-09-30 22:34:23 +04:00
|
|
|
PARSE_BYPASS_ENTRIES,
|
2008-01-17 00:54:33 +03:00
|
|
|
PARSE_ERROR
|
|
|
|
} mParserState;
|
|
|
|
|
|
|
|
nsCString mReadBuf;
|
2008-09-30 22:34:23 +04:00
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
nsCOMArray<nsIURI> mExplicitURIs;
|
2008-09-30 22:34:23 +04: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-17 00:54:33 +03:00
|
|
|
PRBool mNeedsUpdate;
|
2008-09-30 22:34:23 +04:00
|
|
|
PRBool mStrictFileOriginPolicy;
|
2008-01-29 10:54:54 +03:00
|
|
|
|
|
|
|
// manifest hash data
|
|
|
|
nsCOMPtr<nsICryptoHash> mManifestHash;
|
|
|
|
PRBool mManifestHashInitialized;
|
2008-11-06 03:01:07 +03:00
|
|
|
nsCString mManifestHashValue;
|
2008-01-29 10:54:54 +03:00
|
|
|
nsCString mOldManifestHashValue;
|
2008-01-17 00:54:33 +03:00
|
|
|
};
|
|
|
|
|
2009-02-22 22:09:10 +03:00
|
|
|
class nsOfflineCacheUpdateOwner
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual nsresult UpdateFinished(nsOfflineCacheUpdate *aUpdate) = 0;
|
|
|
|
};
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
class nsOfflineCacheUpdate : public nsIOfflineCacheUpdate
|
2010-10-20 21:12:32 +04:00
|
|
|
, public nsIOfflineCacheUpdateObserver
|
2009-02-22 22:09:10 +03:00
|
|
|
, public nsOfflineCacheUpdateOwner
|
2007-07-25 09:35:39 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOFFLINECACHEUPDATE
|
2010-10-20 21:12:32 +04:00
|
|
|
NS_DECL_NSIOFFLINECACHEUPDATEOBSERVER
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
nsOfflineCacheUpdate();
|
|
|
|
~nsOfflineCacheUpdate();
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
static nsresult GetCacheKey(nsIURI *aURI, nsACString &aKey);
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
nsresult Init();
|
|
|
|
|
|
|
|
nsresult Begin();
|
|
|
|
nsresult Cancel();
|
|
|
|
|
|
|
|
void LoadCompleted();
|
2008-11-06 03:01:07 +03:00
|
|
|
void ManifestCheckCompleted(nsresult aStatus,
|
|
|
|
const nsCString &aManifestHash);
|
2010-10-20 21:12:32 +04:00
|
|
|
void StickDocument(nsIURI *aDocumentURI);
|
2008-11-04 13:20:27 +03:00
|
|
|
|
2009-02-22 22:09:10 +03:00
|
|
|
void SetOwner(nsOfflineCacheUpdateOwner *aOwner);
|
|
|
|
|
|
|
|
virtual nsresult UpdateFinished(nsOfflineCacheUpdate *aUpdate);
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
private:
|
2008-01-17 00:54:33 +03:00
|
|
|
nsresult HandleManifest(PRBool *aDoUpdate);
|
2008-08-27 03:09:02 +04:00
|
|
|
nsresult AddURI(nsIURI *aURI, PRUint32 aItemType);
|
2008-01-17 00:54:33 +03:00
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
nsresult ProcessNextURI();
|
2008-01-17 00:54:33 +03:00
|
|
|
|
2008-09-30 22:34:23 +04: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);
|
2010-10-20 21:12:32 +04:00
|
|
|
nsresult ScheduleImplicit();
|
|
|
|
nsresult AssociateDocuments(nsIApplicationCache* cache);
|
2008-01-17 00:54:33 +03:00
|
|
|
|
|
|
|
nsresult GatherObservers(nsCOMArray<nsIOfflineCacheUpdateObserver> &aObservers);
|
2010-10-20 21:12:32 +04:00
|
|
|
nsresult NotifyState(PRUint32 state);
|
2007-07-25 09:35:39 +04:00
|
|
|
nsresult Finish();
|
2010-10-20 21:12:32 +04:00
|
|
|
nsresult FinishNoNotify();
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
enum {
|
|
|
|
STATE_UNINITIALIZED,
|
|
|
|
STATE_INITIALIZED,
|
2008-01-17 00:54:33 +03:00
|
|
|
STATE_CHECKING,
|
|
|
|
STATE_DOWNLOADING,
|
2007-07-25 09:35:39 +04:00
|
|
|
STATE_CANCELLED,
|
|
|
|
STATE_FINISHED
|
|
|
|
} mState;
|
|
|
|
|
2009-02-22 22:09:10 +03:00
|
|
|
nsOfflineCacheUpdateOwner *mOwner;
|
|
|
|
|
2008-11-04 21:59:12 +03:00
|
|
|
PRPackedBool mAddedItems;
|
|
|
|
PRPackedBool mPartialUpdate;
|
|
|
|
PRPackedBool mSucceeded;
|
|
|
|
PRPackedBool mObsolete;
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
nsCString mUpdateDomain;
|
2008-01-17 00:54:33 +03:00
|
|
|
nsCOMPtr<nsIURI> mManifestURI;
|
|
|
|
nsCOMPtr<nsIURI> mDocumentURI;
|
2007-07-25 09:35:39 +04:00
|
|
|
|
2007-07-25 10:31:27 +04:00
|
|
|
nsCString mClientID;
|
2008-08-27 03:09:02 +04:00
|
|
|
nsCOMPtr<nsIApplicationCache> mApplicationCache;
|
|
|
|
nsCOMPtr<nsIApplicationCache> mPreviousApplicationCache;
|
2007-07-25 10:31:27 +04:00
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
nsCOMPtr<nsIObserverService> mObserverService;
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
nsRefPtr<nsOfflineManifestItem> mManifestItem;
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
/* Items being updated */
|
2007-07-25 10:31:27 +04:00
|
|
|
PRInt32 mCurrentItem;
|
2007-07-25 09:35:39 +04:00
|
|
|
nsTArray<nsRefPtr<nsOfflineCacheUpdateItem> > mItems;
|
|
|
|
|
|
|
|
/* Clients watching this update for changes */
|
|
|
|
nsCOMArray<nsIWeakReference> mWeakObservers;
|
|
|
|
nsCOMArray<nsIOfflineCacheUpdateObserver> mObservers;
|
2008-11-04 13:20:27 +03:00
|
|
|
|
|
|
|
/* Documents that requested this update */
|
2010-10-20 21:12:32 +04:00
|
|
|
nsCOMArray<nsIURI> mDocumentURIs;
|
2008-11-06 03:01:07 +03:00
|
|
|
|
|
|
|
/* Reschedule count. When an update is rescheduled due to
|
|
|
|
* mismatched manifests, the reschedule count will be increased. */
|
|
|
|
PRUint32 mRescheduleCount;
|
2009-02-22 22:09:10 +03:00
|
|
|
|
|
|
|
nsRefPtr<nsOfflineCacheUpdate> mImplicitUpdate;
|
2007-07-25 09:35:39 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class nsOfflineCacheUpdateService : public nsIOfflineCacheUpdateService
|
|
|
|
, public nsIObserver
|
2009-02-22 22:09:10 +03:00
|
|
|
, public nsOfflineCacheUpdateOwner
|
2009-03-05 00:37:29 +03:00
|
|
|
, public nsSupportsWeakReference
|
2007-07-25 09:35:39 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOFFLINECACHEUPDATESERVICE
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
nsOfflineCacheUpdateService();
|
|
|
|
~nsOfflineCacheUpdateService();
|
|
|
|
|
|
|
|
nsresult Init();
|
|
|
|
|
2010-10-20 21:12:32 +04:00
|
|
|
nsresult ScheduleUpdate(nsOfflineCacheUpdate *aUpdate);
|
|
|
|
nsresult FindUpdate(nsIURI *aManifestURI,
|
|
|
|
nsIURI *aDocumentURI,
|
|
|
|
nsOfflineCacheUpdate **aUpdate);
|
|
|
|
|
2008-11-04 13:20:27 +03:00
|
|
|
nsresult Schedule(nsIURI *aManifestURI,
|
|
|
|
nsIURI *aDocumentURI,
|
|
|
|
nsIDOMDocument *aDocument,
|
2010-10-20 21:12:32 +04:00
|
|
|
nsIDOMWindow* aWindow,
|
2008-11-04 13:20:27 +03:00
|
|
|
nsIOfflineCacheUpdate **aUpdate);
|
|
|
|
|
2009-02-22 22:09:10 +03:00
|
|
|
virtual nsresult UpdateFinished(nsOfflineCacheUpdate *aUpdate);
|
2007-07-25 09:35:39 +04:00
|
|
|
|
2007-11-09 08:12:59 +03:00
|
|
|
/**
|
|
|
|
* Returns the singleton nsOfflineCacheUpdateService without an addref, or
|
|
|
|
* nsnull if the service couldn't be created.
|
|
|
|
*/
|
|
|
|
static nsOfflineCacheUpdateService *EnsureService();
|
2007-07-25 09:35:39 +04:00
|
|
|
|
2007-11-09 08:12:59 +03:00
|
|
|
/** Addrefs and returns the singleton nsOfflineCacheUpdateService. */
|
|
|
|
static nsOfflineCacheUpdateService *GetInstance();
|
2008-11-04 13:20:27 +03:00
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
private:
|
|
|
|
nsresult ProcessNextUpdate();
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<nsOfflineCacheUpdate> > mUpdates;
|
2008-01-17 00:54:33 +03:00
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
PRBool mDisabled;
|
|
|
|
PRBool mUpdateRunning;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|