Bug 782351 - add volume state to stat() API. r=sicking

This commit is contained in:
Doug Turner 2012-08-14 08:53:20 -07:00
parent c8b229743e
commit dbca26a09f
7 changed files with 83 additions and 13 deletions

View File

@ -64,7 +64,7 @@ DeviceStorageRequestChild::Recv__delete__(const DeviceStorageResponseValue& aVal
{
StatStorageResponse r = aValue;
nsRefPtr<nsIDOMDeviceStorageStat> domstat = new nsDOMDeviceStorageStat(r.freeBytes(), r.totalBytes());
nsRefPtr<nsIDOMDeviceStorageStat> domstat = new nsDOMDeviceStorageStat(r.freeBytes(), r.totalBytes(), r.mountState());
jsval result = InterfaceToJsval(mRequest->GetOwner(), domstat, &NS_GET_IID(nsIDOMDeviceStorageStat));
mRequest->FireSuccess(result);
break;

View File

@ -186,7 +186,6 @@ DeviceStorageRequestParent::PostBlobSuccessEvent::CancelableRun() {
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
nsString mime;
mime.AssignWithConversion(mMimeType);
CopyASCIItoUTF16(mMimeType, mime);
nsCOMPtr<nsIDOMBlob> blob = new nsDOMFileFile(mFile->mPath, mime, mLength, mFile->mFile);
@ -317,7 +316,18 @@ DeviceStorageRequestParent::StatFileEvent::CancelableRun()
NS_DispatchToMainThread(r);
return NS_OK;
}
r = new PostStatResultEvent(mParent, diskUsage, freeSpace);
nsString state;
state.Assign(NS_LITERAL_STRING("available"));
#ifdef MOZ_WIDGET_GONK
rv = GetSDCardStatus(state);
if (NS_FAILED(rv)) {
r = new PostErrorEvent(mParent, POST_ERROR_EVENT_UNKNOWN);
NS_DispatchToMainThread(r);
return NS_OK;
}
#endif
r = new PostStatResultEvent(mParent, diskUsage, freeSpace, state);
NS_DispatchToMainThread(r);
return NS_OK;
}
@ -437,10 +447,12 @@ DeviceStorageRequestParent::PostPathResultEvent::CancelableRun()
DeviceStorageRequestParent::PostStatResultEvent::PostStatResultEvent(DeviceStorageRequestParent* aParent,
PRInt64 aFreeBytes,
PRInt64 aTotalBytes)
PRInt64 aTotalBytes,
nsAString& aState)
: CancelableRunnable(aParent)
, mFreeBytes(aFreeBytes)
, mTotalBytes(aTotalBytes)
, mState(aState)
{
}
@ -453,7 +465,7 @@ DeviceStorageRequestParent::PostStatResultEvent::CancelableRun()
{
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
StatStorageResponse response(mFreeBytes, mTotalBytes);
StatStorageResponse response(mFreeBytes, mTotalBytes, mState);
unused << mParent->Send__delete__(mParent, response);
return NS_OK;
}

View File

@ -175,11 +175,13 @@ private:
public:
PostStatResultEvent(DeviceStorageRequestParent* aParent,
PRInt64 aFreeBytes,
PRInt64 aTotalBytes);
PRInt64 aTotalBytes,
nsAString& aState);
virtual ~PostStatResultEvent();
virtual nsresult CancelableRun();
private:
PRInt64 mFreeBytes, mTotalBytes;
nsString mState;
};
protected:

View File

@ -40,6 +40,7 @@ struct StatStorageResponse
{
PRInt64 totalBytes;
PRInt64 freeBytes;
nsString mountState;
};
union DeviceStorageResponseValue

View File

