Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "FileSystemRootDirectoryEntry.h"
8 : #include "FileSystemRootDirectoryReader.h"
9 : #include "mozilla/dom/FileSystemUtils.h"
10 :
11 : namespace mozilla {
12 : namespace dom {
13 :
14 0 : NS_IMPL_CYCLE_COLLECTION_INHERITED(FileSystemRootDirectoryEntry,
15 : FileSystemDirectoryEntry, mEntries)
16 :
17 0 : NS_IMPL_ADDREF_INHERITED(FileSystemRootDirectoryEntry, FileSystemDirectoryEntry)
18 0 : NS_IMPL_RELEASE_INHERITED(FileSystemRootDirectoryEntry, FileSystemDirectoryEntry)
19 :
20 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileSystemRootDirectoryEntry)
21 0 : NS_INTERFACE_MAP_END_INHERITING(FileSystemDirectoryEntry)
22 :
23 0 : FileSystemRootDirectoryEntry::FileSystemRootDirectoryEntry(nsIGlobalObject* aGlobal,
24 : const Sequence<RefPtr<FileSystemEntry>>& aEntries,
25 0 : FileSystem* aFileSystem)
26 : : FileSystemDirectoryEntry(aGlobal, nullptr, nullptr, aFileSystem)
27 0 : , mEntries(aEntries)
28 : {
29 0 : MOZ_ASSERT(aGlobal);
30 0 : }
31 :
32 0 : FileSystemRootDirectoryEntry::~FileSystemRootDirectoryEntry()
33 0 : {}
34 :
35 : void
36 0 : FileSystemRootDirectoryEntry::GetName(nsAString& aName, ErrorResult& aRv) const
37 : {
38 0 : aName.Truncate();
39 0 : }
40 :
41 : void
42 0 : FileSystemRootDirectoryEntry::GetFullPath(nsAString& aPath, ErrorResult& aRv) const
43 : {
44 0 : aPath.AssignLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL);
45 0 : }
46 :
47 : already_AddRefed<FileSystemDirectoryReader>
48 0 : FileSystemRootDirectoryEntry::CreateReader()
49 : {
50 : RefPtr<FileSystemDirectoryReader> reader =
51 0 : new FileSystemRootDirectoryReader(this, Filesystem(), mEntries);
52 0 : return reader.forget();
53 : }
54 :
55 : void
56 0 : FileSystemRootDirectoryEntry::GetInternal(const nsAString& aPath,
57 : const FileSystemFlags& aFlag,
58 : const Optional<OwningNonNull<FileSystemEntryCallback>>& aSuccessCallback,
59 : const Optional<OwningNonNull<ErrorCallback>>& aErrorCallback,
60 : GetInternalType aType)
61 : {
62 0 : if (!aSuccessCallback.WasPassed() && !aErrorCallback.WasPassed()) {
63 0 : return;
64 : }
65 :
66 0 : if (aFlag.mCreate) {
67 0 : ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
68 0 : NS_ERROR_DOM_SECURITY_ERR);
69 0 : return;
70 : }
71 :
72 0 : nsTArray<nsString> parts;
73 0 : if (!FileSystemUtils::IsValidRelativeDOMPath(aPath, parts)) {
74 0 : ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
75 0 : NS_ERROR_DOM_NOT_FOUND_ERR);
76 0 : return;
77 : }
78 :
79 0 : MOZ_ASSERT(!parts.IsEmpty());
80 :
81 0 : RefPtr<FileSystemEntry> entry;
82 0 : for (uint32_t i = 0; i < mEntries.Length(); ++i) {
83 0 : ErrorResult rv;
84 0 : nsAutoString name;
85 0 : mEntries[i]->GetName(name, rv);
86 :
87 0 : if (NS_WARN_IF(rv.Failed())) {
88 0 : ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
89 0 : rv.StealNSResult());
90 0 : return;
91 : }
92 :
93 0 : if (name == parts[0]) {
94 0 : entry = mEntries[i];
95 0 : break;
96 : }
97 : }
98 :
99 : // Not found.
100 0 : if (!entry) {
101 0 : ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
102 0 : NS_ERROR_DOM_NOT_FOUND_ERR);
103 0 : return;
104 : }
105 :
106 : // No subdirectory in the path.
107 0 : if (parts.Length() == 1) {
108 0 : if ((entry->IsFile() && aType == eGetDirectory) ||
109 0 : (entry->IsDirectory() && aType == eGetFile)) {
110 0 : ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
111 0 : NS_ERROR_DOM_TYPE_MISMATCH_ERR);
112 0 : return;
113 : }
114 :
115 0 : if (aSuccessCallback.WasPassed()) {
116 : RefPtr<EntryCallbackRunnable> runnable =
117 0 : new EntryCallbackRunnable(&aSuccessCallback.Value(), entry);
118 :
119 0 : FileSystemUtils::DispatchRunnable(GetParentObject(), runnable.forget());
120 :
121 : }
122 0 : return;
123 : }
124 :
125 : // Subdirectories, but this is a file.
126 0 : if (entry->IsFile()) {
127 0 : ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
128 0 : NS_ERROR_DOM_NOT_FOUND_ERR);
129 0 : return;
130 : }
131 :
132 : // Let's recreate a path without the first directory.
133 0 : nsAutoString path;
134 0 : for (uint32_t i = 1, len = parts.Length(); i < len; ++i) {
135 0 : path.Append(parts[i]);
136 0 : if (i < len - 1) {
137 0 : path.AppendLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL);
138 : }
139 : }
140 :
141 0 : auto* directoryEntry = static_cast<FileSystemDirectoryEntry*>(entry.get());
142 : directoryEntry->GetInternal(path, aFlag, aSuccessCallback, aErrorCallback,
143 0 : aType);
144 : }
145 :
146 : } // dom namespace
147 : } // mozilla namespace
|