forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileSystemBase.cpp
203 lines (165 loc) · 4.44 KB
/
FileSystemBase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* -*- 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/. */
#include "mozilla/dom/FileSystemBase.h"
#include "DeviceStorageFileSystem.h"
#include "nsCharSeparatedTokenizer.h"
#include "OSFileSystem.h"
namespace mozilla {
namespace dom {
// static
already_AddRefed<FileSystemBase>
FileSystemBase::DeserializeDOMPath(const nsAString& aString)
{
MOZ_ASSERT(XRE_IsParentProcess(), "Only call from parent process!");
AssertIsOnBackgroundThread();
if (StringBeginsWith(aString, NS_LITERAL_STRING("devicestorage-"))) {
// The string representation of devicestorage file system is of the format:
// devicestorage-StorageType-StorageName
nsCharSeparatedTokenizer tokenizer(aString, char16_t('-'));
tokenizer.nextToken();
nsString storageType;
if (tokenizer.hasMoreTokens()) {
storageType = tokenizer.nextToken();
}
nsString storageName;
if (tokenizer.hasMoreTokens()) {
storageName = tokenizer.nextToken();
}
RefPtr<DeviceStorageFileSystem> f =
new DeviceStorageFileSystem(storageType, storageName);
return f.forget();
}
return RefPtr<OSFileSystemParent>(new OSFileSystemParent(aString)).forget();
}
FileSystemBase::FileSystemBase()
: mShutdown(false)
, mPermissionCheckType(eNotSet)
#ifdef DEBUG
, mOwningThread(PR_GetCurrentThread())
#endif
{
}
FileSystemBase::~FileSystemBase()
{
AssertIsOnOwningThread();
}
void
FileSystemBase::Shutdown()
{
AssertIsOnOwningThread();
mShutdown = true;
}
nsISupports*
FileSystemBase::GetParentObject() const
{
AssertIsOnOwningThread();
return nullptr;
}
bool
FileSystemBase::GetRealPath(BlobImpl* aFile, nsIFile** aPath) const
{
AssertIsOnOwningThread();
MOZ_ASSERT(aFile, "aFile Should not be null.");
MOZ_ASSERT(aPath);
nsAutoString filePath;
ErrorResult rv;
aFile->GetMozFullPathInternal(filePath, rv);
if (NS_WARN_IF(rv.Failed())) {
return false;
}
rv = NS_NewNativeLocalFile(NS_ConvertUTF16toUTF8(filePath),
true, aPath);
if (NS_WARN_IF(rv.Failed())) {
return false;
}
return true;
}
bool
FileSystemBase::IsSafeFile(nsIFile* aFile) const
{
AssertIsOnOwningThread();
return false;
}
bool
FileSystemBase::IsSafeDirectory(Directory* aDir) const
{
AssertIsOnOwningThread();
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,
nsAString& aRetval,
ErrorResult& aRv) const
{
AssertIsOnOwningThread();
MOZ_ASSERT(aFile);
aRetval.Truncate();
nsCOMPtr<nsIFile> fileSystemPath;
aRv = NS_NewNativeLocalFile(NS_ConvertUTF16toUTF8(LocalOrDeviceStorageRootPath()),
true, getter_AddRefs(fileSystemPath));
if (NS_WARN_IF(aRv.Failed())) {
return;
}
nsCOMPtr<nsIFile> path;
aRv = aFile->Clone(getter_AddRefs(path));
if (NS_WARN_IF(aRv.Failed())) {
return;
}
nsTArray<nsString> parts;
while (true) {
nsAutoString leafName;
aRv = path->GetLeafName(leafName);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
if (!leafName.IsEmpty()) {
parts.AppendElement(leafName);
}
bool equal = false;
aRv = fileSystemPath->Equals(path, &equal);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
if (equal) {
break;
}
nsCOMPtr<nsIFile> parentPath;
aRv = path->GetParent(getter_AddRefs(parentPath));
if (NS_WARN_IF(aRv.Failed())) {
return;
}
MOZ_ASSERT(parentPath);
aRv = parentPath->Clone(getter_AddRefs(path));
if (NS_WARN_IF(aRv.Failed())) {
return;
}
}
if (parts.IsEmpty()) {
aRetval.AppendLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL);
return;
}
for (int32_t i = parts.Length() - 1; i >= 0; --i) {
aRetval.AppendLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL);
aRetval.Append(parts[i]);
}
}
void
FileSystemBase::AssertIsOnOwningThread() const
{
MOZ_ASSERT(mOwningThread);
MOZ_ASSERT(PR_GetCurrentThread() == mOwningThread);
}
} // namespace dom
} // namespace mozilla