FileSystemQuotaClient.cpp (5468B)
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 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "FileSystemQuotaClient.h" 8 9 #include "datamodel/FileSystemDatabaseManager.h" 10 #include "datamodel/FileSystemFileManager.h" 11 #include "mozilla/dom/FileSystemDataManager.h" 12 #include "mozilla/dom/quota/Assertions.h" 13 #include "mozilla/dom/quota/QuotaCommon.h" 14 #include "mozilla/dom/quota/QuotaManager.h" 15 #include "mozilla/dom/quota/ResultExtensions.h" 16 #include "mozilla/dom/quota/UsageInfo.h" 17 #include "mozilla/ipc/BackgroundParent.h" 18 #include "nsIFile.h" 19 20 namespace mozilla::dom::fs { 21 22 namespace { 23 24 auto toNSResult = [](const auto& aRv) { return ToNSResult(aRv); }; 25 26 } // namespace 27 28 FileSystemQuotaClient::FileSystemQuotaClient() { 29 ::mozilla::ipc::AssertIsOnBackgroundThread(); 30 } 31 32 quota::Client::Type FileSystemQuotaClient::GetType() { 33 return quota::Client::Type::FILESYSTEM; 34 } 35 36 Result<quota::UsageInfo, nsresult> FileSystemQuotaClient::InitOrigin( 37 quota::PersistenceType aPersistenceType, 38 const quota::OriginMetadata& aOriginMetadata, const AtomicBool& aCanceled) { 39 quota::AssertIsOnIOThread(); 40 41 DebugOnly<quota::QuotaManager*> quotaManager = quota::QuotaManager::Get(); 42 MOZ_ASSERT(quotaManager); 43 44 MOZ_ASSERT( 45 !quotaManager->IsTemporaryOriginInitializedInternal(aOriginMetadata)); 46 47 { 48 QM_TRY_INSPECT(const nsCOMPtr<nsIFile>& databaseFile, 49 data::GetDatabaseFile(aOriginMetadata).mapErr(toNSResult)); 50 51 bool exists = false; 52 QM_TRY(MOZ_TO_RESULT(databaseFile->Exists(&exists))); 53 // If database doesn't already exist, we do not create it 54 if (!exists) { 55 return quota::UsageInfo(); 56 } 57 } 58 59 QM_TRY_INSPECT( 60 const ResultConnection& conn, 61 data::GetStorageConnection(aOriginMetadata, /* aDirectoryLockId */ -1) 62 .mapErr(toNSResult)); 63 64 QM_TRY(MOZ_TO_RESULT( 65 data::FileSystemDatabaseManager::RescanUsages(conn, aOriginMetadata))); 66 67 return data::FileSystemDatabaseManager::GetUsage(conn, aOriginMetadata) 68 .mapErr(toNSResult); 69 } 70 71 nsresult FileSystemQuotaClient::InitOriginWithoutTracking( 72 quota::PersistenceType /* aPersistenceType */, 73 const quota::OriginMetadata& /* aOriginMetadata */, 74 const AtomicBool& /* aCanceled */) { 75 quota::AssertIsOnIOThread(); 76 77 // This is called when a storage/permanent/${origin}/fs directory exists. Even 78 // though this shouldn't happen with a "good" profile, we shouldn't return an 79 // error here, since that would cause origin initialization to fail. We just 80 // warn and otherwise ignore that. 81 UNKNOWN_FILE_WARNING( 82 NS_LITERAL_STRING_FROM_CSTRING(FILESYSTEM_DIRECTORY_NAME)); 83 84 return NS_OK; 85 } 86 87 Result<quota::UsageInfo, nsresult> FileSystemQuotaClient::GetUsageForOrigin( 88 quota::PersistenceType aPersistenceType, 89 const quota::OriginMetadata& aOriginMetadata, 90 const AtomicBool& /* aCanceled */) { 91 quota::AssertIsOnIOThread(); 92 93 MOZ_ASSERT(aPersistenceType == 94 quota::PersistenceType::PERSISTENCE_TYPE_DEFAULT); 95 96 quota::QuotaManager* quotaManager = quota::QuotaManager::Get(); 97 MOZ_ASSERT(quotaManager); 98 99 MOZ_ASSERT(quotaManager->IsTemporaryStorageInitializedInternal()); 100 101 // We can't open the database at this point because the quota manager may not 102 // allow it. Use the cached value instead. 103 return quotaManager->GetUsageForClient(aPersistenceType, aOriginMetadata, 104 quota::Client::FILESYSTEM); 105 } 106 107 void FileSystemQuotaClient::OnOriginClearCompleted( 108 const quota::OriginMetadata& aOriginMetadata) { 109 quota::AssertIsOnIOThread(); 110 } 111 112 void FileSystemQuotaClient::OnRepositoryClearCompleted( 113 quota::PersistenceType aPersistenceType) { 114 quota::AssertIsOnIOThread(); 115 } 116 117 void FileSystemQuotaClient::ReleaseIOThreadObjects() { 118 quota::AssertIsOnIOThread(); 119 } 120 121 void FileSystemQuotaClient::AbortOperationsForLocks( 122 const DirectoryLockIdTable& aDirectoryLockIds) { 123 ::mozilla::ipc::AssertIsOnBackgroundThread(); 124 125 data::FileSystemDataManager::AbortOperationsForLocks(aDirectoryLockIds); 126 } 127 128 void FileSystemQuotaClient::AbortOperationsForProcess( 129 ContentParentId aContentParentId) { 130 ::mozilla::ipc::AssertIsOnBackgroundThread(); 131 } 132 133 void FileSystemQuotaClient::AbortAllOperations() { 134 ::mozilla::ipc::AssertIsOnBackgroundThread(); 135 } 136 137 void FileSystemQuotaClient::StartIdleMaintenance() { 138 ::mozilla::ipc::AssertIsOnBackgroundThread(); 139 } 140 141 void FileSystemQuotaClient::StopIdleMaintenance() { 142 ::mozilla::ipc::AssertIsOnBackgroundThread(); 143 } 144 145 void FileSystemQuotaClient::InitiateShutdown() { 146 ::mozilla::ipc::AssertIsOnBackgroundThread(); 147 148 data::FileSystemDataManager::InitiateShutdown(); 149 } 150 151 nsCString FileSystemQuotaClient::GetShutdownStatus() const { 152 ::mozilla::ipc::AssertIsOnBackgroundThread(); 153 154 return "Not implemented"_ns; 155 } 156 157 bool FileSystemQuotaClient::IsShutdownCompleted() const { 158 ::mozilla::ipc::AssertIsOnBackgroundThread(); 159 160 return data::FileSystemDataManager::IsShutdownCompleted(); 161 } 162 163 void FileSystemQuotaClient::ForceKillActors() { 164 ::mozilla::ipc::AssertIsOnBackgroundThread(); 165 166 // Hopefully not needed. 167 } 168 169 void FileSystemQuotaClient::FinalizeShutdown() { 170 ::mozilla::ipc::AssertIsOnBackgroundThread(); 171 172 // Empty for now. 173 } 174 175 } // namespace mozilla::dom::fs