2007-07-25 09:35:39 +04:00
|
|
|
/* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
#include "nsOfflineCacheUpdate.h"
|
|
|
|
|
|
|
|
#include "nsCPrefetchService.h"
|
|
|
|
#include "nsCURILoader.h"
|
2008-11-04 13:20:27 +03:00
|
|
|
#include "nsIApplicationCacheContainer.h"
|
2008-11-04 13:20:27 +03:00
|
|
|
#include "nsIApplicationCacheChannel.h"
|
2008-08-27 03:09:02 +04:00
|
|
|
#include "nsIApplicationCacheService.h"
|
2007-07-25 09:35:39 +04:00
|
|
|
#include "nsICachingChannel.h"
|
2008-11-06 03:01:08 +03:00
|
|
|
#include "nsIContent.h"
|
2010-05-05 22:18:05 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2008-08-28 05:15:32 +04:00
|
|
|
#include "nsIDocumentLoader.h"
|
2008-11-06 03:01:08 +03:00
|
|
|
#include "nsIDOMElement.h"
|
2007-07-25 09:35:39 +04:00
|
|
|
#include "nsIDOMWindow.h"
|
2008-01-17 00:54:33 +03:00
|
|
|
#include "nsIDOMOfflineResourceList.h"
|
2008-11-06 03:01:08 +03:00
|
|
|
#include "nsIDocument.h"
|
2007-07-25 09:35:39 +04:00
|
|
|
#include "nsIObserverService.h"
|
2008-08-27 03:09:02 +04:00
|
|
|
#include "nsIURL.h"
|
2007-07-25 09:35:39 +04:00
|
|
|
#include "nsIWebProgress.h"
|
2008-01-29 10:54:54 +03:00
|
|
|
#include "nsICryptoHash.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
#include "nsICacheEntry.h"
|
2008-10-20 08:12:25 +04:00
|
|
|
#include "nsIPermissionManager.h"
|
|
|
|
#include "nsIPrincipal.h"
|
2007-07-25 09:35:39 +04:00
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsStreamUtils.h"
|
2007-07-25 10:31:27 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2009-01-29 18:22:53 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2012-11-03 20:17:47 +04:00
|
|
|
#include "nsIConsoleService.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2010-08-05 06:15:55 +04:00
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
2011-07-01 12:35:28 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-06-13 07:53:09 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-09-21 20:44:12 +04:00
|
|
|
#include "nsContentUtils.h"
|
2015-10-02 19:13:52 +03:00
|
|
|
#include "nsIPrincipal.h"
|
2007-07-25 09:35:39 +04:00
|
|
|
|
2010-10-21 00:45:19 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
|
2011-07-01 12:35:28 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static const uint32_t kRescheduleLimit = 3;
|
2012-03-31 04:52:06 +04:00
|
|
|
// Max number of retries for every entry of pinned app.
|
2012-08-22 19:56:38 +04:00
|
|
|
static const uint32_t kPinnedEntryRetriesLimit = 3;
|
2012-06-04 18:12:23 +04:00
|
|
|
// Maximum number of parallel items loads
|
2012-08-22 19:56:38 +04:00
|
|
|
static const uint32_t kParallelLoadLimit = 15;
|
2008-11-06 03:01:07 +03:00
|
|
|
|
2012-06-04 18:12:24 +04:00
|
|
|
// Quota for offline apps when preloading
|
2012-08-22 19:56:38 +04:00
|
|
|
static const int32_t kCustomProfileQuota = 512000;
|
2012-06-04 18:12:24 +04:00
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
//
|
2016-05-26 21:48:27 +03:00
|
|
|
// To enable logging (see mozilla/Logging.h for full details):
|
2007-07-25 09:35:39 +04:00
|
|
|
//
|
2016-05-26 21:48:27 +03:00
|
|
|
// set MOZ_LOG=nsOfflineCacheUpdate:5
|
|
|
|
// set MOZ_LOG_FILE=offlineupdate.log
|
2007-07-25 09:35:39 +04:00
|
|
|
//
|
2015-06-04 01:25:57 +03:00
|
|
|
// this enables LogLevel::Debug level information and places all output in
|
2007-07-25 09:35:39 +04:00
|
|
|
// the file offlineupdate.log
|
|
|
|
//
|
2016-01-30 03:30:00 +03:00
|
|
|
extern LazyLogModule gOfflineCacheUpdateLog;
|
2013-11-19 11:50:59 +04:00
|
|
|
|
|
|
|
#undef LOG
|
2015-06-04 01:25:57 +03:00
|
|
|
#define LOG(args) MOZ_LOG(gOfflineCacheUpdateLog, mozilla::LogLevel::Debug, args)
|
2013-11-19 11:50:59 +04:00
|
|
|
|
|
|
|
#undef LOG_ENABLED
|
2015-06-04 01:25:57 +03:00
|
|
|
#define LOG_ENABLED() MOZ_LOG_TEST(gOfflineCacheUpdateLog, mozilla::LogLevel::Debug)
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
class AutoFreeArray {
|
|
|
|
public:
|
2012-08-22 19:56:38 +04:00
|
|
|
AutoFreeArray(uint32_t count, char **values)
|
2007-07-25 09:35:39 +04:00
|
|
|
: mCount(count), mValues(values) {};
|
|
|
|
~AutoFreeArray() { NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(mCount, mValues); }
|
|
|
|
private:
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mCount;
|
2007-07-25 09:35:39 +04:00
|
|
|
char **mValues;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
namespace {
|
2012-11-03 20:17:47 +04:00
|
|
|
|
|
|
|
nsresult
|
2008-09-30 22:34:23 +04:00
|
|
|
DropReferenceFromURL(nsIURI * aURI)
|
|
|
|
{
|
2011-05-22 05:12:46 +04:00
|
|
|
// XXXdholbert If this SetRef fails, callers of this method probably
|
|
|
|
// want to call aURI->CloneIgnoringRef() and use the result of that.
|
|
|
|
return aURI->SetRef(EmptyCString());
|
2008-09-30 22:34:23 +04:00
|
|
|
}
|
|
|
|
|
2012-11-03 20:17:47 +04:00
|
|
|
void
|
|
|
|
LogToConsole(const char * message, nsOfflineCacheUpdateItem * item = nullptr)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIConsoleService> consoleService =
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID);
|
|
|
|
if (consoleService)
|
|
|
|
{
|
|
|
|
nsAutoString messageUTF16 = NS_ConvertUTF8toUTF16(message);
|
|
|
|
if (item && item->mURI) {
|
|
|
|
nsAutoCString uriSpec;
|
|
|
|
item->mURI->GetSpec(uriSpec);
|
|
|
|
|
2014-05-22 07:48:50 +04:00
|
|
|
messageUTF16.AppendLiteral(", URL=");
|
2012-11-03 20:17:47 +04:00
|
|
|
messageUTF16.Append(NS_ConvertUTF8toUTF16(uriSpec));
|
|
|
|
}
|
|
|
|
consoleService->LogStringMessage(messageUTF16.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2012-11-03 20:17:47 +04:00
|
|
|
|
2008-11-06 03:01:07 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsManifestCheck
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsManifestCheck final : public nsIStreamListener
|
2015-03-27 21:52:19 +03:00
|
|
|
, public nsIChannelEventSink
|
|
|
|
, public nsIInterfaceRequestor
|
2008-11-06 03:01:07 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsManifestCheck(nsOfflineCacheUpdate *aUpdate,
|
|
|
|
nsIURI *aURI,
|
2015-10-02 19:13:52 +03:00
|
|
|
nsIURI *aReferrerURI,
|
|
|
|
nsIPrincipal* aLoadingPrincipal)
|
2008-11-06 03:01:07 +03:00
|
|
|
: mUpdate(aUpdate)
|
|
|
|
, mURI(aURI)
|
|
|
|
, mReferrerURI(aReferrerURI)
|
2015-10-02 19:13:52 +03:00
|
|
|
, mLoadingPrincipal(aLoadingPrincipal)
|
2008-11-06 03:01:07 +03:00
|
|
|
{}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
NS_DECL_NSICHANNELEVENTSINK
|
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
|
|
|
|
nsresult Begin();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2014-06-23 22:49:09 +04:00
|
|
|
~nsManifestCheck() {}
|
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
static nsresult ReadManifest(nsIInputStream *aInputStream,
|
|
|
|
void *aClosure,
|
|
|
|
const char *aFromSegment,
|
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t *aBytesConsumed);
|
2008-11-06 03:01:07 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsOfflineCacheUpdate> mUpdate;
|
2008-11-06 03:01:07 +03:00
|
|
|
nsCOMPtr<nsIURI> mURI;
|
|
|
|
nsCOMPtr<nsIURI> mReferrerURI;
|
2015-10-02 19:13:52 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> mLoadingPrincipal;
|
2008-11-06 03:01:07 +03:00
|
|
|
nsCOMPtr<nsICryptoHash> mManifestHash;
|
|
|
|
nsCOMPtr<nsIChannel> mChannel;
|
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsManifestCheck::nsISupports
|
|
|
|
//-----------------------------------------------------------------------------
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsManifestCheck,
|
|
|
|
nsIRequestObserver,
|
|
|
|
nsIStreamListener,
|
|
|
|
nsIChannelEventSink,
|
|
|
|
nsIInterfaceRequestor)
|
2008-11-06 03:01:07 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsManifestCheck <public>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsManifestCheck::Begin()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
mManifestHash = do_CreateInstance("@mozilla.org/security/hash;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mManifestHash->Init(nsICryptoHash::MD5);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(mChannel),
|
|
|
|
mURI,
|
2015-10-02 19:13:52 +03:00
|
|
|
mLoadingPrincipal,
|
|
|
|
nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_IS_BLOCKED,
|
2014-09-21 20:44:12 +04:00
|
|
|
nsIContentPolicy::TYPE_OTHER,
|
|
|
|
nullptr, // loadGroup
|
|
|
|
nullptr, // aCallbacks
|
2008-11-06 03:01:07 +03:00
|
|
|
nsIRequest::LOAD_BYPASS_CACHE);
|
2014-09-21 20:44:12 +04:00
|
|
|
|
2008-11-06 03:01:07 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// configure HTTP specific stuff
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel =
|
|
|
|
do_QueryInterface(mChannel);
|
|
|
|
if (httpChannel) {
|
|
|
|
httpChannel->SetReferrer(mReferrerURI);
|
|
|
|
httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("X-Moz"),
|
|
|
|
NS_LITERAL_CSTRING("offline-resource"),
|
2011-10-17 18:59:28 +04:00
|
|
|
false);
|
2008-11-06 03:01:07 +03:00
|
|
|
}
|
|
|
|
|
2015-08-28 05:45:30 +03:00
|
|
|
return mChannel->AsyncOpen2(this);
|
2008-11-06 03:01:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsManifestCheck <public>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
/* static */ nsresult
|
2008-11-06 03:01:07 +03:00
|
|
|
nsManifestCheck::ReadManifest(nsIInputStream *aInputStream,
|
|
|
|
void *aClosure,
|
|
|
|
const char *aFromSegment,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t *aBytesConsumed)
|
2008-11-06 03:01:07 +03:00
|
|
|
{
|
|
|
|
nsManifestCheck *manifestCheck =
|
|
|
|
static_cast<nsManifestCheck*>(aClosure);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
*aBytesConsumed = aCount;
|
|
|
|
|
|
|
|
rv = manifestCheck->mManifestHash->Update(
|
2012-08-22 19:56:38 +04:00
|
|
|
reinterpret_cast<const uint8_t *>(aFromSegment), aCount);
|
2008-11-06 03:01:07 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsManifestCheck::nsIStreamListener
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsManifestCheck::OnStartRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsManifestCheck::OnDataAvailable(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsIInputStream *aStream,
|
2012-09-06 06:41:02 +04:00
|
|
|
uint64_t aOffset,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aCount)
|
2008-11-06 03:01:07 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesRead;
|
2008-11-06 03:01:07 +03:00
|
|
|
aStream->ReadSegments(ReadManifest, this, aCount, &bytesRead);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsManifestCheck::OnStopRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString manifestHash;
|
2008-11-06 03:01:07 +03:00
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mManifestHash->Finish(true, manifestHash);
|
2008-11-06 03:01:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mUpdate->ManifestCheckCompleted(aStatus, manifestHash);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsManifestCheck::nsIInterfaceRequestor
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsManifestCheck::GetInterface(const nsIID &aIID, void **aResult)
|
|
|
|
{
|
|
|
|
if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
*aResult = static_cast<nsIChannelEventSink *>(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsManifestCheck::nsIChannelEventSink
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-08-05 06:15:55 +04:00
|
|
|
nsManifestCheck::AsyncOnChannelRedirect(nsIChannel *aOldChannel,
|
|
|
|
nsIChannel *aNewChannel,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aFlags,
|
2010-08-05 06:15:55 +04:00
|
|
|
nsIAsyncVerifyRedirectCallback *callback)
|
2008-11-06 03:01:07 +03:00
|
|
|
{
|
|
|
|
// Redirects should cause the load (and therefore the update) to fail.
|
2010-08-05 06:15:55 +04:00
|
|
|
if (aFlags & nsIChannelEventSink::REDIRECT_INTERNAL) {
|
|
|
|
callback->OnRedirectVerifyCallback(NS_OK);
|
2008-11-06 03:01:07 +03:00
|
|
|
return NS_OK;
|
2010-08-05 06:15:55 +04:00
|
|
|
}
|
2012-11-03 20:17:47 +04:00
|
|
|
|
|
|
|
LogToConsole("Manifest check failed because its response is a redirect");
|
|
|
|
|
2008-11-06 03:01:07 +03:00
|
|
|
aOldChannel->Cancel(NS_ERROR_ABORT);
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineCacheUpdateItem::nsISupports
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsOfflineCacheUpdateItem,
|
|
|
|
nsIRequestObserver,
|
|
|
|
nsIStreamListener,
|
|
|
|
nsIRunnable,
|
|
|
|
nsIInterfaceRequestor,
|
|
|
|
nsIChannelEventSink)
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineCacheUpdateItem <public>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2012-06-04 18:12:23 +04:00
|
|
|
nsOfflineCacheUpdateItem::nsOfflineCacheUpdateItem(nsIURI *aURI,
|
2007-07-25 09:35:39 +04:00
|
|
|
nsIURI *aReferrerURI,
|
2015-10-02 19:13:52 +03:00
|
|
|
nsIPrincipal* aLoadingPrincipal,
|
2012-06-04 18:12:24 +04:00
|
|
|
nsIApplicationCache *aApplicationCache,
|
2008-08-27 03:09:02 +04:00
|
|
|
nsIApplicationCache *aPreviousApplicationCache,
|
2016-07-27 14:08:00 +03:00
|
|
|
uint32_t type,
|
|
|
|
uint32_t loadFlags)
|
2007-07-25 09:35:39 +04:00
|
|
|
: mURI(aURI)
|
|
|
|
, mReferrerURI(aReferrerURI)
|
2015-10-02 19:13:52 +03:00
|
|
|
, mLoadingPrincipal(aLoadingPrincipal)
|
2012-06-04 18:12:24 +04:00
|
|
|
, mApplicationCache(aApplicationCache)
|
2008-08-27 03:09:02 +04:00
|
|
|
, mPreviousApplicationCache(aPreviousApplicationCache)
|
|
|
|
, mItemType(type)
|
2016-07-27 14:08:00 +03:00
|
|
|
, mLoadFlags(loadFlags)
|
2012-07-30 18:20:58 +04:00
|
|
|
, mChannel(nullptr)
|
2013-12-18 19:11:54 +04:00
|
|
|
, mState(LoadStatus::UNINITIALIZED)
|
2007-07-25 09:35:39 +04:00
|
|
|
, mBytesRead(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsOfflineCacheUpdateItem::~nsOfflineCacheUpdateItem()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-06-04 18:12:23 +04:00
|
|
|
nsOfflineCacheUpdateItem::OpenChannel(nsOfflineCacheUpdate *aUpdate)
|
2007-07-25 09:35:39 +04:00
|
|
|
{
|
2008-08-27 03:09:02 +04:00
|
|
|
if (LOG_ENABLED()) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString spec;
|
2008-08-27 03:09:02 +04:00
|
|
|
mURI->GetSpec(spec);
|
|
|
|
LOG(("%p: Opening channel for %s", this, spec.get()));
|
|
|
|
}
|
|
|
|
|
2012-10-22 21:59:07 +04:00
|
|
|
if (mUpdate) {
|
|
|
|
// Holding a reference to the update means this item is already
|
|
|
|
// in progress (has a channel, or is just in between OnStopRequest()
|
|
|
|
// and its Run() call. We must never open channel on this item again.
|
|
|
|
LOG((" %p is already running! ignoring", this));
|
|
|
|
return NS_ERROR_ALREADY_OPENED;
|
|
|
|
}
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
nsresult rv = nsOfflineCacheUpdate::GetCacheKey(mURI, mCacheKey);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-10-02 17:55:00 +04:00
|
|
|
uint32_t flags = nsIRequest::LOAD_BACKGROUND |
|
2015-02-25 16:44:00 +03:00
|
|
|
nsICachingChannel::LOAD_ONLY_IF_MODIFIED;
|
2012-10-02 17:55:00 +04:00
|
|
|
|
|
|
|
if (mApplicationCache == mPreviousApplicationCache) {
|
|
|
|
// Same app cache to read from and to write to is used during
|
|
|
|
// an only-update-check procedure. Here we protect the existing
|
|
|
|
// cache from being modified.
|
|
|
|
flags |= nsIRequest::INHIBIT_CACHING;
|
|
|
|
}
|
|
|
|
|
2016-07-27 14:08:00 +03:00
|
|
|
flags |= mLoadFlags;
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(mChannel),
|
|
|
|
mURI,
|
2015-10-02 19:13:52 +03:00
|
|
|
mLoadingPrincipal,
|
2015-08-28 05:45:30 +03:00
|
|
|
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
|
2014-09-21 20:44:12 +04:00
|
|
|
nsIContentPolicy::TYPE_OTHER,
|
|
|
|
nullptr, // aLoadGroup
|
|
|
|
this, // aCallbacks
|
2012-10-02 17:55:00 +04:00
|
|
|
flags);
|
2014-09-21 20:44:12 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-11-04 13:20:27 +03:00
|
|
|
nsCOMPtr<nsIApplicationCacheChannel> appCacheChannel =
|
2008-08-27 03:09:02 +04:00
|
|
|
do_QueryInterface(mChannel, &rv);
|
|
|
|
|
2008-11-04 13:20:27 +03:00
|
|
|
// Support for nsIApplicationCacheChannel is required.
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Use the existing application cache as the cache to check.
|
2008-11-04 13:20:27 +03:00
|
|
|
rv = appCacheChannel->SetApplicationCache(mPreviousApplicationCache);
|
2007-07-25 09:35:39 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-10-02 17:55:00 +04:00
|
|
|
// Set the new application cache as the target for write.
|
2012-07-11 22:20:17 +04:00
|
|
|
rv = appCacheChannel->SetApplicationCacheForWrite(mApplicationCache);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
// configure HTTP specific stuff
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel =
|
|
|
|
do_QueryInterface(mChannel);
|
|
|
|
if (httpChannel) {
|
|
|
|
httpChannel->SetReferrer(mReferrerURI);
|
|
|
|
httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("X-Moz"),
|
|
|
|
NS_LITERAL_CSTRING("offline-resource"),
|
2011-10-17 18:59:28 +04:00
|
|
|
false);
|
2007-07-25 09:35:39 +04:00
|
|
|
}
|
|
|
|
|
2015-08-28 05:45:30 +03:00
|
|
|
rv = mChannel->AsyncOpen2(this);
|
2007-07-25 09:35:39 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-06-04 18:12:23 +04:00
|
|
|
mUpdate = aUpdate;
|
|
|
|
|
2013-12-18 19:11:54 +04:00
|
|
|
mState = LoadStatus::REQUESTED;
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsOfflineCacheUpdateItem::Cancel()
|
|
|
|
{
|
|
|
|
if (mChannel) {
|
|
|
|
mChannel->Cancel(NS_ERROR_ABORT);
|
2012-07-30 18:20:58 +04:00
|
|
|
mChannel = nullptr;
|
2007-07-25 09:35:39 +04:00
|
|
|
}
|
|
|
|
|
2013-12-18 19:11:54 +04:00
|
|
|
mState = LoadStatus::UNINITIALIZED;
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineCacheUpdateItem::nsIStreamListener
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdateItem::OnStartRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext)
|
|
|
|
{
|
2013-12-18 19:11:54 +04:00
|
|
|
mState = LoadStatus::RECEIVING;
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdateItem::OnDataAvailable(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsIInputStream *aStream,
|
2012-09-06 06:41:02 +04:00
|
|
|
uint64_t aOffset,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aCount)
|
2007-07-25 09:35:39 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesRead = 0;
|
2012-07-30 18:20:58 +04:00
|
|
|
aStream->ReadSegments(NS_DiscardSegment, nullptr, aCount, &bytesRead);
|
2007-07-25 09:35:39 +04:00
|
|
|
mBytesRead += bytesRead;
|
2012-09-06 06:41:02 +04:00
|
|
|
LOG(("loaded %u bytes into offline cache [offset=%llu]\n",
|
2007-07-25 09:35:39 +04:00
|
|
|
bytesRead, aOffset));
|
2012-05-23 00:12:40 +04:00
|
|
|
|
|
|
|
mUpdate->OnByteProgress(bytesRead);
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdateItem::OnStopRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
2012-11-03 20:17:47 +04:00
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
nsAutoCString spec;
|
|
|
|
mURI->GetSpec(spec);
|
|
|
|
LOG(("%p: Done fetching offline item %s [status=%x]\n",
|
|
|
|
this, spec.get(), aStatus));
|
|
|
|
}
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
if (mBytesRead == 0 && aStatus == NS_OK) {
|
|
|
|
// we didn't need to read (because LOAD_ONLY_IF_MODIFIED was
|
|
|
|
// specified), but the object should report loadedSize as if it
|
|
|
|
// did.
|
|
|
|
mChannel->GetContentLength(&mBytesRead);
|
2012-05-23 00:12:40 +04:00
|
|
|
mUpdate->OnByteProgress(mBytesRead);
|
2007-07-25 09:35:39 +04:00
|
|
|
}
|
|
|
|
|
2013-01-07 19:01:00 +04:00
|
|
|
if (NS_FAILED(aStatus)) {
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(mChannel);
|
|
|
|
if (httpChannel) {
|
|
|
|
bool isNoStore;
|
|
|
|
if (NS_SUCCEEDED(httpChannel->IsNoStoreResponse(&isNoStore))
|
|
|
|
&& isNoStore) {
|
|
|
|
LogToConsole("Offline cache manifest item has Cache-control: no-store header",
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-25 10:31:27 +04:00
|
|
|
// We need to notify the update that the load is complete, but we
|
|
|
|
// want to give the channel a chance to close the cache entries.
|
|
|
|
NS_DispatchToCurrentThread(this);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineCacheUpdateItem::nsIRunnable
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdateItem::Run()
|
|
|
|
{
|
2012-06-04 18:12:23 +04:00
|
|
|
// Set mState to LOADED here rather than in OnStopRequest to prevent
|
|
|
|
// race condition when checking state of all mItems in ProcessNextURI().
|
|
|
|
// If state would have been set in OnStopRequest we could mistakenly
|
|
|
|
// take this item as already finished and finish the update process too
|
|
|
|
// early when ProcessNextURI() would get called between OnStopRequest()
|
|
|
|
// and Run() of this item. Finish() would then have been called twice.
|
2013-12-18 19:11:54 +04:00
|
|
|
mState = LoadStatus::LOADED;
|
2012-06-04 18:12:23 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsOfflineCacheUpdate> update;
|
2012-06-04 18:12:23 +04:00
|
|
|
update.swap(mUpdate);
|
|
|
|
update->LoadCompleted(this);
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineCacheUpdateItem::nsIInterfaceRequestor
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdateItem::GetInterface(const nsIID &aIID, void **aResult)
|
|
|
|
{
|
|
|
|
if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
*aResult = static_cast<nsIChannelEventSink *>(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineCacheUpdateItem::nsIChannelEventSink
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-08-05 06:15:55 +04:00
|
|
|
nsOfflineCacheUpdateItem::AsyncOnChannelRedirect(nsIChannel *aOldChannel,
|
|
|
|
nsIChannel *aNewChannel,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aFlags,
|
2010-08-05 06:15:55 +04:00
|
|
|
nsIAsyncVerifyRedirectCallback *cb)
|
2007-07-25 09:35:39 +04:00
|
|
|
{
|
2009-02-27 17:09:06 +03:00
|
|
|
if (!(aFlags & nsIChannelEventSink::REDIRECT_INTERNAL)) {
|
|
|
|
// Don't allow redirect in case of non-internal redirect and cancel
|
|
|
|
// the channel to clean the cache entry.
|
2012-11-03 20:17:47 +04:00
|
|
|
LogToConsole("Offline cache manifest failed because an item redirects", this);
|
|
|
|
|
2009-02-27 17:09:06 +03:00
|
|
|
aOldChannel->Cancel(NS_ERROR_ABORT);
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
nsCOMPtr<nsIURI> newURI;
|
|
|
|
nsresult rv = aNewChannel->GetURI(getter_AddRefs(newURI));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2012-07-11 22:20:17 +04:00
|
|
|
nsCOMPtr<nsIApplicationCacheChannel> appCacheChannel =
|
2009-02-27 17:09:06 +03:00
|
|
|
do_QueryInterface(aNewChannel);
|
2012-07-11 22:20:17 +04:00
|
|
|
if (appCacheChannel) {
|
|
|
|
rv = appCacheChannel->SetApplicationCacheForWrite(mApplicationCache);
|
2012-06-04 18:12:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-07-25 10:31:27 +04:00
|
|
|
}
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString oldScheme;
|
2008-01-17 00:54:33 +03:00
|
|
|
mURI->GetScheme(oldScheme);
|
2007-07-25 09:35:39 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool match;
|
2008-01-17 00:54:33 +03:00
|
|
|
if (NS_FAILED(newURI->SchemeIs(oldScheme.get(), &match)) || !match) {
|
|
|
|
LOG(("rejected: redirected to a different scheme\n"));
|
|
|
|
return NS_ERROR_ABORT;
|
2007-07-25 09:35:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP request headers are not automatically forwarded to the new channel.
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aNewChannel);
|
|
|
|
NS_ENSURE_STATE(httpChannel);
|
|
|
|
|
|
|
|
httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("X-Moz"),
|
|
|
|
NS_LITERAL_CSTRING("offline-resource"),
|
2011-10-17 18:59:28 +04:00
|
|
|
false);
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
mChannel = aNewChannel;
|
|
|
|
|
2010-08-05 06:15:55 +04:00
|
|
|
cb->OnRedirectVerifyCallback(NS_OK);
|
2007-07-25 09:35:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-02-27 17:09:06 +03:00
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsOfflineCacheUpdateItem::GetRequestSucceeded(bool * succeeded)
|
2009-02-27 17:09:06 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*succeeded = false;
|
2009-02-27 17:09:06 +03:00
|
|
|
|
|
|
|
if (!mChannel)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(mChannel, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool reqSucceeded;
|
2009-02-27 17:09:06 +03:00
|
|
|
rv = httpChannel->GetRequestSucceeded(&reqSucceeded);
|
|
|
|
if (NS_ERROR_NOT_AVAILABLE == rv)
|
|
|
|
return NS_OK;
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!reqSucceeded) {
|
|
|
|
LOG(("Request failed"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult channelStatus;
|
|
|
|
rv = httpChannel->GetStatus(&channelStatus);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (NS_FAILED(channelStatus)) {
|
|
|
|
LOG(("Channel status=0x%08x", channelStatus));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
*succeeded = true;
|
2009-02-27 17:09:06 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-04 18:12:23 +04:00
|
|
|
bool
|
|
|
|
nsOfflineCacheUpdateItem::IsScheduled()
|
|
|
|
{
|
2013-12-18 19:11:54 +04:00
|
|
|
return mState == LoadStatus::UNINITIALIZED;
|
2012-06-04 18:12:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsOfflineCacheUpdateItem::IsInProgress()
|
|
|
|
{
|
2013-12-18 19:11:54 +04:00
|
|
|
return mState == LoadStatus::REQUESTED ||
|
|
|
|
mState == LoadStatus::RECEIVING;
|
2012-06-04 18:12:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsOfflineCacheUpdateItem::IsCompleted()
|
|
|
|
{
|
2013-12-18 19:11:54 +04:00
|
|
|
return mState == LoadStatus::LOADED;
|
2012-06-04 18:12:23 +04:00
|
|
|
}
|
|
|
|
|
2013-12-18 19:11:54 +04:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheUpdateItem::GetStatus(uint16_t *aStatus)
|
2007-07-25 09:35:39 +04:00
|
|
|
{
|
|
|
|
if (!mChannel) {
|
|
|
|
*aStatus = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(mChannel, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t httpStatus;
|
2007-07-25 09:35:39 +04:00
|
|
|
rv = httpChannel->GetResponseStatus(&httpStatus);
|
|
|
|
if (rv == NS_ERROR_NOT_AVAILABLE) {
|
|
|
|
*aStatus = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-08-22 19:56:38 +04:00
|
|
|
*aStatus = uint16_t(httpStatus);
|
2007-07-25 09:35:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineManifestItem
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineManifestItem <public>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2012-06-04 18:12:23 +04:00
|
|
|
nsOfflineManifestItem::nsOfflineManifestItem(nsIURI *aURI,
|
2008-01-17 00:54:33 +03:00
|
|
|
nsIURI *aReferrerURI,
|
2015-10-02 19:13:52 +03:00
|
|
|
nsIPrincipal* aLoadingPrincipal,
|
2012-06-04 18:12:24 +04:00
|
|
|
nsIApplicationCache *aApplicationCache,
|
2012-07-11 22:20:17 +04:00
|
|
|
nsIApplicationCache *aPreviousApplicationCache)
|
2015-10-02 19:13:52 +03:00
|
|
|
: nsOfflineCacheUpdateItem(aURI, aReferrerURI, aLoadingPrincipal,
|
2012-07-11 22:20:17 +04:00
|
|
|
aApplicationCache, aPreviousApplicationCache,
|
2016-07-27 14:08:00 +03:00
|
|
|
nsIApplicationCache::ITEM_MANIFEST, 0)
|
2008-01-17 00:54:33 +03:00
|
|
|
, mParserState(PARSE_INIT)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mNeedsUpdate(true)
|
2016-01-12 21:16:59 +03:00
|
|
|
, mStrictFileOriginPolicy(false)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mManifestHashInitialized(false)
|
2008-01-17 00:54:33 +03:00
|
|
|
{
|
2008-09-30 22:34:23 +04:00
|
|
|
ReadStrictFileOriginPolicyPref();
|
2008-01-17 00:54:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsOfflineManifestItem::~nsOfflineManifestItem()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineManifestItem <private>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/* static */
|
2016-08-12 10:36:22 +03:00
|
|
|
nsresult
|
2008-01-17 00:54:33 +03:00
|
|
|
nsOfflineManifestItem::ReadManifest(nsIInputStream *aInputStream,
|
|
|
|
void *aClosure,
|
|
|
|
const char *aFromSegment,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t *aBytesConsumed)
|
2008-01-17 00:54:33 +03:00
|
|
|
{
|
|
|
|
nsOfflineManifestItem *manifest =
|
|
|
|
static_cast<nsOfflineManifestItem*>(aClosure);
|
|
|
|
|
2008-01-29 10:54:54 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
*aBytesConsumed = aCount;
|
|
|
|
|
|
|
|
if (manifest->mParserState == PARSE_ERROR) {
|
|
|
|
// parse already failed, ignore this
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-01-29 10:54:54 +03:00
|
|
|
if (!manifest->mManifestHashInitialized) {
|
|
|
|
// Avoid re-creation of crypto hash when it fails from some reason the first time
|
2011-10-17 18:59:28 +04:00
|
|
|
manifest->mManifestHashInitialized = true;
|
2008-01-29 10:54:54 +03:00
|
|
|
|
|
|
|
manifest->mManifestHash = do_CreateInstance("@mozilla.org/security/hash;1", &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = manifest->mManifestHash->Init(nsICryptoHash::MD5);
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
manifest->mManifestHash = nullptr;
|
2008-01-29 10:54:54 +03:00
|
|
|
LOG(("Could not initialize manifest hash for byte-to-byte check, rv=%08x", rv));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (manifest->mManifestHash) {
|
2012-08-22 19:56:38 +04:00
|
|
|
rv = manifest->mManifestHash->Update(reinterpret_cast<const uint8_t *>(aFromSegment), aCount);
|
2008-01-29 10:54:54 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
manifest->mManifestHash = nullptr;
|
2008-01-29 10:54:54 +03:00
|
|
|
LOG(("Could not update manifest hash, rv=%08x", rv));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
manifest->mReadBuf.Append(aFromSegment, aCount);
|
|
|
|
|
|
|
|
nsCString::const_iterator begin, iter, end;
|
|
|
|
manifest->mReadBuf.BeginReading(begin);
|
|
|
|
manifest->mReadBuf.EndReading(end);
|
|
|
|
|
|
|
|
for (iter = begin; iter != end; iter++) {
|
|
|
|
if (*iter == '\r' || *iter == '\n') {
|
2015-10-20 06:28:34 +03:00
|
|
|
rv = manifest->HandleManifestLine(begin, iter);
|
2012-03-31 04:52:06 +04:00
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(("HandleManifestLine failed with 0x%08x", rv));
|
2012-06-04 18:12:23 +04:00
|
|
|
*aBytesConsumed = 0; // Avoid assertion failure in stream tee
|
2008-01-17 00:54:33 +03:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
begin = iter;
|
|
|
|
begin++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// any leftovers are saved for next time
|
|
|
|
manifest->mReadBuf = Substring(begin, end);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineManifestItem::AddNamespace(uint32_t namespaceType,
|
2008-09-30 22:34:23 +04:00
|
|
|
const nsCString &namespaceSpec,
|
|
|
|
const nsCString &data)
|
|
|
|
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
if (!mNamespaces) {
|
|
|
|
mNamespaces = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIApplicationCacheNamespace> ns =
|
|
|
|
do_CreateInstance(NS_APPLICATIONCACHENAMESPACE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-10-28 07:33:12 +03:00
|
|
|
rv = ns->Init(namespaceType, namespaceSpec, data);
|
2008-09-30 22:34:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = mNamespaces->AppendElement(ns, false);
|
2008-09-30 22:34:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
nsresult
|
|
|
|
nsOfflineManifestItem::HandleManifestLine(const nsCString::const_iterator &aBegin,
|
|
|
|
const nsCString::const_iterator &aEnd)
|
|
|
|
{
|
|
|
|
nsCString::const_iterator begin = aBegin;
|
|
|
|
nsCString::const_iterator end = aEnd;
|
|
|
|
|
|
|
|
// all lines ignore trailing spaces and tabs
|
|
|
|
nsCString::const_iterator last = end;
|
|
|
|
--last;
|
|
|
|
while (end != begin && (*last == ' ' || *last == '\t')) {
|
|
|
|
--end;
|
|
|
|
--last;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mParserState == PARSE_INIT) {
|
|
|
|
// Allow a UTF-8 BOM
|
|
|
|
if (begin != end && static_cast<unsigned char>(*begin) == 0xef) {
|
|
|
|
if (++begin == end || static_cast<unsigned char>(*begin) != 0xbb ||
|
|
|
|
++begin == end || static_cast<unsigned char>(*begin) != 0xbf) {
|
|
|
|
mParserState = PARSE_ERROR;
|
2012-11-03 20:17:47 +04:00
|
|
|
LogToConsole("Offline cache manifest BOM error", this);
|
2008-01-17 00:54:33 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
++begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsCSubstring &magic = Substring(begin, end);
|
|
|
|
|
|
|
|
if (!magic.EqualsLiteral("CACHE MANIFEST")) {
|
|
|
|
mParserState = PARSE_ERROR;
|
2015-10-24 02:02:56 +03:00
|
|
|
LogToConsole("Offline cache manifest magic incorrect", this);
|
2008-01-17 00:54:33 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mParserState = PARSE_CACHE_ENTRIES;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// lines other than the first ignore leading spaces and tabs
|
|
|
|
while (begin != end && (*begin == ' ' || *begin == '\t'))
|
|
|
|
begin++;
|
|
|
|
|
|
|
|
// ignore blank lines and comments
|
|
|
|
if (begin == end || *begin == '#')
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
const nsCSubstring &line = Substring(begin, end);
|
|
|
|
|
|
|
|
if (line.EqualsLiteral("CACHE:")) {
|
|
|
|
mParserState = PARSE_CACHE_ENTRIES;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line.EqualsLiteral("FALLBACK:")) {
|
|
|
|
mParserState = PARSE_FALLBACK_ENTRIES;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line.EqualsLiteral("NETWORK:")) {
|
2008-09-30 22:34:23 +04:00
|
|
|
mParserState = PARSE_BYPASS_ENTRIES;
|
2008-01-17 00:54:33 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-14 13:52:35 +04:00
|
|
|
// Every other section type we don't know must be silently ignored.
|
|
|
|
nsCString::const_iterator lastChar = end;
|
|
|
|
if (*(--lastChar) == ':') {
|
|
|
|
mParserState = PARSE_UNKNOWN_SECTION;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
switch(mParserState) {
|
|
|
|
case PARSE_INIT:
|
|
|
|
case PARSE_ERROR: {
|
|
|
|
// this should have been dealt with earlier
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2008-08-28 05:15:32 +04:00
|
|
|
|
2012-07-14 13:52:35 +04:00
|
|
|
case PARSE_UNKNOWN_SECTION: {
|
|
|
|
// just jump over
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
case PARSE_CACHE_ENTRIES: {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), line, nullptr, mURI);
|
2008-01-17 00:54:33 +03:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
2008-09-30 22:34:23 +04:00
|
|
|
if (NS_FAILED(DropReferenceFromURL(uri)))
|
|
|
|
break;
|
2008-01-17 00:54:33 +03:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString scheme;
|
2008-01-17 00:54:33 +03:00
|
|
|
uri->GetScheme(scheme);
|
|
|
|
|
|
|
|
// Manifest URIs must have the same scheme as the manifest.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool match;
|
2008-01-17 00:54:33 +03:00
|
|
|
if (NS_FAILED(mURI->SchemeIs(scheme.get(), &match)) || !match)
|
|
|
|
break;
|
|
|
|
|
|
|
|
mExplicitURIs.AppendObject(uri);
|
2016-07-27 14:08:00 +03:00
|
|
|
|
|
|
|
if (!NS_SecurityCompareURIs(mURI, uri,
|
|
|
|
mStrictFileOriginPolicy)) {
|
|
|
|
mAnonymousURIs.AppendObject(uri);
|
|
|
|
}
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
break;
|
|
|
|
}
|
2008-09-30 22:34:23 +04:00
|
|
|
|
|
|
|
case PARSE_FALLBACK_ENTRIES: {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t separator = line.FindChar(' ');
|
2008-09-30 22:34:23 +04:00
|
|
|
if (separator == kNotFound) {
|
|
|
|
separator = line.FindChar('\t');
|
|
|
|
if (separator == kNotFound)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString namespaceSpec(Substring(line, 0, separator));
|
|
|
|
nsCString fallbackSpec(Substring(line, separator + 1));
|
|
|
|
namespaceSpec.CompressWhitespace();
|
|
|
|
fallbackSpec.CompressWhitespace();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> namespaceURI;
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = NS_NewURI(getter_AddRefs(namespaceURI), namespaceSpec, nullptr, mURI);
|
2008-09-30 22:34:23 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
if (NS_FAILED(DropReferenceFromURL(namespaceURI)))
|
|
|
|
break;
|
|
|
|
rv = namespaceURI->GetAsciiSpec(namespaceSpec);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> fallbackURI;
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = NS_NewURI(getter_AddRefs(fallbackURI), fallbackSpec, nullptr, mURI);
|
2008-09-30 22:34:23 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
if (NS_FAILED(DropReferenceFromURL(fallbackURI)))
|
|
|
|
break;
|
|
|
|
rv = fallbackURI->GetAsciiSpec(fallbackSpec);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Manifest and namespace must be same origin
|
|
|
|
if (!NS_SecurityCompareURIs(mURI, namespaceURI,
|
|
|
|
mStrictFileOriginPolicy))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Fallback and namespace must be same origin
|
|
|
|
if (!NS_SecurityCompareURIs(namespaceURI, fallbackURI,
|
|
|
|
mStrictFileOriginPolicy))
|
|
|
|
break;
|
|
|
|
|
|
|
|
mFallbackURIs.AppendObject(fallbackURI);
|
|
|
|
|
2008-11-04 21:59:12 +03:00
|
|
|
AddNamespace(nsIApplicationCacheNamespace::NAMESPACE_FALLBACK,
|
2008-09-30 22:34:23 +04:00
|
|
|
namespaceSpec, fallbackSpec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PARSE_BYPASS_ENTRIES: {
|
2010-11-25 23:49:19 +03:00
|
|
|
if (line[0] == '*' && (line.Length() == 1 || line[1] == ' ' || line[1] == '\t'))
|
|
|
|
{
|
|
|
|
// '*' indicates to make the online whitelist wildcard flag open,
|
|
|
|
// i.e. do allow load of resources not present in the offline cache
|
|
|
|
// or not conforming any namespace.
|
|
|
|
// We achive that simply by adding an 'empty' - i.e. universal
|
|
|
|
// namespace of BYPASS type into the cache.
|
|
|
|
AddNamespace(nsIApplicationCacheNamespace::NAMESPACE_BYPASS,
|
|
|
|
EmptyCString(), EmptyCString());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
nsCOMPtr<nsIURI> bypassURI;
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = NS_NewURI(getter_AddRefs(bypassURI), line, nullptr, mURI);
|
2008-09-30 22:34:23 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString scheme;
|
2008-09-30 22:34:23 +04:00
|
|
|
bypassURI->GetScheme(scheme);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool equals;
|
2008-09-30 22:34:23 +04:00
|
|
|
if (NS_FAILED(mURI->SchemeIs(scheme.get(), &equals)) || !equals)
|
|
|
|
break;
|
|
|
|
if (NS_FAILED(DropReferenceFromURL(bypassURI)))
|
|
|
|
break;
|
|
|
|
nsCString spec;
|
|
|
|
if (NS_FAILED(bypassURI->GetAsciiSpec(spec)))
|
|
|
|
break;
|
|
|
|
|
|
|
|
AddNamespace(nsIApplicationCacheNamespace::NAMESPACE_BYPASS,
|
|
|
|
spec, EmptyCString());
|
2008-01-17 00:54:33 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-31 04:52:06 +04:00
|
|
|
nsresult
|
2008-01-29 10:54:54 +03:00
|
|
|
nsOfflineManifestItem::GetOldManifestContentHash(nsIRequest *aRequest)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsICachingChannel> cachingChannel = do_QueryInterface(aRequest, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-03-31 04:52:06 +04:00
|
|
|
// load the main cache token that is actually the old offline cache token and
|
2008-01-29 10:54:54 +03:00
|
|
|
// read previous manifest content hash value
|
|
|
|
nsCOMPtr<nsISupports> cacheToken;
|
|
|
|
cachingChannel->GetCacheToken(getter_AddRefs(cacheToken));
|
|
|
|
if (cacheToken) {
|
2013-09-20 13:11:25 +04:00
|
|
|
nsCOMPtr<nsICacheEntry> cacheDescriptor(do_QueryInterface(cacheToken, &rv));
|
2008-01-29 10:54:54 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-03-31 04:52:06 +04:00
|
|
|
|
2008-01-29 10:54:54 +03:00
|
|
|
rv = cacheDescriptor->GetMetaDataElement("offline-manifest-hash", getter_Copies(mOldManifestHashValue));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
mOldManifestHashValue.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-31 04:52:06 +04:00
|
|
|
nsresult
|
2008-01-29 10:54:54 +03:00
|
|
|
nsOfflineManifestItem::CheckNewManifestContentHash(nsIRequest *aRequest)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!mManifestHash) {
|
|
|
|
// Nothing to compare against...
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString newManifestHashValue;
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = mManifestHash->Finish(true, mManifestHashValue);
|
2012-07-30 18:20:58 +04:00
|
|
|
mManifestHash = nullptr;
|
2008-01-29 10:54:54 +03:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(("Could not finish manifest hash, rv=%08x", rv));
|
|
|
|
// This is not critical error
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ParseSucceeded()) {
|
|
|
|
// Parsing failed, the hash is not valid
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-11-06 03:01:07 +03:00
|
|
|
if (mOldManifestHashValue == mManifestHashValue) {
|
2008-01-29 10:54:54 +03:00
|
|
|
LOG(("Update not needed, downloaded manifest content is byte-for-byte identical"));
|
2011-10-17 18:59:28 +04:00
|
|
|
mNeedsUpdate = false;
|
2008-01-29 10:54:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store the manifest content hash value to the new
|
|
|
|
// offline cache token
|
|
|
|
nsCOMPtr<nsICachingChannel> cachingChannel = do_QueryInterface(aRequest, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> cacheToken;
|
|
|
|
cachingChannel->GetOfflineCacheToken(getter_AddRefs(cacheToken));
|
|
|
|
if (cacheToken) {
|
2013-09-20 13:11:25 +04:00
|
|
|
nsCOMPtr<nsICacheEntry> cacheDescriptor(do_QueryInterface(cacheToken, &rv));
|
2008-01-29 10:54:54 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-03-31 04:52:06 +04:00
|
|
|
|
2008-11-06 03:01:07 +03:00
|
|
|
rv = cacheDescriptor->SetMetaDataElement("offline-manifest-hash", mManifestHashValue.get());
|
2008-01-29 10:54:54 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
void
|
|
|
|
nsOfflineManifestItem::ReadStrictFileOriginPolicyPref()
|
|
|
|
{
|
|
|
|
mStrictFileOriginPolicy =
|
2011-09-29 10:19:26 +04:00
|
|
|
Preferences::GetBool("security.fileuri.strict_origin_policy", true);
|
2008-09-30 22:34:23 +04:00
|
|
|
}
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineManifestItem::OnStartRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannel> channel = do_QueryInterface(aRequest, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool succeeded;
|
2008-01-17 00:54:33 +03:00
|
|
|
rv = channel->GetRequestSucceeded(&succeeded);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!succeeded) {
|
|
|
|
LOG(("HTTP request failed"));
|
2012-11-03 20:17:47 +04:00
|
|
|
LogToConsole("Offline cache manifest HTTP request failed", this);
|
2008-01-17 00:54:33 +03:00
|
|
|
mParserState = PARSE_ERROR;
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2008-01-29 10:54:54 +03:00
|
|
|
rv = GetOldManifestContentHash(aRequest);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
return nsOfflineCacheUpdateItem::OnStartRequest(aRequest, aContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineManifestItem::OnDataAvailable(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsIInputStream *aStream,
|
2012-09-06 06:41:02 +04:00
|
|
|
uint64_t aOffset,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aCount)
|
2008-01-17 00:54:33 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesRead = 0;
|
2008-01-17 00:54:33 +03:00
|
|
|
aStream->ReadSegments(ReadManifest, this, aCount, &bytesRead);
|
|
|
|
mBytesRead += bytesRead;
|
|
|
|
|
|
|
|
if (mParserState == PARSE_ERROR) {
|
|
|
|
LOG(("OnDataAvailable is canceling the request due a parse error\n"));
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("loaded %u bytes into offline cache [offset=%u]\n",
|
|
|
|
bytesRead, aOffset));
|
|
|
|
|
|
|
|
// All the parent method does is read and discard, don't bother
|
|
|
|
// chaining up.
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineManifestItem::OnStopRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
|
|
|
if (mBytesRead == 0) {
|
2013-01-21 12:38:40 +04:00
|
|
|
// We didn't need to read (because LOAD_ONLY_IF_MODIFIED was
|
|
|
|
// specified).
|
2011-10-17 18:59:28 +04:00
|
|
|
mNeedsUpdate = false;
|
2008-01-29 10:54:54 +03:00
|
|
|
} else {
|
2013-01-21 12:38:40 +04:00
|
|
|
// Handle any leftover manifest data.
|
|
|
|
nsCString::const_iterator begin, end;
|
|
|
|
mReadBuf.BeginReading(begin);
|
|
|
|
mReadBuf.EndReading(end);
|
|
|
|
nsresult rv = HandleManifestLine(begin, end);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-01-29 10:54:54 +03:00
|
|
|
rv = CheckNewManifestContentHash(aRequest);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-01-17 00:54:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nsOfflineCacheUpdateItem::OnStopRequest(aRequest, aContext, aStatus);
|
|
|
|
}
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineCacheUpdate::nsISupports
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsOfflineCacheUpdate,
|
|
|
|
nsIOfflineCacheUpdateObserver,
|
|
|
|
nsIOfflineCacheUpdate,
|
|
|
|
nsIRunnable)
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineCacheUpdate <public>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsOfflineCacheUpdate::nsOfflineCacheUpdate()
|
|
|
|
: mState(STATE_UNINITIALIZED)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mAddedItems(false)
|
|
|
|
, mPartialUpdate(false)
|
2012-10-02 17:55:00 +04:00
|
|
|
, mOnlyCheckUpdate(false)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mSucceeded(true)
|
|
|
|
, mObsolete(false)
|
2012-06-04 18:12:23 +04:00
|
|
|
, mItemsInProgress(0)
|
2008-11-06 03:01:07 +03:00
|
|
|
, mRescheduleCount(0)
|
2012-03-31 04:52:06 +04:00
|
|
|
, mPinnedEntryRetriesCount(0)
|
|
|
|
, mPinned(false)
|
2016-01-12 21:16:59 +03:00
|
|
|
, mByteProgress(0)
|
2007-07-25 09:35:39 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsOfflineCacheUpdate::~nsOfflineCacheUpdate()
|
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheUpdate::~nsOfflineCacheUpdate [%p]", this));
|
|
|
|
}
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
/* static */
|
|
|
|
nsresult
|
|
|
|
nsOfflineCacheUpdate::GetCacheKey(nsIURI *aURI, nsACString &aKey)
|
|
|
|
{
|
|
|
|
aKey.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> newURI;
|
2011-05-22 05:12:46 +04:00
|
|
|
nsresult rv = aURI->CloneIgnoringRef(getter_AddRefs(newURI));
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = newURI->GetAsciiSpec(aKey);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
nsresult
|
2015-10-02 19:13:52 +03:00
|
|
|
nsOfflineCacheUpdate::InitInternal(nsIURI *aManifestURI,
|
|
|
|
nsIPrincipal* aLoadingPrincipal)
|
2007-07-25 09:35:39 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
// Only http and https applications are supported.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool match;
|
2008-01-17 00:54:33 +03:00
|
|
|
rv = aManifestURI->SchemeIs("http", &match);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!match) {
|
|
|
|
rv = aManifestURI->SchemeIs("https", &match);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!match)
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
mManifestURI = aManifestURI;
|
2015-10-02 19:13:52 +03:00
|
|
|
mLoadingPrincipal = aLoadingPrincipal;
|
2008-01-17 00:54:33 +03:00
|
|
|
|
2008-01-23 07:31:50 +03:00
|
|
|
rv = mManifestURI->GetAsciiHost(mUpdateDomain);
|
2008-01-17 00:54:33 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-10-02 17:55:00 +04:00
|
|
|
mPartialUpdate = false;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsOfflineCacheUpdate::Init(nsIURI *aManifestURI,
|
|
|
|
nsIURI *aDocumentURI,
|
2015-10-02 19:13:52 +03:00
|
|
|
nsIPrincipal* aLoadingPrincipal,
|
2012-10-02 17:55:00 +04:00
|
|
|
nsIDOMDocument *aDocument,
|
2016-01-18 22:20:08 +03:00
|
|
|
nsIFile *aCustomProfileDir)
|
2012-10-02 17:55:00 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Make sure the service has been initialized
|
|
|
|
nsOfflineCacheUpdateService* service =
|
|
|
|
nsOfflineCacheUpdateService::EnsureService();
|
|
|
|
if (!service)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
LOG(("nsOfflineCacheUpdate::Init [%p]", this));
|
|
|
|
|
2015-10-02 19:13:52 +03:00
|
|
|
rv = InitInternal(aManifestURI, aLoadingPrincipal);
|
2012-10-02 17:55:00 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
nsCOMPtr<nsIApplicationCacheService> cacheService =
|
|
|
|
do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv);
|
2007-07-25 09:35:39 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2016-01-18 22:20:08 +03:00
|
|
|
nsAutoCString originSuffix;
|
|
|
|
rv = aLoadingPrincipal->GetOriginSuffix(originSuffix);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-31 10:36:00 +04:00
|
|
|
mDocumentURI = aDocumentURI;
|
|
|
|
|
2012-06-04 18:12:24 +04:00
|
|
|
if (aCustomProfileDir) {
|
2016-01-18 22:20:08 +03:00
|
|
|
rv = cacheService->BuildGroupIDForSuffix(aManifestURI, originSuffix, mGroupID);
|
2012-07-31 10:36:00 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-06-04 18:12:24 +04:00
|
|
|
// Create only a new offline application cache in the custom profile
|
|
|
|
// This is a preload of a new cache.
|
2007-07-25 09:35:39 +04:00
|
|
|
|
2012-06-04 18:12:24 +04:00
|
|
|
// XXX Custom updates don't support "updating" of an existing cache
|
|
|
|
// in the custom profile at the moment. This support can be, though,
|
|
|
|
// simply added as well when needed.
|
2012-07-30 18:20:58 +04:00
|
|
|
mPreviousApplicationCache = nullptr;
|
2012-06-04 18:12:24 +04:00
|
|
|
|
2012-07-31 10:36:00 +04:00
|
|
|
rv = cacheService->CreateCustomApplicationCache(mGroupID,
|
2012-06-04 18:12:24 +04:00
|
|
|
aCustomProfileDir,
|
|
|
|
kCustomProfileQuota,
|
|
|
|
getter_AddRefs(mApplicationCache));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mCustomProfileDir = aCustomProfileDir;
|
|
|
|
}
|
|
|
|
else {
|
2016-01-18 22:20:08 +03:00
|
|
|
rv = cacheService->BuildGroupIDForSuffix(aManifestURI, originSuffix, mGroupID);
|
2012-07-31 10:36:00 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = cacheService->GetActiveCache(mGroupID,
|
2012-06-04 18:12:24 +04:00
|
|
|
getter_AddRefs(mPreviousApplicationCache));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-31 10:36:00 +04:00
|
|
|
rv = cacheService->CreateApplicationCache(mGroupID,
|
2012-06-04 18:12:24 +04:00
|
|
|
getter_AddRefs(mApplicationCache));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2008-08-25 08:51:19 +04:00
|
|
|
|
2012-03-31 04:52:06 +04:00
|
|
|
rv = nsOfflineCacheUpdateService::OfflineAppPinnedForURI(aDocumentURI,
|
2013-10-24 00:36:09 +04:00
|
|
|
nullptr,
|
2012-03-31 04:52:06 +04:00
|
|
|
&mPinned);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
mState = STATE_INITIALIZED;
|
2008-08-28 05:15:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-02 17:55:00 +04:00
|
|
|
nsresult
|
|
|
|
nsOfflineCacheUpdate::InitForUpdateCheck(nsIURI *aManifestURI,
|
2015-10-02 19:13:52 +03:00
|
|
|
nsIPrincipal* aLoadingPrincipal,
|
2012-10-02 17:55:00 +04:00
|
|
|
nsIObserver *aObserver)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Make sure the service has been initialized
|
|
|
|
nsOfflineCacheUpdateService* service =
|
|
|
|
nsOfflineCacheUpdateService::EnsureService();
|
|
|
|
if (!service)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
LOG(("nsOfflineCacheUpdate::InitForUpdateCheck [%p]", this));
|
|
|
|
|
2015-10-02 19:13:52 +03:00
|
|
|
rv = InitInternal(aManifestURI, aLoadingPrincipal);
|
2012-10-02 17:55:00 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIApplicationCacheService> cacheService =
|
|
|
|
do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2016-01-18 22:20:08 +03:00
|
|
|
nsAutoCString originSuffix;
|
|
|
|
rv = aLoadingPrincipal->GetOriginSuffix(originSuffix);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = cacheService->BuildGroupIDForSuffix(aManifestURI, originSuffix, mGroupID);
|
2012-10-02 17:55:00 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = cacheService->GetActiveCache(mGroupID,
|
|
|
|
getter_AddRefs(mPreviousApplicationCache));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// To load the manifest properly using current app cache to satisfy and
|
|
|
|
// also to compare the cached content hash value we have to set 'some'
|
|
|
|
// app cache to write to on the channel. Otherwise the cached version will
|
|
|
|
// be used and no actual network request will be made. We use the same
|
|
|
|
// app cache here. OpenChannel prevents caching in this case using
|
|
|
|
// INHIBIT_CACHING load flag.
|
|
|
|
mApplicationCache = mPreviousApplicationCache;
|
|
|
|
|
|
|
|
rv = nsOfflineCacheUpdateService::OfflineAppPinnedForURI(aManifestURI,
|
2013-10-24 00:36:09 +04:00
|
|
|
nullptr,
|
2012-10-02 17:55:00 +04:00
|
|
|
&mPinned);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mUpdateAvailableObserver = aObserver;
|
|
|
|
mOnlyCheckUpdate = true;
|
|
|
|
|
|
|
|
mState = STATE_INITIALIZED;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-08-28 05:15:32 +04:00
|
|
|
nsresult
|
|
|
|
nsOfflineCacheUpdate::InitPartial(nsIURI *aManifestURI,
|
|
|
|
const nsACString& clientID,
|
2015-10-02 19:13:52 +03:00
|
|
|
nsIURI *aDocumentURI,
|
|
|
|
nsIPrincipal *aLoadingPrincipal)
|
2008-08-28 05:15:32 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Make sure the service has been initialized
|
|
|
|
nsOfflineCacheUpdateService* service =
|
|
|
|
nsOfflineCacheUpdateService::EnsureService();
|
|
|
|
if (!service)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
LOG(("nsOfflineCacheUpdate::InitPartial [%p]", this));
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mPartialUpdate = true;
|
2008-08-28 05:15:32 +04:00
|
|
|
mDocumentURI = aDocumentURI;
|
2015-10-02 19:13:52 +03:00
|
|
|
mLoadingPrincipal = aLoadingPrincipal;
|
2007-07-25 09:35:39 +04:00
|
|
|
|
2008-08-28 05:15:32 +04:00
|
|
|
mManifestURI = aManifestURI;
|
|
|
|
rv = mManifestURI->GetAsciiHost(mUpdateDomain);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIApplicationCacheService> cacheService =
|
|
|
|
do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-11 22:20:17 +04:00
|
|
|
rv = cacheService->GetApplicationCache(clientID,
|
2008-08-28 05:15:32 +04:00
|
|
|
getter_AddRefs(mApplicationCache));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!mApplicationCache) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString manifestSpec;
|
2008-08-28 05:15:32 +04:00
|
|
|
rv = GetCacheKey(mManifestURI, manifestSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = cacheService->CreateApplicationCache
|
|
|
|
(manifestSpec, getter_AddRefs(mApplicationCache));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2012-07-31 10:36:00 +04:00
|
|
|
rv = mApplicationCache->GetManifestURI(getter_AddRefs(mManifestURI));
|
2008-08-28 05:15:32 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-31 10:36:00 +04:00
|
|
|
nsAutoCString groupID;
|
|
|
|
rv = mApplicationCache->GetGroupID(groupID);
|
2008-08-28 05:15:32 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-03-31 04:52:06 +04:00
|
|
|
rv = nsOfflineCacheUpdateService::OfflineAppPinnedForURI(aDocumentURI,
|
2013-10-24 00:36:09 +04:00
|
|
|
nullptr,
|
2012-03-31 04:52:06 +04:00
|
|
|
&mPinned);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-08-28 05:15:32 +04:00
|
|
|
mState = STATE_INITIALIZED;
|
2007-07-25 09:35:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsOfflineCacheUpdate::HandleManifest(bool *aDoUpdate)
|
2008-01-17 00:54:33 +03:00
|
|
|
{
|
|
|
|
// Be pessimistic
|
2011-10-17 18:59:28 +04:00
|
|
|
*aDoUpdate = false;
|
2008-01-17 00:54:33 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool succeeded;
|
2009-02-27 17:09:06 +03:00
|
|
|
nsresult rv = mManifestItem->GetRequestSucceeded(&succeeded);
|
2008-01-17 00:54:33 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2009-02-27 17:09:06 +03:00
|
|
|
if (!succeeded || !mManifestItem->ParseSucceeded()) {
|
2008-01-17 00:54:33 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mManifestItem->NeedsUpdate()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add items requested by the manifest.
|
|
|
|
const nsCOMArray<nsIURI> &manifestURIs = mManifestItem->GetExplicitURIs();
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < manifestURIs.Count(); i++) {
|
2008-08-27 03:09:02 +04:00
|
|
|
rv = AddURI(manifestURIs[i], nsIApplicationCache::ITEM_EXPLICIT);
|
2008-01-17 00:54:33 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2016-07-27 14:08:00 +03:00
|
|
|
const nsCOMArray<nsIURI> &anonURIs = mManifestItem->GetAnonymousURIs();
|
|
|
|
for (int32_t i = 0; i < anonURIs.Count(); i++) {
|
|
|
|
rv = AddURI(anonURIs[i], nsIApplicationCache::ITEM_EXPLICIT,
|
|
|
|
nsIRequest::LOAD_ANONYMOUS);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
const nsCOMArray<nsIURI> &fallbackURIs = mManifestItem->GetFallbackURIs();
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < fallbackURIs.Count(); i++) {
|
2008-09-30 22:34:23 +04:00
|
|
|
rv = AddURI(fallbackURIs[i], nsIApplicationCache::ITEM_FALLBACK);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
// The document that requested the manifest is implicitly included
|
|
|
|
// as part of that manifest update.
|
2008-08-27 03:09:02 +04:00
|
|
|
rv = AddURI(mDocumentURI, nsIApplicationCache::ITEM_IMPLICIT);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Add items previously cached implicitly
|
|
|
|
rv = AddExistingItems(nsIApplicationCache::ITEM_IMPLICIT);
|
2008-01-17 00:54:33 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Add items requested by the script API
|
2008-08-27 03:09:02 +04:00
|
|
|
rv = AddExistingItems(nsIApplicationCache::ITEM_DYNAMIC);
|
2008-01-17 00:54:33 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
// Add opportunistically cached items conforming current opportunistic
|
|
|
|
// namespace list
|
|
|
|
rv = AddExistingItems(nsIApplicationCache::ITEM_OPPORTUNISTIC,
|
|
|
|
&mManifestItem->GetOpportunisticNamespaces());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
*aDoUpdate = true;
|
2008-01-17 00:54:33 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-02 17:55:00 +04:00
|
|
|
bool
|
|
|
|
nsOfflineCacheUpdate::CheckUpdateAvailability()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
bool succeeded;
|
|
|
|
rv = mManifestItem->GetRequestSucceeded(&succeeded);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
if (!succeeded || !mManifestItem->ParseSucceeded()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mPinned) {
|
|
|
|
uint16_t status;
|
|
|
|
rv = mManifestItem->GetStatus(&status);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
// Treat these as there would be an update available,
|
|
|
|
// since this is indication of demand to remove this
|
|
|
|
// offline cache.
|
|
|
|
if (status == 404 || status == 410) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mManifestItem->NeedsUpdate();
|
|
|
|
}
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
void
|
2012-06-04 18:12:23 +04:00
|
|
|
nsOfflineCacheUpdate::LoadCompleted(nsOfflineCacheUpdateItem *aItem)
|
2007-07-25 09:35:39 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2012-06-04 18:12:23 +04:00
|
|
|
LOG(("nsOfflineCacheUpdate::LoadCompleted [%p]", this));
|
|
|
|
|
|
|
|
if (mState == STATE_FINISHED) {
|
|
|
|
LOG((" after completion, ignoring"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-14 10:09:23 +04:00
|
|
|
// Keep the object alive through a Finish() call.
|
|
|
|
nsCOMPtr<nsIOfflineCacheUpdate> kungFuDeathGrip(this);
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
if (mState == STATE_CANCELLED) {
|
2012-12-19 01:41:15 +04:00
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_ERROR);
|
2008-01-17 00:54:33 +03:00
|
|
|
Finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == STATE_CHECKING) {
|
|
|
|
// Manifest load finished.
|
|
|
|
|
2012-10-02 17:55:00 +04:00
|
|
|
if (mOnlyCheckUpdate) {
|
|
|
|
Finish();
|
|
|
|
NotifyUpdateAvailability(CheckUpdateAvailability());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
NS_ASSERTION(mManifestItem,
|
|
|
|
"Must have a manifest item in STATE_CHECKING.");
|
2012-06-04 18:12:23 +04:00
|
|
|
NS_ASSERTION(mManifestItem == aItem,
|
|
|
|
"Unexpected aItem in nsOfflineCacheUpdate::LoadCompleted");
|
2008-01-17 00:54:33 +03:00
|
|
|
|
2008-11-04 21:59:12 +03:00
|
|
|
// A 404 or 410 is interpreted as an intentional removal of
|
|
|
|
// the manifest file, rather than a transient server error.
|
|
|
|
// Obsolete this cache group if one of these is returned.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t status;
|
2008-11-04 21:59:12 +03:00
|
|
|
rv = mManifestItem->GetStatus(&status);
|
|
|
|
if (status == 404 || status == 410) {
|
2012-11-03 20:17:47 +04:00
|
|
|
LogToConsole("Offline cache manifest removed, cache cleared", mManifestItem);
|
2011-10-17 18:59:28 +04:00
|
|
|
mSucceeded = false;
|
2009-02-10 09:40:40 +03:00
|
|
|
if (mPreviousApplicationCache) {
|
2012-03-31 04:52:06 +04:00
|
|
|
if (mPinned) {
|
|
|
|
// Do not obsolete a pinned application.
|
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_NOUPDATE);
|
|
|
|
} else {
|
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_OBSOLETE);
|
|
|
|
mObsolete = true;
|
|
|
|
}
|
2009-02-10 09:40:40 +03:00
|
|
|
} else {
|
2010-10-20 21:12:32 +04:00
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_ERROR);
|
2012-03-31 04:52:06 +04:00
|
|
|
mObsolete = true;
|
2009-02-10 09:40:40 +03:00
|
|
|
}
|
2008-11-04 21:59:12 +03:00
|
|
|
Finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool doUpdate;
|
2008-01-17 00:54:33 +03:00
|
|
|
if (NS_FAILED(HandleManifest(&doUpdate))) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mSucceeded = false;
|
2010-10-20 21:12:32 +04:00
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_ERROR);
|
2008-01-17 00:54:33 +03:00
|
|
|
Finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!doUpdate) {
|
2012-11-03 20:17:47 +04:00
|
|
|
LogToConsole("Offline cache doesn't need to update", mManifestItem);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mSucceeded = false;
|
2009-02-22 22:09:10 +03:00
|
|
|
|
2010-10-20 21:12:32 +04:00
|
|
|
AssociateDocuments(mPreviousApplicationCache);
|
2009-02-22 22:09:10 +03:00
|
|
|
|
2008-11-06 03:01:08 +03:00
|
|
|
ScheduleImplicit();
|
2009-02-22 22:09:10 +03:00
|
|
|
|
|
|
|
// If we didn't need an implicit update, we can
|
|
|
|
// send noupdate and end the update now.
|
|
|
|
if (!mImplicitUpdate) {
|
2010-10-20 21:12:32 +04:00
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_NOUPDATE);
|
2009-02-22 22:09:10 +03:00
|
|
|
Finish();
|
|
|
|
}
|
2008-01-17 00:54:33 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
rv = mApplicationCache->MarkEntry(mManifestItem->mCacheKey,
|
|
|
|
mManifestItem->mItemType);
|
|
|
|
if (NS_FAILED(rv)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mSucceeded = false;
|
2010-10-20 21:12:32 +04:00
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_ERROR);
|
2008-08-27 03:09:02 +04:00
|
|
|
Finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
mState = STATE_DOWNLOADING;
|
2010-10-20 21:12:32 +04:00
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_DOWNLOADING);
|
2008-01-17 00:54:33 +03:00
|
|
|
|
|
|
|
// Start fetching resources.
|
|
|
|
ProcessNextURI();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normal load finished.
|
2012-06-04 18:12:23 +04:00
|
|
|
if (mItemsInProgress) // Just to be safe here!
|
|
|
|
--mItemsInProgress;
|
2007-07-25 10:31:27 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool succeeded;
|
2012-06-04 18:12:23 +04:00
|
|
|
rv = aItem->GetRequestSucceeded(&succeeded);
|
2007-07-25 10:31:27 +04:00
|
|
|
|
2012-06-04 18:12:23 +04:00
|
|
|
if (mPinned && NS_SUCCEEDED(rv) && succeeded) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t dummy_cache_type;
|
2012-06-04 18:12:23 +04:00
|
|
|
rv = mApplicationCache->GetTypes(aItem->mCacheKey, &dummy_cache_type);
|
2012-03-31 04:52:06 +04:00
|
|
|
bool item_doomed = NS_FAILED(rv); // can not find it? -> doomed
|
|
|
|
|
|
|
|
if (item_doomed &&
|
|
|
|
mPinnedEntryRetriesCount < kPinnedEntryRetriesLimit &&
|
2012-06-04 18:12:23 +04:00
|
|
|
(aItem->mItemType & (nsIApplicationCache::ITEM_EXPLICIT |
|
|
|
|
nsIApplicationCache::ITEM_FALLBACK))) {
|
2012-04-01 02:18:08 +04:00
|
|
|
rv = EvictOneNonPinned();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mSucceeded = false;
|
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_ERROR);
|
|
|
|
Finish();
|
|
|
|
return;
|
|
|
|
}
|
2012-03-31 04:52:06 +04:00
|
|
|
|
2012-06-04 18:12:23 +04:00
|
|
|
// This reverts the item state to UNINITIALIZED that makes it to
|
|
|
|
// be scheduled for download again.
|
|
|
|
rv = aItem->Cancel();
|
2012-04-01 02:18:08 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mSucceeded = false;
|
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_ERROR);
|
|
|
|
Finish();
|
|
|
|
return;
|
2012-03-31 04:52:06 +04:00
|
|
|
}
|
|
|
|
|
2012-04-01 02:18:08 +04:00
|
|
|
mPinnedEntryRetriesCount++;
|
2012-06-04 18:12:23 +04:00
|
|
|
|
2012-11-03 20:17:47 +04:00
|
|
|
LogToConsole("An unpinned offline cache deleted");
|
|
|
|
|
2012-06-04 18:12:23 +04:00
|
|
|
// Retry this item.
|
2012-04-01 02:18:08 +04:00
|
|
|
ProcessNextURI();
|
|
|
|
return;
|
2012-03-31 04:52:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 18:12:23 +04:00
|
|
|
// According to parallelism this may imply more pinned retries count,
|
|
|
|
// but that is not critical, since at one moment the algoritm will
|
|
|
|
// stop anyway. Also, this code may soon be completely removed
|
|
|
|
// after we have a separate storage for pinned apps.
|
2012-03-31 04:52:06 +04:00
|
|
|
mPinnedEntryRetriesCount = 0;
|
|
|
|
|
2009-02-27 17:09:06 +03:00
|
|
|
// Check for failures. 3XX, 4XX and 5XX errors on items explicitly
|
2008-10-20 08:12:25 +04:00
|
|
|
// listed in the manifest will cause the update to fail.
|
2009-02-27 17:09:06 +03:00
|
|
|
if (NS_FAILED(rv) || !succeeded) {
|
2012-06-04 18:12:23 +04:00
|
|
|
if (aItem->mItemType &
|
2008-10-20 08:12:25 +04:00
|
|
|
(nsIApplicationCache::ITEM_EXPLICIT |
|
|
|
|
nsIApplicationCache::ITEM_FALLBACK)) {
|
2012-11-03 20:17:47 +04:00
|
|
|
LogToConsole("Offline cache manifest item failed to load", aItem);
|
2011-10-17 18:59:28 +04:00
|
|
|
mSucceeded = false;
|
2008-10-20 08:12:25 +04:00
|
|
|
}
|
|
|
|
} else {
|
2012-06-04 18:12:23 +04:00
|
|
|
rv = mApplicationCache->MarkEntry(aItem->mCacheKey, aItem->mItemType);
|
2008-10-20 08:12:25 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mSucceeded = false;
|
2008-10-20 08:12:25 +04:00
|
|
|
}
|
2007-07-25 10:31:27 +04:00
|
|
|
}
|
2007-07-25 09:35:39 +04:00
|
|
|
|
2008-10-20 08:12:25 +04:00
|
|
|
if (!mSucceeded) {
|
2010-10-20 21:12:32 +04:00
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_ERROR);
|
2008-08-27 03:09:02 +04:00
|
|
|
Finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-14 13:10:42 +04:00
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_ITEMCOMPLETED);
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
ProcessNextURI();
|
|
|
|
}
|
|
|
|
|
2008-11-06 03:01:07 +03:00
|
|
|
void
|
|
|
|
nsOfflineCacheUpdate::ManifestCheckCompleted(nsresult aStatus,
|
|
|
|
const nsCString &aManifestHash)
|
|
|
|
{
|
2009-04-14 10:09:23 +04:00
|
|
|
// Keep the object alive through a Finish() call.
|
|
|
|
nsCOMPtr<nsIOfflineCacheUpdate> kungFuDeathGrip(this);
|
|
|
|
|
2008-11-06 03:01:07 +03:00
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString firstManifestHash;
|
2008-11-06 03:01:07 +03:00
|
|
|
mManifestItem->GetManifestHash(firstManifestHash);
|
|
|
|
if (aManifestHash != firstManifestHash) {
|
2010-10-20 21:12:32 +04:00
|
|
|
LOG(("Manifest has changed during cache items download [%p]", this));
|
2012-11-03 20:17:47 +04:00
|
|
|
LogToConsole("Offline cache manifest changed during update", mManifestItem);
|
2008-11-06 03:01:07 +03:00
|
|
|
aStatus = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(aStatus)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mSucceeded = false;
|
2010-10-20 21:12:32 +04:00
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_ERROR);
|
2008-11-06 03:01:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(aStatus) && mRescheduleCount < kRescheduleLimit) {
|
2012-03-31 04:52:06 +04:00
|
|
|
// Do the final stuff but prevent notification of STATE_FINISHED.
|
2010-10-20 21:12:32 +04:00
|
|
|
// That would disconnect listeners that are responsible for document
|
|
|
|
// association after a successful update. Forwarding notifications
|
|
|
|
// from a new update through this dead update to them is absolutely
|
|
|
|
// correct.
|
|
|
|
FinishNoNotify();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsOfflineCacheUpdate> newUpdate =
|
2008-11-06 03:01:07 +03:00
|
|
|
new nsOfflineCacheUpdate();
|
2012-03-31 04:52:06 +04:00
|
|
|
// Leave aDocument argument null. Only glues and children keep
|
2010-10-20 21:12:32 +04:00
|
|
|
// document instances.
|
2015-10-02 19:13:52 +03:00
|
|
|
newUpdate->Init(mManifestURI, mDocumentURI, mLoadingPrincipal, nullptr,
|
2016-01-18 22:20:08 +03:00
|
|
|
mCustomProfileDir);
|
2010-10-20 21:12:32 +04:00
|
|
|
|
|
|
|
// In a rare case the manifest will not be modified on the next refetch
|
|
|
|
// transfer all master document URIs to the new update to ensure that
|
|
|
|
// all documents refering it will be properly cached.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < mDocumentURIs.Count(); i++) {
|
2010-10-20 21:12:32 +04:00
|
|
|
newUpdate->StickDocument(mDocumentURIs[i]);
|
2008-11-06 03:01:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
newUpdate->mRescheduleCount = mRescheduleCount + 1;
|
2011-10-17 18:59:28 +04:00
|
|
|
newUpdate->AddObserver(this, false);
|
2008-11-06 03:01:07 +03:00
|
|
|
newUpdate->Schedule();
|
|
|
|
}
|
2010-10-20 21:12:32 +04:00
|
|
|
else {
|
2012-11-03 20:17:47 +04:00
|
|
|
LogToConsole("Offline cache update done", mManifestItem);
|
2010-10-20 21:12:32 +04:00
|
|
|
Finish();
|
|
|
|
}
|
2008-11-06 03:01:07 +03:00
|
|
|
}
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
nsresult
|
|
|
|
nsOfflineCacheUpdate::Begin()
|
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheUpdate::Begin [%p]", this));
|
|
|
|
|
2009-04-14 10:09:23 +04:00
|
|
|
// Keep the object alive through a ProcessNextURI()/Finish() call.
|
|
|
|
nsCOMPtr<nsIOfflineCacheUpdate> kungFuDeathGrip(this);
|
|
|
|
|
2012-06-04 18:12:23 +04:00
|
|
|
mItemsInProgress = 0;
|
2008-01-17 00:54:33 +03:00
|
|
|
|
2013-05-10 18:16:56 +04:00
|
|
|
if (mState == STATE_CANCELLED) {
|
2016-05-05 11:45:00 +03:00
|
|
|
nsresult rv = NS_DispatchToMainThread(NewRunnableMethod(this,
|
|
|
|
&nsOfflineCacheUpdate::AsyncFinishWithError));
|
2013-05-10 18:16:56 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
if (mPartialUpdate) {
|
|
|
|
mState = STATE_DOWNLOADING;
|
2010-10-20 21:12:32 +04:00
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_DOWNLOADING);
|
2008-01-17 00:54:33 +03:00
|
|
|
ProcessNextURI();
|
|
|
|
return NS_OK;
|
2007-07-25 09:35:39 +04:00
|
|
|
}
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
// Start checking the manifest.
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2007-07-25 09:35:39 +04:00
|
|
|
|
2012-06-04 18:12:23 +04:00
|
|
|
mManifestItem = new nsOfflineManifestItem(mManifestURI,
|
2008-08-27 03:09:02 +04:00
|
|
|
mDocumentURI,
|
2015-10-02 19:13:52 +03:00
|
|
|
mLoadingPrincipal,
|
2012-06-04 18:12:24 +04:00
|
|
|
mApplicationCache,
|
2012-07-11 22:20:17 +04:00
|
|
|
mPreviousApplicationCache);
|
2008-01-17 00:54:33 +03:00
|
|
|
if (!mManifestItem) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
mState = STATE_CHECKING;
|
2012-05-23 00:12:40 +04:00
|
|
|
mByteProgress = 0;
|
2010-10-20 21:12:32 +04:00
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_CHECKING);
|
2008-01-17 00:54:33 +03:00
|
|
|
|
2012-06-04 18:12:23 +04:00
|
|
|
nsresult rv = mManifestItem->OpenChannel(this);
|
2008-01-17 00:54:33 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-06-04 18:12:23 +04:00
|
|
|
LoadCompleted(mManifestItem);
|
2008-01-17 00:54:33 +03:00
|
|
|
}
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineCacheUpdate <private>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheUpdate::AddExistingItems(uint32_t aType,
|
2008-09-30 22:34:23 +04:00
|
|
|
nsTArray<nsCString>* namespaceFilter)
|
2007-07-25 09:35:39 +04:00
|
|
|
{
|
2008-08-27 03:09:02 +04:00
|
|
|
if (!mPreviousApplicationCache) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
if (namespaceFilter && namespaceFilter->Length() == 0) {
|
|
|
|
// Don't bother to walk entries when there are no namespaces
|
|
|
|
// defined.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count = 0;
|
2012-07-30 18:20:58 +04:00
|
|
|
char **keys = nullptr;
|
2008-08-27 03:09:02 +04:00
|
|
|
nsresult rv = mPreviousApplicationCache->GatherEntries(aType,
|
|
|
|
&count, &keys);
|
2007-07-25 09:35:39 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
AutoFreeArray autoFree(count, keys);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2008-09-30 22:34:23 +04:00
|
|
|
if (namespaceFilter) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool found = false;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t j = 0; j < namespaceFilter->Length() && !found; j++) {
|
2008-09-30 22:34:23 +04:00
|
|
|
found = StringBeginsWith(nsDependentCString(keys[i]),
|
|
|
|
namespaceFilter->ElementAt(j));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
if (NS_SUCCEEDED(NS_NewURI(getter_AddRefs(uri), keys[i]))) {
|
2008-08-27 03:09:02 +04:00
|
|
|
rv = AddURI(uri, aType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-07-25 09:35:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsOfflineCacheUpdate::ProcessNextURI()
|
|
|
|
{
|
2009-04-14 10:09:23 +04:00
|
|
|
// Keep the object alive through a Finish() call.
|
|
|
|
nsCOMPtr<nsIOfflineCacheUpdate> kungFuDeathGrip(this);
|
|
|
|
|
2012-06-04 18:12:23 +04:00
|
|
|
LOG(("nsOfflineCacheUpdate::ProcessNextURI [%p, inprogress=%d, numItems=%d]",
|
|
|
|
this, mItemsInProgress, mItems.Length()));
|
2007-07-25 09:35:39 +04:00
|
|
|
|
2012-10-22 21:59:07 +04:00
|
|
|
if (mState != STATE_DOWNLOADING) {
|
|
|
|
LOG((" should only be called from the DOWNLOADING state, ignoring"));
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsOfflineCacheUpdateItem * runItem = nullptr;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t completedItems = 0;
|
|
|
|
for (uint32_t i = 0; i < mItems.Length(); ++i) {
|
2012-06-04 18:12:23 +04:00
|
|
|
nsOfflineCacheUpdateItem * item = mItems[i];
|
|
|
|
|
|
|
|
if (item->IsScheduled()) {
|
|
|
|
runItem = item;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item->IsCompleted())
|
|
|
|
++completedItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (completedItems == mItems.Length()) {
|
|
|
|
LOG(("nsOfflineCacheUpdate::ProcessNextURI [%p]: all items loaded", this));
|
|
|
|
|
2008-11-06 03:01:07 +03:00
|
|
|
if (mPartialUpdate) {
|
|
|
|
return Finish();
|
|
|
|
} else {
|
|
|
|
// Verify that the manifest wasn't changed during the
|
|
|
|
// update, to prevent capturing a cache while the server
|
|
|
|
// is being updated. The check will call
|
|
|
|
// ManifestCheckCompleted() when it's done.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsManifestCheck> manifestCheck =
|
2015-10-02 19:13:52 +03:00
|
|
|
new nsManifestCheck(this, mManifestURI, mDocumentURI, mLoadingPrincipal);
|
2008-11-06 03:01:07 +03:00
|
|
|
if (NS_FAILED(manifestCheck->Begin())) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mSucceeded = false;
|
2010-10-20 21:12:32 +04:00
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_ERROR);
|
2008-11-06 03:01:07 +03:00
|
|
|
return Finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-07-25 09:35:39 +04:00
|
|
|
}
|
|
|
|
|
2012-06-04 18:12:23 +04:00
|
|
|
if (!runItem) {
|
|
|
|
LOG(("nsOfflineCacheUpdate::ProcessNextURI [%p]:"
|
|
|
|
" No more items to include in parallel load", this));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
if (LOG_ENABLED()) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString spec;
|
2012-06-04 18:12:23 +04:00
|
|
|
runItem->mURI->GetSpec(spec);
|
2007-07-25 09:35:39 +04:00
|
|
|
LOG(("%p: Opening channel for %s", this, spec.get()));
|
|
|
|
}
|
|
|
|
|
2012-06-04 18:12:23 +04:00
|
|
|
++mItemsInProgress;
|
2010-10-20 21:12:32 +04:00
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_ITEMSTARTED);
|
2008-01-17 00:54:33 +03:00
|
|
|
|
2012-06-04 18:12:23 +04:00
|
|
|
nsresult rv = runItem->OpenChannel(this);
|
2007-07-25 09:35:39 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-06-04 18:12:23 +04:00
|
|
|
LoadCompleted(runItem);
|
2007-07-25 09:35:39 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-06-04 18:12:23 +04:00
|
|
|
if (mItemsInProgress >= kParallelLoadLimit) {
|
|
|
|
LOG(("nsOfflineCacheUpdate::ProcessNextURI [%p]:"
|
|
|
|
" At parallel load limit", this));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This calls this method again via a post triggering
|
|
|
|
// a parallel item load
|
|
|
|
return NS_DispatchToCurrentThread(this);
|
2007-07-25 09:35:39 +04:00
|
|
|
}
|
|
|
|
|
2012-08-14 13:10:42 +04:00
|
|
|
void
|
2008-01-17 00:54:33 +03:00
|
|
|
nsOfflineCacheUpdate::GatherObservers(nsCOMArray<nsIOfflineCacheUpdateObserver> &aObservers)
|
2007-07-25 09:35:39 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < mWeakObservers.Count(); i++) {
|
2007-07-25 09:35:39 +04:00
|
|
|
nsCOMPtr<nsIOfflineCacheUpdateObserver> observer =
|
|
|
|
do_QueryReferent(mWeakObservers[i]);
|
|
|
|
if (observer)
|
2008-01-17 00:54:33 +03:00
|
|
|
aObservers.AppendObject(observer);
|
2007-07-25 09:35:39 +04:00
|
|
|
else
|
|
|
|
mWeakObservers.RemoveObjectAt(i--);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < mObservers.Count(); i++) {
|
2008-01-17 00:54:33 +03:00
|
|
|
aObservers.AppendObject(mObservers[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 13:10:42 +04:00
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheUpdate::NotifyState(uint32_t state)
|
2008-01-17 00:54:33 +03:00
|
|
|
{
|
2010-10-20 21:12:32 +04:00
|
|
|
LOG(("nsOfflineCacheUpdate::NotifyState [%p, %d]", this, state));
|
2008-08-27 03:09:02 +04:00
|
|
|
|
2012-11-03 20:17:47 +04:00
|
|
|
if (state == STATE_ERROR) {
|
|
|
|
LogToConsole("Offline cache update error", mManifestItem);
|
|
|
|
}
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
nsCOMArray<nsIOfflineCacheUpdateObserver> observers;
|
2012-08-14 13:10:42 +04:00
|
|
|
GatherObservers(observers);
|
2008-01-17 00:54:33 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < observers.Count(); i++) {
|
2010-10-20 21:12:32 +04:00
|
|
|
observers[i]->UpdateStateChanged(this, state);
|
2008-01-17 00:54:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-02 17:55:00 +04:00
|
|
|
void
|
|
|
|
nsOfflineCacheUpdate::NotifyUpdateAvailability(bool updateAvailable)
|
|
|
|
{
|
|
|
|
if (!mUpdateAvailableObserver)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LOG(("nsOfflineCacheUpdate::NotifyUpdateAvailability [this=%p, avail=%d]",
|
|
|
|
this, updateAvailable));
|
|
|
|
|
|
|
|
const char* topic = updateAvailable
|
|
|
|
? "offline-cache-update-available"
|
|
|
|
: "offline-cache-update-unavailable";
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserver> observer;
|
|
|
|
observer.swap(mUpdateAvailableObserver);
|
|
|
|
observer->Observe(mManifestURI, topic, nullptr);
|
|
|
|
}
|
|
|
|
|
2012-08-14 13:10:42 +04:00
|
|
|
void
|
2010-10-20 21:12:32 +04:00
|
|
|
nsOfflineCacheUpdate::AssociateDocuments(nsIApplicationCache* cache)
|
2008-01-17 00:54:33 +03:00
|
|
|
{
|
2013-11-06 19:59:04 +04:00
|
|
|
if (!cache) {
|
|
|
|
LOG(("nsOfflineCacheUpdate::AssociateDocuments bypassed"
|
|
|
|
", no cache provided [this=%p]", this));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
nsCOMArray<nsIOfflineCacheUpdateObserver> observers;
|
2012-08-14 13:10:42 +04:00
|
|
|
GatherObservers(observers);
|
2008-01-17 00:54:33 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < observers.Count(); i++) {
|
2010-10-20 21:12:32 +04:00
|
|
|
observers[i]->ApplicationCacheAvailable(cache);
|
2007-07-25 09:35:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-06 03:01:08 +03:00
|
|
|
void
|
2010-10-20 21:12:32 +04:00
|
|
|
nsOfflineCacheUpdate::StickDocument(nsIURI *aDocumentURI)
|
|
|
|
{
|
|
|
|
if (!aDocumentURI)
|
|
|
|
return;
|
2008-11-06 03:01:08 +03:00
|
|
|
|
2010-10-20 21:12:32 +04:00
|
|
|
mDocumentURIs.AppendObject(aDocumentURI);
|
2008-11-06 03:01:08 +03:00
|
|
|
}
|
|
|
|
|
2009-02-22 22:09:10 +03:00
|
|
|
void
|
|
|
|
nsOfflineCacheUpdate::SetOwner(nsOfflineCacheUpdateOwner *aOwner)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mOwner, "Tried to set cache update owner twice.");
|
2014-07-30 23:52:05 +04:00
|
|
|
mOwner = aOwner;
|
2009-02-22 22:09:10 +03:00
|
|
|
}
|
|
|
|
|
2012-07-31 10:36:00 +04:00
|
|
|
bool
|
|
|
|
nsOfflineCacheUpdate::IsForGroupID(const nsCSubstring &groupID)
|
|
|
|
{
|
|
|
|
return mGroupID == groupID;
|
|
|
|
}
|
|
|
|
|
2014-07-16 18:16:30 +04:00
|
|
|
bool
|
|
|
|
nsOfflineCacheUpdate::IsForProfile(nsIFile* aCustomProfileDir)
|
|
|
|
{
|
|
|
|
if (!mCustomProfileDir && !aCustomProfileDir)
|
|
|
|
return true;
|
|
|
|
if (!mCustomProfileDir || !aCustomProfileDir)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool equals;
|
|
|
|
nsresult rv = mCustomProfileDir->Equals(aCustomProfileDir, &equals);
|
|
|
|
|
|
|
|
return NS_SUCCEEDED(rv) && equals;
|
|
|
|
}
|
|
|
|
|
2009-02-22 22:09:10 +03:00
|
|
|
nsresult
|
|
|
|
nsOfflineCacheUpdate::UpdateFinished(nsOfflineCacheUpdate *aUpdate)
|
|
|
|
{
|
2009-04-14 10:09:23 +04:00
|
|
|
// Keep the object alive through a Finish() call.
|
|
|
|
nsCOMPtr<nsIOfflineCacheUpdate> kungFuDeathGrip(this);
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mImplicitUpdate = nullptr;
|
2009-02-22 22:09:10 +03:00
|
|
|
|
2010-10-20 21:12:32 +04:00
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_NOUPDATE);
|
2009-02-22 22:09:10 +03:00
|
|
|
Finish();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-23 00:12:40 +04:00
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheUpdate::OnByteProgress(uint64_t byteIncrement)
|
2012-05-23 00:12:40 +04:00
|
|
|
{
|
|
|
|
mByteProgress += byteIncrement;
|
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_ITEMPROGRESS);
|
|
|
|
}
|
|
|
|
|
2008-11-06 03:01:08 +03:00
|
|
|
nsresult
|
|
|
|
nsOfflineCacheUpdate::ScheduleImplicit()
|
|
|
|
{
|
2010-10-20 21:12:32 +04:00
|
|
|
if (mDocumentURIs.Count() == 0)
|
2008-11-06 03:01:08 +03:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsOfflineCacheUpdate> update = new nsOfflineCacheUpdate();
|
2008-11-06 03:01:08 +03:00
|
|
|
NS_ENSURE_TRUE(update, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString clientID;
|
2008-11-06 03:01:08 +03:00
|
|
|
if (mPreviousApplicationCache) {
|
|
|
|
rv = mPreviousApplicationCache->GetClientID(clientID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2012-07-11 22:20:17 +04:00
|
|
|
else if (mApplicationCache) {
|
|
|
|
rv = mApplicationCache->GetClientID(clientID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2008-11-06 03:01:08 +03:00
|
|
|
else {
|
2012-07-11 22:20:17 +04:00
|
|
|
NS_ERROR("Offline cache update not having set mApplicationCache?");
|
2008-11-06 03:01:08 +03:00
|
|
|
}
|
|
|
|
|
2015-10-02 19:13:52 +03:00
|
|
|
rv = update->InitPartial(mManifestURI, clientID, mDocumentURI, mLoadingPrincipal);
|
2008-11-06 03:01:08 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < mDocumentURIs.Count(); i++) {
|
2012-03-31 04:52:06 +04:00
|
|
|
rv = update->AddURI(mDocumentURIs[i],
|
2010-10-20 21:12:32 +04:00
|
|
|
nsIApplicationCache::ITEM_IMPLICIT);
|
2008-11-06 03:01:08 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2009-02-22 22:09:10 +03:00
|
|
|
update->SetOwner(this);
|
|
|
|
rv = update->Begin();
|
2008-11-06 03:01:08 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2009-02-22 22:09:10 +03:00
|
|
|
mImplicitUpdate = update;
|
|
|
|
|
2008-11-06 03:01:08 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-11-04 13:20:27 +03:00
|
|
|
nsresult
|
2010-10-20 21:12:32 +04:00
|
|
|
nsOfflineCacheUpdate::FinishNoNotify()
|
2007-07-25 09:35:39 +04:00
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheUpdate::Finish [%p]", this));
|
|
|
|
|
|
|
|
mState = STATE_FINISHED;
|
|
|
|
|
2012-10-02 17:55:00 +04:00
|
|
|
if (!mPartialUpdate && !mOnlyCheckUpdate) {
|
2007-07-25 10:31:27 +04:00
|
|
|
if (mSucceeded) {
|
2008-09-30 22:34:23 +04:00
|
|
|
nsIArray *namespaces = mManifestItem->GetNamespaces();
|
|
|
|
nsresult rv = mApplicationCache->AddNamespaces(namespaces);
|
|
|
|
if (NS_FAILED(rv)) {
|
2010-10-20 21:12:32 +04:00
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_ERROR);
|
2011-10-17 18:59:28 +04:00
|
|
|
mSucceeded = false;
|
2008-09-30 22:34:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = mApplicationCache->Activate();
|
2008-01-17 00:54:33 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2010-10-20 21:12:32 +04:00
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_ERROR);
|
2011-10-17 18:59:28 +04:00
|
|
|
mSucceeded = false;
|
2008-01-17 00:54:33 +03:00
|
|
|
}
|
2008-11-04 13:20:27 +03:00
|
|
|
|
2010-10-20 21:12:32 +04:00
|
|
|
AssociateDocuments(mApplicationCache);
|
2007-07-25 10:31:27 +04:00
|
|
|
}
|
|
|
|
|
2008-11-04 21:59:12 +03:00
|
|
|
if (mObsolete) {
|
|
|
|
nsCOMPtr<nsIApplicationCacheService> appCacheService =
|
|
|
|
do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID);
|
|
|
|
if (appCacheService) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString groupID;
|
2008-11-04 21:59:12 +03:00
|
|
|
mApplicationCache->GetGroupID(groupID);
|
|
|
|
appCacheService->DeactivateGroup(groupID);
|
|
|
|
}
|
2012-06-04 18:12:23 +04:00
|
|
|
}
|
2008-11-04 21:59:12 +03:00
|
|
|
|
2007-07-25 10:31:27 +04:00
|
|
|
if (!mSucceeded) {
|
|
|
|
// Update was not merged, mark all the loads as failures
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mItems.Length(); i++) {
|
2007-07-25 10:31:27 +04:00
|
|
|
mItems[i]->Cancel();
|
|
|
|
}
|
2008-08-27 03:09:02 +04:00
|
|
|
|
|
|
|
mApplicationCache->Discard();
|
2007-07-25 10:31:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-22 22:09:10 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
if (mOwner) {
|
|
|
|
rv = mOwner->UpdateFinished(this);
|
2014-02-06 18:16:41 +04:00
|
|
|
// mozilla::WeakPtr is missing some key features, like setting it to
|
|
|
|
// null explicitly.
|
|
|
|
mOwner = mozilla::WeakPtr<nsOfflineCacheUpdateOwner>();
|
2009-02-22 22:09:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2007-07-25 09:35:39 +04:00
|
|
|
}
|
|
|
|
|
2010-10-20 21:12:32 +04:00
|
|
|
nsresult
|
|
|
|
nsOfflineCacheUpdate::Finish()
|
|
|
|
{
|
|
|
|
nsresult rv = FinishNoNotify();
|
|
|
|
|
|
|
|
NotifyState(nsIOfflineCacheUpdateObserver::STATE_FINISHED);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-05-10 18:16:56 +04:00
|
|
|
void
|
|
|
|
nsOfflineCacheUpdate::AsyncFinishWithError()
|
|
|
|
{
|
|
|
|
NotifyState(nsOfflineCacheUpdate::STATE_ERROR);
|
|
|
|
Finish();
|
|
|
|
}
|
|
|
|
|
2012-03-31 04:52:06 +04:00
|
|
|
static nsresult
|
|
|
|
EvictOneOfCacheGroups(nsIApplicationCacheService *cacheService,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count, const char * const *groups)
|
2012-03-31 04:52:06 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), groups[i]);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsDependentCString group_name(groups[i]);
|
|
|
|
nsCOMPtr<nsIApplicationCache> cache;
|
|
|
|
rv = cacheService->GetActiveCache(group_name, getter_AddRefs(cache));
|
|
|
|
// Maybe someone in another thread or process have deleted it.
|
|
|
|
if (NS_FAILED(rv) || !cache)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool pinned;
|
|
|
|
rv = nsOfflineCacheUpdateService::OfflineAppPinnedForURI(uri,
|
2013-10-24 00:36:09 +04:00
|
|
|
nullptr,
|
2012-03-31 04:52:06 +04:00
|
|
|
&pinned);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!pinned) {
|
2012-04-01 02:18:08 +04:00
|
|
|
rv = cache->Discard();
|
|
|
|
return NS_OK;
|
2012-03-31 04:52:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2012-04-01 02:18:08 +04:00
|
|
|
nsresult
|
|
|
|
nsOfflineCacheUpdate::EvictOneNonPinned()
|
2012-03-31 04:52:06 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIApplicationCacheService> cacheService =
|
|
|
|
do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count;
|
2012-03-31 04:52:06 +04:00
|
|
|
char **groups;
|
|
|
|
rv = cacheService->GetGroupsTimeOrdered(&count, &groups);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-04-01 02:18:08 +04:00
|
|
|
rv = EvictOneOfCacheGroups(cacheService, count, groups);
|
2012-03-31 04:52:06 +04:00
|
|
|
|
|
|
|
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(count, groups);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineCacheUpdate::nsIOfflineCacheUpdate
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdate::GetUpdateDomain(nsACString &aUpdateDomain)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mState >= STATE_INITIALIZED, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
|
|
|
aUpdateDomain = mUpdateDomain;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheUpdate::GetStatus(uint16_t *aStatus)
|
2008-01-17 00:54:33 +03:00
|
|
|
{
|
|
|
|
switch (mState) {
|
|
|
|
case STATE_CHECKING :
|
2008-05-09 04:09:26 +04:00
|
|
|
*aStatus = nsIDOMOfflineResourceList::CHECKING;
|
|
|
|
return NS_OK;
|
2008-01-17 00:54:33 +03:00
|
|
|
case STATE_DOWNLOADING :
|
2008-05-09 04:09:26 +04:00
|
|
|
*aStatus = nsIDOMOfflineResourceList::DOWNLOADING;
|
|
|
|
return NS_OK;
|
2008-01-17 00:54:33 +03:00
|
|
|
default :
|
2008-05-09 04:09:26 +04:00
|
|
|
*aStatus = nsIDOMOfflineResourceList::IDLE;
|
|
|
|
return NS_OK;
|
2008-01-17 00:54:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsOfflineCacheUpdate::GetPartial(bool *aPartial)
|
2008-01-17 00:54:33 +03:00
|
|
|
{
|
2012-10-02 17:55:00 +04:00
|
|
|
*aPartial = mPartialUpdate || mOnlyCheckUpdate;
|
2008-01-17 00:54:33 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdate::GetManifestURI(nsIURI **aManifestURI)
|
2007-07-25 09:35:39 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mState >= STATE_INITIALIZED, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
NS_IF_ADDREF(*aManifestURI = mManifestURI);
|
2007-07-25 09:35:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsOfflineCacheUpdate::GetSucceeded(bool *aSucceeded)
|
2008-01-17 00:54:33 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mState == STATE_FINISHED, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
*aSucceeded = mSucceeded;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsOfflineCacheUpdate::GetIsUpgrade(bool *aIsUpgrade)
|
2008-08-27 03:09:02 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mState >= STATE_INITIALIZED, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*aIsUpgrade = (mPreviousApplicationCache != nullptr);
|
2008-08-27 03:09:02 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
nsresult
|
2016-07-27 14:08:00 +03:00
|
|
|
nsOfflineCacheUpdate::AddURI(nsIURI *aURI, uint32_t aType, uint32_t aLoadFlags)
|
2007-07-25 09:35:39 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mState >= STATE_INITIALIZED, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
if (mState >= STATE_DOWNLOADING)
|
2007-07-25 09:35:39 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
// Resource URIs must have the same scheme as the manifest.
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString scheme;
|
2008-01-17 00:54:33 +03:00
|
|
|
aURI->GetScheme(scheme);
|
2007-07-25 09:35:39 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool match;
|
2008-01-17 00:54:33 +03:00
|
|
|
if (NS_FAILED(mManifestURI->SchemeIs(scheme.get(), &match)) || !match)
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-07-25 09:35:39 +04:00
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
// Don't fetch the same URI twice.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mItems.Length(); i++) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool equals;
|
2016-07-27 14:08:00 +03:00
|
|
|
if (NS_SUCCEEDED(mItems[i]->mURI->Equals(aURI, &equals)) && equals &&
|
|
|
|
mItems[i]->mLoadFlags == aLoadFlags) {
|
2008-08-27 03:09:02 +04:00
|
|
|
// retain both types.
|
|
|
|
mItems[i]->mItemType |= aType;
|
2008-01-17 00:54:33 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsOfflineCacheUpdateItem> item =
|
2013-01-21 12:38:40 +04:00
|
|
|
new nsOfflineCacheUpdateItem(aURI,
|
2012-06-04 18:12:24 +04:00
|
|
|
mDocumentURI,
|
2015-10-02 19:13:52 +03:00
|
|
|
mLoadingPrincipal,
|
2012-06-04 18:12:24 +04:00
|
|
|
mApplicationCache,
|
2013-01-21 12:38:40 +04:00
|
|
|
mPreviousApplicationCache,
|
2016-07-27 14:08:00 +03:00
|
|
|
aType,
|
|
|
|
aLoadFlags);
|
2007-07-25 09:35:39 +04:00
|
|
|
if (!item) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
mItems.AppendElement(item);
|
2011-10-17 18:59:28 +04:00
|
|
|
mAddedItems = true;
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-01-17 00:54:33 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdate::AddDynamicURI(nsIURI *aURI)
|
|
|
|
{
|
2012-03-31 04:52:06 +04:00
|
|
|
if (GeckoProcessType_Default != XRE_GetProcessType())
|
2010-10-20 21:12:32 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
// If this is a partial update and the resource is already in the
|
|
|
|
// cache, we should only mark the entry, not fetch it again.
|
|
|
|
if (mPartialUpdate) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString key;
|
2008-08-27 03:09:02 +04:00
|
|
|
GetCacheKey(aURI, key);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t types;
|
2008-08-27 03:09:02 +04:00
|
|
|
nsresult rv = mApplicationCache->GetTypes(key, &types);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
if (!(types & nsIApplicationCache::ITEM_DYNAMIC)) {
|
|
|
|
mApplicationCache->MarkEntry
|
|
|
|
(key, nsIApplicationCache::ITEM_DYNAMIC);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return AddURI(aURI, nsIApplicationCache::ITEM_DYNAMIC);
|
2008-01-17 00:54:33 +03:00
|
|
|
}
|
|
|
|
|
2012-12-19 01:41:15 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdate::Cancel()
|
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheUpdate::Cancel [%p]", this));
|
|
|
|
|
|
|
|
if ((mState == STATE_FINISHED) || (mState == STATE_CANCELLED)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mState = STATE_CANCELLED;
|
|
|
|
mSucceeded = false;
|
|
|
|
|
|
|
|
// Cancel all running downloads
|
|
|
|
for (uint32_t i = 0; i < mItems.Length(); ++i) {
|
|
|
|
nsOfflineCacheUpdateItem * item = mItems[i];
|
|
|
|
|
|
|
|
if (item->IsInProgress())
|
|
|
|
item->Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdate::AddObserver(nsIOfflineCacheUpdateObserver *aObserver,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aHoldWeak)
|
2007-07-25 09:35:39 +04:00
|
|
|
{
|
2010-10-20 21:12:32 +04:00
|
|
|
LOG(("nsOfflineCacheUpdate::AddObserver [%p] to update [%p]", aObserver, this));
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
NS_ENSURE_TRUE(mState >= STATE_INITIALIZED, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
|
|
|
if (aHoldWeak) {
|
|
|
|
nsCOMPtr<nsIWeakReference> weakRef = do_GetWeakReference(aObserver);
|
|
|
|
mWeakObservers.AppendObject(weakRef);
|
|
|
|
} else {
|
|
|
|
mObservers.AppendObject(aObserver);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdate::RemoveObserver(nsIOfflineCacheUpdateObserver *aObserver)
|
|
|
|
{
|
2010-10-20 21:12:32 +04:00
|
|
|
LOG(("nsOfflineCacheUpdate::RemoveObserver [%p] from update [%p]", aObserver, this));
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
NS_ENSURE_TRUE(mState >= STATE_INITIALIZED, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < mWeakObservers.Count(); i++) {
|
2007-07-25 09:35:39 +04:00
|
|
|
nsCOMPtr<nsIOfflineCacheUpdateObserver> observer =
|
|
|
|
do_QueryReferent(mWeakObservers[i]);
|
|
|
|
if (observer == aObserver) {
|
|
|
|
mWeakObservers.RemoveObjectAt(i);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < mObservers.Count(); i++) {
|
2007-07-25 09:35:39 +04:00
|
|
|
if (mObservers[i] == aObserver) {
|
|
|
|
mObservers.RemoveObjectAt(i);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-23 00:12:40 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheUpdate::GetByteProgress(uint64_t * _result)
|
2012-05-23 00:12:40 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(_result);
|
|
|
|
|
|
|
|
*_result = mByteProgress;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdate::Schedule()
|
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheUpdate::Schedule [%p]", this));
|
|
|
|
|
2007-11-09 08:12:59 +03:00
|
|
|
nsOfflineCacheUpdateService* service =
|
|
|
|
nsOfflineCacheUpdateService::EnsureService();
|
2007-07-25 09:35:39 +04:00
|
|
|
|
|
|
|
if (!service) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2010-10-20 21:12:32 +04:00
|
|
|
return service->ScheduleUpdate(this);
|
2009-03-05 00:37:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-10-20 21:12:32 +04:00
|
|
|
nsOfflineCacheUpdate::UpdateStateChanged(nsIOfflineCacheUpdate *aUpdate,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aState)
|
2009-03-05 00:37:29 +03:00
|
|
|
{
|
2010-10-20 21:12:32 +04:00
|
|
|
if (aState == nsIOfflineCacheUpdateObserver::STATE_FINISHED) {
|
|
|
|
// Take the mSucceeded flag from the underlying update, we will be
|
2012-03-31 04:52:06 +04:00
|
|
|
// queried for it soon. mSucceeded of this update is false (manifest
|
2010-10-20 21:12:32 +04:00
|
|
|
// check failed) but the subsequent re-fetch update might succeed
|
2011-09-29 10:19:26 +04:00
|
|
|
bool succeeded;
|
2010-10-20 21:12:32 +04:00
|
|
|
aUpdate->GetSucceeded(&succeeded);
|
|
|
|
mSucceeded = succeeded;
|
2007-07-25 09:35:39 +04:00
|
|
|
}
|
|
|
|
|
2012-08-14 13:10:42 +04:00
|
|
|
NotifyState(aState);
|
2010-10-20 21:12:32 +04:00
|
|
|
if (aState == nsIOfflineCacheUpdateObserver::STATE_FINISHED)
|
|
|
|
aUpdate->RemoveObserver(this);
|
2007-07-25 09:35:39 +04:00
|
|
|
|
2012-08-14 13:10:42 +04:00
|
|
|
return NS_OK;
|
2008-10-20 08:12:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-10-20 21:12:32 +04:00
|
|
|
nsOfflineCacheUpdate::ApplicationCacheAvailable(nsIApplicationCache *applicationCache)
|
2008-10-20 08:12:25 +04:00
|
|
|
{
|
2012-08-14 13:10:42 +04:00
|
|
|
AssociateDocuments(applicationCache);
|
|
|
|
return NS_OK;
|
2008-10-20 08:12:25 +04:00
|
|
|
}
|
2012-06-04 18:12:23 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineCacheUpdate::nsIRunable
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdate::Run()
|
|
|
|
{
|
|
|
|
ProcessNextURI();
|
|
|
|
return NS_OK;
|
|
|
|
}
|