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.

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim: set ts=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 file,
     5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #ifndef mozilla_dom_file_fileservice_h__
     8 #define mozilla_dom_file_fileservice_h__
    10 #include "FileCommon.h"
    12 #include "nsIObserver.h"
    14 #include "nsClassHashtable.h"
    15 #include "mozilla/Attributes.h"
    17 #include "mozilla/dom/file/FileHelper.h"
    18 #include "mozilla/dom/file/LockedFile.h"
    20 BEGIN_FILE_NAMESPACE
    22 class FileService MOZ_FINAL : public nsIObserver
    23 {
    24 public:
    25   NS_DECL_ISUPPORTS
    26   NS_DECL_NSIOBSERVER
    28   // Returns a non-owning reference!
    29   static FileService*
    30   GetOrCreate();
    32   // Returns a non-owning reference!
    33   static FileService*
    34   Get();
    36   static void
    37   Shutdown();
    39   // Returns true if we've begun the shutdown process.
    40   static bool
    41   IsShuttingDown();
    43   nsresult
    44   Enqueue(LockedFile* aLockedFile, FileHelper* aFileHelper);
    46   void
    47   NotifyLockedFileCompleted(LockedFile* aLockedFile);
    49   void
    50   WaitForStoragesToComplete(nsTArray<nsCOMPtr<nsIFileStorage> >& aStorages,
    51                             nsIRunnable* aCallback);
    53   void
    54   AbortLockedFilesForStorage(nsIFileStorage* aFileStorage);
    56   bool
    57   HasLockedFilesForStorage(nsIFileStorage* aFileStorage);
    59   nsIEventTarget*
    60   StreamTransportTarget()
    61   {
    62     NS_ASSERTION(mStreamTransportTarget, "This should never be null!");
    63     return mStreamTransportTarget;
    64   }
    66 private:
    67   class LockedFileQueue MOZ_FINAL : public FileHelperListener
    68   {
    69     friend class FileService;
    71   public:
    72     NS_IMETHOD_(MozExternalRefCountType)
    73     AddRef() MOZ_OVERRIDE;
    75     NS_IMETHOD_(MozExternalRefCountType)
    76     Release() MOZ_OVERRIDE;
    78     inline nsresult
    79     Enqueue(FileHelper* aFileHelper);
    81     virtual void
    82     OnFileHelperComplete(FileHelper* aFileHelper) MOZ_OVERRIDE;
    84   private:
    85     inline
    86     LockedFileQueue(LockedFile* aLockedFile);
    88     nsresult
    89     ProcessQueue();
    91     ThreadSafeAutoRefCnt mRefCnt;
    92     NS_DECL_OWNINGTHREAD
    93     nsRefPtr<LockedFile> mLockedFile;
    94     nsTArray<nsRefPtr<FileHelper> > mQueue;
    95     nsRefPtr<FileHelper> mCurrentHelper;
    96   };
    98   struct DelayedEnqueueInfo
    99   {
   100     nsRefPtr<LockedFile> mLockedFile;
   101     nsRefPtr<FileHelper> mFileHelper;
   102   };
   104   class FileStorageInfo
   105   {
   106     friend class FileService;
   108   public:
   109     inline LockedFileQueue*
   110     CreateLockedFileQueue(LockedFile* aLockedFile);
   112     inline LockedFileQueue*
   113     GetLockedFileQueue(LockedFile* aLockedFile);
   115     void
   116     RemoveLockedFileQueue(LockedFile* aLockedFile);
   118     bool
   119     HasRunningLockedFiles()
   120     {
   121       return !mLockedFileQueues.IsEmpty();
   122     }
   124     inline bool
   125     HasRunningLockedFiles(nsIFileStorage* aFileStorage);
   127     inline DelayedEnqueueInfo*
   128     CreateDelayedEnqueueInfo(LockedFile* aLockedFile, FileHelper* aFileHelper);
   130     inline void
   131     CollectRunningAndDelayedLockedFiles(
   132                                  nsIFileStorage* aFileStorage,
   133                                  nsTArray<nsRefPtr<LockedFile> >& aLockedFiles);
   135     void
   136     LockFileForReading(const nsAString& aFileName)
   137     {
   138       mFilesReading.PutEntry(aFileName);
   139     }
   141     void
   142     LockFileForWriting(const nsAString& aFileName)
   143     {
   144       mFilesWriting.PutEntry(aFileName);
   145     }
   147     bool
   148     IsFileLockedForReading(const nsAString& aFileName)
   149     {
   150       return mFilesReading.Contains(aFileName);
   151     }
   153     bool
   154     IsFileLockedForWriting(const nsAString& aFileName)
   155     {
   156       return mFilesWriting.Contains(aFileName);
   157     }
   159   private:
   160     FileStorageInfo()
   161     {
   162     }
   164     nsTArray<nsRefPtr<LockedFileQueue> > mLockedFileQueues;
   165     nsTArray<DelayedEnqueueInfo> mDelayedEnqueueInfos;
   166     nsTHashtable<nsStringHashKey> mFilesReading;
   167     nsTHashtable<nsStringHashKey> mFilesWriting;
   168   };
   170   struct StoragesCompleteCallback
   171   {
   172     nsTArray<nsCOMPtr<nsIFileStorage> > mStorages;
   173     nsCOMPtr<nsIRunnable> mCallback;
   174   };
   176   FileService();
   177   ~FileService();
   179   nsresult
   180   Init();
   182   nsresult
   183   Cleanup();
   185   bool
   186   MaybeFireCallback(StoragesCompleteCallback& aCallback);
   188   nsCOMPtr<nsIEventTarget> mStreamTransportTarget;
   189   nsClassHashtable<nsCStringHashKey, FileStorageInfo> mFileStorageInfos;
   190   nsTArray<StoragesCompleteCallback> mCompleteCallbacks;
   191 };
   193 END_FILE_NAMESPACE
   195 #endif /* mozilla_dom_file_fileservice_h__ */

mercurial