dom/file/FileService.h

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

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 #ifndef mozilla_dom_file_fileservice_h__
michael@0 8 #define mozilla_dom_file_fileservice_h__
michael@0 9
michael@0 10 #include "FileCommon.h"
michael@0 11
michael@0 12 #include "nsIObserver.h"
michael@0 13
michael@0 14 #include "nsClassHashtable.h"
michael@0 15 #include "mozilla/Attributes.h"
michael@0 16
michael@0 17 #include "mozilla/dom/file/FileHelper.h"
michael@0 18 #include "mozilla/dom/file/LockedFile.h"
michael@0 19
michael@0 20 BEGIN_FILE_NAMESPACE
michael@0 21
michael@0 22 class FileService MOZ_FINAL : public nsIObserver
michael@0 23 {
michael@0 24 public:
michael@0 25 NS_DECL_ISUPPORTS
michael@0 26 NS_DECL_NSIOBSERVER
michael@0 27
michael@0 28 // Returns a non-owning reference!
michael@0 29 static FileService*
michael@0 30 GetOrCreate();
michael@0 31
michael@0 32 // Returns a non-owning reference!
michael@0 33 static FileService*
michael@0 34 Get();
michael@0 35
michael@0 36 static void
michael@0 37 Shutdown();
michael@0 38
michael@0 39 // Returns true if we've begun the shutdown process.
michael@0 40 static bool
michael@0 41 IsShuttingDown();
michael@0 42
michael@0 43 nsresult
michael@0 44 Enqueue(LockedFile* aLockedFile, FileHelper* aFileHelper);
michael@0 45
michael@0 46 void
michael@0 47 NotifyLockedFileCompleted(LockedFile* aLockedFile);
michael@0 48
michael@0 49 void
michael@0 50 WaitForStoragesToComplete(nsTArray<nsCOMPtr<nsIFileStorage> >& aStorages,
michael@0 51 nsIRunnable* aCallback);
michael@0 52
michael@0 53 void
michael@0 54 AbortLockedFilesForStorage(nsIFileStorage* aFileStorage);
michael@0 55
michael@0 56 bool
michael@0 57 HasLockedFilesForStorage(nsIFileStorage* aFileStorage);
michael@0 58
michael@0 59 nsIEventTarget*
michael@0 60 StreamTransportTarget()
michael@0 61 {
michael@0 62 NS_ASSERTION(mStreamTransportTarget, "This should never be null!");
michael@0 63 return mStreamTransportTarget;
michael@0 64 }
michael@0 65
michael@0 66 private:
michael@0 67 class LockedFileQueue MOZ_FINAL : public FileHelperListener
michael@0 68 {
michael@0 69 friend class FileService;
michael@0 70
michael@0 71 public:
michael@0 72 NS_IMETHOD_(MozExternalRefCountType)
michael@0 73 AddRef() MOZ_OVERRIDE;
michael@0 74
michael@0 75 NS_IMETHOD_(MozExternalRefCountType)
michael@0 76 Release() MOZ_OVERRIDE;
michael@0 77
michael@0 78 inline nsresult
michael@0 79 Enqueue(FileHelper* aFileHelper);
michael@0 80
michael@0 81 virtual void
michael@0 82 OnFileHelperComplete(FileHelper* aFileHelper) MOZ_OVERRIDE;
michael@0 83
michael@0 84 private:
michael@0 85 inline
michael@0 86 LockedFileQueue(LockedFile* aLockedFile);
michael@0 87
michael@0 88 nsresult
michael@0 89 ProcessQueue();
michael@0 90
michael@0 91 ThreadSafeAutoRefCnt mRefCnt;
michael@0 92 NS_DECL_OWNINGTHREAD
michael@0 93 nsRefPtr<LockedFile> mLockedFile;
michael@0 94 nsTArray<nsRefPtr<FileHelper> > mQueue;
michael@0 95 nsRefPtr<FileHelper> mCurrentHelper;
michael@0 96 };
michael@0 97
michael@0 98 struct DelayedEnqueueInfo
michael@0 99 {
michael@0 100 nsRefPtr<LockedFile> mLockedFile;
michael@0 101 nsRefPtr<FileHelper> mFileHelper;
michael@0 102 };
michael@0 103
michael@0 104 class FileStorageInfo
michael@0 105 {
michael@0 106 friend class FileService;
michael@0 107
michael@0 108 public:
michael@0 109 inline LockedFileQueue*
michael@0 110 CreateLockedFileQueue(LockedFile* aLockedFile);
michael@0 111
michael@0 112 inline LockedFileQueue*
michael@0 113 GetLockedFileQueue(LockedFile* aLockedFile);
michael@0 114
michael@0 115 void
michael@0 116 RemoveLockedFileQueue(LockedFile* aLockedFile);
michael@0 117
michael@0 118 bool
michael@0 119 HasRunningLockedFiles()
michael@0 120 {
michael@0 121 return !mLockedFileQueues.IsEmpty();
michael@0 122 }
michael@0 123
michael@0 124 inline bool
michael@0 125 HasRunningLockedFiles(nsIFileStorage* aFileStorage);
michael@0 126
michael@0 127 inline DelayedEnqueueInfo*
michael@0 128 CreateDelayedEnqueueInfo(LockedFile* aLockedFile, FileHelper* aFileHelper);
michael@0 129
michael@0 130 inline void
michael@0 131 CollectRunningAndDelayedLockedFiles(
michael@0 132 nsIFileStorage* aFileStorage,
michael@0 133 nsTArray<nsRefPtr<LockedFile> >& aLockedFiles);
michael@0 134
michael@0 135 void
michael@0 136 LockFileForReading(const nsAString& aFileName)
michael@0 137 {
michael@0 138 mFilesReading.PutEntry(aFileName);
michael@0 139 }
michael@0 140
michael@0 141 void
michael@0 142 LockFileForWriting(const nsAString& aFileName)
michael@0 143 {
michael@0 144 mFilesWriting.PutEntry(aFileName);
michael@0 145 }
michael@0 146
michael@0 147 bool
michael@0 148 IsFileLockedForReading(const nsAString& aFileName)
michael@0 149 {
michael@0 150 return mFilesReading.Contains(aFileName);
michael@0 151 }
michael@0 152
michael@0 153 bool
michael@0 154 IsFileLockedForWriting(const nsAString& aFileName)
michael@0 155 {
michael@0 156 return mFilesWriting.Contains(aFileName);
michael@0 157 }
michael@0 158
michael@0 159 private:
michael@0 160 FileStorageInfo()
michael@0 161 {
michael@0 162 }
michael@0 163
michael@0 164 nsTArray<nsRefPtr<LockedFileQueue> > mLockedFileQueues;
michael@0 165 nsTArray<DelayedEnqueueInfo> mDelayedEnqueueInfos;
michael@0 166 nsTHashtable<nsStringHashKey> mFilesReading;
michael@0 167 nsTHashtable<nsStringHashKey> mFilesWriting;
michael@0 168 };
michael@0 169
michael@0 170 struct StoragesCompleteCallback
michael@0 171 {
michael@0 172 nsTArray<nsCOMPtr<nsIFileStorage> > mStorages;
michael@0 173 nsCOMPtr<nsIRunnable> mCallback;
michael@0 174 };
michael@0 175
michael@0 176 FileService();
michael@0 177 ~FileService();
michael@0 178
michael@0 179 nsresult
michael@0 180 Init();
michael@0 181
michael@0 182 nsresult
michael@0 183 Cleanup();
michael@0 184
michael@0 185 bool
michael@0 186 MaybeFireCallback(StoragesCompleteCallback& aCallback);
michael@0 187
michael@0 188 nsCOMPtr<nsIEventTarget> mStreamTransportTarget;
michael@0 189 nsClassHashtable<nsCStringHashKey, FileStorageInfo> mFileStorageInfos;
michael@0 190 nsTArray<StoragesCompleteCallback> mCompleteCallbacks;
michael@0 191 };
michael@0 192
michael@0 193 END_FILE_NAMESPACE
michael@0 194
michael@0 195 #endif /* mozilla_dom_file_fileservice_h__ */

mercurial