Bug 782351 - add volume state to stat() API. r=sicking
authorDoug Turner <dougt@dougt.org>
Tue, 14 Aug 2012 08:53:20 -0700
changeset 108349 8c4e3f8254b44092d42e0d99ce5d9c3bc874f5fb
parent 108348 9b175938ba35f91bbcb389970dc64344fd2f544b
child 108350 1ffe70e0ef4da18ae510df534ff30077efe519eb
push id214
push userakeybl@mozilla.com
push dateWed, 14 Nov 2012 20:38:59 +0000
treeherdermozilla-release@c8b08ec8e1aa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssicking
bugs782351
milestone17.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 782351 - add volume state to stat() API. r=sicking
dom/devicestorage/DeviceStorageRequestChild.cpp
dom/devicestorage/DeviceStorageRequestParent.cpp
dom/devicestorage/DeviceStorageRequestParent.h
dom/devicestorage/PDeviceStorageRequest.ipdl
dom/devicestorage/nsDeviceStorage.cpp
dom/devicestorage/nsDeviceStorage.h
dom/interfaces/devicestorage/nsIDOMDeviceStorageStat.idl
--- a/dom/devicestorage/DeviceStorageRequestChild.cpp
+++ b/dom/devicestorage/DeviceStorageRequestChild.cpp
@@ -59,17 +59,17 @@ DeviceStorageRequestChild::Recv__delete_
       mRequest->FireSuccess(result);
       break;
     }
 
     case DeviceStorageResponseValue::TStatStorageResponse:
     {
       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;
     }
 
     case DeviceStorageResponseValue::TEnumerationResponse:
     {
       EnumerationResponse r = aValue;
--- a/dom/devicestorage/DeviceStorageRequestParent.cpp
+++ b/dom/devicestorage/DeviceStorageRequestParent.cpp
@@ -181,17 +181,16 @@ DeviceStorageRequestParent::PostBlobSucc
 
 DeviceStorageRequestParent::PostBlobSuccessEvent::~PostBlobSuccessEvent() {}
 
 nsresult
 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);
 
   ContentParent* cp = static_cast<ContentParent*>(mParent->Manager());
   BlobParent* actor = cp->GetOrCreateActorForBlob(blob);
 
   BlobResponse response;
@@ -312,17 +311,28 @@ DeviceStorageRequestParent::StatFileEven
   PRUint64 diskUsage = DeviceStorageFile::DirectoryDiskUsage(mFile->mFile);
   PRInt64 freeSpace = 0;
   nsresult rv = mFile->mFile->GetDiskSpaceAvailable(&freeSpace);
   if (NS_FAILED(rv)) {
     r = new PostErrorEvent(mParent, POST_ERROR_EVENT_UNKNOWN);
     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;
 }
 
 DeviceStorageRequestParent::ReadFileEvent::ReadFileEvent(DeviceStorageRequestParent* aParent,
                                                          DeviceStorageFile* aFile)
   : CancelableRunnable(aParent)
   , mFile(aFile)
@@ -432,33 +442,35 @@ DeviceStorageRequestParent::PostPathResu
 
   SuccessResponse response;
   unused <<  mParent->Send__delete__(mParent, response);
   return NS_OK;
 }
 
 DeviceStorageRequestParent::PostStatResultEvent::PostStatResultEvent(DeviceStorageRequestParent* aParent,
                                                                      PRInt64 aFreeBytes,
-                                                                     PRInt64 aTotalBytes)
+                                                                     PRInt64 aTotalBytes,
+                                                                     nsAString& aState)
   : CancelableRunnable(aParent)
   , mFreeBytes(aFreeBytes)
   , mTotalBytes(aTotalBytes)
+  , mState(aState)
 {
 }
 
 DeviceStorageRequestParent::PostStatResultEvent::~PostStatResultEvent()
 {
 }
 
 nsresult
 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;
 }
 
 
 } // namespace devicestorage
 } // namespace dom
 } // namespace mozilla
--- a/dom/devicestorage/DeviceStorageRequestParent.h
+++ b/dom/devicestorage/DeviceStorageRequestParent.h
@@ -170,21 +170,23 @@ private:
       nsString mPath;
   };
 
   class PostStatResultEvent : public CancelableRunnable
  {
     public:
       PostStatResultEvent(DeviceStorageRequestParent* aParent,
                           PRInt64 aFreeBytes,
-                          PRInt64 aTotalBytes);
+                          PRInt64 aTotalBytes,
+                          nsAString& aState);
       virtual ~PostStatResultEvent();
       virtual nsresult CancelableRun();
     private:
       PRInt64 mFreeBytes, mTotalBytes;
