2016-06-07 01:55:17 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2016-08-18 10:18:05 +03:00
|
|
|
#include "FileSystemRootDirectoryEntry.h"
|
2016-08-18 10:18:51 +03:00
|
|
|
#include "FileSystemRootDirectoryReader.h"
|
2016-06-07 01:55:17 +03:00
|
|
|
#include "mozilla/dom/FileSystemUtils.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2016-08-18 10:18:05 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(FileSystemRootDirectoryEntry,
|
|
|
|
FileSystemDirectoryEntry, mEntries)
|
2016-06-07 01:55:17 +03:00
|
|
|
|
2016-08-18 10:18:05 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(FileSystemRootDirectoryEntry, FileSystemDirectoryEntry)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(FileSystemRootDirectoryEntry, FileSystemDirectoryEntry)
|
2016-06-07 01:55:17 +03:00
|
|
|
|
2016-08-18 10:18:05 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileSystemRootDirectoryEntry)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(FileSystemDirectoryEntry)
|
2016-06-07 01:55:17 +03:00
|
|
|
|
2016-08-18 10:18:05 +03:00
|
|
|
FileSystemRootDirectoryEntry::FileSystemRootDirectoryEntry(nsIGlobalObject* aGlobal,
|
|
|
|
const Sequence<RefPtr<FileSystemEntry>>& aEntries,
|
|
|
|
FileSystem* aFileSystem)
|
2016-11-03 09:55:30 +03:00
|
|
|
: FileSystemDirectoryEntry(aGlobal, nullptr, nullptr, aFileSystem)
|
2016-06-07 01:55:17 +03:00
|
|
|
, mEntries(aEntries)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aGlobal);
|
|
|
|
}
|
|
|
|
|
2016-08-18 10:18:05 +03:00
|
|
|
FileSystemRootDirectoryEntry::~FileSystemRootDirectoryEntry()
|
2016-06-07 01:55:17 +03:00
|
|
|
{}
|
|
|
|
|
|
|
|
void
|
2016-08-18 10:18:05 +03:00
|
|
|
FileSystemRootDirectoryEntry::GetName(nsAString& aName, ErrorResult& aRv) const
|
2016-06-07 01:55:17 +03:00
|
|
|
{
|
|
|
|
aName.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-08-18 10:18:05 +03:00
|
|
|
FileSystemRootDirectoryEntry::GetFullPath(nsAString& aPath, ErrorResult& aRv) const
|
2016-06-07 01:55:17 +03:00
|
|
|
{
|
|
|
|
aPath.AssignLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL);
|
|
|
|
}
|
|
|
|
|
2016-08-18 10:18:51 +03:00
|
|
|
already_AddRefed<FileSystemDirectoryReader>
|
2016-11-03 09:55:30 +03:00
|
|
|
FileSystemRootDirectoryEntry::CreateReader()
|
2016-06-07 01:55:17 +03:00
|
|
|
{
|
2016-08-18 10:18:51 +03:00
|
|
|
RefPtr<FileSystemDirectoryReader> reader =
|
2016-11-03 09:55:30 +03:00
|
|
|
new FileSystemRootDirectoryReader(this, Filesystem(), mEntries);
|
2016-06-07 01:55:17 +03:00
|
|
|
return reader.forget();
|
|
|
|
}
|
|
|
|
|
2016-06-07 01:55:17 +03:00
|
|
|
void
|
2016-08-18 10:18:05 +03:00
|
|
|
FileSystemRootDirectoryEntry::GetInternal(const nsAString& aPath,
|
|
|
|
const FileSystemFlags& aFlag,
|
2016-08-18 10:19:15 +03:00
|
|
|
const Optional<OwningNonNull<FileSystemEntryCallback>>& aSuccessCallback,
|
2016-08-18 10:18:05 +03:00
|
|
|
const Optional<OwningNonNull<ErrorCallback>>& aErrorCallback,
|
2016-11-03 09:55:30 +03:00
|
|
|
GetInternalType aType)
|
2016-06-07 01:55:17 +03:00
|
|
|
{
|
|
|
|
if (!aSuccessCallback.WasPassed() && !aErrorCallback.WasPassed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aFlag.mCreate) {
|
|
|
|
ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
|
|
|
|
NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<nsString> parts;
|
|
|
|
if (!FileSystemUtils::IsValidRelativeDOMPath(aPath, parts)) {
|
|
|
|
ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
|
|
|
|
NS_ERROR_DOM_NOT_FOUND_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!parts.IsEmpty());
|
|
|
|
|
2016-08-18 10:17:25 +03:00
|
|
|
RefPtr<FileSystemEntry> entry;
|
2016-06-07 01:55:17 +03:00
|
|
|
for (uint32_t i = 0; i < mEntries.Length(); ++i) {
|
|
|
|
ErrorResult rv;
|
|
|
|
nsAutoString name;
|
|
|
|
mEntries[i]->GetName(name, rv);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
|
|
|
|
rv.StealNSResult());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name == parts[0]) {
|
|
|
|
entry = mEntries[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not found.
|
|
|
|
if (!entry) {
|
|
|
|
ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
|
|
|
|
NS_ERROR_DOM_NOT_FOUND_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No subdirectory in the path.
|
|
|
|
if (parts.Length() == 1) {
|
|
|
|
if ((entry->IsFile() && aType == eGetDirectory) ||
|
|
|
|
(entry->IsDirectory() && aType == eGetFile)) {
|
|
|
|
ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
|
|
|
|
NS_ERROR_DOM_TYPE_MISMATCH_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aSuccessCallback.WasPassed()) {
|
|
|
|
RefPtr<EntryCallbackRunnable> runnable =
|
|
|
|
new EntryCallbackRunnable(&aSuccessCallback.Value(), entry);
|
2017-04-05 10:47:34 +03:00
|
|
|
|
|
|
|
FileSystemUtils::DispatchRunnable(GetParentObject(), runnable.forget());
|
|
|
|
|
2016-06-07 01:55:17 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subdirectories, but this is a file.
|
|
|
|
if (entry->IsFile()) {
|
|
|
|
ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
|
|
|
|
NS_ERROR_DOM_NOT_FOUND_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's recreate a path without the first directory.
|
|
|
|
nsAutoString path;
|
|
|
|
for (uint32_t i = 1, len = parts.Length(); i < len; ++i) {
|
|
|
|
path.Append(parts[i]);
|
|
|
|
if (i < len - 1) {
|
|
|
|
path.AppendLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-18 10:18:05 +03:00
|
|
|
auto* directoryEntry = static_cast<FileSystemDirectoryEntry*>(entry.get());
|
2016-06-07 01:55:17 +03:00
|
|
|
directoryEntry->GetInternal(path, aFlag, aSuccessCallback, aErrorCallback,
|
|
|
|
aType);
|
|
|
|
}
|
|
|
|
|
2016-06-07 01:55:17 +03:00
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|