1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/indexedDB/FileInfo.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,154 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "FileInfo.h" 1.11 +#include "nsThreadUtils.h" 1.12 +#include "mozilla/dom/quota/QuotaManager.h" 1.13 + 1.14 +USING_INDEXEDDB_NAMESPACE 1.15 + 1.16 +namespace { 1.17 + 1.18 +class CleanupFileRunnable MOZ_FINAL : public nsIRunnable 1.19 +{ 1.20 +public: 1.21 + NS_DECL_THREADSAFE_ISUPPORTS 1.22 + NS_DECL_NSIRUNNABLE 1.23 + 1.24 + CleanupFileRunnable(FileManager* aFileManager, int64_t aFileId); 1.25 + 1.26 +private: 1.27 + nsRefPtr<FileManager> mFileManager; 1.28 + int64_t mFileId; 1.29 +}; 1.30 + 1.31 +} // anonymous namespace 1.32 + 1.33 +// static 1.34 +FileInfo* 1.35 +FileInfo::Create(FileManager* aFileManager, int64_t aId) 1.36 +{ 1.37 + MOZ_ASSERT(aId > 0, "Wrong id!"); 1.38 + 1.39 + if (aId <= INT16_MAX) { 1.40 + return new FileInfo16(aFileManager, aId); 1.41 + } 1.42 + 1.43 + if (aId <= INT32_MAX) { 1.44 + return new FileInfo32(aFileManager, aId); 1.45 + } 1.46 + 1.47 + return new FileInfo64(aFileManager, aId); 1.48 +} 1.49 + 1.50 +void 1.51 +FileInfo::GetReferences(int32_t* aRefCnt, int32_t* aDBRefCnt, 1.52 + int32_t* aSliceRefCnt) 1.53 +{ 1.54 + if (IndexedDatabaseManager::IsClosed()) { 1.55 + NS_ERROR("Shouldn't be called after shutdown!"); 1.56 + 1.57 + if (aRefCnt) { 1.58 + *aRefCnt = -1; 1.59 + } 1.60 + 1.61 + if (aDBRefCnt) { 1.62 + *aDBRefCnt = -1; 1.63 + } 1.64 + 1.65 + if (aSliceRefCnt) { 1.66 + *aSliceRefCnt = -1; 1.67 + } 1.68 + 1.69 + return; 1.70 + } 1.71 + 1.72 + MutexAutoLock lock(IndexedDatabaseManager::FileMutex()); 1.73 + 1.74 + if (aRefCnt) { 1.75 + *aRefCnt = mRefCnt; 1.76 + } 1.77 + 1.78 + if (aDBRefCnt) { 1.79 + *aDBRefCnt = mDBRefCnt; 1.80 + } 1.81 + 1.82 + if (aSliceRefCnt) { 1.83 + *aSliceRefCnt = mSliceRefCnt; 1.84 + } 1.85 +} 1.86 + 1.87 +void 1.88 +FileInfo::UpdateReferences(mozilla::ThreadSafeAutoRefCnt& aRefCount, 1.89 + int32_t aDelta, bool aClear) 1.90 +{ 1.91 + if (IndexedDatabaseManager::IsClosed()) { 1.92 + NS_ERROR("Shouldn't be called after shutdown!"); 1.93 + return; 1.94 + } 1.95 + 1.96 + bool needsCleanup; 1.97 + { 1.98 + MutexAutoLock lock(IndexedDatabaseManager::FileMutex()); 1.99 + 1.100 + aRefCount = aClear ? 0 : aRefCount + aDelta; 1.101 + 1.102 + if (mRefCnt + mDBRefCnt + mSliceRefCnt > 0) { 1.103 + return; 1.104 + } 1.105 + 1.106 + mFileManager->mFileInfos.Remove(Id()); 1.107 + 1.108 + needsCleanup = !mFileManager->Invalidated(); 1.109 + } 1.110 + 1.111 + if (needsCleanup) { 1.112 + Cleanup(); 1.113 + } 1.114 + 1.115 + delete this; 1.116 +} 1.117 + 1.118 +void 1.119 +FileInfo::Cleanup() 1.120 +{ 1.121 + nsRefPtr<CleanupFileRunnable> cleaner = 1.122 + new CleanupFileRunnable(mFileManager, Id()); 1.123 + 1.124 + // IndexedDatabaseManager is main-thread only. 1.125 + if (!NS_IsMainThread()) { 1.126 + NS_DispatchToMainThread(cleaner); 1.127 + return; 1.128 + } 1.129 + 1.130 + cleaner->Run(); 1.131 +} 1.132 + 1.133 +CleanupFileRunnable::CleanupFileRunnable(FileManager* aFileManager, 1.134 + int64_t aFileId) 1.135 +: mFileManager(aFileManager), mFileId(aFileId) 1.136 +{ 1.137 +} 1.138 + 1.139 +NS_IMPL_ISUPPORTS(CleanupFileRunnable, 1.140 + nsIRunnable) 1.141 + 1.142 +NS_IMETHODIMP 1.143 +CleanupFileRunnable::Run() 1.144 +{ 1.145 + if (mozilla::dom::quota::QuotaManager::IsShuttingDown()) { 1.146 + return NS_OK; 1.147 + } 1.148 + 1.149 + nsRefPtr<IndexedDatabaseManager> mgr = IndexedDatabaseManager::Get(); 1.150 + MOZ_ASSERT(mgr); 1.151 + 1.152 + if (NS_FAILED(mgr->AsyncDeleteFile(mFileManager, mFileId))) { 1.153 + NS_WARNING("Failed to delete file asynchronously!"); 1.154 + } 1.155 + 1.156 + return NS_OK; 1.157 +}