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