Skip to content

Commit

Permalink
Bug 1263992 - patch 1 - Remove DirectoryType enum, r=smaug
Browse files Browse the repository at this point in the history
  • Loading branch information
bakulf committed Apr 18, 2016
1 parent 1612db7 commit 1cfd6aa
Show file tree
Hide file tree
Showing 19 changed files with 83 additions and 174 deletions.
25 changes: 6 additions & 19 deletions dom/base/StructuredCloneHolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,7 @@ WriteBlob(JSStructuredCloneWriter* aWriter,
}

// A directory is serialized as:
// - pair of ints: SCTAG_DOM_DIRECTORY, 0
// - pair of ints: type (eDOMRootDirectory/eDOMNotRootDirectory) - path length
// - pair of ints: SCTAG_DOM_DIRECTORY, path length
// - path as string
bool
WriteDirectory(JSStructuredCloneWriter* aWriter,
Expand All @@ -721,36 +720,25 @@ WriteDirectory(JSStructuredCloneWriter* aWriter,
aDirectory->GetFullRealPath(path);

size_t charSize = sizeof(nsString::char_type);
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_DIRECTORY, 0) &&
JS_WriteUint32Pair(aWriter, (uint32_t)aDirectory->Type(),
path.Length()) &&
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_DIRECTORY, path.Length()) &&
JS_WriteBytes(aWriter, path.get(), path.Length() * charSize);
}

JSObject*
ReadDirectory(JSContext* aCx,
JSStructuredCloneReader* aReader,
uint32_t aZero,
uint32_t aPathLength,
StructuredCloneHolder* aHolder)
{
MOZ_ASSERT(aCx);
MOZ_ASSERT(aReader);
MOZ_ASSERT(aHolder);
MOZ_ASSERT(aZero == 0);

uint32_t directoryType, lengthOfString;
if (!JS_ReadUint32Pair(aReader, &directoryType, &lengthOfString)) {
return nullptr;
}

MOZ_ASSERT(directoryType == Directory::eDOMRootDirectory ||
directoryType == Directory::eNotDOMRootDirectory);

nsAutoString path;
path.SetLength(lengthOfString);
path.SetLength(aPathLength);
size_t charSize = sizeof(nsString::char_type);
if (!JS_ReadBytes(aReader, (void*) path.BeginWriting(),
lengthOfString * charSize)) {
aPathLength * charSize)) {
return nullptr;
}

Expand All @@ -769,8 +757,7 @@ ReadDirectory(JSContext* aCx,
JS::Rooted<JS::Value> val(aCx);
{
RefPtr<Directory> directory =
Directory::Create(aHolder->ParentDuringRead(), file,
(Directory::DirectoryType) directoryType);
Directory::Create(aHolder->ParentDuringRead(), file);

if (!ToJSValue(aCx, directory, &val)) {
return nullptr;
Expand Down
4 changes: 1 addition & 3 deletions dom/filesystem/CreateDirectoryTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ CreateDirectoryTaskChild::HandlerCallback()
}

RefPtr<Directory> dir = Directory::Create(mFileSystem->GetParentObject(),
mTargetPath,
Directory::eNotDOMRootDirectory,
mFileSystem);
mTargetPath, mFileSystem);
MOZ_ASSERT(dir);

mPromise->MaybeResolve(dir);
Expand Down
26 changes: 24 additions & 2 deletions dom/filesystem/DeviceStorageFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,32 @@ DeviceStorageFileSystem::GetParentObject() const
}

void
DeviceStorageFileSystem::GetRootName(nsAString& aRetval) const
DeviceStorageFileSystem::GetDirectoryName(nsIFile* aFile, nsAString& aRetval,
ErrorResult& aRv) const
{
AssertIsOnOwningThread();
aRetval = mStorageName;
MOZ_ASSERT(aFile);

nsCOMPtr<nsIFile> rootPath;
aRv = NS_NewLocalFile(LocalOrDeviceStorageRootPath(), false,
getter_AddRefs(rootPath));
if (NS_WARN_IF(aRv.Failed())) {
return;
}

bool equal = false;
aRv = aFile->Equals(rootPath, &equal);
if (NS_WARN_IF(aRv.Failed())) {
return;
}

if (equal) {
aRetval = mStorageName;
return;
}

FileSystemBase::GetDirectoryName(aFile, aRetval, aRv);
NS_WARN_IF(aRv.Failed());
}

bool
Expand Down
3 changes: 2 additions & 1 deletion dom/filesystem/DeviceStorageFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class DeviceStorageFileSystem
GetParentObject() const override;

virtual void
GetRootName(nsAString& aRetval) const override;
GetDirectoryName(nsIFile* aFile, nsAString& aRetval,
ErrorResult& aRv) const override;

