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 "mozilla/dom/FileSystemBase.h"
8
9 #include "nsCharSeparatedTokenizer.h"
10 #include "OSFileSystem.h"
11
12 namespace mozilla {
13 namespace dom {
14
FileSystemBase()15 FileSystemBase::FileSystemBase() : mShutdown(false) {}
16
~FileSystemBase()17 FileSystemBase::~FileSystemBase() { AssertIsOnOwningThread(); }
18
Shutdown()19 void FileSystemBase::Shutdown() {
20 AssertIsOnOwningThread();
21 mShutdown = true;
22 }
23
GetParentObject() const24 nsISupports* FileSystemBase::GetParentObject() const {
25 AssertIsOnOwningThread();
26 return nullptr;
27 }
28
GetRealPath(BlobImpl * aFile,nsIFile ** aPath) const29 bool FileSystemBase::GetRealPath(BlobImpl* aFile, nsIFile** aPath) const {
30 AssertIsOnOwningThread();
31 MOZ_ASSERT(aFile, "aFile Should not be null.");
32 MOZ_ASSERT(aPath);
33
34 nsAutoString filePath;
35 ErrorResult rv;
36 aFile->GetMozFullPathInternal(filePath, rv);
37 if (NS_WARN_IF(rv.Failed())) {
38 rv.SuppressException();
39 return false;
40 }
41
42 rv = NS_NewLocalFile(filePath, true, aPath);
43 if (NS_WARN_IF(rv.Failed())) {
44 rv.SuppressException();
45 return false;
46 }
47
48 return true;
49 }
50
IsSafeFile(nsIFile * aFile) const51 bool FileSystemBase::IsSafeFile(nsIFile* aFile) const {
52 AssertIsOnOwningThread();
53 return false;
54 }
55
IsSafeDirectory(Directory * aDir) const56 bool FileSystemBase::IsSafeDirectory(Directory* aDir) const {
57 AssertIsOnOwningThread();
58 return false;
59 }
60
GetDirectoryName(nsIFile * aFile,nsAString & aRetval,ErrorResult & aRv) const61 void FileSystemBase::GetDirectoryName(nsIFile* aFile, nsAString& aRetval,
62 ErrorResult& aRv) const {
63 AssertIsOnOwningThread();
64 MOZ_ASSERT(aFile);
65
66 aRv = aFile->GetLeafName(aRetval);
67 NS_WARNING_ASSERTION(!aRv.Failed(), "GetLeafName failed");
68 }
69
GetDOMPath(nsIFile * aFile,nsAString & aRetval,ErrorResult & aRv) const70 void FileSystemBase::GetDOMPath(nsIFile* aFile, nsAString& aRetval,
71 ErrorResult& aRv) const {
72 AssertIsOnOwningThread();
73 MOZ_ASSERT(aFile);
74
75 aRetval.Truncate();
76
77 nsCOMPtr<nsIFile> fileSystemPath;
78 aRv = NS_NewLocalFile(LocalRootPath(), true, getter_AddRefs(fileSystemPath));
79 if (NS_WARN_IF(aRv.Failed())) {
80 return;
81 }
82
83 nsCOMPtr<nsIFile> path;
84 aRv = aFile->Clone(getter_AddRefs(path));
85 if (NS_WARN_IF(aRv.Failed())) {
86 return;
87 }
88
89 nsTArray<nsString> parts;
90
91 while (true) {
92 nsAutoString leafName;
93 aRv = path->GetLeafName(leafName);
94 if (NS_WARN_IF(aRv.Failed())) {
95 return;
96 }
97
98 if (!leafName.IsEmpty()) {
99 parts.AppendElement(leafName);
100 }
101
102 bool equal = false;
103 aRv = fileSystemPath->Equals(path, &equal);
104 if (NS_WARN_IF(aRv.Failed())) {
105 return;
106 }
107
108 if (equal) {
109 break;
110 }
111
112 nsCOMPtr<nsIFile> parentPath;
113 aRv = path->GetParent(getter_AddRefs(parentPath));
114 if (NS_WARN_IF(aRv.Failed())) {
115 return;
116 }
117
118 MOZ_ASSERT(parentPath);
119
120 aRv = parentPath->Clone(getter_AddRefs(path));
121 if (NS_WARN_IF(aRv.Failed())) {
122 return;
123 }
124 }
125
126 if (parts.IsEmpty()) {
127 aRetval.AppendLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL);
128 return;
129 }
130
131 for (int32_t i = parts.Length() - 1; i >= 0; --i) {
132 aRetval.AppendLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL);
133 aRetval.Append(parts[i]);
134 }
135 }
136
AssertIsOnOwningThread() const137 void FileSystemBase::AssertIsOnOwningThread() const {
138 NS_ASSERT_OWNINGTHREAD(FileSystemBase);
139 }
140
141 } // namespace dom
142 } // namespace mozilla
143