+      nsString mState;
    };
 
 protected:
   void AddRunnable(CancelableRunnable* aRunnable) {
     mRunnables.AppendElement(aRunnable);
   }
   void RemoveRunnable(CancelableRunnable* aRunnable) {
     mRunnables.RemoveElement(aRunnable);
--- a/dom/devicestorage/PDeviceStorageRequest.ipdl
+++ b/dom/devicestorage/PDeviceStorageRequest.ipdl
@@ -35,16 +35,17 @@ struct EnumerationResponse
 {
   DeviceStorageFileValue[] paths;
 };
 
 struct StatStorageResponse
 {
   PRInt64 totalBytes;
   PRInt64 freeBytes;
+  nsString mountState;
 };
 
 union DeviceStorageResponseValue
 {
   ErrorResponse;
   SuccessResponse;
   BlobResponse;
   EnumerationResponse;
--- a/dom/devicestorage/nsDeviceStorage.cpp
+++ b/dom/devicestorage/nsDeviceStorage.cpp
@@ -36,16 +36,17 @@
 #include "nsIObserverService.h"
 #include "GeneratedEvents.h"
 #include "mozilla/dom/PermissionMessageUtils.h"
 
 // Microsoft's API Name hackery sucks
 #undef CreateEvent
 
 #ifdef MOZ_WIDGET_GONK
+#include "nsIVolume.h"
 #include "nsIVolumeService.h"
 #endif
 
 using namespace mozilla::dom;
 using namespace mozilla::dom::devicestorage;
 
 #include "nsDirectoryServiceDefs.h"
 
@@ -341,16 +342,44 @@ DeviceStorageFile::DirectoryDiskUsage(ns
     }
   }
   return 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)
 {
   nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
   obs->AddObserver(aObserver, NS_VOLUME_STATE_CHANGED, false);
 }
 
 static void
@@ -822,42 +851,44 @@ void
 nsDOMDeviceStorageCursor::IPDLRelease()
 {
   Release();
 }
 
 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);
     }
 
   ~PostStatResultEvent() {}
 
   NS_IMETHOD Run()
   {
     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,
 				    &NS_GET_IID(nsIDOMDeviceStorageStat));
 
     mRequest->FireSuccess(result);
     mRequest = nullptr;
     return NS_OK;
   }
 
 private:
   PRInt64 mFreeBytes, mTotalBytes;
+  nsString mState;
   nsRefPtr<DOMRequest> mRequest;
 };
 
 
 class PostResultEvent : public nsRunnable
 {
 public:
   PostResultEvent(nsRefPtr<DOMRequest>& aRequest, DeviceStorageFile* aFile)
@@ -1033,18 +1064,27 @@ public:
     PRUint64 diskUsage = DeviceStorageFile::DirectoryDiskUsage(mFile->mFile);
     PRInt64 freeSpace = 0;
     nsresult rv = mFile->mFile->GetDiskSpaceAvailable(&freeSpace);
     if (NS_FAILED(rv)) {
       r = new PostErrorEvent(mRequest, POST_ERROR_EVENT_UNKNOWN, mFile);
       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;
   }
 
 private:
   nsRefPtr<DeviceStorageFile> mFile;
   nsRefPtr<DOMRequest> mRequest;
 };
@@ -1716,19 +1756,20 @@ NS_INTERFACE_MAP_BEGIN(nsDOMDeviceStorag
   NS_INTERFACE_MAP_ENTRY(nsIDOMDeviceStorageStat)
   NS_INTERFACE_MAP_ENTRY(nsISupports)
   NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(DeviceStorageStat)
 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)
 {
 }
 
 nsDOMDeviceStorageStat::~nsDOMDeviceStorageStat()
 {
 }
 
 NS_IMETHODIMP
@@ -1741,16 +1782,23 @@ nsDOMDeviceStorageStat::GetTotalBytes(PR
 NS_IMETHODIMP
 nsDOMDeviceStorageStat::GetFreeBytes(PRUint64 *aFreeBytes)
 {
   *aFreeBytes = mFreeBytes;
   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)
 {
   if (!strcmp(aTopic, "file-watcher-update")) {
 
     // data strings will have the format of
     //  reason:path
     nsDependentString data(aData);
 
--- a/dom/devicestorage/nsDeviceStorage.h
+++ b/dom/devicestorage/nsDeviceStorage.h
@@ -113,22 +113,26 @@ private:
 
 class nsDOMDeviceStorageStat MOZ_FINAL
   : public nsIDOMDeviceStorageStat
 {
 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
 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
--- a/dom/interfaces/devicestorage/nsIDOMDeviceStorageStat.idl
+++ b/dom/interfaces/devicestorage/nsIDOMDeviceStorageStat.idl
@@ -1,12 +1,15 @@
 /* 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/. */
 
 #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;
 };