Bug 959591 - Allow device storage media dirs to be overridden (esp useful for desktop). r=fabrice

This commit is contained in:
Dave Hylands 2014-03-12 14:15:03 -07:00
parent 6dd24386e5
commit 1d9cb952e2

View File

@ -48,6 +48,7 @@
#include "nsIPermissionManager.h" #include "nsIPermissionManager.h"
#include "nsIStringBundle.h" #include "nsIStringBundle.h"
#include "nsIDocument.h" #include "nsIDocument.h"
#include "nsPrintfCString.h"
#include <algorithm> #include <algorithm>
#include "private/pprio.h" #include "private/pprio.h"
#include "nsContentPermissionHelper.h" #include "nsContentPermissionHelper.h"
@ -194,7 +195,7 @@ public:
#endif #endif
nsCOMPtr<nsIFile> apps; nsCOMPtr<nsIFile> apps;
nsCOMPtr<nsIFile> crashes; nsCOMPtr<nsIFile> crashes;
nsCOMPtr<nsIFile> temp; nsCOMPtr<nsIFile> overrideRootDir;
}; };
static StaticRefPtr<GlobalDirs> sDirs; static StaticRefPtr<GlobalDirs> sDirs;
@ -678,13 +679,34 @@ InitDirs()
if (mozilla::Preferences::GetBool("device.storage.testing", false)) { if (mozilla::Preferences::GetBool("device.storage.testing", false)) {
dirService->Get(NS_OS_TEMP_DIR, NS_GET_IID(nsIFile), dirService->Get(NS_OS_TEMP_DIR, NS_GET_IID(nsIFile),
getter_AddRefs(sDirs->temp)); getter_AddRefs(sDirs->overrideRootDir));
if (sDirs->temp) { if (sDirs->overrideRootDir) {
sDirs->temp->AppendRelativeNativePath( sDirs->overrideRootDir->AppendRelativeNativePath(
NS_LITERAL_CSTRING("device-storage-testing")); NS_LITERAL_CSTRING("device-storage-testing"));
sDirs->temp->Create(nsIFile::DIRECTORY_TYPE, 0777);
sDirs->temp->Normalize();
} }
} else {
// For users running on desktop, it's convenient to be able to override
// all of the directories to point to a single tree, much like what happens
// on a real device.
const nsAdoptingString& overrideRootDir =
mozilla::Preferences::GetString("device.storage.overrideRootDir");
if (overrideRootDir) {
NS_NewLocalFile(overrideRootDir, false,
getter_AddRefs(sDirs->overrideRootDir));
}
}
if (sDirs->overrideRootDir) {
nsresult rv
= sDirs->overrideRootDir->Create(nsIFile::DIRECTORY_TYPE, 0777);
if (NS_FAILED(rv) && rv != NS_ERROR_FILE_ALREADY_EXISTS) {
nsString path;
sDirs->overrideRootDir->GetPath(path);
nsPrintfCString msg("DeviceStorage: Unable to create directory '%s'",
NS_LossyConvertUTF16toASCII(path).get());
NS_WARNING(msg.get());
}
sDirs->overrideRootDir->Normalize();
} }
} }
@ -715,6 +737,7 @@ DeviceStorageFile::GetRootDirectoryForType(const nsAString& aStorageType,
{ {
nsCOMPtr<nsIFile> f; nsCOMPtr<nsIFile> f;
*aFile = nullptr; *aFile = nullptr;
bool allowOverride = true;
InitDirs(); InitDirs();
@ -761,6 +784,7 @@ DeviceStorageFile::GetRootDirectoryForType(const nsAString& aStorageType,
// Apps directory // Apps directory
else if (aStorageType.EqualsLiteral(DEVICESTORAGE_APPS)) { else if (aStorageType.EqualsLiteral(DEVICESTORAGE_APPS)) {
f = sDirs->apps; f = sDirs->apps;
allowOverride = false;
} }
// default SDCard // default SDCard
@ -775,17 +799,19 @@ DeviceStorageFile::GetRootDirectoryForType(const nsAString& aStorageType,
// crash reports directory. // crash reports directory.
else if (aStorageType.EqualsLiteral(DEVICESTORAGE_CRASHES)) { else if (aStorageType.EqualsLiteral(DEVICESTORAGE_CRASHES)) {
f = sDirs->crashes; f = sDirs->crashes;
allowOverride = false;
} else { } else {
// Not a storage type that we recognize. Return null // Not a storage type that we recognize. Return null
return; return;
} }
// In testing, we default all device storage types to a temp directory. // In testing, we default all device storage types to a temp directory.
// sDirs->temp will only have been initialized (in InitDirs) if the // sDirs->overrideRootDir will only have been initialized (in InitDirs)
// preference device.storage.testing was set to true. We can't test the // if the preference device.storage.testing was set to true, or if
// preference directly here, since we may not be on the main thread. // device.storage.overrideRootDir is set. We can't test the preferences
if (sDirs->temp) { // directly here, since we may not be on the main thread.
f = sDirs->temp; if (allowOverride && sDirs->overrideRootDir) {
f = sDirs->overrideRootDir;
} }
if (f) { if (f) {