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