Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "FileService.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "nsIFile.h" |
michael@0 | 10 | #include "nsIFileStorage.h" |
michael@0 | 11 | #include "nsIObserverService.h" |
michael@0 | 12 | #include "nsIStreamTransportService.h" |
michael@0 | 13 | |
michael@0 | 14 | #include "nsNetCID.h" |
michael@0 | 15 | |
michael@0 | 16 | #include "FileHandle.h" |
michael@0 | 17 | #include "FileRequest.h" |
michael@0 | 18 | |
michael@0 | 19 | USING_FILE_NAMESPACE |
michael@0 | 20 | |
michael@0 | 21 | namespace { |
michael@0 | 22 | |
michael@0 | 23 | FileService* gInstance = nullptr; |
michael@0 | 24 | bool gShutdown = false; |
michael@0 | 25 | |
michael@0 | 26 | } // anonymous namespace |
michael@0 | 27 | |
michael@0 | 28 | FileService::FileService() |
michael@0 | 29 | { |
michael@0 | 30 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 31 | NS_ASSERTION(!gInstance, "More than one instance!"); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | FileService::~FileService() |
michael@0 | 35 | { |
michael@0 | 36 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 37 | NS_ASSERTION(!gInstance, "More than one instance!"); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | nsresult |
michael@0 | 41 | FileService::Init() |
michael@0 | 42 | { |
michael@0 | 43 | nsresult rv; |
michael@0 | 44 | mStreamTransportTarget = |
michael@0 | 45 | do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv); |
michael@0 | 46 | |
michael@0 | 47 | return rv; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | nsresult |
michael@0 | 51 | FileService::Cleanup() |
michael@0 | 52 | { |
michael@0 | 53 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 54 | |
michael@0 | 55 | nsIThread* thread = NS_GetCurrentThread(); |
michael@0 | 56 | while (mFileStorageInfos.Count()) { |
michael@0 | 57 | if (!NS_ProcessNextEvent(thread)) { |
michael@0 | 58 | NS_ERROR("Failed to process next event!"); |
michael@0 | 59 | break; |
michael@0 | 60 | } |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | // Make sure the service is still accessible while any generated callbacks |
michael@0 | 64 | // are processed. |
michael@0 | 65 | nsresult rv = NS_ProcessPendingEvents(thread); |
michael@0 | 66 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 67 | |
michael@0 | 68 | if (!mCompleteCallbacks.IsEmpty()) { |
michael@0 | 69 | // Run all callbacks manually now. |
michael@0 | 70 | for (uint32_t index = 0; index < mCompleteCallbacks.Length(); index++) { |
michael@0 | 71 | mCompleteCallbacks[index].mCallback->Run(); |
michael@0 | 72 | } |
michael@0 | 73 | mCompleteCallbacks.Clear(); |
michael@0 | 74 | |
michael@0 | 75 | // And make sure they get processed. |
michael@0 | 76 | rv = NS_ProcessPendingEvents(thread); |
michael@0 | 77 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | return NS_OK; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | // static |
michael@0 | 84 | FileService* |
michael@0 | 85 | FileService::GetOrCreate() |
michael@0 | 86 | { |
michael@0 | 87 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 88 | |
michael@0 | 89 | if (gShutdown) { |
michael@0 | 90 | NS_WARNING("Calling GetOrCreate() after shutdown!"); |
michael@0 | 91 | return nullptr; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | if (!gInstance) { |
michael@0 | 95 | nsRefPtr<FileService> service(new FileService); |
michael@0 | 96 | |
michael@0 | 97 | nsresult rv = service->Init(); |
michael@0 | 98 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 99 | |
michael@0 | 100 | nsCOMPtr<nsIObserverService> obs = |
michael@0 | 101 | do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv); |
michael@0 | 102 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 103 | |
michael@0 | 104 | rv = obs->AddObserver(service, "profile-before-change", false); |
michael@0 | 105 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 106 | |
michael@0 | 107 | // The observer service now owns us. |
michael@0 | 108 | gInstance = service; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | return gInstance; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | // static |
michael@0 | 115 | FileService* |
michael@0 | 116 | FileService::Get() |
michael@0 | 117 | { |
michael@0 | 118 | // Does not return an owning reference. |
michael@0 | 119 | return gInstance; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | // static |
michael@0 | 123 | void |
michael@0 | 124 | FileService::Shutdown() |
michael@0 | 125 | { |
michael@0 | 126 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 127 | |
michael@0 | 128 | gShutdown = true; |
michael@0 | 129 | |
michael@0 | 130 | if (gInstance) { |
michael@0 | 131 | if (NS_FAILED(gInstance->Cleanup())) { |
michael@0 | 132 | NS_WARNING("Failed to shutdown file service!"); |
michael@0 | 133 | } |
michael@0 | 134 | gInstance = nullptr; |
michael@0 | 135 | } |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | // static |
michael@0 | 139 | bool |
michael@0 | 140 | FileService::IsShuttingDown() |
michael@0 | 141 | { |
michael@0 | 142 | return gShutdown; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | nsresult |
michael@0 | 146 | FileService::Enqueue(LockedFile* aLockedFile, FileHelper* aFileHelper) |
michael@0 | 147 | { |
michael@0 | 148 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 149 | NS_ASSERTION(aLockedFile, "Null pointer!"); |
michael@0 | 150 | |
michael@0 | 151 | FileHandle* fileHandle = aLockedFile->mFileHandle; |
michael@0 | 152 | |
michael@0 | 153 | if (fileHandle->mFileStorage->IsInvalidated()) { |
michael@0 | 154 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | const nsACString& storageId = fileHandle->mFileStorage->Id(); |
michael@0 | 158 | const nsAString& fileName = fileHandle->mFileName; |
michael@0 | 159 | bool modeIsWrite = aLockedFile->mMode == FileMode::Readwrite; |
michael@0 | 160 | |
michael@0 | 161 | FileStorageInfo* fileStorageInfo; |
michael@0 | 162 | if (!mFileStorageInfos.Get(storageId, &fileStorageInfo)) { |
michael@0 | 163 | nsAutoPtr<FileStorageInfo> newFileStorageInfo(new FileStorageInfo()); |
michael@0 | 164 | |
michael@0 | 165 | mFileStorageInfos.Put(storageId, newFileStorageInfo); |
michael@0 | 166 | |
michael@0 | 167 | fileStorageInfo = newFileStorageInfo.forget(); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | LockedFileQueue* existingLockedFileQueue = |
michael@0 | 171 | fileStorageInfo->GetLockedFileQueue(aLockedFile); |
michael@0 | 172 | |
michael@0 | 173 | if (existingLockedFileQueue) { |
michael@0 | 174 | existingLockedFileQueue->Enqueue(aFileHelper); |
michael@0 | 175 | return NS_OK; |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | bool lockedForReading = fileStorageInfo->IsFileLockedForReading(fileName); |
michael@0 | 179 | bool lockedForWriting = fileStorageInfo->IsFileLockedForWriting(fileName); |
michael@0 | 180 | |
michael@0 | 181 | if (modeIsWrite) { |
michael@0 | 182 | if (!lockedForWriting) { |
michael@0 | 183 | fileStorageInfo->LockFileForWriting(fileName); |
michael@0 | 184 | } |
michael@0 | 185 | } |
michael@0 | 186 | else { |
michael@0 | 187 | if (!lockedForReading) { |
michael@0 | 188 | fileStorageInfo->LockFileForReading(fileName); |
michael@0 | 189 | } |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | if (lockedForWriting || (lockedForReading && modeIsWrite)) { |
michael@0 | 193 | fileStorageInfo->CreateDelayedEnqueueInfo(aLockedFile, aFileHelper); |
michael@0 | 194 | } |
michael@0 | 195 | else { |
michael@0 | 196 | LockedFileQueue* lockedFileQueue = |
michael@0 | 197 | fileStorageInfo->CreateLockedFileQueue(aLockedFile); |
michael@0 | 198 | |
michael@0 | 199 | if (aFileHelper) { |
michael@0 | 200 | // Enqueue() will queue the file helper if there's already something |
michael@0 | 201 | // running. That can't fail, so no need to eventually remove |
michael@0 | 202 | // fileStorageInfo from the hash table. |
michael@0 | 203 | // |
michael@0 | 204 | // If the file helper is free to run then AsyncRun() is called on the |
michael@0 | 205 | // file helper. AsyncRun() is responsible for calling all necessary |
michael@0 | 206 | // callbacks when something fails. We're propagating the error here, |
michael@0 | 207 | // however there's no need to eventually remove fileStorageInfo from |
michael@0 | 208 | // the hash table. Code behind AsyncRun() will take care of it. The last |
michael@0 | 209 | // item in the code path is NotifyLockedFileCompleted() which removes |
michael@0 | 210 | // fileStorageInfo from the hash table if there are no locked files for |
michael@0 | 211 | // the file storage. |
michael@0 | 212 | nsresult rv = lockedFileQueue->Enqueue(aFileHelper); |
michael@0 | 213 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 214 | } |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | return NS_OK; |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | void |
michael@0 | 221 | FileService::NotifyLockedFileCompleted(LockedFile* aLockedFile) |
michael@0 | 222 | { |
michael@0 | 223 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 224 | NS_ASSERTION(aLockedFile, "Null pointer!"); |
michael@0 | 225 | |
michael@0 | 226 | FileHandle* fileHandle = aLockedFile->mFileHandle; |
michael@0 | 227 | const nsACString& storageId = fileHandle->mFileStorage->Id(); |
michael@0 | 228 | |
michael@0 | 229 | FileStorageInfo* fileStorageInfo; |
michael@0 | 230 | if (!mFileStorageInfos.Get(storageId, &fileStorageInfo)) { |
michael@0 | 231 | NS_ERROR("We don't know anyting about this locked file?!"); |
michael@0 | 232 | return; |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | fileStorageInfo->RemoveLockedFileQueue(aLockedFile); |
michael@0 | 236 | |
michael@0 | 237 | if (!fileStorageInfo->HasRunningLockedFiles()) { |
michael@0 | 238 | mFileStorageInfos.Remove(storageId); |
michael@0 | 239 | |
michael@0 | 240 | // See if we need to fire any complete callbacks. |
michael@0 | 241 | uint32_t index = 0; |
michael@0 | 242 | while (index < mCompleteCallbacks.Length()) { |
michael@0 | 243 | if (MaybeFireCallback(mCompleteCallbacks[index])) { |
michael@0 | 244 | mCompleteCallbacks.RemoveElementAt(index); |
michael@0 | 245 | } |
michael@0 | 246 | else { |
michael@0 | 247 | index++; |
michael@0 | 248 | } |
michael@0 | 249 | } |
michael@0 | 250 | } |
michael@0 | 251 | } |
michael@0 | 252 | |
michael@0 | 253 | void |
michael@0 | 254 | FileService::WaitForStoragesToComplete( |
michael@0 | 255 | nsTArray<nsCOMPtr<nsIFileStorage> >& aStorages, |
michael@0 | 256 | nsIRunnable* aCallback) |
michael@0 | 257 | { |
michael@0 | 258 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 259 | NS_ASSERTION(!aStorages.IsEmpty(), "No databases to wait on!"); |
michael@0 | 260 | NS_ASSERTION(aCallback, "Null pointer!"); |
michael@0 | 261 | |
michael@0 | 262 | StoragesCompleteCallback* callback = mCompleteCallbacks.AppendElement(); |
michael@0 | 263 | callback->mCallback = aCallback; |
michael@0 | 264 | callback->mStorages.SwapElements(aStorages); |
michael@0 | 265 | |
michael@0 | 266 | if (MaybeFireCallback(*callback)) { |
michael@0 | 267 | mCompleteCallbacks.RemoveElementAt(mCompleteCallbacks.Length() - 1); |
michael@0 | 268 | } |
michael@0 | 269 | } |
michael@0 | 270 | |
michael@0 | 271 | void |
michael@0 | 272 | FileService::AbortLockedFilesForStorage(nsIFileStorage* aFileStorage) |
michael@0 | 273 | { |
michael@0 | 274 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 275 | NS_ASSERTION(aFileStorage, "Null pointer!"); |
michael@0 | 276 | |
michael@0 | 277 | FileStorageInfo* fileStorageInfo; |
michael@0 | 278 | if (!mFileStorageInfos.Get(aFileStorage->Id(), &fileStorageInfo)) { |
michael@0 | 279 | return; |
michael@0 | 280 | } |
michael@0 | 281 | |
michael@0 | 282 | nsAutoTArray<nsRefPtr<LockedFile>, 10> lockedFiles; |
michael@0 | 283 | fileStorageInfo->CollectRunningAndDelayedLockedFiles(aFileStorage, |
michael@0 | 284 | lockedFiles); |
michael@0 | 285 | |
michael@0 | 286 | for (uint32_t index = 0; index < lockedFiles.Length(); index++) { |
michael@0 | 287 | ErrorResult ignored; |
michael@0 | 288 | lockedFiles[index]->Abort(ignored); |
michael@0 | 289 | } |
michael@0 | 290 | } |
michael@0 | 291 | |
michael@0 | 292 | bool |
michael@0 | 293 | FileService::HasLockedFilesForStorage(nsIFileStorage* aFileStorage) |
michael@0 | 294 | { |
michael@0 | 295 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 296 | NS_ASSERTION(aFileStorage, "Null pointer!"); |
michael@0 | 297 | |
michael@0 | 298 | FileStorageInfo* fileStorageInfo; |
michael@0 | 299 | if (!mFileStorageInfos.Get(aFileStorage->Id(), &fileStorageInfo)) { |
michael@0 | 300 | return false; |
michael@0 | 301 | } |
michael@0 | 302 | |
michael@0 | 303 | return fileStorageInfo->HasRunningLockedFiles(aFileStorage); |
michael@0 | 304 | } |
michael@0 | 305 | |
michael@0 | 306 | NS_IMPL_ISUPPORTS(FileService, nsIObserver) |
michael@0 | 307 | |
michael@0 | 308 | NS_IMETHODIMP |
michael@0 | 309 | FileService::Observe(nsISupports* aSubject, const char* aTopic, |
michael@0 | 310 | const char16_t* aData) |
michael@0 | 311 | { |
michael@0 | 312 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 313 | NS_ASSERTION(!strcmp(aTopic, "profile-before-change"), "Wrong topic!"); |
michael@0 | 314 | |
michael@0 | 315 | Shutdown(); |
michael@0 | 316 | |
michael@0 | 317 | return NS_OK; |
michael@0 | 318 | } |
michael@0 | 319 | |
michael@0 | 320 | bool |
michael@0 | 321 | FileService::MaybeFireCallback(StoragesCompleteCallback& aCallback) |
michael@0 | 322 | { |
michael@0 | 323 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 324 | |
michael@0 | 325 | for (uint32_t index = 0; index < aCallback.mStorages.Length(); index++) { |
michael@0 | 326 | if (mFileStorageInfos.Get(aCallback.mStorages[index]->Id(), nullptr)) { |
michael@0 | 327 | return false; |
michael@0 | 328 | } |
michael@0 | 329 | } |
michael@0 | 330 | |
michael@0 | 331 | aCallback.mCallback->Run(); |
michael@0 | 332 | return true; |
michael@0 | 333 | } |
michael@0 | 334 | |
michael@0 | 335 | FileService::LockedFileQueue::LockedFileQueue(LockedFile* aLockedFile) |
michael@0 | 336 | : mLockedFile(aLockedFile) |
michael@0 | 337 | { |
michael@0 | 338 | NS_ASSERTION(aLockedFile, "Null pointer!"); |
michael@0 | 339 | } |
michael@0 | 340 | |
michael@0 | 341 | NS_IMPL_ADDREF(FileService::LockedFileQueue) |
michael@0 | 342 | NS_IMPL_RELEASE(FileService::LockedFileQueue) |
michael@0 | 343 | |
michael@0 | 344 | nsresult |
michael@0 | 345 | FileService::LockedFileQueue::Enqueue(FileHelper* aFileHelper) |
michael@0 | 346 | { |
michael@0 | 347 | mQueue.AppendElement(aFileHelper); |
michael@0 | 348 | |
michael@0 | 349 | nsresult rv; |
michael@0 | 350 | if (mLockedFile->mRequestMode == LockedFile::PARALLEL) { |
michael@0 | 351 | rv = aFileHelper->AsyncRun(this); |
michael@0 | 352 | } |
michael@0 | 353 | else { |
michael@0 | 354 | rv = ProcessQueue(); |
michael@0 | 355 | } |
michael@0 | 356 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 357 | |
michael@0 | 358 | return NS_OK; |
michael@0 | 359 | } |
michael@0 | 360 | |
michael@0 | 361 | void |
michael@0 | 362 | FileService::LockedFileQueue::OnFileHelperComplete(FileHelper* aFileHelper) |
michael@0 | 363 | { |
michael@0 | 364 | if (mLockedFile->mRequestMode == LockedFile::PARALLEL) { |
michael@0 | 365 | int32_t index = mQueue.IndexOf(aFileHelper); |
michael@0 | 366 | NS_ASSERTION(index != -1, "We don't know anything about this helper!"); |
michael@0 | 367 | |
michael@0 | 368 | mQueue.RemoveElementAt(index); |
michael@0 | 369 | } |
michael@0 | 370 | else { |
michael@0 | 371 | NS_ASSERTION(mCurrentHelper == aFileHelper, "How can this happen?!"); |
michael@0 | 372 | |
michael@0 | 373 | mCurrentHelper = nullptr; |
michael@0 | 374 | |
michael@0 | 375 | nsresult rv = ProcessQueue(); |
michael@0 | 376 | if (NS_FAILED(rv)) { |
michael@0 | 377 | return; |
michael@0 | 378 | } |
michael@0 | 379 | } |
michael@0 | 380 | } |
michael@0 | 381 | |
michael@0 | 382 | nsresult |
michael@0 | 383 | FileService::LockedFileQueue::ProcessQueue() |
michael@0 | 384 | { |
michael@0 | 385 | if (mQueue.IsEmpty() || mCurrentHelper) { |
michael@0 | 386 | return NS_OK; |
michael@0 | 387 | } |
michael@0 | 388 | |
michael@0 | 389 | mCurrentHelper = mQueue[0]; |
michael@0 | 390 | mQueue.RemoveElementAt(0); |
michael@0 | 391 | |
michael@0 | 392 | nsresult rv = mCurrentHelper->AsyncRun(this); |
michael@0 | 393 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 394 | |
michael@0 | 395 | return NS_OK; |
michael@0 | 396 | } |
michael@0 | 397 | |
michael@0 | 398 | FileService::LockedFileQueue* |
michael@0 | 399 | FileService::FileStorageInfo::CreateLockedFileQueue(LockedFile* aLockedFile) |
michael@0 | 400 | { |
michael@0 | 401 | nsRefPtr<LockedFileQueue>* lockedFileQueue = |
michael@0 | 402 | mLockedFileQueues.AppendElement(); |
michael@0 | 403 | *lockedFileQueue = new LockedFileQueue(aLockedFile); |
michael@0 | 404 | return lockedFileQueue->get(); |
michael@0 | 405 | } |
michael@0 | 406 | |
michael@0 | 407 | FileService::LockedFileQueue* |
michael@0 | 408 | FileService::FileStorageInfo::GetLockedFileQueue(LockedFile* aLockedFile) |
michael@0 | 409 | { |
michael@0 | 410 | uint32_t count = mLockedFileQueues.Length(); |
michael@0 | 411 | for (uint32_t index = 0; index < count; index++) { |
michael@0 | 412 | nsRefPtr<LockedFileQueue>& lockedFileQueue = mLockedFileQueues[index]; |
michael@0 | 413 | if (lockedFileQueue->mLockedFile == aLockedFile) { |
michael@0 | 414 | return lockedFileQueue; |
michael@0 | 415 | } |
michael@0 | 416 | } |
michael@0 | 417 | return nullptr; |
michael@0 | 418 | } |
michael@0 | 419 | |
michael@0 | 420 | void |
michael@0 | 421 | FileService::FileStorageInfo::RemoveLockedFileQueue(LockedFile* aLockedFile) |
michael@0 | 422 | { |
michael@0 | 423 | for (uint32_t index = 0; index < mDelayedEnqueueInfos.Length(); index++) { |
michael@0 | 424 | if (mDelayedEnqueueInfos[index].mLockedFile == aLockedFile) { |
michael@0 | 425 | NS_ASSERTION(!mDelayedEnqueueInfos[index].mFileHelper, "Should be null!"); |
michael@0 | 426 | mDelayedEnqueueInfos.RemoveElementAt(index); |
michael@0 | 427 | return; |
michael@0 | 428 | } |
michael@0 | 429 | } |
michael@0 | 430 | |
michael@0 | 431 | uint32_t lockedFileCount = mLockedFileQueues.Length(); |
michael@0 | 432 | |
michael@0 | 433 | // We can't just remove entries from lock hash tables, we have to rebuild |
michael@0 | 434 | // them instead. Multiple LockedFile objects may lock the same file |
michael@0 | 435 | // (one entry can represent multiple locks). |
michael@0 | 436 | |
michael@0 | 437 | mFilesReading.Clear(); |
michael@0 | 438 | mFilesWriting.Clear(); |
michael@0 | 439 | |
michael@0 | 440 | for (uint32_t index = 0, count = lockedFileCount; index < count; index++) { |
michael@0 | 441 | LockedFile* lockedFile = mLockedFileQueues[index]->mLockedFile; |
michael@0 | 442 | if (lockedFile == aLockedFile) { |
michael@0 | 443 | NS_ASSERTION(count == lockedFileCount, "More than one match?!"); |
michael@0 | 444 | |
michael@0 | 445 | mLockedFileQueues.RemoveElementAt(index); |
michael@0 | 446 | index--; |
michael@0 | 447 | count--; |
michael@0 | 448 | |
michael@0 | 449 | continue; |
michael@0 | 450 | } |
michael@0 | 451 | |
michael@0 | 452 | const nsAString& fileName = lockedFile->mFileHandle->mFileName; |
michael@0 | 453 | |
michael@0 | 454 | if (lockedFile->mMode == FileMode::Readwrite) { |
michael@0 | 455 | if (!IsFileLockedForWriting(fileName)) { |
michael@0 | 456 | LockFileForWriting(fileName); |
michael@0 | 457 | } |
michael@0 | 458 | } |
michael@0 | 459 | else { |
michael@0 | 460 | if (!IsFileLockedForReading(fileName)) { |
michael@0 | 461 | LockFileForReading(fileName); |
michael@0 | 462 | } |
michael@0 | 463 | } |
michael@0 | 464 | } |
michael@0 | 465 | |
michael@0 | 466 | NS_ASSERTION(mLockedFileQueues.Length() == lockedFileCount - 1, |
michael@0 | 467 | "Didn't find the locked file we were looking for!"); |
michael@0 | 468 | |
michael@0 | 469 | nsTArray<DelayedEnqueueInfo> delayedEnqueueInfos; |
michael@0 | 470 | delayedEnqueueInfos.SwapElements(mDelayedEnqueueInfos); |
michael@0 | 471 | |
michael@0 | 472 | for (uint32_t index = 0; index < delayedEnqueueInfos.Length(); index++) { |
michael@0 | 473 | DelayedEnqueueInfo& delayedEnqueueInfo = delayedEnqueueInfos[index]; |
michael@0 | 474 | if (NS_FAILED(gInstance->Enqueue(delayedEnqueueInfo.mLockedFile, |
michael@0 | 475 | delayedEnqueueInfo.mFileHelper))) { |
michael@0 | 476 | NS_WARNING("Enqueue failed!"); |
michael@0 | 477 | } |
michael@0 | 478 | } |
michael@0 | 479 | } |
michael@0 | 480 | |
michael@0 | 481 | bool |
michael@0 | 482 | FileService::FileStorageInfo::HasRunningLockedFiles( |
michael@0 | 483 | nsIFileStorage* aFileStorage) |
michael@0 | 484 | { |
michael@0 | 485 | for (uint32_t index = 0; index < mLockedFileQueues.Length(); index++) { |
michael@0 | 486 | LockedFile* lockedFile = mLockedFileQueues[index]->mLockedFile; |
michael@0 | 487 | if (lockedFile->mFileHandle->mFileStorage == aFileStorage) { |
michael@0 | 488 | return true; |
michael@0 | 489 | } |
michael@0 | 490 | } |
michael@0 | 491 | return false; |
michael@0 | 492 | } |
michael@0 | 493 | |
michael@0 | 494 | FileService::DelayedEnqueueInfo* |
michael@0 | 495 | FileService::FileStorageInfo::CreateDelayedEnqueueInfo(LockedFile* aLockedFile, |
michael@0 | 496 | FileHelper* aFileHelper) |
michael@0 | 497 | { |
michael@0 | 498 | DelayedEnqueueInfo* info = mDelayedEnqueueInfos.AppendElement(); |
michael@0 | 499 | info->mLockedFile = aLockedFile; |
michael@0 | 500 | info->mFileHelper = aFileHelper; |
michael@0 | 501 | return info; |
michael@0 | 502 | } |
michael@0 | 503 | |
michael@0 | 504 | void |
michael@0 | 505 | FileService::FileStorageInfo::CollectRunningAndDelayedLockedFiles( |
michael@0 | 506 | nsIFileStorage* aFileStorage, |
michael@0 | 507 | nsTArray<nsRefPtr<LockedFile> >& aLockedFiles) |
michael@0 | 508 | { |
michael@0 | 509 | for (uint32_t index = 0; index < mLockedFileQueues.Length(); index++) { |
michael@0 | 510 | LockedFile* lockedFile = mLockedFileQueues[index]->mLockedFile; |
michael@0 | 511 | if (lockedFile->mFileHandle->mFileStorage == aFileStorage) { |
michael@0 | 512 | aLockedFiles.AppendElement(lockedFile); |
michael@0 | 513 | } |
michael@0 | 514 | } |
michael@0 | 515 | |
michael@0 | 516 | for (uint32_t index = 0; index < mDelayedEnqueueInfos.Length(); index++) { |
michael@0 | 517 | LockedFile* lockedFile = mDelayedEnqueueInfos[index].mLockedFile; |
michael@0 | 518 | if (lockedFile->mFileHandle->mFileStorage == aFileStorage) { |
michael@0 | 519 | aLockedFiles.AppendElement(lockedFile); |
michael@0 | 520 | } |
michael@0 | 521 | } |
michael@0 | 522 | } |