dom/filehandle/FileService.h
author Jan Varga <jan.varga@gmail.com>
Wed, 07 May 2014 16:32:12 +0200
changeset 201268 03fdda1af1f4fc0bdf7bd71c765b89216a5a2737
parent 194309 dom/file/FileService.h@ad7d8b0efb321de10f2ac7191e32ff6e278c84f9
child 201269 ef60f8aa9a017213edff0e6413b43c4eed15e66b
permissions -rw-r--r--
Bug 975696 - Split ArchiveReader and FileHandle implementation into separate dirs. r=bent f=baku

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_FileService_h
#define mozilla_dom_FileService_h

#include "mozilla/Attributes.h"
#include "mozilla/dom/FileHelper.h"
#include "nsClassHashtable.h"
#include "nsIObserver.h"
#include "nsAutoPtr.h"
#include "nsCOMPtr.h"
#include "nsDebug.h"
#include "nsHashKeys.h"
#include "nsTArray.h"
#include "nsTHashtable.h"

class nsAString;
class nsIEventTarget;
class nsIFileStorage;
class nsIRunnable;

namespace mozilla {
namespace dom {

class LockedFile;

class FileService MOZ_FINAL : public nsIObserver
{
public:
  NS_DECL_ISUPPORTS
  NS_DECL_NSIOBSERVER

  // Returns a non-owning reference!
  static FileService*
  GetOrCreate();

  // Returns a non-owning reference!
  static FileService*
  Get();

  static void
  Shutdown();

  // Returns true if we've begun the shutdown process.
  static bool
  IsShuttingDown();

  nsresult
  Enqueue(LockedFile* aLockedFile, FileHelper* aFileHelper);

  void
  NotifyLockedFileCompleted(LockedFile* aLockedFile);

  void
  WaitForStoragesToComplete(nsTArray<nsCOMPtr<nsIFileStorage> >& aStorages,
                            nsIRunnable* aCallback);

  void
  AbortLockedFilesForStorage(nsIFileStorage* aFileStorage);

  bool
  HasLockedFilesForStorage(nsIFileStorage* aFileStorage);

  nsIEventTarget*
  StreamTransportTarget()
  {
    NS_ASSERTION(mStreamTransportTarget, "This should never be null!");
    return mStreamTransportTarget;
  }

private:
  class LockedFileQueue MOZ_FINAL : public FileHelperListener
  {
    friend class FileService;

  public:
    NS_IMETHOD_(MozExternalRefCountType)
    AddRef() MOZ_OVERRIDE;

    NS_IMETHOD_(MozExternalRefCountType)
    Release() MOZ_OVERRIDE;

    inline nsresult
    Enqueue(FileHelper* aFileHelper);

    virtual void
    OnFileHelperComplete(FileHelper* aFileHelper) MOZ_OVERRIDE;

  private:
    inline
    LockedFileQueue(LockedFile* aLockedFile);

    nsresult
    ProcessQueue();

    ThreadSafeAutoRefCnt mRefCnt;
    NS_DECL_OWNINGTHREAD
    nsRefPtr<LockedFile> mLockedFile;
    nsTArray<nsRefPtr<FileHelper> > mQueue;
    nsRefPtr<FileHelper> mCurrentHelper;
  };

  struct DelayedEnqueueInfo
  {
    DelayedEnqueueInfo();
    ~DelayedEnqueueInfo();

    nsRefPtr<LockedFile> mLockedFile;
    nsRefPtr<FileHelper> mFileHelper;
  };

  class FileStorageInfo
  {
    friend class FileService;

  public:
    inline LockedFileQueue*
    CreateLockedFileQueue(LockedFile* aLockedFile);

    inline LockedFileQueue*
    GetLockedFileQueue(LockedFile* aLockedFile);

    void
    RemoveLockedFileQueue(LockedFile* aLockedFile);

    bool
    HasRunningLockedFiles()
    {
      return !mLockedFileQueues.IsEmpty();
    }

    inline bool
    HasRunningLockedFiles(nsIFileStorage* aFileStorage);

    inline DelayedEnqueueInfo*
    CreateDelayedEnqueueInfo(LockedFile* aLockedFile, FileHelper* aFileHelper);

    inline void
    CollectRunningAndDelayedLockedFiles(
                                 nsIFileStorage* aFileStorage,
                                 nsTArray<nsRefPtr<LockedFile> >& aLockedFiles);

    void
    LockFileForReading(const nsAString& aFileName)
    {
      mFilesReading.PutEntry(aFileName);
    }

    void
    LockFileForWriting(const nsAString& aFileName)
    {
      mFilesWriting.PutEntry(aFileName);
    }

    bool
    IsFileLockedForReading(const nsAString& aFileName)
    {
      return mFilesReading.Contains(aFileName);
    }

    bool
    IsFileLockedForWriting(const nsAString& aFileName)
    {
      return mFilesWriting.Contains(aFileName);
    }

  private:
    FileStorageInfo()
    {
    }

    nsTArray<nsRefPtr<LockedFileQueue> > mLockedFileQueues;
    nsTArray<DelayedEnqueueInfo> mDelayedEnqueueInfos;
    nsTHashtable<nsStringHashKey> mFilesReading;
    nsTHashtable<nsStringHashKey> mFilesWriting;
  };

  struct StoragesCompleteCallback
  {
    nsTArray<nsCOMPtr<nsIFileStorage> > mStorages;
    nsCOMPtr<nsIRunnable> mCallback;
  };

  FileService();
  ~FileService();

  nsresult
  Init();

  nsresult
  Cleanup();

  bool
  MaybeFireCallback(StoragesCompleteCallback& aCallback);

  nsCOMPtr<nsIEventTarget> mStreamTransportTarget;
  nsClassHashtable<nsCStringHashKey, FileStorageInfo> mFileStorageInfos;
  nsTArray<StoragesCompleteCallback> mCompleteCallbacks;
};

} // namespace dom
} // namespace mozilla

#endif // mozilla_dom_FileService_h