Bug 861894 - Avoid apps to schedule new offline cache downloads while device free space is low. r=honzab

This commit is contained in:
Fernando Jiménez
2013-05-10 16:16:56 +02:00
parent bd99240e6a
commit 19c1bfd913
3 changed files with 51 additions and 0 deletions

View File

@@ -1778,6 +1778,16 @@ nsOfflineCacheUpdate::Begin()
mItemsInProgress = 0;
if (mState == STATE_CANCELLED) {
nsRefPtr<nsRunnableMethod<nsOfflineCacheUpdate> > errorNotification =
NS_NewRunnableMethod(this,
&nsOfflineCacheUpdate::AsyncFinishWithError);
nsresult rv = NS_DispatchToMainThread(errorNotification);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
if (mPartialUpdate) {
mState = STATE_DOWNLOADING;
NotifyState(nsIOfflineCacheUpdateObserver::STATE_DOWNLOADING);
@@ -2153,6 +2163,13 @@ nsOfflineCacheUpdate::Finish()
return rv;
}
void
nsOfflineCacheUpdate::AsyncFinishWithError()
{
NotifyState(nsOfflineCacheUpdate::STATE_ERROR);
Finish();
}
static nsresult
EvictOneOfCacheGroups(nsIApplicationCacheService *cacheService,
uint32_t count, const char * const *groups)

View File

@@ -239,6 +239,8 @@ private:
nsresult Finish();
nsresult FinishNoNotify();
void AsyncFinishWithError();
// Find one non-pinned cache group and evict it.
nsresult EvictOneNonPinned();
@@ -355,6 +357,7 @@ private:
bool mDisabled;
bool mUpdateRunning;
bool mLowFreeSpace;
};
#endif

View File

@@ -46,6 +46,7 @@
#include "nsIAsyncVerifyRedirectCallback.h"
#include "mozilla/Preferences.h"
#include "mozilla/Attributes.h"
#include "nsIDiskSpaceWatcher.h"
using namespace mozilla;
@@ -261,6 +262,7 @@ NS_IMPL_ISUPPORTS3(nsOfflineCacheUpdateService,
nsOfflineCacheUpdateService::nsOfflineCacheUpdateService()
: mDisabled(false)
, mUpdateRunning(false)
, mLowFreeSpace(false)
{
}
@@ -288,6 +290,19 @@ nsOfflineCacheUpdateService::Init()
true);
NS_ENSURE_SUCCESS(rv, rv);
// 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);
gOfflineCacheUpdateService = this;
return NS_OK;
@@ -408,6 +423,11 @@ nsOfflineCacheUpdateService::ProcessNextUpdate()
if (mUpdates.Length() > 0) {
mUpdateRunning = true;
// Canceling the update before Begin() call will make the update
// asynchronously finish with an error.
if (mLowFreeSpace) {
mUpdates[0]->Cancel();
}
return mUpdates[0]->Begin();
}
@@ -599,6 +619,17 @@ nsOfflineCacheUpdateService::Observe(nsISupports *aSubject,
mDisabled = true;
}
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;
}
}
return NS_OK;
}