2012-06-03 16:33:52 +00:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
2014-05-07 14:32:12 +00:00
|
|
|
#ifndef mozilla_dom_FileService_h
|
|
|
|
#define mozilla_dom_FileService_h
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2014-05-07 14:32:12 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/dom/FileHelper.h"
|
|
|
|
#include "nsClassHashtable.h"
|
2012-06-03 16:33:52 +00:00
|
|
|
#include "nsIObserver.h"
|
2014-05-07 14:32:12 +00:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsTHashtable.h"
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2014-05-07 14:32:12 +00:00
|
|
|
class nsAString;
|
|
|
|
class nsIEventTarget;
|
2014-05-07 14:33:02 +00:00
|
|
|
class nsIOfflineStorage;
|
2014-05-07 14:32:12 +00:00
|
|
|
class nsIRunnable;
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2014-05-07 14:32:12 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2014-07-17 16:40:54 +00:00
|
|
|
class FileHandleBase;
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2012-06-15 02:31:55 +00:00
|
|
|
class FileService MOZ_FINAL : public nsIObserver
|
2012-06-03 16:33:52 +00:00
|
|
|
{
|
|
|
|
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
|
2014-07-17 16:40:54 +00:00
|
|
|
Enqueue(FileHandleBase* aFileHandle, FileHelper* aFileHelper);
|
2012-06-03 16:33:52 +00:00
|
|
|
|
|
|
|
void
|
2014-07-17 16:40:54 +00:00
|
|
|
NotifyFileHandleCompleted(FileHandleBase* aFileHandle);
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
void
|
2014-05-07 14:33:02 +00:00
|
|
|
WaitForStoragesToComplete(nsTArray<nsCOMPtr<nsIOfflineStorage> >& aStorages,
|
2013-03-26 11:13:17 +00:00
|
|
|
nsIRunnable* aCallback);
|
2012-06-03 16:33:52 +00:00
|
|
|
|
|
|
|
void
|
2014-06-12 03:35:29 +00:00
|
|
|
AbortFileHandlesForStorage(nsIOfflineStorage* aStorage);
|
2012-06-03 16:33:52 +00:00
|
|
|
|
|
|
|
bool
|
2014-06-12 03:35:29 +00:00
|
|
|
HasFileHandlesForStorage(nsIOfflineStorage* aStorage);
|
2012-06-03 16:33:52 +00:00
|
|
|
|
|
|
|
nsIEventTarget*
|
|
|
|
StreamTransportTarget()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mStreamTransportTarget, "This should never be null!");
|
|
|
|
return mStreamTransportTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-12 03:35:29 +00:00
|
|
|
class FileHandleQueue MOZ_FINAL : public FileHelperListener
|
2012-06-03 16:33:52 +00:00
|
|
|
{
|
|
|
|
friend class FileService;
|
|
|
|
|
|
|
|
public:
|
2014-03-27 20:38:33 +00:00
|
|
|
NS_IMETHOD_(MozExternalRefCountType)
|
2013-06-05 16:15:48 +00:00
|
|
|
AddRef() MOZ_OVERRIDE;
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2014-03-27 20:38:33 +00:00
|
|
|
NS_IMETHOD_(MozExternalRefCountType)
|
2013-06-05 16:15:48 +00:00
|
|
|
Release() MOZ_OVERRIDE;
|
2012-06-03 16:33:52 +00:00
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
Enqueue(FileHelper* aFileHelper);
|
|
|
|
|
|
|
|
virtual void
|
2013-06-05 16:15:48 +00:00
|
|
|
OnFileHelperComplete(FileHelper* aFileHelper) MOZ_OVERRIDE;
|
2012-06-03 16:33:52 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
inline
|
2014-09-01 22:26:43 +00:00
|
|
|
explicit FileHandleQueue(FileHandleBase* aFileHandle);
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2014-06-23 19:56:07 +00:00
|
|
|
~FileHandleQueue();
|
|
|
|
|
2012-06-03 16:33:52 +00:00
|
|
|
nsresult
|
|
|
|
ProcessQueue();
|
|
|
|
|
2013-07-19 02:21:20 +00:00
|
|
|
ThreadSafeAutoRefCnt mRefCnt;
|
2012-06-03 16:33:52 +00:00
|
|
|
NS_DECL_OWNINGTHREAD
|
2014-07-17 16:40:54 +00:00
|
|
|
nsRefPtr<FileHandleBase> mFileHandle;
|
2012-06-03 16:33:52 +00:00
|
|
|
nsTArray<nsRefPtr<FileHelper> > mQueue;
|
|
|
|
nsRefPtr<FileHelper> mCurrentHelper;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DelayedEnqueueInfo
|
|
|
|
{
|
2014-05-07 14:32:12 +00:00
|
|
|
DelayedEnqueueInfo();
|
|
|
|
~DelayedEnqueueInfo();
|
|
|
|
|
2014-07-17 16:40:54 +00:00
|
|
|
nsRefPtr<FileHandleBase> mFileHandle;
|
2012-06-03 16:33:52 +00:00
|
|
|
nsRefPtr<FileHelper> mFileHelper;
|
|
|
|
};
|
|
|
|
|
2014-05-07 14:33:02 +00:00
|
|
|
class StorageInfo
|
2012-06-03 16:33:52 +00:00
|
|
|
{
|
|
|
|
friend class FileService;
|
|
|
|
|
|
|
|
public:
|
2014-06-12 03:35:29 +00:00
|
|
|
inline FileHandleQueue*
|
2014-07-17 16:40:54 +00:00
|
|
|
CreateFileHandleQueue(FileHandleBase* aFileHandle);
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2014-06-12 03:35:29 +00:00
|
|
|
inline FileHandleQueue*
|
2014-07-17 16:40:54 +00:00
|
|
|
GetFileHandleQueue(FileHandleBase* aFileHandle);
|
2012-06-03 16:33:52 +00:00
|
|
|
|
|
|
|
void
|
2014-07-17 16:40:54 +00:00
|
|
|
RemoveFileHandleQueue(FileHandleBase* aFileHandle);
|
2012-06-03 16:33:52 +00:00
|
|
|
|
|
|
|
bool
|
2014-06-12 03:35:29 +00:00
|
|
|
HasRunningFileHandles()
|
2012-06-03 16:33:52 +00:00
|
|
|
{
|
2014-06-12 03:35:29 +00:00
|
|
|
return !mFileHandleQueues.IsEmpty();
|
2012-06-03 16:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2014-06-12 03:35:29 +00:00
|
|
|
HasRunningFileHandles(nsIOfflineStorage* aStorage);
|
2012-06-03 16:33:52 +00:00
|
|
|
|
|
|
|
inline DelayedEnqueueInfo*
|
2014-07-17 16:40:54 +00:00
|
|
|
CreateDelayedEnqueueInfo(FileHandleBase* aFileHandle,
|
|
|
|
FileHelper* aFileHelper);
|
2012-06-03 16:33:52 +00:00
|
|
|
|
|
|
|
inline void
|
2014-06-12 03:35:29 +00:00
|
|
|
CollectRunningAndDelayedFileHandles(
|
2014-07-17 16:40:54 +00:00
|
|
|
nsIOfflineStorage* aStorage,
|
|
|
|
nsTArray<nsRefPtr<FileHandleBase>>& aFileHandles);
|
2012-06-03 16:33:52 +00:00
|
|
|
|
|
|
|
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:
|
2014-05-07 14:33:02 +00:00
|
|
|
StorageInfo()
|
2012-06-03 16:33:52 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:29 +00:00
|
|
|
nsTArray<nsRefPtr<FileHandleQueue>> mFileHandleQueues;
|
2012-06-03 16:33:52 +00:00
|
|
|
nsTArray<DelayedEnqueueInfo> mDelayedEnqueueInfos;
|
|
|
|
nsTHashtable<nsStringHashKey> mFilesReading;
|
|
|
|
nsTHashtable<nsStringHashKey> mFilesWriting;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct StoragesCompleteCallback
|
|
|
|
{
|
2014-05-07 14:33:02 +00:00
|
|
|
nsTArray<nsCOMPtr<nsIOfflineStorage> > mStorages;
|
2012-06-03 16:33:52 +00:00
|
|
|
nsCOMPtr<nsIRunnable> mCallback;
|
|
|
|
};
|
|
|
|
|
|
|
|
FileService();
|
|
|
|
~FileService();
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Init();
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Cleanup();
|
|
|
|
|
|
|
|
bool
|
|
|
|
MaybeFireCallback(StoragesCompleteCallback& aCallback);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> mStreamTransportTarget;
|
2014-05-07 14:33:02 +00:00
|
|
|
nsClassHashtable<nsCStringHashKey, StorageInfo> mStorageInfos;
|
2012-06-03 16:33:52 +00:00
|
|
|
nsTArray<StoragesCompleteCallback> mCompleteCallbacks;
|
|
|
|
};
|
|
|
|
|
2014-05-07 14:32:12 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2014-05-07 14:32:12 +00:00
|
|
|
#endif // mozilla_dom_FileService_h
|