diff --git a/dom/bluetooth/BluetoothOppManager.cpp b/dom/bluetooth/BluetoothOppManager.cpp index 7847e768d66d..aec315cdadfc 100644 --- a/dom/bluetooth/BluetoothOppManager.cpp +++ b/dom/bluetooth/BluetoothOppManager.cpp @@ -29,7 +29,9 @@ #include "nsIOutputStream.h" #include "nsNetUtil.h" -#define TARGET_FOLDER "/sdcard/downloads/bluetooth/" +#define TARGET_ROOT "/sdcard/" +#define TARGET_SUBDIR "downloads/bluetooth/" +#define TARGET_FOLDER TARGET_ROOT TARGET_SUBDIR USING_BLUETOOTH_NAMESPACE using namespace mozilla; @@ -561,6 +563,11 @@ BluetoothOppManager::CreateFile() */ f->GetLeafName(sFileName); + nsString fullFileName; + f->GetPath(fullFileName); + MOZ_ASSERT(StringBeginsWith(fullFileName, NS_LITERAL_STRING(TARGET_ROOT))); + nsDependentSubstring storagePath = Substring(fullFileName, strlen(TARGET_ROOT)); + mDsFile = nullptr; nsCOMPtr mimeSvc = do_GetService(NS_MIMESERVICE_CONTRACTID); @@ -570,11 +577,11 @@ BluetoothOppManager::CreateFile() if (NS_SUCCEEDED(rv)) { if (StringBeginsWith(mimeType, NS_LITERAL_CSTRING("image/"))) { - mDsFile = new DeviceStorageFile(NS_LITERAL_STRING("pictures"), f); + mDsFile = new DeviceStorageFile(NS_LITERAL_STRING("pictures"), storagePath); } else if (StringBeginsWith(mimeType, NS_LITERAL_CSTRING("video/"))) { - mDsFile = new DeviceStorageFile(NS_LITERAL_STRING("movies"), f); + mDsFile = new DeviceStorageFile(NS_LITERAL_STRING("movies"), storagePath); } else if (StringBeginsWith(mimeType, NS_LITERAL_CSTRING("audio/"))) { - mDsFile = new DeviceStorageFile(NS_LITERAL_STRING("music"), f); + mDsFile = new DeviceStorageFile(NS_LITERAL_STRING("music"), storagePath); } else { NS_WARNING("Couldn't recognize the mimetype of received file."); } diff --git a/dom/camera/GonkCameraControl.cpp b/dom/camera/GonkCameraControl.cpp index bf5a08ffc886..85907d7ca0f0 100644 --- a/dom/camera/GonkCameraControl.cpp +++ b/dom/camera/GonkCameraControl.cpp @@ -905,7 +905,8 @@ nsGonkCameraControl::StartRecordingImpl(StartRecordingTask* aStartRecording) */ nsCOMPtr filename = aStartRecording->mFolder; filename->AppendRelativePath(aStartRecording->mFilename); - mVideoFile = new DeviceStorageFile(NS_LITERAL_STRING("videos"), filename); + mVideoFile = new DeviceStorageFile(NS_LITERAL_STRING("videos"), + aStartRecording->mFilename); nsAutoCString nativeFilename; filename->GetNativePath(nativeFilename); diff --git a/dom/devicestorage/DeviceStorage.h b/dom/devicestorage/DeviceStorage.h index 4d478306548a..78d0d64d9228 100644 --- a/dom/devicestorage/DeviceStorage.h +++ b/dom/devicestorage/DeviceStorage.h @@ -20,10 +20,18 @@ public: nsCOMPtr mFile; nsString mPath; nsString mStorageType; + nsString mRootDir; bool mEditable; - DeviceStorageFile(const nsAString& aStorageType, nsIFile* aFile, const nsAString& aPath); - DeviceStorageFile(const nsAString& aStorageType, nsIFile* aFile); + // Used when the path will be set later via SetPath. + DeviceStorageFile(const nsAString& aStorageType); + // Used for non-enumeration purposes. + DeviceStorageFile(const nsAString& aStorageType, const nsAString& aPath); + // Used for enumerations. When you call Enumerate, you can pass in a directory to enumerate + // and the results that are returned are relative to that directory, files related to an + // enumeration need to know the "root of the enumeration" directory. + DeviceStorageFile(const nsAString& aStorageType, const nsAString& aRootDir, const nsAString& aPath); + void SetPath(const nsAString& aPath); void SetEditable(bool aEditable); @@ -32,6 +40,7 @@ public: // we want to make sure that the names of file can't reach // outside of the type of storage the user asked for. bool IsSafePath(); + bool IsSafePath(const nsAString& aPath); nsresult Remove(); nsresult Write(nsIInputStream* aInputStream); @@ -40,10 +49,13 @@ public: void collectFilesInternal(nsTArray > &aFiles, PRTime aSince, nsAString& aRootPath); static void DirectoryDiskUsage(nsIFile* aFile, uint64_t* aSoFar, const nsAString& aStorageType); - + static void GetRootDirectoryForType(const nsAString& aType, + const nsAString& aVolName, + nsIFile** aFile); private: + void Init(const nsAString& aStorageType); void NormalizeFilePath(); - void AppendRelativePath(); + void AppendRelativePath(const nsAString& aPath); }; /* diff --git a/dom/devicestorage/DeviceStorageRequestChild.cpp b/dom/devicestorage/DeviceStorageRequestChild.cpp index 9b5ef97f29ee..9c90591c3b42 100644 --- a/dom/devicestorage/DeviceStorageRequestChild.cpp +++ b/dom/devicestorage/DeviceStorageRequestChild.cpp @@ -100,14 +100,9 @@ DeviceStorageRequestChild::Recv__delete__(const DeviceStorageResponseValue& aVal uint32_t count = r.paths().Length(); for (uint32_t i = 0; i < count; i++) { - nsCOMPtr f; - nsresult rv = NS_NewLocalFile(r.paths()[i].fullpath(), false, getter_AddRefs(f)); - if (NS_FAILED(rv)) { - continue; - } - - nsRefPtr dsf = new DeviceStorageFile(r.paths()[i].type(), f); - dsf->SetPath(r.paths()[i].name()); + nsRefPtr dsf = new DeviceStorageFile(r.type(), + r.relpath(), + r.paths()[i].name()); cursor->mFiles.AppendElement(dsf); } diff --git a/dom/devicestorage/DeviceStorageRequestParent.cpp b/dom/devicestorage/DeviceStorageRequestParent.cpp index e046024e962f..8f438445614a 100644 --- a/dom/devicestorage/DeviceStorageRequestParent.cpp +++ b/dom/devicestorage/DeviceStorageRequestParent.cpp @@ -34,10 +34,7 @@ DeviceStorageRequestParent::Dispatch() { DeviceStorageAddParams p = mParams; - nsCOMPtr f; - NS_NewLocalFile(p.fullpath(), false, getter_AddRefs(f)); - - nsRefPtr dsf = new DeviceStorageFile(p.type(), f); + nsRefPtr dsf = new DeviceStorageFile(p.type(), p.relpath()); BlobParent* bp = static_cast(p.blobParent()); nsCOMPtr blob = bp->GetBlob(); @@ -56,12 +53,7 @@ DeviceStorageRequestParent::Dispatch() case DeviceStorageParams::TDeviceStorageGetParams: { DeviceStorageGetParams p = mParams; - - nsCOMPtr f; - NS_NewLocalFile(p.fullpath(), false, getter_AddRefs(f)); - - nsRefPtr dsf = new DeviceStorageFile(p.type(), f); - dsf->SetPath(p.name()); + nsRefPtr dsf = new DeviceStorageFile(p.type(), p.rootDir(), p.relpath()); nsRefPtr r = new ReadFileEvent(this, dsf); nsCOMPtr target = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID); @@ -74,10 +66,7 @@ DeviceStorageRequestParent::Dispatch() { DeviceStorageDeleteParams p = mParams; - nsCOMPtr f; - NS_NewLocalFile(p.fullpath(), false, getter_AddRefs(f)); - - nsRefPtr dsf = new DeviceStorageFile(p.type(), f); + nsRefPtr dsf = new DeviceStorageFile(p.type(), p.relpath()); nsRefPtr r = new DeleteFileEvent(this, dsf); nsCOMPtr target = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID); @@ -90,10 +79,7 @@ DeviceStorageRequestParent::Dispatch() { DeviceStorageFreeSpaceParams p = mParams; - nsCOMPtr f; - NS_NewLocalFile(p.fullpath(), false, getter_AddRefs(f)); - - nsRefPtr dsf = new DeviceStorageFile(p.type(), f); + nsRefPtr dsf = new DeviceStorageFile(p.type(), p.relpath()); nsRefPtr r = new FreeSpaceFileEvent(this, dsf); nsCOMPtr target = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID); @@ -106,10 +92,7 @@ DeviceStorageRequestParent::Dispatch() { DeviceStorageUsedSpaceParams p = mParams; - nsCOMPtr f; - NS_NewLocalFile(p.fullpath(), false, getter_AddRefs(f)); - - nsRefPtr dsf = new DeviceStorageFile(p.type(), f); + nsRefPtr dsf = new DeviceStorageFile(p.type(), p.relpath()); nsRefPtr r = new UsedSpaceFileEvent(this, dsf); nsCOMPtr target = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID); @@ -121,7 +104,9 @@ DeviceStorageRequestParent::Dispatch() case DeviceStorageParams::TDeviceStorageAvailableParams: { DeviceStorageAvailableParams p = mParams; - nsRefPtr r = new PostAvailableResultEvent(this, p.fullpath()); + + nsRefPtr dsf = new DeviceStorageFile(p.type(), p.relpath()); + nsRefPtr r = new PostAvailableResultEvent(this, dsf); NS_DispatchToMainThread(r); break; } @@ -129,11 +114,7 @@ DeviceStorageRequestParent::Dispatch() case DeviceStorageParams::TDeviceStorageEnumerationParams: { DeviceStorageEnumerationParams p = mParams; - - nsCOMPtr f; - NS_NewLocalFile(p.fullpath(), false, getter_AddRefs(f)); - - nsRefPtr dsf = new DeviceStorageFile(p.type(), f); + nsRefPtr dsf = new DeviceStorageFile(p.type(), p.relpath(), NS_LITERAL_STRING("")); nsRefPtr r = new EnumerateFileEvent(this, dsf, p.since()); nsCOMPtr target = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID); @@ -382,8 +363,12 @@ DeviceStorageRequestParent::PostBlobSuccessEvent::CancelableRun() { } DeviceStorageRequestParent::PostEnumerationSuccessEvent::PostEnumerationSuccessEvent(DeviceStorageRequestParent* aParent, + const nsAString& aStorageType, + const nsAString& aRelPath, InfallibleTArray& aPaths) : CancelableRunnable(aParent) + , mStorageType(aStorageType) + , mRelPath(aRelPath) , mPaths(aPaths) { } @@ -394,7 +379,7 @@ nsresult DeviceStorageRequestParent::PostEnumerationSuccessEvent::CancelableRun() { NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); - EnumerationResponse response(mPaths); + EnumerationResponse response(mStorageType, mRelPath, mPaths); unused << mParent->Send__delete__(mParent, response); return NS_OK; } @@ -621,13 +606,11 @@ DeviceStorageRequestParent::EnumerateFileEvent::CancelableRun() uint32_t count = files.Length(); for (uint32_t i = 0; i < count; i++) { - nsString fullpath; - files[i]->mFile->GetPath(fullpath); - DeviceStorageFileValue dsvf(mFile->mStorageType, files[i]->mPath, fullpath); + DeviceStorageFileValue dsvf(files[i]->mPath); values.AppendElement(dsvf); } - r = new PostEnumerationSuccessEvent(mParent, values); + r = new PostEnumerationSuccessEvent(mParent, mFile->mStorageType, mFile->mRootDir, values); NS_DispatchToMainThread(r); return NS_OK; } @@ -655,9 +638,9 @@ DeviceStorageRequestParent::PostPathResultEvent::CancelableRun() } DeviceStorageRequestParent::PostAvailableResultEvent::PostAvailableResultEvent(DeviceStorageRequestParent* aParent, - const nsAString &aPath) + DeviceStorageFile* aFile) : CancelableRunnable(aParent) - , mPath(aPath) + , mFile(aFile) { } @@ -673,7 +656,7 @@ DeviceStorageRequestParent::PostAvailableResultEvent::CancelableRun() nsString state; state.Assign(NS_LITERAL_STRING("available")); #ifdef MOZ_WIDGET_GONK - nsresult rv = GetSDCardStatus(mPath, state); + nsresult rv = GetSDCardStatus(mFile->mPath, state); if (NS_FAILED(rv)) { state.Assign(NS_LITERAL_STRING("unavailable")); } diff --git a/dom/devicestorage/DeviceStorageRequestParent.h b/dom/devicestorage/DeviceStorageRequestParent.h index 7a124a71b56f..5aee8dce45ef 100644 --- a/dom/devicestorage/DeviceStorageRequestParent.h +++ b/dom/devicestorage/DeviceStorageRequestParent.h @@ -105,10 +105,15 @@ private: class PostEnumerationSuccessEvent : public CancelableRunnable { public: - PostEnumerationSuccessEvent(DeviceStorageRequestParent* aParent, InfallibleTArray& aPaths); + PostEnumerationSuccessEvent(DeviceStorageRequestParent* aParent, + const nsAString& aStorageType, + const nsAString& aRelPath, + InfallibleTArray& aPaths); virtual ~PostEnumerationSuccessEvent(); virtual nsresult CancelableRun(); private: + const nsString mStorageType; + const nsString mRelPath; InfallibleTArray mPaths; }; @@ -211,11 +216,11 @@ private: class PostAvailableResultEvent : public CancelableRunnable { public: - PostAvailableResultEvent(DeviceStorageRequestParent* aParent, const nsAString& aPath); + PostAvailableResultEvent(DeviceStorageRequestParent* aParent, DeviceStorageFile* aFile); virtual ~PostAvailableResultEvent(); virtual nsresult CancelableRun(); private: - nsString mPath; + nsRefPtr mFile; }; protected: diff --git a/dom/devicestorage/PDeviceStorageRequest.ipdl b/dom/devicestorage/PDeviceStorageRequest.ipdl index 6d0073f745f5..a79ae1b3e042 100644 --- a/dom/devicestorage/PDeviceStorageRequest.ipdl +++ b/dom/devicestorage/PDeviceStorageRequest.ipdl @@ -27,13 +27,13 @@ struct BlobResponse struct DeviceStorageFileValue { - nsString type; nsString name; - nsString fullpath; }; struct EnumerationResponse { + nsString type; + nsString relpath; DeviceStorageFileValue[] paths; }; diff --git a/dom/devicestorage/nsDeviceStorage.cpp b/dom/devicestorage/nsDeviceStorage.cpp index 1e848f9083d6..8d8e6bc1b162 100644 --- a/dom/devicestorage/nsDeviceStorage.cpp +++ b/dom/devicestorage/nsDeviceStorage.cpp @@ -311,35 +311,151 @@ private: }; DeviceStorageFile::DeviceStorageFile(const nsAString& aStorageType, - nsIFile* aFile, + const nsAString& aRootDir, + const nsAString& aPath) + : mPath(aPath) + , mStorageType(aStorageType) + , mRootDir(aRootDir) + , mEditable(false) +{ + Init(aStorageType); + AppendRelativePath(mRootDir); + if (!mPath.EqualsLiteral("")) { + AppendRelativePath(mPath); + } + NormalizeFilePath(); +} + +DeviceStorageFile::DeviceStorageFile(const nsAString& aStorageType, const nsAString& aPath) : mPath(aPath) , mStorageType(aStorageType) , mEditable(false) { - NS_ASSERTION(aFile, "Must not create a DeviceStorageFile with a null nsIFile"); - // always take a clone - nsCOMPtr file; - aFile->Clone(getter_AddRefs(mFile)); - - AppendRelativePath(); + Init(aStorageType); + AppendRelativePath(aPath); NormalizeFilePath(); +} + +DeviceStorageFile::DeviceStorageFile(const nsAString& aStorageType) + : mStorageType(aStorageType) + , mEditable(false) +{ + Init(aStorageType); +} + +void +DeviceStorageFile::Init(const nsAString& aStorageType) +{ + // The hard-coded sdcard below will change as part of bug 858416 + DeviceStorageFile::GetRootDirectoryForType(aStorageType, + NS_LITERAL_STRING("sdcard"), + getter_AddRefs(mFile)); DebugOnly typeChecker = DeviceStorageTypeChecker::CreateOrGet(); NS_ASSERTION(typeChecker, "DeviceStorageTypeChecker is null"); } -DeviceStorageFile::DeviceStorageFile(const nsAString& aStorageType, nsIFile* aFile) - : mStorageType(aStorageType) - , mEditable(false) +void +DeviceStorageFile::GetRootDirectoryForType(const nsAString &aType, const + nsAString &aVolName, + nsIFile** aFile) { - NS_ASSERTION(aFile, "Must not create a DeviceStorageFile with a null nsIFile"); - // always take a clone - nsCOMPtr file; - aFile->Clone(getter_AddRefs(mFile)); + nsCOMPtr f; + nsCOMPtr dirService = do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID); + NS_ASSERTION(dirService, "Must have directory service"); - DebugOnly typeChecker = DeviceStorageTypeChecker::CreateOrGet(); - NS_ASSERTION(typeChecker, "DeviceStorageTypeChecker is null"); +#ifdef MOZ_WIDGET_GONK + nsString volMountPoint(NS_LITERAL_STRING("/sdcard")); + if (!aVolName.EqualsLiteral("")) { + nsCOMPtr vs = do_GetService(NS_VOLUMESERVICE_CONTRACTID); + if (vs) { + nsresult rv; + nsCOMPtr vol; + rv = vs->GetVolumeByName(aVolName, getter_AddRefs(vol)); + if (NS_SUCCEEDED(rv)) { + vol->GetMountPoint(volMountPoint); + } + } + } +#endif + + // Picture directory + if (aType.EqualsLiteral(DEVICESTORAGE_PICTURES)) { +#ifdef MOZ_WIDGET_GONK + NS_NewLocalFile(volMountPoint, false, getter_AddRefs(f)); +#elif defined (MOZ_WIDGET_COCOA) + dirService->Get(NS_OSX_PICTURE_DOCUMENTS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); +#elif defined (XP_UNIX) + dirService->Get(NS_UNIX_XDG_PICTURES_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); +#elif defined (XP_WIN) + dirService->Get(NS_WIN_PICTURES_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); +#endif + } + + // Video directory + else if (aType.EqualsLiteral(DEVICESTORAGE_VIDEOS)) { +#ifdef MOZ_WIDGET_GONK + NS_NewLocalFile(volMountPoint, false, getter_AddRefs(f)); +#elif defined (MOZ_WIDGET_COCOA) + dirService->Get(NS_OSX_MOVIE_DOCUMENTS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); +#elif defined (XP_UNIX) + dirService->Get(NS_UNIX_XDG_VIDEOS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); +#elif defined (XP_WIN) + dirService->Get(NS_WIN_VIDEOS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); +#endif + } + + // Music directory + else if (aType.EqualsLiteral(DEVICESTORAGE_MUSIC)) { +#ifdef MOZ_WIDGET_GONK + NS_NewLocalFile(volMountPoint, false, getter_AddRefs(f)); +#elif defined (MOZ_WIDGET_COCOA) + dirService->Get(NS_OSX_MUSIC_DOCUMENTS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); +#elif defined (XP_UNIX) + dirService->Get(NS_UNIX_XDG_MUSIC_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); +#elif defined (XP_WIN) + dirService->Get(NS_WIN_MUSIC_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); +#endif + } + + // Apps directory + else if (aType.EqualsLiteral(DEVICESTORAGE_APPS)) { +#ifdef MOZ_WIDGET_GONK + NS_NewLocalFile(NS_LITERAL_STRING("/data"), false, getter_AddRefs(f)); +#else + dirService->Get(NS_APP_USER_PROFILE_50_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); + if (f) { + f->AppendRelativeNativePath(NS_LITERAL_CSTRING("webapps")); + } +#endif + } + + // default SDCard + else if (aType.EqualsLiteral(DEVICESTORAGE_SDCARD)) { +#ifdef MOZ_WIDGET_GONK + NS_NewLocalFile(volMountPoint, false, getter_AddRefs(f)); +#else + // Eventually, on desktop, we want to do something smarter -- for example, + // detect when an sdcard is inserted, and use that instead of this. + dirService->Get(NS_APP_USER_PROFILE_50_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); + if (f) { + f->AppendRelativeNativePath(NS_LITERAL_CSTRING("fake-sdcard")); + } +#endif + } + + // in testing, we default all device storage types to a temp directory + if (f && mozilla::Preferences::GetBool("device.storage.testing", false)) { + dirService->Get(NS_OS_TEMP_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); + if (f) { + f->AppendRelativeNativePath(NS_LITERAL_CSTRING("device-storage-testing")); + f->Create(nsIFile::DIRECTORY_TYPE, 0777); + f->Normalize(); + } + } + + NS_IF_ADDREF(*aFile = f.get()); } void @@ -357,10 +473,16 @@ DeviceStorageFile::SetEditable(bool aEditable) { // outside of the type of storage the user asked for. bool DeviceStorageFile::IsSafePath() +{ + return IsSafePath(mRootDir) && IsSafePath(mPath); +} + +bool +DeviceStorageFile::IsSafePath(const nsAString& aPath) { nsAString::const_iterator start, end; - mPath.BeginReading(start); - mPath.EndReading(end); + aPath.BeginReading(start); + aPath.EndReading(end); // if the path has a ~ or \ in it, return false. NS_NAMED_LITERAL_STRING(tilde, "~"); @@ -370,7 +492,7 @@ DeviceStorageFile::IsSafePath() return false; } // split on /. if any token is "", ., or .., return false. - NS_ConvertUTF16toUTF8 cname(mPath); + NS_ConvertUTF16toUTF8 cname(aPath); char* buffer = cname.BeginWriting(); const char* token; @@ -397,12 +519,12 @@ DeviceStorageFile::NormalizeFilePath() { } void -DeviceStorageFile::AppendRelativePath() { +DeviceStorageFile::AppendRelativePath(const nsAString& aPath) { #if defined(XP_WIN) // replace forward slashes with backslashes, // since nsLocalFileWin chokes on them nsString temp; - temp.Assign(mPath); + temp.Assign(aPath); PRUnichar* cur = temp.BeginWriting(); PRUnichar* end = temp.EndWriting(); @@ -413,7 +535,7 @@ DeviceStorageFile::AppendRelativePath() { } mFile->AppendRelativePath(temp); #else - mFile->AppendRelativePath(mPath); + mFile->AppendRelativePath(aPath); #endif } @@ -535,7 +657,6 @@ DeviceStorageFile::CollectFiles(nsTArray > &aFiles, if (NS_FAILED(rv)) { return; } - return collectFilesInternal(aFiles, aSince, rootPath); } @@ -584,12 +705,10 @@ DeviceStorageFile::collectFilesInternal(nsTArray > & nsDependentSubstring newPath = Substring(fullpath, len); if (isDir) { - DeviceStorageFile dsf(mStorageType, f); - dsf.SetPath(newPath); + DeviceStorageFile dsf(mStorageType, mRootDir, newPath); dsf.collectFilesInternal(aFiles, aSince, aRootPath); } else if (isFile) { - nsRefPtr dsf = new DeviceStorageFile(mStorageType, f); - dsf->SetPath(newPath); + nsRefPtr dsf = new DeviceStorageFile(mStorageType, mRootDir, newPath); aFiles.AppendElement(dsf); } } @@ -705,11 +824,13 @@ UnregisterForSDCardChanges(nsIObserver* aObserver) #endif void -nsDOMDeviceStorage::SetRootDirectoryForType(const nsAString& aType, const nsAString& aVolName) +nsDOMDeviceStorage::SetRootDirectoryForType(const nsAString& aType, + const nsAString& aVolName) { nsCOMPtr f; - nsCOMPtr dirService = do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID); - NS_ASSERTION(dirService, "Must have directory service"); + DeviceStorageFile::GetRootDirectoryForType(aType, + aVolName, + getter_AddRefs(f)); mVolumeName = NS_LITERAL_STRING(""); #ifdef MOZ_WIDGET_GONK @@ -726,84 +847,7 @@ nsDOMDeviceStorage::SetRootDirectoryForType(const nsAString& aType, const nsAStr } } } -#endif - // Picture directory - if (aType.EqualsLiteral(DEVICESTORAGE_PICTURES)) { -#ifdef MOZ_WIDGET_GONK - NS_NewLocalFile(volMountPoint, false, getter_AddRefs(f)); -#elif defined (MOZ_WIDGET_COCOA) - dirService->Get(NS_OSX_PICTURE_DOCUMENTS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); -#elif defined (XP_UNIX) - dirService->Get(NS_UNIX_XDG_PICTURES_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); -#elif defined (XP_WIN) - dirService->Get(NS_WIN_PICTURES_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); -#endif - } - - // Video directory - else if (aType.EqualsLiteral(DEVICESTORAGE_VIDEOS)) { -#ifdef MOZ_WIDGET_GONK - NS_NewLocalFile(volMountPoint, false, getter_AddRefs(f)); -#elif defined (MOZ_WIDGET_COCOA) - dirService->Get(NS_OSX_MOVIE_DOCUMENTS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); -#elif defined (XP_UNIX) - dirService->Get(NS_UNIX_XDG_VIDEOS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); -#elif defined (XP_WIN) - dirService->Get(NS_WIN_VIDEOS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); -#endif - } - - // Music directory - else if (aType.EqualsLiteral(DEVICESTORAGE_MUSIC)) { -#ifdef MOZ_WIDGET_GONK - NS_NewLocalFile(volMountPoint, false, getter_AddRefs(f)); -#elif defined (MOZ_WIDGET_COCOA) - dirService->Get(NS_OSX_MUSIC_DOCUMENTS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); -#elif defined (XP_UNIX) - dirService->Get(NS_UNIX_XDG_MUSIC_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); -#elif defined (XP_WIN) - dirService->Get(NS_WIN_MUSIC_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); -#endif - } - - // Apps directory - else if (aType.EqualsLiteral(DEVICESTORAGE_APPS)) { -#ifdef MOZ_WIDGET_GONK - NS_NewLocalFile(NS_LITERAL_STRING("/data"), false, getter_AddRefs(f)); -#else - dirService->Get(NS_APP_USER_PROFILE_50_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); - if (f) { - f->AppendRelativeNativePath(NS_LITERAL_CSTRING("webapps")); - } -#endif - } - - // default SDCard - else if (aType.EqualsLiteral(DEVICESTORAGE_SDCARD)) { -#ifdef MOZ_WIDGET_GONK - NS_NewLocalFile(volMountPoint, false, getter_AddRefs(f)); -#else - // Eventually, on desktop, we want to do something smarter -- for example, - // detect when an sdcard is inserted, and use that instead of this. - dirService->Get(NS_APP_USER_PROFILE_50_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); - if (f) { - f->AppendRelativeNativePath(NS_LITERAL_CSTRING("fake-sdcard")); - } -#endif - } - - // in testing, we default all device storage types to a temp directory - if (f && mozilla::Preferences::GetBool("device.storage.testing", false)) { - dirService->Get(NS_OS_TEMP_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f)); - if (f) { - f->AppendRelativeNativePath(NS_LITERAL_CSTRING("device-storage-testing")); - f->Create(nsIFile::DIRECTORY_TYPE, 0777); - f->Normalize(); - } - } - -#ifdef MOZ_WIDGET_GONK RegisterForSDCardChanges(this); #endif @@ -1025,20 +1069,13 @@ ContinueCursorEvent::Continue() return; } - nsString fullpath; - nsresult rv = file->mFile->GetPath(fullpath); - if (NS_FAILED(rv)) { - NS_ASSERTION(false, "GetPath failed to return a valid path"); - return; - } - nsDOMDeviceStorageCursor* cursor = static_cast(mRequest.get()); nsString cursorStorageType; cursor->GetStorageType(cursorStorageType); DeviceStorageRequestChild* child = new DeviceStorageRequestChild(mRequest, file); child->SetCallback(cursor); - DeviceStorageGetParams params(cursorStorageType, file->mPath, fullpath); + DeviceStorageGetParams params(cursorStorageType, file->mRootDir, file->mPath); ContentChild::GetSingleton()->SendPDeviceStorageRequestConstructor(child, params); mRequest = nullptr; } @@ -1179,17 +1216,8 @@ nsDOMDeviceStorageCursor::Allow() } if (XRE_GetProcessType() != GeckoProcessType_Default) { - - nsString fullpath; - nsresult rv = mFile->mFile->GetPath(fullpath); - - if (NS_FAILED(rv)) { - // just do nothing - return NS_OK; - } - PDeviceStorageRequestChild* child = new DeviceStorageRequestChild(this, mFile); - DeviceStorageEnumerationParams params(mFile->mStorageType, fullpath, mSince); + DeviceStorageEnumerationParams params(mFile->mStorageType, mFile->mRootDir, mSince); ContentChild::GetSingleton()->SendPDeviceStorageRequestConstructor(child, params); return NS_OK; } @@ -1649,14 +1677,6 @@ public: return NS_ERROR_FAILURE; } - nsString fullpath; - nsresult rv = mFile->mFile->GetPath(fullpath); - - if (NS_FAILED(rv)) { - // just do nothing - return NS_OK; - } - switch(mRequestType) { case DEVICE_STORAGE_REQUEST_CREATE: { @@ -1686,8 +1706,7 @@ public: DeviceStorageAddParams params; params.blobChild() = actor; params.type() = mFile->mStorageType; - params.name() = mFile->mPath; - params.fullpath() = fullpath; + params.relpath() = mFile->mPath; PDeviceStorageRequestChild* child = new DeviceStorageRequestChild(mRequest, mFile); ContentChild::GetSingleton()->SendPDeviceStorageRequestConstructor(child, params); @@ -1713,7 +1732,7 @@ public: if (XRE_GetProcessType() != GeckoProcessType_Default) { PDeviceStorageRequestChild* child = new DeviceStorageRequestChild(mRequest, mFile); - DeviceStorageGetParams params(mFile->mStorageType, mFile->mPath, fullpath); + DeviceStorageGetParams params(mFile->mStorageType, mFile->mRootDir, mFile->mPath); ContentChild::GetSingleton()->SendPDeviceStorageRequestConstructor(child, params); return NS_OK; } @@ -1737,7 +1756,7 @@ public: if (XRE_GetProcessType() != GeckoProcessType_Default) { PDeviceStorageRequestChild* child = new DeviceStorageRequestChild(mRequest, mFile); - DeviceStorageDeleteParams params(mFile->mStorageType, fullpath); + DeviceStorageDeleteParams params(mFile->mStorageType, mFile->mPath); ContentChild::GetSingleton()->SendPDeviceStorageRequestConstructor(child, params); return NS_OK; } @@ -1749,7 +1768,7 @@ public: { if (XRE_GetProcessType() != GeckoProcessType_Default) { PDeviceStorageRequestChild* child = new DeviceStorageRequestChild(mRequest, mFile); - DeviceStorageFreeSpaceParams params(mFile->mStorageType, fullpath); + DeviceStorageFreeSpaceParams params(mFile->mStorageType, mFile->mPath); ContentChild::GetSingleton()->SendPDeviceStorageRequestConstructor(child, params); return NS_OK; } @@ -1761,7 +1780,7 @@ public: { if (XRE_GetProcessType() != GeckoProcessType_Default) { PDeviceStorageRequestChild* child = new DeviceStorageRequestChild(mRequest, mFile); - DeviceStorageUsedSpaceParams params(mFile->mStorageType, fullpath); + DeviceStorageUsedSpaceParams params(mFile->mStorageType, mFile->mPath); ContentChild::GetSingleton()->SendPDeviceStorageRequestConstructor(child, params); return NS_OK; } @@ -1773,7 +1792,7 @@ public: { if (XRE_GetProcessType() != GeckoProcessType_Default) { PDeviceStorageRequestChild* child = new DeviceStorageRequestChild(mRequest, mFile); - DeviceStorageAvailableParams params(mFile->mStorageType, fullpath); + DeviceStorageAvailableParams params(mFile->mStorageType, mFile->mPath); ContentChild::GetSingleton()->SendPDeviceStorageRequestConstructor(child, params); return NS_OK; } @@ -2023,7 +2042,7 @@ nsDOMDeviceStorage::AddNamed(nsIDOMBlob *aBlob, nsCOMPtr r; - nsRefPtr dsf = new DeviceStorageFile(mStorageType, mRootDirectory, aPath); + nsRefPtr dsf = new DeviceStorageFile(mStorageType, aPath); if (!dsf->IsSafePath()) { r = new PostErrorEvent(request, POST_ERROR_EVENT_PERMISSION_DENIED); } else if (!typeChecker->Check(mStorageType, dsf->mFile) || @@ -2079,7 +2098,7 @@ nsDOMDeviceStorage::GetInternal(const JS::Value & aPath, return NS_OK; } - nsRefPtr dsf = new DeviceStorageFile(mStorageType, mRootDirectory, path); + nsRefPtr dsf = new DeviceStorageFile(mStorageType, path); dsf->SetEditable(aEditable); if (!dsf->IsSafePath()) { r = new PostErrorEvent(request, POST_ERROR_EVENT_PERMISSION_DENIED); @@ -2112,7 +2131,7 @@ nsDOMDeviceStorage::Delete(const JS::Value & aPath, JSContext* aCx, nsIDOMDOMReq return NS_OK; } - nsRefPtr dsf = new DeviceStorageFile(mStorageType, mRootDirectory, path); + nsRefPtr dsf = new DeviceStorageFile(mStorageType, path); if (!dsf->IsSafePath()) { r = new PostErrorEvent(request, POST_ERROR_EVENT_PERMISSION_DENIED); @@ -2136,7 +2155,7 @@ nsDOMDeviceStorage::FreeSpace(nsIDOMDOMRequest** aRetval) nsRefPtr request = new DOMRequest(win); NS_ADDREF(*aRetval = request); - nsRefPtr dsf = new DeviceStorageFile(mStorageType, mRootDirectory); + nsRefPtr dsf = new DeviceStorageFile(mStorageType); nsCOMPtr r = new DeviceStorageRequest(DEVICE_STORAGE_REQUEST_FREE_SPACE, win, mPrincipal, @@ -2157,7 +2176,7 @@ nsDOMDeviceStorage::UsedSpace(nsIDOMDOMRequest** aRetval) nsRefPtr request = new DOMRequest(win); NS_ADDREF(*aRetval = request); - nsRefPtr dsf = new DeviceStorageFile(mStorageType, mRootDirectory); + nsRefPtr dsf = new DeviceStorageFile(mStorageType); nsCOMPtr r = new DeviceStorageRequest(DEVICE_STORAGE_REQUEST_USED_SPACE, win, mPrincipal, @@ -2178,7 +2197,7 @@ nsDOMDeviceStorage::Available(nsIDOMDOMRequest** aRetval) nsRefPtr request = new DOMRequest(win); NS_ADDREF(*aRetval = request); - nsRefPtr dsf = new DeviceStorageFile(mStorageType, mRootDirectory); + nsRefPtr dsf = new DeviceStorageFile(mStorageType); nsCOMPtr r = new DeviceStorageRequest(DEVICE_STORAGE_REQUEST_AVAILABLE, win, mPrincipal, @@ -2278,7 +2297,7 @@ nsDOMDeviceStorage::EnumerateInternal(const JS::Value & aName, since = ExtractDateFromOptions(aCx, aOptions); } - nsRefPtr dsf = new DeviceStorageFile(mStorageType, mRootDirectory, path); + nsRefPtr dsf = new DeviceStorageFile(mStorageType, path, NS_LITERAL_STRING("")); dsf->SetEditable(aEditable); nsRefPtr cursor = new nsDOMDeviceStorageCursor(win, mPrincipal, @@ -2452,7 +2471,7 @@ nsDOMDeviceStorage::AddEventListener(const nsAString & aType, } nsRefPtr request = new DOMRequest(win); - nsRefPtr dsf = new DeviceStorageFile(mStorageType, mRootDirectory); + nsRefPtr dsf = new DeviceStorageFile(mStorageType); nsCOMPtr r = new DeviceStorageRequest(DEVICE_STORAGE_REQUEST_WATCH, win, mPrincipal, dsf, request, this); NS_DispatchToMainThread(r); @@ -2473,7 +2492,7 @@ nsDOMDeviceStorage::AddEventListener(const nsAString & aType, } nsRefPtr request = new DOMRequest(win); - nsRefPtr dsf = new DeviceStorageFile(mStorageType, mRootDirectory); + nsRefPtr dsf = new DeviceStorageFile(mStorageType); nsCOMPtr r = new DeviceStorageRequest(DEVICE_STORAGE_REQUEST_WATCH, win, mPrincipal, dsf, request, this); NS_DispatchToMainThread(r); diff --git a/dom/ipc/ContentChild.cpp b/dom/ipc/ContentChild.cpp index 1e86af0b3092..a186189b0226 100644 --- a/dom/ipc/ContentChild.cpp +++ b/dom/ipc/ContentChild.cpp @@ -1176,10 +1176,7 @@ ContentChild::RecvLastPrivateDocShellDestroyed() bool ContentChild::RecvFilePathUpdate(const nsString& type, const nsString& path, const nsCString& aReason) { - nsCOMPtr file; - NS_NewLocalFile(path, false, getter_AddRefs(file)); - - nsRefPtr dsf = new DeviceStorageFile(type, file); + nsRefPtr dsf = new DeviceStorageFile(type, path); nsString reason; CopyASCIItoUTF16(aReason, reason); diff --git a/dom/ipc/ContentParent.cpp b/dom/ipc/ContentParent.cpp index ea2fc83feb8f..1134faee6b39 100644 --- a/dom/ipc/ContentParent.cpp +++ b/dom/ipc/ContentParent.cpp @@ -1531,10 +1531,7 @@ ContentParent::Observe(nsISupports* aSubject, CopyUTF16toUTF8(aData, creason); DeviceStorageFile* file = static_cast(aSubject); - nsString path; - file->mFile->GetPath(path); - - unused << SendFilePathUpdate(file->mStorageType, path, creason); + unused << SendFilePathUpdate(file->mStorageType, file->mPath, creason); } #ifdef MOZ_WIDGET_GONK else if(!strcmp(aTopic, NS_VOLUME_STATE_CHANGED)) { @@ -2349,15 +2346,11 @@ ContentParent::RecvAsyncMessage(const nsString& aMsg, } bool -ContentParent::RecvFilePathUpdateNotify(const nsString& aType, const nsString& aFilePath, const nsCString& aReason) +ContentParent::RecvFilePathUpdateNotify(const nsString& aType, + const nsString& aFilePath, + const nsCString& aReason) { - nsCOMPtr file; - nsresult rv = NS_NewLocalFile(aFilePath, false, getter_AddRefs(file)); - if (NS_FAILED(rv)) { - // ignore - return true; - } - nsRefPtr dsf = new DeviceStorageFile(aType, file); + nsRefPtr dsf = new DeviceStorageFile(aType, aFilePath); nsCOMPtr obs = mozilla::services::GetObserverService(); if (!obs) { diff --git a/dom/ipc/PContent.ipdl b/dom/ipc/PContent.ipdl index f3a5064a3d24..ebcd9612a28e 100644 --- a/dom/ipc/PContent.ipdl +++ b/dom/ipc/PContent.ipdl @@ -63,46 +63,45 @@ struct FontListEntry { struct DeviceStorageFreeSpaceParams { nsString type; - nsString fullpath; + nsString relpath; }; struct DeviceStorageUsedSpaceParams { nsString type; - nsString fullpath; + nsString relpath; }; struct DeviceStorageAvailableParams { nsString type; - nsString fullpath; + nsString relpath; }; struct DeviceStorageAddParams { nsString type; + nsString relpath; PBlob blob; - nsString name; - nsString fullpath; }; struct DeviceStorageGetParams { nsString type; - nsString name; - nsString fullpath; + nsString rootDir; + nsString relpath; }; struct DeviceStorageDeleteParams { nsString type; - nsString fullpath; + nsString relpath; }; struct DeviceStorageEnumerationParams { nsString type; - nsString fullpath; + nsString relpath; uint64_t since; };