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/. */
7 #include "mozilla/dom/FileSystemBase.h"
9 #include "mozilla/ErrorResult.h"
10 #include "mozilla/dom/BlobImpl.h"
11 #include "mozilla/dom/FileSystemUtils.h"
13 #include "OSFileSystem.h"
15 namespace mozilla::dom
{
17 FileSystemBase::FileSystemBase() : mShutdown(false) {}
19 FileSystemBase::~FileSystemBase() { AssertIsOnOwningThread(); }
21 void FileSystemBase::Shutdown() {
22 AssertIsOnOwningThread();
26 nsIGlobalObject
* FileSystemBase::GetParentObject() const {
27 AssertIsOnOwningThread();
31 bool FileSystemBase::GetRealPath(BlobImpl
* aFile
, nsIFile
** aPath
) const {
32 AssertIsOnOwningThread();
33 MOZ_ASSERT(aFile
, "aFile Should not be null.");
36 nsAutoString filePath
;
38 aFile
->GetMozFullPathInternal(filePath
, rv
);
39 if (NS_WARN_IF(rv
.Failed())) {
40 rv
.SuppressException();
44 rv
= NS_NewLocalFile(filePath
, true, aPath
);
45 if (NS_WARN_IF(rv
.Failed())) {
46 rv
.SuppressException();
53 bool FileSystemBase::IsSafeFile(nsIFile
* aFile
) const {
54 AssertIsOnOwningThread();
58 bool FileSystemBase::IsSafeDirectory(Directory
* aDir
) const {
59 AssertIsOnOwningThread();
63 void FileSystemBase::GetDirectoryName(nsIFile
* aFile
, nsAString
& aRetval
,
64 ErrorResult
& aRv
) const {
65 AssertIsOnOwningThread();
68 aRv
= aFile
->GetLeafName(aRetval
);
69 NS_WARNING_ASSERTION(!aRv
.Failed(), "GetLeafName failed");
72 void FileSystemBase::GetDOMPath(nsIFile
* aFile
, nsAString
& aRetval
,
73 ErrorResult
& aRv
) const {
74 AssertIsOnOwningThread();
79 nsCOMPtr
<nsIFile
> fileSystemPath
;
80 aRv
= NS_NewLocalFile(LocalRootPath(), true, getter_AddRefs(fileSystemPath
));
81 if (NS_WARN_IF(aRv
.Failed())) {
85 nsCOMPtr
<nsIFile
> path
;
86 aRv
= aFile
->Clone(getter_AddRefs(path
));
87 if (NS_WARN_IF(aRv
.Failed())) {
91 nsTArray
<nsString
> parts
;
94 nsAutoString leafName
;
95 aRv
= path
->GetLeafName(leafName
);
96 if (NS_WARN_IF(aRv
.Failed())) {
100 if (!leafName
.IsEmpty()) {
101 parts
.AppendElement(leafName
);
105 aRv
= fileSystemPath
->Equals(path
, &equal
);
106 if (NS_WARN_IF(aRv
.Failed())) {
114 nsCOMPtr
<nsIFile
> parentPath
;
115 aRv
= path
->GetParent(getter_AddRefs(parentPath
));
116 if (NS_WARN_IF(aRv
.Failed())) {
120 MOZ_ASSERT(parentPath
);
122 aRv
= parentPath
->Clone(getter_AddRefs(path
));
123 if (NS_WARN_IF(aRv
.Failed())) {
128 if (parts
.IsEmpty()) {
129 aRetval
.AppendLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL
);
133 for (int32_t i
= parts
.Length() - 1; i
>= 0; --i
) {
134 aRetval
.AppendLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL
);
135 aRetval
.Append(parts
[i
]);
139 void FileSystemBase::AssertIsOnOwningThread() const {
140 NS_ASSERT_OWNINGTHREAD(FileSystemBase
);
143 } // namespace mozilla::dom