2010-10-20 21:12:32 +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/. */
|
2010-10-20 21:12:32 +04:00
|
|
|
|
2012-10-22 21:59:07 +04:00
|
|
|
#if defined(MOZ_LOGGING)
|
|
|
|
#define FORCE_PR_LOG
|
|
|
|
#endif
|
|
|
|
|
2010-10-20 21:12:32 +04:00
|
|
|
#include "OfflineCacheUpdateChild.h"
|
|
|
|
#include "OfflineCacheUpdateParent.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#include "OfflineCacheUpdateGlue.h"
|
|
|
|
#include "nsOfflineCacheUpdate.h"
|
|
|
|
|
|
|
|
#include "nsCPrefetchService.h"
|
|
|
|
#include "nsCURILoader.h"
|
|
|
|
#include "nsIApplicationCacheContainer.h"
|
|
|
|
#include "nsIApplicationCacheChannel.h"
|
|
|
|
#include "nsIApplicationCacheService.h"
|
|
|
|
#include "nsICache.h"
|
|
|
|
#include "nsICacheService.h"
|
|
|
|
#include "nsICacheSession.h"
|
|
|
|
#include "nsICachingChannel.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocumentLoader.h"
|
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsIDOMOfflineResourceList.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsIWebProgress.h"
|
2012-07-31 10:36:00 +04:00
|
|
|
#include "nsIWebNavigation.h"
|
2010-10-20 21:12:32 +04:00
|
|
|
#include "nsICryptoHash.h"
|
|
|
|
#include "nsICacheEntryDescriptor.h"
|
|
|
|
#include "nsIPermissionManager.h"
|
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsProxyRelease.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
#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"
|
2013-05-10 18:16:56 +04:00
|
|
|
#include "nsIDiskSpaceWatcher.h"
|
2011-07-01 12:35:28 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2010-10-20 21:12:32 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
static nsOfflineCacheUpdateService *gOfflineCacheUpdateService = nullptr;
|
2010-10-20 21:12:32 +04:00
|
|
|
|
|
|
|
typedef mozilla::docshell::OfflineCacheUpdateParent OfflineCacheUpdateParent;
|
|
|
|
typedef mozilla::docshell::OfflineCacheUpdateChild OfflineCacheUpdateChild;
|
|
|
|
typedef mozilla::docshell::OfflineCacheUpdateGlue OfflineCacheUpdateGlue;
|
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
//
|
|
|
|
// To enable logging (see prlog.h for full details):
|
|
|
|
//
|
|
|
|
// set NSPR_LOG_MODULES=nsOfflineCacheUpdate:5
|
|
|
|
// set NSPR_LOG_FILE=offlineupdate.log
|
|
|
|
//
|
|
|
|
// this enables PR_LOG_ALWAYS level information and places all output in
|
|
|
|
// the file offlineupdate.log
|
|
|
|
//
|
|
|
|
PRLogModuleInfo *gOfflineCacheUpdateLog;
|
|
|
|
#endif
|
2012-10-14 01:27:31 +04:00
|
|
|
#undef LOG
|
2010-10-20 21:12:32 +04:00
|
|
|
#define LOG(args) PR_LOG(gOfflineCacheUpdateLog, 4, args)
|
|
|
|
#define LOG_ENABLED() PR_LOG_TEST(gOfflineCacheUpdateLog, 4)
|
|
|
|
|
|
|
|
class AutoFreeArray {
|
|
|
|
public:
|
2012-08-22 19:56:38 +04:00
|
|
|
AutoFreeArray(uint32_t count, char **values)
|
2010-10-20 21:12:32 +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;
|
2010-10-20 21:12:32 +04:00
|
|
|
char **mValues;
|
|
|
|
};
|
|
|
|
|
2012-10-26 15:02:47 +04:00
|
|
|
namespace { // anon
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
GetAppIDAndInBrowserFromWindow(nsIDOMWindow *aWindow,
|
|
|
|
uint32_t *aAppId,
|
|
|
|
bool *aInBrowser)
|
|
|
|
{
|
|
|
|
*aAppId = NECKO_NO_APP_ID;
|
|
|
|
*aInBrowser = false;
|
|
|
|
|
|
|
|
if (!aWindow) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-25 08:25:00 +04:00
|
|
|
nsCOMPtr<nsILoadContext> loadContext = do_GetInterface(aWindow);
|
2012-10-26 15:02:47 +04:00
|
|
|
if (!loadContext) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
rv = loadContext->GetAppId(aAppId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = loadContext->GetIsInBrowserElement(aInBrowser);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anon
|
|
|
|
|
2010-10-20 21:12:32 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineCachePendingUpdate
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2012-06-13 07:53:09 +04:00
|
|
|
class nsOfflineCachePendingUpdate MOZ_FINAL : public nsIWebProgressListener
|
|
|
|
, public nsSupportsWeakReference
|
2010-10-20 21:12:32 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIWEBPROGRESSLISTENER
|
|
|
|
|
|
|
|
nsOfflineCachePendingUpdate(nsOfflineCacheUpdateService *aService,
|
|
|
|
nsIURI *aManifestURI,
|
|
|
|
nsIURI *aDocumentURI,
|
|
|
|
nsIDOMDocument *aDocument)
|
|
|
|
: mService(aService)
|
|
|
|
, mManifestURI(aManifestURI)
|
|
|
|
, mDocumentURI(aDocumentURI)
|
2013-10-17 21:59:55 +04:00
|
|
|
, mDidReleaseThis(false)
|
2010-10-20 21:12:32 +04:00
|
|
|
{
|
|
|
|
mDocument = do_GetWeakReference(aDocument);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsOfflineCacheUpdateService> mService;
|
|
|
|
nsCOMPtr<nsIURI> mManifestURI;
|
|
|
|
nsCOMPtr<nsIURI> mDocumentURI;
|
|
|
|
nsCOMPtr<nsIWeakReference> mDocument;
|
2013-10-17 21:59:55 +04:00
|
|
|
bool mDidReleaseThis;
|
2010-10-20 21:12:32 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS2(nsOfflineCachePendingUpdate,
|
|
|
|
nsIWebProgressListener,
|
|
|
|
nsISupportsWeakReference)
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineCacheUpdateService::nsIWebProgressListener
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCachePendingUpdate::OnProgressChange(nsIWebProgress *aProgress,
|
|
|
|
nsIRequest *aRequest,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t curSelfProgress,
|
|
|
|
int32_t maxSelfProgress,
|
|
|
|
int32_t curTotalProgress,
|
|
|
|
int32_t maxTotalProgress)
|
2010-10-20 21:12:32 +04:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("notification excluded in AddProgressListener(...)");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCachePendingUpdate::OnStateChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest *aRequest,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t progressStateFlags,
|
2010-10-20 21:12:32 +04:00
|
|
|
nsresult aStatus)
|
|
|
|
{
|
2013-10-17 21:59:55 +04:00
|
|
|
if (mDidReleaseThis) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-10-20 21:12:32 +04:00
|
|
|
nsCOMPtr<nsIDOMDocument> updateDoc = do_QueryReferent(mDocument);
|
|
|
|
if (!updateDoc) {
|
|
|
|
// The document that scheduled this update has gone away,
|
|
|
|
// we don't need to listen anymore.
|
|
|
|
aWebProgress->RemoveProgressListener(this);
|
2013-10-17 21:59:55 +04:00
|
|
|
MOZ_ASSERT(!mDidReleaseThis);
|
|
|
|
mDidReleaseThis = true;
|
2010-10-20 21:12:32 +04:00
|
|
|
NS_RELEASE_THIS();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(progressStateFlags & STATE_STOP)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMWindow> window;
|
|
|
|
aWebProgress->GetDOMWindow(getter_AddRefs(window));
|
|
|
|
if (!window) return NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocument> progressDoc;
|
|
|
|
window->GetDocument(getter_AddRefs(progressDoc));
|
|
|
|
if (!progressDoc) return NS_OK;
|
|
|
|
|
|
|
|
if (!SameCOMIdentity(progressDoc, updateDoc)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("nsOfflineCachePendingUpdate::OnStateChange [%p, doc=%p]",
|
|
|
|
this, progressDoc.get()));
|
|
|
|
|
|
|
|
// Only schedule the update if the document loaded successfully
|
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
2012-10-26 15:02:47 +04:00
|
|
|
// Get extended origin attributes
|
|
|
|
uint32_t appId;
|
|
|
|
bool isInBrowserElement;
|
|
|
|
nsresult rv = GetAppIDAndInBrowserFromWindow(window, &appId, &isInBrowserElement);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-10-20 21:12:32 +04:00
|
|
|
nsCOMPtr<nsIOfflineCacheUpdate> update;
|
|
|
|
mService->Schedule(mManifestURI, mDocumentURI,
|
2012-10-26 15:02:47 +04:00
|
|
|
updateDoc, window, nullptr,
|
|
|
|
appId, isInBrowserElement, getter_AddRefs(update));
|
2013-10-17 21:59:55 +04:00
|
|
|
if (mDidReleaseThis) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-10-20 21:12:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
aWebProgress->RemoveProgressListener(this);
|
2013-10-17 21:59:55 +04:00
|
|
|
MOZ_ASSERT(!mDidReleaseThis);
|
|
|
|
mDidReleaseThis = true;
|
2010-10-20 21:12:32 +04:00
|
|
|
NS_RELEASE_THIS();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCachePendingUpdate::OnLocationChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest* aRequest,
|
2011-11-10 18:01:11 +04:00
|
|
|
nsIURI *location,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aFlags)
|
2010-10-20 21:12:32 +04:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("notification excluded in AddProgressListener(...)");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCachePendingUpdate::OnStatusChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest* aRequest,
|
|
|
|
nsresult aStatus,
|
|
|
|
const PRUnichar* aMessage)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("notification excluded in AddProgressListener(...)");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCachePendingUpdate::OnSecurityChange(nsIWebProgress *aWebProgress,
|
|
|
|
nsIRequest *aRequest,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t state)
|
2010-10-20 21:12:32 +04:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("notification excluded in AddProgressListener(...)");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineCacheUpdateService::nsISupports
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS3(nsOfflineCacheUpdateService,
|
|
|
|
nsIOfflineCacheUpdateService,
|
|
|
|
nsIObserver,
|
|
|
|
nsISupportsWeakReference)
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineCacheUpdateService <public>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsOfflineCacheUpdateService::nsOfflineCacheUpdateService()
|
2011-10-17 18:59:28 +04:00
|
|
|
: mDisabled(false)
|
|
|
|
, mUpdateRunning(false)
|
2013-05-10 18:16:56 +04:00
|
|
|
, mLowFreeSpace(false)
|
2010-10-20 21:12:32 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsOfflineCacheUpdateService::~nsOfflineCacheUpdateService()
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
gOfflineCacheUpdateService = nullptr;
|
2010-10-20 21:12:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsOfflineCacheUpdateService::Init()
|
|
|
|
{
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
if (!gOfflineCacheUpdateLog)
|
|
|
|
gOfflineCacheUpdateLog = PR_NewLogModule("nsOfflineCacheUpdate");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Observe xpcom-shutdown event
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (!observerService)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsresult rv = observerService->AddObserver(this,
|
|
|
|
NS_XPCOM_SHUTDOWN_OBSERVER_ID,
|
2011-10-17 18:59:28 +04:00
|
|
|
true);
|
2010-10-20 21:12:32 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-05-10 18:16:56 +04:00
|
|
|
// Get the current status of the disk in terms of free space and observe
|
|
|
|
// low device storage notifications.
|
|
|
|
nsCOMPtr<nsIDiskSpaceWatcher> diskSpaceWatcherService =
|
|
|
|
do_GetService("@mozilla.org/toolkit/disk-space-watcher;1");
|
|
|
|
if (diskSpaceWatcherService) {
|
|
|
|
diskSpaceWatcherService->GetIsDiskFull(&mLowFreeSpace);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Could not get disk status from nsIDiskSpaceWatcher");
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = observerService->AddObserver(this, "disk-space-watcher", false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-10-20 21:12:32 +04:00
|
|
|
gOfflineCacheUpdateService = this;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
nsOfflineCacheUpdateService *
|
|
|
|
nsOfflineCacheUpdateService::GetInstance()
|
|
|
|
{
|
|
|
|
if (!gOfflineCacheUpdateService) {
|
|
|
|
gOfflineCacheUpdateService = new nsOfflineCacheUpdateService();
|
|
|
|
if (!gOfflineCacheUpdateService)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-10-20 21:12:32 +04:00
|
|
|
NS_ADDREF(gOfflineCacheUpdateService);
|
|
|
|
nsresult rv = gOfflineCacheUpdateService->Init();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(gOfflineCacheUpdateService);
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-10-20 21:12:32 +04:00
|
|
|
}
|
|
|
|
return gOfflineCacheUpdateService;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(gOfflineCacheUpdateService);
|
|
|
|
|
|
|
|
return gOfflineCacheUpdateService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
nsOfflineCacheUpdateService *
|
|
|
|
nsOfflineCacheUpdateService::EnsureService()
|
|
|
|
{
|
|
|
|
if (!gOfflineCacheUpdateService) {
|
|
|
|
// Make the service manager hold a long-lived reference to the service
|
|
|
|
nsCOMPtr<nsIOfflineCacheUpdateService> service =
|
|
|
|
do_GetService(NS_OFFLINECACHEUPDATESERVICE_CONTRACTID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return gOfflineCacheUpdateService;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsOfflineCacheUpdateService::ScheduleUpdate(nsOfflineCacheUpdate *aUpdate)
|
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheUpdateService::Schedule [%p, update=%p]",
|
|
|
|
this, aUpdate));
|
|
|
|
|
|
|
|
aUpdate->SetOwner(this);
|
|
|
|
|
|
|
|
mUpdates.AppendElement(aUpdate);
|
|
|
|
ProcessNextUpdate();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdateService::ScheduleOnDocumentStop(nsIURI *aManifestURI,
|
|
|
|
nsIURI *aDocumentURI,
|
|
|
|
nsIDOMDocument *aDocument)
|
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheUpdateService::ScheduleOnDocumentStop [%p, manifestURI=%p, documentURI=%p doc=%p]",
|
|
|
|
this, aManifestURI, aDocumentURI, aDocument));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDocument);
|
|
|
|
nsCOMPtr<nsISupports> container = doc->GetContainer();
|
|
|
|
nsCOMPtr<nsIWebProgress> progress = do_QueryInterface(container);
|
|
|
|
NS_ENSURE_TRUE(progress, NS_ERROR_INVALID_ARG);
|
|
|
|
|
|
|
|
// Proceed with cache update
|
|
|
|
nsRefPtr<nsOfflineCachePendingUpdate> update =
|
|
|
|
new nsOfflineCachePendingUpdate(this, aManifestURI,
|
|
|
|
aDocumentURI, aDocument);
|
|
|
|
NS_ENSURE_TRUE(update, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
nsresult rv = progress->AddProgressListener
|
|
|
|
(update, nsIWebProgress::NOTIFY_STATE_DOCUMENT);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// The update will release when it has scheduled itself.
|
|
|
|
update.forget();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsOfflineCacheUpdateService::UpdateFinished(nsOfflineCacheUpdate *aUpdate)
|
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheUpdateService::UpdateFinished [%p, update=%p]",
|
|
|
|
this, aUpdate));
|
|
|
|
|
|
|
|
NS_ASSERTION(mUpdates.Length() > 0 &&
|
|
|
|
mUpdates[0] == aUpdate, "Unknown update completed");
|
|
|
|
|
|
|
|
// keep this item alive until we're done notifying observers
|
|
|
|
nsRefPtr<nsOfflineCacheUpdate> update = mUpdates[0];
|
|
|
|
mUpdates.RemoveElementAt(0);
|
2011-10-17 18:59:28 +04:00
|
|
|
mUpdateRunning = false;
|
2010-10-20 21:12:32 +04:00
|
|
|
|
|
|
|
ProcessNextUpdate();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineCacheUpdateService <private>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsOfflineCacheUpdateService::ProcessNextUpdate()
|
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheUpdateService::ProcessNextUpdate [%p, num=%d]",
|
|
|
|
this, mUpdates.Length()));
|
|
|
|
|
|
|
|
if (mDisabled)
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
|
|
|
|
if (mUpdateRunning)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
if (mUpdates.Length() > 0) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mUpdateRunning = true;
|
2013-05-10 18:16:56 +04:00
|
|
|
// Canceling the update before Begin() call will make the update
|
|
|
|
// asynchronously finish with an error.
|
|
|
|
if (mLowFreeSpace) {
|
|
|
|
mUpdates[0]->Cancel();
|
|
|
|
}
|
2010-10-20 21:12:32 +04:00
|
|
|
return mUpdates[0]->Begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineCacheUpdateService::nsIOfflineCacheUpdateService
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheUpdateService::GetNumUpdates(uint32_t *aNumUpdates)
|
2010-10-20 21:12:32 +04:00
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheUpdateService::GetNumUpdates [%p]", this));
|
|
|
|
|
|
|
|
*aNumUpdates = mUpdates.Length();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheUpdateService::GetUpdate(uint32_t aIndex,
|
2010-10-20 21:12:32 +04:00
|
|
|
nsIOfflineCacheUpdate **aUpdate)
|
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheUpdateService::GetUpdate [%p, %d]", this, aIndex));
|
|
|
|
|
|
|
|
if (aIndex < mUpdates.Length()) {
|
|
|
|
NS_ADDREF(*aUpdate = mUpdates[aIndex]);
|
|
|
|
} else {
|
2012-07-30 18:20:58 +04:00
|
|
|
*aUpdate = nullptr;
|
2010-10-20 21:12:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsOfflineCacheUpdateService::FindUpdate(nsIURI *aManifestURI,
|
2012-10-26 15:02:47 +04:00
|
|
|
uint32_t aAppID,
|
|
|
|
bool aInBrowser,
|
2010-10-20 21:12:32 +04:00
|
|
|
nsOfflineCacheUpdate **aUpdate)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2012-07-31 10:36:00 +04:00
|
|
|
nsCOMPtr<nsIApplicationCacheService> cacheService =
|
|
|
|
do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoCString groupID;
|
2012-10-26 15:02:47 +04:00
|
|
|
rv = cacheService->BuildGroupIDForApp(aManifestURI,
|
|
|
|
aAppID, aInBrowser,
|
|
|
|
groupID);
|
2012-07-31 10:36:00 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-10-20 21:12:32 +04:00
|
|
|
nsRefPtr<nsOfflineCacheUpdate> update;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mUpdates.Length(); i++) {
|
2010-10-20 21:12:32 +04:00
|
|
|
update = mUpdates[i];
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool partial;
|
2010-10-20 21:12:32 +04:00
|
|
|
rv = update->GetPartial(&partial);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (partial) {
|
|
|
|
// Partial updates aren't considered
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-07-31 10:36:00 +04:00
|
|
|
if (update->IsForGroupID(groupID)) {
|
|
|
|
update.swap(*aUpdate);
|
|
|
|
return NS_OK;
|
2010-10-20 21:12:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsOfflineCacheUpdateService::Schedule(nsIURI *aManifestURI,
|
|
|
|
nsIURI *aDocumentURI,
|
|
|
|
nsIDOMDocument *aDocument,
|
|
|
|
nsIDOMWindow* aWindow,
|
2012-06-06 06:08:30 +04:00
|
|
|
nsIFile* aCustomProfileDir,
|
2012-10-26 15:02:47 +04:00
|
|
|
uint32_t aAppID,
|
|
|
|
bool aInBrowser,
|
2010-10-20 21:12:32 +04:00
|
|
|
nsIOfflineCacheUpdate **aUpdate)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIOfflineCacheUpdate> update;
|
|
|
|
if (GeckoProcessType_Default != XRE_GetProcessType()) {
|
|
|
|
update = new OfflineCacheUpdateChild(aWindow);
|
|
|
|
}
|
2011-04-03 06:14:00 +04:00
|
|
|
else {
|
2010-10-20 21:12:32 +04:00
|
|
|
update = new OfflineCacheUpdateGlue();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2013-05-01 20:57:58 +04:00
|
|
|
if (aWindow) {
|
|
|
|
// Ensure there is window.applicationCache object that is
|
|
|
|
// responsible for association of the new applicationCache
|
|
|
|
// with the corresponding document. Just ignore the result.
|
|
|
|
nsCOMPtr<nsIDOMOfflineResourceList> appCacheWindowObject;
|
|
|
|
aWindow->GetApplicationCache(getter_AddRefs(appCacheWindowObject));
|
|
|
|
}
|
|
|
|
|
2012-07-31 10:36:00 +04:00
|
|
|
rv = update->Init(aManifestURI, aDocumentURI, aDocument,
|
2012-10-26 15:02:47 +04:00
|
|
|
aCustomProfileDir, aAppID, aInBrowser);
|
2010-10-20 21:12:32 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = update->Schedule();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ADDREF(*aUpdate = update);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdateService::ScheduleUpdate(nsIURI *aManifestURI,
|
|
|
|
nsIURI *aDocumentURI,
|
|
|
|
nsIDOMWindow *aWindow,
|
|
|
|
nsIOfflineCacheUpdate **aUpdate)
|
|
|
|
{
|
2012-10-26 15:02:47 +04:00
|
|
|
// Get extended origin attributes
|
|
|
|
uint32_t appId;
|
|
|
|
bool isInBrowser;
|
|
|
|
nsresult rv = GetAppIDAndInBrowserFromWindow(aWindow, &appId, &isInBrowser);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return Schedule(aManifestURI, aDocumentURI, nullptr, aWindow, nullptr,
|
|
|
|
appId, isInBrowser, aUpdate);
|
2012-06-04 18:12:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdateService::ScheduleCustomProfileUpdate(nsIURI *aManifestURI,
|
|
|
|
nsIURI *aDocumentURI,
|
2012-06-06 06:08:30 +04:00
|
|
|
nsIFile *aProfileDir,
|
2012-06-04 18:12:24 +04:00
|
|
|
nsIOfflineCacheUpdate **aUpdate)
|
|
|
|
{
|
|
|
|
// The profile directory is mandatory
|
|
|
|
NS_ENSURE_ARG(aProfileDir);
|
|
|
|
|
2012-10-26 15:02:47 +04:00
|
|
|
return Schedule(aManifestURI, aDocumentURI, nullptr, nullptr, aProfileDir,
|
|
|
|
NECKO_NO_APP_ID, false, aUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdateService::ScheduleAppUpdate(nsIURI *aManifestURI,
|
|
|
|
nsIURI *aDocumentURI,
|
|
|
|
uint32_t aAppID, bool aInBrowser,
|
|
|
|
nsIOfflineCacheUpdate **aUpdate)
|
|
|
|
{
|
|
|
|
return Schedule(aManifestURI, aDocumentURI, nullptr, nullptr, nullptr,
|
|
|
|
aAppID, aInBrowser, aUpdate);
|
2010-10-20 21:12:32 +04:00
|
|
|
}
|
|
|
|
|
2012-10-02 17:55:00 +04:00
|
|
|
NS_IMETHODIMP nsOfflineCacheUpdateService::CheckForUpdate(nsIURI *aManifestURI,
|
|
|
|
uint32_t aAppID,
|
|
|
|
bool aInBrowser,
|
|
|
|
nsIObserver *aObserver)
|
|
|
|
{
|
|
|
|
if (GeckoProcessType_Default != XRE_GetProcessType()) {
|
|
|
|
// Not intended to support this on child processes
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOfflineCacheUpdate> update = new OfflineCacheUpdateGlue();
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
rv = update->InitForUpdateCheck(aManifestURI, aAppID, aInBrowser, aObserver);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = update->Schedule();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-10-20 21:12:32 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineCacheUpdateService::nsIObserver
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdateService::Observe(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
|
|
|
const PRUnichar *aData)
|
|
|
|
{
|
|
|
|
if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
|
|
|
|
if (mUpdates.Length() > 0)
|
|
|
|
mUpdates[0]->Cancel();
|
2011-10-17 18:59:28 +04:00
|
|
|
mDisabled = true;
|
2010-10-20 21:12:32 +04:00
|
|
|
}
|
|
|
|
|
2013-05-10 18:16:56 +04:00
|
|
|
if (!strcmp(aTopic, "disk-space-watcher")) {
|
|
|
|
if (NS_LITERAL_STRING("full").Equals(aData)) {
|
|
|
|
mLowFreeSpace = true;
|
|
|
|
for (uint32_t i = 0; i < mUpdates.Length(); i++) {
|
|
|
|
mUpdates[i]->Cancel();
|
|
|
|
}
|
|
|
|
} else if (NS_LITERAL_STRING("free").Equals(aData)) {
|
|
|
|
mLowFreeSpace = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-20 21:12:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsOfflineCacheUpdateService::nsIOfflineCacheUpdateService
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdateService::OfflineAppAllowed(nsIPrincipal *aPrincipal,
|
|
|
|
nsIPrefBranch *aPrefBranch,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool *aAllowed)
|
2010-10-20 21:12:32 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> codebaseURI;
|
|
|
|
nsresult rv = aPrincipal->GetURI(getter_AddRefs(codebaseURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return OfflineAppAllowedForURI(codebaseURI, aPrefBranch, aAllowed);
|
|
|
|
}
|
|
|
|
|
2012-03-31 04:52:06 +04:00
|
|
|
static nsresult
|
|
|
|
OfflineAppPermForURI(nsIURI *aURI,
|
|
|
|
nsIPrefBranch *aPrefBranch,
|
|
|
|
bool pinned,
|
|
|
|
bool *aAllowed)
|
2010-10-20 21:12:32 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*aAllowed = false;
|
2010-10-20 21:12:32 +04:00
|
|
|
if (!aURI)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> innerURI = NS_GetInnermostURI(aURI);
|
|
|
|
if (!innerURI)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// only http and https applications can use offline APIs.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool match;
|
2010-10-20 21:12:32 +04:00
|
|
|
nsresult rv = innerURI->SchemeIs("http", &match);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!match) {
|
|
|
|
rv = innerURI->SchemeIs("https", &match);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!match) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPermissionManager> permissionManager =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
|
|
|
|
if (!permissionManager) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t perm;
|
2012-03-31 04:52:06 +04:00
|
|
|
const char *permName = pinned ? "pin-app" : "offline-app";
|
|
|
|
permissionManager->TestExactPermission(innerURI, permName, &perm);
|
2010-10-20 21:12:32 +04:00
|
|
|
|
2012-09-28 07:05:59 +04:00
|
|
|
if (perm == nsIPermissionManager::ALLOW_ACTION ||
|
|
|
|
perm == nsIOfflineCacheUpdateService::ALLOW_NO_WARN) {
|
2012-03-31 04:52:06 +04:00
|
|
|
*aAllowed = true;
|
2010-10-20 21:12:32 +04:00
|
|
|
}
|
|
|
|
|
2013-08-16 13:34:21 +04:00
|
|
|
// offline-apps.allow_by_default is now effective at the cache selection
|
|
|
|
// algorithm code (nsContentSink).
|
|
|
|
|
2010-10-20 21:12:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-03-31 04:52:06 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheUpdateService::OfflineAppAllowedForURI(nsIURI *aURI,
|
|
|
|
nsIPrefBranch *aPrefBranch,
|
|
|
|
bool *aAllowed)
|
|
|
|
{
|
|
|
|
return OfflineAppPermForURI(aURI, aPrefBranch, false, aAllowed);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsOfflineCacheUpdateService::OfflineAppPinnedForURI(nsIURI *aDocumentURI,
|
|
|
|
nsIPrefBranch *aPrefBranch,
|
|
|
|
bool *aPinned)
|
|
|
|
{
|
|
|
|
return OfflineAppPermForURI(aDocumentURI, aPrefBranch, true, aPinned);
|
|
|
|
}
|