virtual bool
IsSafeFile(nsIFile* aFile) const override;
Expand Down
40 changes: 9 additions & 31 deletions dom/filesystem/Directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ Directory::GetRoot(FileSystemBase* aFileSystem, ErrorResult& aRv)
}

RefPtr<GetFileOrDirectoryTaskChild> task =
GetFileOrDirectoryTaskChild::Create(aFileSystem, path, eDOMRootDirectory,
true, aRv);
GetFileOrDirectoryTaskChild::Create(aFileSystem, path, true, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
Expand All @@ -149,7 +148,7 @@ Directory::GetRoot(FileSystemBase* aFileSystem, ErrorResult& aRv)

/* static */ already_AddRefed<Directory>
Directory::Create(nsISupports* aParent, nsIFile* aFile,
DirectoryType aType, FileSystemBase* aFileSystem)
FileSystemBase* aFileSystem)
{
MOZ_ASSERT(aParent);
MOZ_ASSERT(aFile);
Expand All @@ -158,27 +157,17 @@ Directory::Create(nsISupports* aParent, nsIFile* aFile,
bool isDir;
nsresult rv = aFile->IsDirectory(&isDir);
MOZ_ASSERT(NS_SUCCEEDED(rv) && isDir);

if (aType == eNotDOMRootDirectory) {
RefPtr<nsIFile> parent;
rv = aFile->GetParent(getter_AddRefs(parent));
// We must have a parent if this is not the root directory.
MOZ_ASSERT(NS_SUCCEEDED(rv) && parent);
}
#endif

RefPtr<Directory> directory =
new Directory(aParent, aFile, aType, aFileSystem);
RefPtr<Directory> directory = new Directory(aParent, aFile, aFileSystem);
return directory.forget();
}

Directory::Directory(nsISupports* aParent,
nsIFile* aFile,
DirectoryType aType,
FileSystemBase* aFileSystem)
: mParent(aParent)
, mFile(aFile)
, mType(aType)
{
MOZ_ASSERT(aFile);

Expand Down Expand Up @@ -213,18 +202,12 @@ Directory::GetName(nsAString& aRetval, ErrorResult& aRv)
{
aRetval.Truncate();

if (mType == eDOMRootDirectory) {
RefPtr<FileSystemBase> fs = GetFileSystem(aRv);
if (NS_WARN_IF(aRv.Failed())) {
return;
}

fs->GetRootName(aRetval);
RefPtr<FileSystemBase> fs = GetFileSystem(aRv);
if (NS_WARN_IF(aRv.Failed())) {
return;
}

aRv = mFile->GetLeafName(aRetval);
NS_WARN_IF(aRv.Failed());
fs->GetDirectoryName(mFile, aRetval, aRv);
}

already_AddRefed<Promise>
Expand Down Expand Up @@ -318,8 +301,7 @@ Directory::Get(const nsAString& aPath, ErrorResult& aRv)
}

RefPtr<GetFileOrDirectoryTaskChild> task =
GetFileOrDirectoryTaskChild::Create(fs, realPath, eNotDOMRootDirectory,
false, aRv);
GetFileOrDirectoryTaskChild::Create(fs, realPath, false, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
Expand Down Expand Up @@ -409,7 +391,7 @@ Directory::GetPath(nsAString& aRetval, ErrorResult& aRv)
return;
}

fs->GetDOMPath(mFile, mType, mPath, aRv);
fs->GetDOMPath(mFile, mPath, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
Expand Down Expand Up @@ -438,7 +420,7 @@ Directory::GetFilesAndDirectories(ErrorResult& aRv)
}

RefPtr<GetDirectoryListingTaskChild> task =
GetDirectoryListingTaskChild::Create(fs, mFile, mType, mFilters, aRv);
GetDirectoryListingTaskChild::Create(fs, mFile, mFilters, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
Expand Down Expand Up @@ -478,10 +460,6 @@ FileSystemBase*
Directory::GetFileSystem(ErrorResult& aRv)
{
if (!mFileSystem) {
// Any subdir inherits the FileSystem of the parent Directory. If we are
// here it's because we are dealing with the DOM root.
MOZ_ASSERT(mType == eDOMRootDirectory);

nsAutoString path;
aRv = mFile->GetPath(path);
if (NS_WARN_IF(aRv.Failed())) {
Expand Down
21 changes: 2 additions & 19 deletions dom/filesystem/Directory.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,9 @@ class Directory final
static already_AddRefed<Promise>
GetRoot(FileSystemBase* aFileSystem, ErrorResult& aRv);

enum DirectoryType {
// When a directory is selected using a HTMLInputElement, that will be the
// DOM root directory and its name will be '/'. All the sub directory will
// be called with they real name. We use this enum to mark what we must
// consider the '/' of this DOM filesystem.
eDOMRootDirectory,

// All the sub directories of the '/' will be marked using this other value.
eNotDOMRootDirectory
};

static already_AddRefed<Directory>
Create(nsISupports* aParent, nsIFile* aDirectory,
DirectoryType aType, FileSystemBase* aFileSystem = 0);
FileSystemBase* aFileSystem = 0);

// ========= Begin WebIDL bindings. ===========

Expand Down Expand Up @@ -145,17 +134,12 @@ class Directory final
FileSystemBase*
GetFileSystem(ErrorResult& aRv);

DirectoryType Type() const
{
return mType;
}

bool
ClonableToDifferentThreadOrProcess() const;

private:
Directory(nsISupports* aParent,
nsIFile* aFile, DirectoryType aType,
nsIFile* aFile,
FileSystemBase* aFileSystem = nullptr);
~Directory();

Expand All @@ -172,7 +156,6 @@ class Directory final
nsCOMPtr<nsISupports> mParent;
RefPtr<FileSystemBase> mFileSystem;
nsCOMPtr<nsIFile> mFile;
DirectoryType mType;

nsString mFilters;
nsString mPath;
Expand Down
37 changes: 20 additions & 17 deletions dom/filesystem/FileSystemBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,26 @@ FileSystemBase::IsSafeDirectory(Directory* aDir) const
return false;
}

void
FileSystemBase::GetDirectoryName(nsIFile* aFile, nsAString& aRetval,
ErrorResult& aRv) const
{
AssertIsOnOwningThread();
MOZ_ASSERT(aFile);

aRv = aFile->GetLeafName(aRetval);
NS_WARN_IF(aRv.Failed());
}

void
FileSystemBase::GetDOMPath(nsIFile* aFile,
Directory::DirectoryType aType,
nsAString& aRetval,
ErrorResult& aRv) const
{
AssertIsOnOwningThread();
MOZ_ASSERT(aFile);

if (aType == Directory::eDOMRootDirectory) {
aRetval.AssignLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL);
return;
}
aRetval.Truncate();

nsCOMPtr<nsIFile> fileSystemPath;
aRv = NS_NewNativeLocalFile(NS_ConvertUTF16toUTF8(LocalOrDeviceStorageRootPath()),
Expand All @@ -131,8 +138,6 @@ FileSystemBase::GetDOMPath(nsIFile* aFile,
return;
}

MOZ_ASSERT(FileSystemUtils::IsDescendantPath(fileSystemPath, aFile));

nsCOMPtr<nsIFile> path;
aRv = aFile->Clone(getter_AddRefs(path));
if (NS_WARN_IF(aRv.Failed())) {
Expand All @@ -142,23 +147,23 @@ FileSystemBase::GetDOMPath(nsIFile* aFile,
nsTArray<nsString> parts;

while (true) {
bool equal = false;
aRv = fileSystemPath->Equals(path, &equal);
nsAutoString leafName;
aRv = path->GetLeafName(leafName);
if (NS_WARN_IF(aRv.Failed())) {
return;
}

if (equal) {
break;
}
parts.AppendElement(leafName);

nsAutoString leafName;
aRv = path->GetLeafName(leafName);
bool equal = false;
aRv = fileSystemPath->Equals(path, &equal);
if (NS_WARN_IF(aRv.Failed())) {
return;
}

parts.AppendElement(leafName);
if (equal) {
break;
}

nsCOMPtr<nsIFile> parentPath;
aRv = path->GetParent(getter_AddRefs(parentPath));
Expand All @@ -176,8 +181,6 @@ FileSystemBase::GetDOMPath(nsIFile* aFile,

MOZ_ASSERT(!parts.IsEmpty());

aRetval.Truncate();

for (int32_t i = parts.Length() - 1; i >= 0; --i) {
aRetval.AppendLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL);
aRetval.Append(parts[i]);
Expand Down
10 changes: 3 additions & 7 deletions dom/filesystem/FileSystemBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,12 @@ class FileSystemBase
virtual nsISupports*
GetParentObject() const;

/*
* Get the virtual name of the root directory. This name will be exposed to
* the content page.
*/
virtual void
GetRootName(nsAString& aRetval) const = 0;
GetDirectoryName(nsIFile* aFile, nsAString& aRetval,
ErrorResult& aRv) const;

void
GetDOMPath(nsIFile* aFile, Directory::DirectoryType aType,
nsAString& aRetval, ErrorResult& aRv) const;
GetDOMPath(nsIFile* aFile, nsAString& aRetval, ErrorResult& aRv) const;

/*
* Return the local root path of the FileSystem implementation.
Expand Down
Loading

0 comments on commit 1cfd6aa

Please sign in to comment.