dom/indexedDB/FileInfo.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "FileInfo.h"
michael@0 8 #include "nsThreadUtils.h"
michael@0 9 #include "mozilla/dom/quota/QuotaManager.h"
michael@0 10
michael@0 11 USING_INDEXEDDB_NAMESPACE
michael@0 12
michael@0 13 namespace {
michael@0 14
michael@0 15 class CleanupFileRunnable MOZ_FINAL : public nsIRunnable
michael@0 16 {
michael@0 17 public:
michael@0 18 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 19 NS_DECL_NSIRUNNABLE
michael@0 20
michael@0 21 CleanupFileRunnable(FileManager* aFileManager, int64_t aFileId);
michael@0 22
michael@0 23 private:
michael@0 24 nsRefPtr<FileManager> mFileManager;
michael@0 25 int64_t mFileId;
michael@0 26 };
michael@0 27
michael@0 28 } // anonymous namespace
michael@0 29
michael@0 30 // static
michael@0 31 FileInfo*
michael@0 32 FileInfo::Create(FileManager* aFileManager, int64_t aId)
michael@0 33 {
michael@0 34 MOZ_ASSERT(aId > 0, "Wrong id!");
michael@0 35
michael@0 36 if (aId <= INT16_MAX) {
michael@0 37 return new FileInfo16(aFileManager, aId);
michael@0 38 }
michael@0 39
michael@0 40 if (aId <= INT32_MAX) {
michael@0 41 return new FileInfo32(aFileManager, aId);
michael@0 42 }
michael@0 43
michael@0 44 return new FileInfo64(aFileManager, aId);
michael@0 45 }
michael@0 46
michael@0 47 void
michael@0 48 FileInfo::GetReferences(int32_t* aRefCnt, int32_t* aDBRefCnt,
michael@0 49 int32_t* aSliceRefCnt)
michael@0 50 {
michael@0 51 if (IndexedDatabaseManager::IsClosed()) {
michael@0 52 NS_ERROR("Shouldn't be called after shutdown!");
michael@0 53
michael@0 54 if (aRefCnt) {
michael@0 55 *aRefCnt = -1;
michael@0 56 }
michael@0 57
michael@0 58 if (aDBRefCnt) {
michael@0 59 *aDBRefCnt = -1;
michael@0 60 }
michael@0 61
michael@0 62 if (aSliceRefCnt) {
michael@0 63 *aSliceRefCnt = -1;
michael@0 64 }
michael@0 65
michael@0 66 return;
michael@0 67 }
michael@0 68
michael@0 69 MutexAutoLock lock(IndexedDatabaseManager::FileMutex());
michael@0 70
michael@0 71 if (aRefCnt) {
michael@0 72 *aRefCnt = mRefCnt;
michael@0 73 }
michael@0 74
michael@0 75 if (aDBRefCnt) {
michael@0 76 *aDBRefCnt = mDBRefCnt;
michael@0 77 }
michael@0 78
michael@0 79 if (aSliceRefCnt) {
michael@0 80 *aSliceRefCnt = mSliceRefCnt;
michael@0 81 }
michael@0 82 }
michael@0 83
michael@0 84 void
michael@0 85 FileInfo::UpdateReferences(mozilla::ThreadSafeAutoRefCnt& aRefCount,
michael@0 86 int32_t aDelta, bool aClear)
michael@0 87 {
michael@0 88 if (IndexedDatabaseManager::IsClosed()) {
michael@0 89 NS_ERROR("Shouldn't be called after shutdown!");
michael@0 90 return;
michael@0 91 }
michael@0 92
michael@0 93 bool needsCleanup;
michael@0 94 {
michael@0 95 MutexAutoLock lock(IndexedDatabaseManager::FileMutex());
michael@0 96
michael@0 97 aRefCount = aClear ? 0 : aRefCount + aDelta;
michael@0 98
michael@0 99 if (mRefCnt + mDBRefCnt + mSliceRefCnt > 0) {
michael@0 100 return;
michael@0 101 }
michael@0 102
michael@0 103 mFileManager->mFileInfos.Remove(Id());
michael@0 104
michael@0 105 needsCleanup = !mFileManager->Invalidated();
michael@0 106 }
michael@0 107
michael@0 108 if (needsCleanup) {
michael@0 109 Cleanup();
michael@0 110 }
michael@0 111
michael@0 112 delete this;
michael@0 113 }
michael@0 114
michael@0 115 void
michael@0 116 FileInfo::Cleanup()
michael@0 117 {
michael@0 118 nsRefPtr<CleanupFileRunnable> cleaner =
michael@0 119 new CleanupFileRunnable(mFileManager, Id());
michael@0 120
michael@0 121 // IndexedDatabaseManager is main-thread only.
michael@0 122 if (!NS_IsMainThread()) {
michael@0 123 NS_DispatchToMainThread(cleaner);
michael@0 124 return;
michael@0 125 }
michael@0 126
michael@0 127 cleaner->Run();
michael@0 128 }
michael@0 129
michael@0 130 CleanupFileRunnable::CleanupFileRunnable(FileManager* aFileManager,
michael@0 131 int64_t aFileId)
michael@0 132 : mFileManager(aFileManager), mFileId(aFileId)
michael@0 133 {
michael@0 134 }
michael@0 135
michael@0 136 NS_IMPL_ISUPPORTS(CleanupFileRunnable,
michael@0 137 nsIRunnable)
michael@0 138
michael@0 139 NS_IMETHODIMP
michael@0 140 CleanupFileRunnable::Run()
michael@0 141 {
michael@0 142 if (mozilla::dom::quota::QuotaManager::IsShuttingDown()) {
michael@0 143 return NS_OK;
michael@0 144 }
michael@0 145
michael@0 146 nsRefPtr<IndexedDatabaseManager> mgr = IndexedDatabaseManager::Get();
michael@0 147 MOZ_ASSERT(mgr);
michael@0 148
michael@0 149 if (NS_FAILED(mgr->AsyncDeleteFile(mFileManager, mFileId))) {
michael@0 150 NS_WARNING("Failed to delete file asynchronously!");
michael@0 151 }
michael@0 152
michael@0 153 return NS_OK;
michael@0 154 }

mercurial