@ -41,6 +41,7 @@
#undef CreateEvent
#ifdef MOZ_WIDGET_GONK
#include "nsIVolume.h"
#include "nsIVolumeService.h"
#endif
@ -346,6 +347,34 @@ DeviceStorageFile::DirectoryDiskUsage(nsIFile* aFile, PRUint64 aSoFar)
NS_IMPL_THREADSAFE_ISUPPORTS0(DeviceStorageFile)
#ifdef MOZ_WIDGET_GONK
nsresult
GetSDCardStatus(nsAString& aState) {
nsCOMPtr<nsIVolumeService> vs = do_GetService(NS_VOLUMESERVICE_CONTRACTID);
if (!vs) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIVolume> vol;
vs->GetVolumeByName(NS_LITERAL_STRING("sdcard"), getter_AddRefs(vol));
if (!vol) {
return NS_ERROR_FAILURE;
}
PRInt32 state;
nsresult rv = vol->GetState(&state);
if (NS_FAILED(rv)) {
return NS_ERROR_FAILURE;
}
if (state == nsIVolume::STATE_MOUNTED) {
aState.AssignASCII("available");
} else if (state == nsIVolume::STATE_SHARED || state == nsIVolume::STATE_SHAREDMNT) {
aState.AssignASCII("shared");
} else {
aState.AssignASCII("unavailable");
}
return NS_OK;
}
static void
RegisterForSDCardChanges(nsIObserver* aObserver)
{
@ -827,9 +856,10 @@ nsDOMDeviceStorageCursor::IPDLRelease()
class PostStatResultEvent : public nsRunnable
{
public:
PostStatResultEvent(nsRefPtr<DOMRequest>& aRequest, PRInt64 aFreeBytes, PRInt64 aTotalBytes)
PostStatResultEvent(nsRefPtr<DOMRequest>& aRequest, PRInt64 aFreeBytes, PRInt64 aTotalBytes, nsAString& aState)
: mFreeBytes(aFreeBytes)
, mTotalBytes(aTotalBytes)
, mState(aState)
{
mRequest.swap(aRequest);
}
@ -840,7 +870,7 @@ public:
{
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
nsRefPtr<nsIDOMDeviceStorageStat> domstat = new nsDOMDeviceStorageStat(mFreeBytes, mTotalBytes);
nsRefPtr<nsIDOMDeviceStorageStat> domstat = new nsDOMDeviceStorageStat(mFreeBytes, mTotalBytes, mState);
jsval result = InterfaceToJsval(mRequest->GetOwner(),
domstat,
@ -853,6 +883,7 @@ public:
private:
PRInt64 mFreeBytes, mTotalBytes;
nsString mState;
nsRefPtr<DOMRequest> mRequest;
};
@ -1038,8 +1069,17 @@ public:
NS_DispatchToMainThread(r);
return NS_OK;
}
r = new PostStatResultEvent(mRequest, diskUsage, freeSpace);
nsString state;
state.Assign(NS_LITERAL_STRING("available"));
#ifdef MOZ_WIDGET_GONK
rv = GetSDCardStatus(state);
if (NS_FAILED(rv)) {
r = new PostErrorEvent(mRequest, POST_ERROR_EVENT_UNKNOWN, mFile);
NS_DispatchToMainThread(r);
return NS_OK;
}
#endif
r = new PostStatResultEvent(mRequest, diskUsage, freeSpace, state);
NS_DispatchToMainThread(r);
return NS_OK;
}
@ -1721,9 +1761,10 @@ NS_INTERFACE_MAP_END
NS_IMPL_ADDREF(nsDOMDeviceStorageStat)
NS_IMPL_RELEASE(nsDOMDeviceStorageStat)
nsDOMDeviceStorageStat::nsDOMDeviceStorageStat(PRUint64 aFreeBytes, PRUint64 aTotalBytes)
nsDOMDeviceStorageStat::nsDOMDeviceStorageStat(PRUint64 aFreeBytes, PRUint64 aTotalBytes, nsAString& aState)
: mFreeBytes(aFreeBytes)
, mTotalBytes(aTotalBytes)
, mState(aState)
{
}
@ -1745,6 +1786,13 @@ nsDOMDeviceStorageStat::GetFreeBytes(PRUint64 *aFreeBytes)
return NS_OK;
}
NS_IMETHODIMP
nsDOMDeviceStorageStat::GetState(nsAString& aState)
{
aState.Assign(mState);
return NS_OK;
}
NS_IMETHODIMP
nsDOMDeviceStorage::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData)
{

View File

@ -118,11 +118,12 @@ public:
NS_DECL_ISUPPORTS
NS_DECL_NSIDOMDEVICESTORAGESTAT
nsDOMDeviceStorageStat(PRUint64 aFreeBytes, PRUint64 aTotalBytes);
nsDOMDeviceStorageStat(PRUint64 aFreeBytes, PRUint64 aTotalBytes, nsAString& aState);
private:
~nsDOMDeviceStorageStat();
PRUint64 mFreeBytes, mTotalBytes;
nsString mState;
};
//helpers
@ -130,5 +131,8 @@ jsval StringToJsval(nsPIDOMWindow* aWindow, nsAString& aString);
jsval nsIFileToJsval(nsPIDOMWindow* aWindow, DeviceStorageFile* aFile);
jsval InterfaceToJsval(nsPIDOMWindow* aWindow, nsISupports* aObject, const nsIID* aIID);
#ifdef MOZ_WIDGET_GONK
nsresult GetSDCardStatus(nsAString& aState);
#endif
#endif

View File

@ -4,9 +4,12 @@
#include "domstubs.idl"
[scriptable, uuid(b951ec07-d5db-42fc-bf4c-4eded202f7f5)]
[scriptable, uuid(0e22289a-469d-42c0-98dd-ae9831bd6a6d)]
interface nsIDOMDeviceStorageStat : nsISupports
{
readonly attribute PRUint64 totalBytes;
readonly attribute PRUint64 freeBytes;
// "shared", "available", "unavailable"
readonly attribute DOMString state;
};