2013-04-16 14:16:08 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 sw=2 et tw=80: */
|
2012-08-01 23:29:34 -07:00
|
|
|
/* 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 DeviceStorage_h
|
|
|
|
#define DeviceStorage_h
|
|
|
|
|
|
|
|
#include "nsIDOMDeviceStorage.h"
|
|
|
|
#include "nsIFile.h"
|
2012-07-30 07:58:26 -07:00
|
|
|
#include "nsIPrincipal.h"
|
2012-08-01 23:32:04 -07:00
|
|
|
#include "nsIObserver.h"
|
2012-08-01 23:29:34 -07:00
|
|
|
#include "nsDOMEventTargetHelper.h"
|
2013-05-11 02:10:18 -07:00
|
|
|
#include "mozilla/RefPtr.h"
|
2013-01-09 07:03:28 -08:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2013-05-11 02:10:18 -07:00
|
|
|
#include "DOMRequest.h"
|
2012-08-01 23:29:34 -07:00
|
|
|
|
2013-05-02 16:26:31 -07:00
|
|
|
#define DEVICESTORAGE_PICTURES "pictures"
|
|
|
|
#define DEVICESTORAGE_VIDEOS "videos"
|
|
|
|
#define DEVICESTORAGE_MUSIC "music"
|
|
|
|
#define DEVICESTORAGE_APPS "apps"
|
|
|
|
#define DEVICESTORAGE_SDCARD "sdcard"
|
2013-08-23 07:59:03 -07:00
|
|
|
#define DEVICESTORAGE_CRASHES "crashes"
|
2013-05-02 16:26:31 -07:00
|
|
|
|
2013-09-06 10:50:24 -07:00
|
|
|
class nsIInputStream;
|
|
|
|
|
2013-05-31 23:54:46 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2013-05-31 23:56:00 -07:00
|
|
|
class DeviceStorageEnumerationParameters;
|
|
|
|
class DOMCursor;
|
2013-05-31 23:54:46 -07:00
|
|
|
class DOMRequest;
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2012-12-04 18:00:39 -08:00
|
|
|
class DeviceStorageFile MOZ_FINAL
|
|
|
|
: public nsISupports {
|
|
|
|
public:
|
|
|
|
nsCOMPtr<nsIFile> mFile;
|
|
|
|
nsString mStorageType;
|
2013-05-11 02:10:18 -07:00
|
|
|
nsString mStorageName;
|
2013-04-18 07:13:23 -07:00
|
|
|
nsString mRootDir;
|
2013-05-11 02:10:18 -07:00
|
|
|
nsString mPath;
|
2012-12-04 18:00:39 -08:00
|
|
|
bool mEditable;
|
2013-05-17 07:41:53 -07:00
|
|
|
nsString mMimeType;
|
|
|
|
uint64_t mLength;
|
|
|
|
uint64_t mLastModifiedDate;
|
2012-12-04 18:00:39 -08:00
|
|
|
|
2013-04-18 07:13:23 -07:00
|
|
|
// Used when the path will be set later via SetPath.
|
2013-05-11 02:10:18 -07:00
|
|
|
DeviceStorageFile(const nsAString& aStorageType,
|
|
|
|
const nsAString& aStorageName);
|
2013-04-18 07:13:23 -07:00
|
|
|
// Used for non-enumeration purposes.
|
2013-05-11 02:10:18 -07:00
|
|
|
DeviceStorageFile(const nsAString& aStorageType,
|
|
|
|
const nsAString& aStorageName,
|
|
|
|
const nsAString& aPath);
|
2013-08-02 16:35:16 -07:00
|
|
|
// Used for enumerations. When you call Enumerate, you can pass in a
|
|
|
|
// directory to enumerate and the results that are returned are relative to
|
|
|
|
// that directory, files related to an enumeration need to know the "root of
|
|
|
|
// the enumeration" directory.
|
2013-05-11 02:10:18 -07:00
|
|
|
DeviceStorageFile(const nsAString& aStorageType,
|
|
|
|
const nsAString& aStorageName,
|
|
|
|
const nsAString& aRootDir,
|
|
|
|
const nsAString& aPath);
|
2013-04-18 07:13:23 -07:00
|
|
|
|
2012-12-04 18:00:39 -08:00
|
|
|
void SetPath(const nsAString& aPath);
|
|
|
|
void SetEditable(bool aEditable);
|
|
|
|
|
2013-08-02 16:35:16 -07:00
|
|
|
static already_AddRefed<DeviceStorageFile>
|
|
|
|
CreateUnique(nsAString& aFileName,
|
|
|
|
uint32_t aFileType,
|
|
|
|
uint32_t aFileAttributes);
|
2013-05-11 02:10:18 -07:00
|
|
|
|
2013-07-18 19:21:20 -07:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2012-12-04 18:00:39 -08:00
|
|
|
|
2013-05-11 02:10:18 -07:00
|
|
|
bool IsAvailable();
|
2013-08-02 16:34:59 -07:00
|
|
|
void GetFullPath(nsAString& aFullPath);
|
2013-05-11 02:10:18 -07:00
|
|
|
|
2012-12-04 18:00:39 -08:00
|
|
|
// we want to make sure that the names of file can't reach
|
|
|
|
// outside of the type of storage the user asked for.
|
|
|
|
bool IsSafePath();
|
2013-04-18 07:13:23 -07:00
|
|
|
bool IsSafePath(const nsAString& aPath);
|
2012-12-04 18:00:39 -08:00
|
|
|
|
2013-05-11 02:10:18 -07:00
|
|
|
void Dump(const char* label);
|
|
|
|
|
2012-12-04 18:00:39 -08:00
|
|
|
nsresult Remove();
|
|
|
|
nsresult Write(nsIInputStream* aInputStream);
|
|
|
|
nsresult Write(InfallibleTArray<uint8_t>& bits);
|
2013-05-11 02:10:18 -07:00
|
|
|
void CollectFiles(nsTArray<nsRefPtr<DeviceStorageFile> >& aFiles,
|
|
|
|
PRTime aSince = 0);
|
|
|
|
void collectFilesInternal(nsTArray<nsRefPtr<DeviceStorageFile> >& aFiles,
|
|
|
|
PRTime aSince, nsAString& aRootPath);
|
|
|
|
|
|
|
|
void AccumDiskUsage(uint64_t* aPicturesSoFar, uint64_t* aVideosSoFar,
|
|
|
|
uint64_t* aMusicSoFar, uint64_t* aTotalSoFar);
|
|
|
|
|
|
|
|
void GetDiskFreeSpace(int64_t* aSoFar);
|
|
|
|
void GetStatus(nsAString& aStatus);
|
2013-09-05 23:11:58 -07:00
|
|
|
void DoFormat(nsAString& aStatus);
|
2013-05-11 02:10:18 -07:00
|
|
|
static void GetRootDirectoryForType(const nsAString& aStorageType,
|
|
|
|
const nsAString& aStorageName,
|
2013-04-18 07:13:23 -07:00
|
|
|
nsIFile** aFile);
|
2013-05-17 07:41:53 -07:00
|
|
|
|
|
|
|
nsresult CalculateSizeAndModifiedDate();
|
|
|
|
nsresult CalculateMimeType();
|
|
|
|
|
2012-12-04 18:00:39 -08:00
|
|
|
private:
|
2013-05-11 02:10:18 -07:00
|
|
|
void Init();
|
2012-12-04 18:00:39 -08:00
|
|
|
void NormalizeFilePath();
|
2013-04-18 07:13:23 -07:00
|
|
|
void AppendRelativePath(const nsAString& aPath);
|
2013-05-11 02:10:18 -07:00
|
|
|
void AccumDirectoryUsage(nsIFile* aFile,
|
|
|
|
uint64_t* aPicturesSoFar,
|
|
|
|
uint64_t* aVideosSoFar,
|
|
|
|
uint64_t* aMusicSoFar,
|
|
|
|
uint64_t* aTotalSoFar);
|
2012-12-04 18:00:39 -08:00
|
|
|
};
|
|
|
|
|
2013-02-12 13:16:58 -08:00
|
|
|
/*
|
|
|
|
The FileUpdateDispatcher converts file-watcher-notify
|
|
|
|
observer events to file-watcher-update events. This is
|
|
|
|
used to be able to broadcast events from one child to
|
|
|
|
another child in B2G. (f.e., if one child decides to add
|
|
|
|
a file, we want to be able to able to send a onchange
|
|
|
|
notifications to every other child watching that device
|
|
|
|
storage object).
|
|
|
|
|
|
|
|
We create this object (via GetSingleton) in two places:
|
|
|
|
* ContentParent::Init (for IPC)
|
|
|
|
* nsDOMDeviceStorage::Init (for non-ipc)
|
|
|
|
*/
|
2013-01-09 07:03:28 -08:00
|
|
|
class FileUpdateDispatcher MOZ_FINAL
|
|
|
|
: public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
static FileUpdateDispatcher* GetSingleton();
|
|
|
|
private:
|
|
|
|
static mozilla::StaticRefPtr<FileUpdateDispatcher> sSingleton;
|
|
|
|
};
|
|
|
|
|
2012-08-01 23:29:34 -07:00
|
|
|
class nsDOMDeviceStorage MOZ_FINAL
|
2013-02-07 02:19:08 -08:00
|
|
|
: public nsDOMEventTargetHelper
|
|
|
|
, public nsIDOMDeviceStorage
|
2012-08-01 23:32:04 -07:00
|
|
|
, public nsIObserver
|
2012-08-01 23:29:34 -07:00
|
|
|
{
|
2013-05-31 23:54:46 -07:00
|
|
|
typedef mozilla::ErrorResult ErrorResult;
|
2013-05-31 23:56:00 -07:00
|
|
|
typedef mozilla::dom::DeviceStorageEnumerationParameters
|
|
|
|
EnumerationParameters;
|
|
|
|
typedef mozilla::dom::DOMCursor DOMCursor;
|
2013-05-31 23:54:46 -07:00
|
|
|
typedef mozilla::dom::DOMRequest DOMRequest;
|
2012-08-01 23:29:34 -07:00
|
|
|
public:
|
2013-05-11 02:10:18 -07:00
|
|
|
typedef nsTArray<nsString> VolumeNameArray;
|
|
|
|
|
2013-02-07 02:19:08 -08:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2012-08-01 23:29:34 -07:00
|
|
|
NS_DECL_NSIDOMDEVICESTORAGE
|
|
|
|
|
2012-08-01 23:32:04 -07:00
|
|
|
NS_DECL_NSIOBSERVER
|
2012-08-01 23:29:34 -07:00
|
|
|
NS_DECL_NSIDOMEVENTTARGET
|
2013-08-02 16:35:16 -07:00
|
|
|
|
2013-10-22 16:32:04 -07:00
|
|
|
virtual nsEventListenerManager*
|
|
|
|
GetExistingListenerManager() const MOZ_OVERRIDE;
|
|
|
|
virtual nsEventListenerManager*
|
|
|
|
GetOrCreateListenerManager() MOZ_OVERRIDE;
|
|
|
|
|
2013-08-02 16:35:16 -07:00
|
|
|
virtual void
|
|
|
|
AddEventListener(const nsAString& aType,
|
2013-09-23 17:25:17 -07:00
|
|
|
mozilla::dom::EventListener* aListener,
|
2013-08-02 16:35:16 -07:00
|
|
|
bool aUseCapture,
|
|
|
|
const mozilla::dom::Nullable<bool>& aWantsUntrusted,
|
|
|
|
ErrorResult& aRv) MOZ_OVERRIDE;
|
|
|
|
|
2013-04-16 14:16:08 -07:00
|
|
|
virtual void RemoveEventListener(const nsAString& aType,
|
2013-09-23 17:25:17 -07:00
|
|
|
mozilla::dom::EventListener* aListener,
|
2013-04-16 14:16:08 -07:00
|
|
|
bool aUseCapture,
|
2013-05-31 23:54:46 -07:00
|
|
|
ErrorResult& aRv) MOZ_OVERRIDE;
|
2012-08-01 23:29:34 -07:00
|
|
|
|
|
|
|
nsDOMDeviceStorage();
|
|
|
|
|
2013-05-11 02:10:18 -07:00
|
|
|
nsresult Init(nsPIDOMWindow* aWindow, const nsAString& aType,
|
|
|
|
const nsAString& aVolName);
|
|
|
|
|
|
|
|
bool IsAvailable();
|
2013-08-02 16:35:16 -07:00
|
|
|
bool IsFullPath(const nsAString& aPath)
|
|
|
|
{
|
|
|
|
return aPath.Length() > 0 && aPath.CharAt(0) == '/';
|
|
|
|
}
|
2012-08-01 23:29:34 -07:00
|
|
|
|
2013-08-02 16:35:16 -07:00
|
|
|
void SetRootDirectoryForType(const nsAString& aType,
|
|
|
|
const nsAString& aVolName);
|
2013-04-02 12:52:21 -07:00
|
|
|
|
2013-05-31 23:56:00 -07:00
|
|
|
// WebIDL
|
|
|
|
nsPIDOMWindow*
|
|
|
|
GetParentObject() const
|
|
|
|
{
|
|
|
|
return GetOwner();
|
|
|
|
}
|
|
|
|
virtual JSObject*
|
|
|
|
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
|
|
|
|
|
|
|
|
IMPL_EVENT_HANDLER(change)
|
|
|
|
|
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
Add(nsIDOMBlob* aBlob, ErrorResult& aRv);
|
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
AddNamed(nsIDOMBlob* aBlob, const nsAString& aPath, ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
Get(const nsAString& aPath, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
return GetInternal(aPath, false, aRv);
|
|
|
|
}
|
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
GetEditable(const nsAString& aPath, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
return GetInternal(aPath, true, aRv);
|
|
|
|
}
|
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
Delete(const nsAString& aPath, ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<DOMCursor>
|
|
|
|
Enumerate(const EnumerationParameters& aOptions, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
return Enumerate(NullString(), aOptions, aRv);
|
|
|
|
}
|
|
|
|
already_AddRefed<DOMCursor>
|
|
|
|
Enumerate(const nsAString& aPath, const EnumerationParameters& aOptions,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
already_AddRefed<DOMCursor>
|
|
|
|
EnumerateEditable(const EnumerationParameters& aOptions, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
return EnumerateEditable(NullString(), aOptions, aRv);
|
|
|
|
}
|
|
|
|
already_AddRefed<DOMCursor>
|
|
|
|
EnumerateEditable(const nsAString& aPath,
|
|
|
|
const EnumerationParameters& aOptions, ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<DOMRequest> FreeSpace(ErrorResult& aRv);
|
|
|
|
already_AddRefed<DOMRequest> UsedSpace(ErrorResult& aRv);
|
|
|
|
already_AddRefed<DOMRequest> Available(ErrorResult& aRv);
|
2013-09-05 23:11:58 -07:00
|
|
|
already_AddRefed<DOMRequest> Format(ErrorResult& aRv);
|
2013-05-31 23:56:00 -07:00
|
|
|
|
2013-06-12 09:30:23 -07:00
|
|
|
bool Default();
|
|
|
|
|
2013-05-31 23:56:00 -07:00
|
|
|
// Uses XPCOM GetStorageName
|
|
|
|
|
2013-08-02 16:35:16 -07:00
|
|
|
static void
|
|
|
|
CreateDeviceStorageFor(nsPIDOMWindow* aWin,
|
|
|
|
const nsAString& aType,
|
|
|
|
nsDOMDeviceStorage** aStore);
|
|
|
|
|
|
|
|
static void
|
|
|
|
CreateDeviceStoragesFor(nsPIDOMWindow* aWin,
|
|
|
|
const nsAString& aType,
|
|
|
|
nsTArray<nsRefPtr<nsDOMDeviceStorage> >& aStores);
|
2012-08-01 23:29:34 -07:00
|
|
|
|
|
|
|
void Shutdown();
|
|
|
|
|
2013-05-11 02:10:18 -07:00
|
|
|
static void GetOrderedVolumeNames(nsTArray<nsString>& aVolumeNames);
|
|
|
|
|
2013-08-02 16:34:59 -07:00
|
|
|
static void GetDefaultStorageName(const nsAString& aStorageType,
|
|
|
|
nsAString &aStorageName);
|
2013-05-11 02:10:18 -07:00
|
|
|
|
2013-08-02 16:34:59 -07:00
|
|
|
static bool ParseFullPath(const nsAString& aFullPath,
|
|
|
|
nsAString& aOutStorageName,
|
|
|
|
nsAString& aOutStoragePath);
|
2012-08-01 23:29:34 -07:00
|
|
|
private:
|
|
|
|
~nsDOMDeviceStorage();
|
|
|
|
|
2013-05-31 23:56:00 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
GetInternal(const nsAString& aPath, bool aEditable, ErrorResult& aRv);
|
2012-08-01 23:29:34 -07:00
|
|
|
|
2013-05-31 23:54:46 -07:00
|
|
|
void
|
|
|
|
GetInternal(nsPIDOMWindow* aWin, const nsAString& aPath, DOMRequest* aRequest,
|
|
|
|
bool aEditable);
|
2013-05-11 02:10:18 -07:00
|
|
|
|
2013-05-31 23:54:46 -07:00
|
|
|
void
|
|
|
|
DeleteInternal(nsPIDOMWindow* aWin, const nsAString& aPath,
|
|
|
|
DOMRequest* aRequest);
|
2013-05-11 02:10:18 -07:00
|
|
|
|
2013-05-31 23:56:00 -07:00
|
|
|
already_AddRefed<DOMCursor>
|
|
|
|
EnumerateInternal(const nsAString& aName,
|
|
|
|
const EnumerationParameters& aOptions, bool aEditable,
|
|
|
|
ErrorResult& aRv);
|
2012-08-01 23:29:34 -07:00
|
|
|
|
2012-08-30 15:17:37 -07:00
|
|
|
nsString mStorageType;
|
|
|
|
nsCOMPtr<nsIFile> mRootDirectory;
|
2013-05-11 02:10:18 -07:00
|
|
|
nsString mStorageName;
|
2013-08-02 16:34:59 -07:00
|
|
|
|
|
|
|
already_AddRefed<nsDOMDeviceStorage> GetStorage(const nsAString& aFullPath,
|
2013-05-11 02:10:18 -07:00
|
|
|
nsAString& aOutStoragePath);
|
2013-08-02 16:35:16 -07:00
|
|
|
already_AddRefed<nsDOMDeviceStorage>
|
|
|
|
GetStorageByName(const nsAString &aStorageName);
|
2012-08-01 23:29:34 -07:00
|
|
|
|
2012-07-30 07:58:26 -07:00
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
2012-08-01 23:29:34 -07:00
|
|
|
|
2012-08-17 19:43:00 -07:00
|
|
|
bool mIsWatchingFile;
|
2012-08-17 19:43:00 -07:00
|
|
|
bool mAllowedToWatchFile;
|
2012-08-17 19:43:00 -07:00
|
|
|
|
2012-08-30 15:17:37 -07:00
|
|
|
nsresult Notify(const char* aReason, class DeviceStorageFile* aFile);
|
2012-08-17 19:43:00 -07:00
|
|
|
|
2012-08-01 23:29:34 -07:00
|
|
|
friend class WatchFileEvent;
|
|
|
|
friend class DeviceStorageRequest;
|
|
|
|
|
2013-05-11 02:10:18 -07:00
|
|
|
class VolumeNameCache : public mozilla::RefCounted<VolumeNameCache>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsTArray<nsString> mVolumeNames;
|
|
|
|
};
|
|
|
|
static mozilla::StaticRefPtr<VolumeNameCache> sVolumeNameCache;
|
|
|
|
|
2012-08-09 15:41:18 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2013-08-22 16:12:25 -07:00
|
|
|
nsString mLastStatus;
|
2013-08-02 16:34:59 -07:00
|
|
|
void DispatchMountChangeEvent(nsAString& aVolumeStatus);
|
2012-08-09 15:41:18 -07:00
|
|
|
#endif
|
|
|
|
|
2012-08-01 23:29:34 -07:00
|
|
|
// nsIDOMDeviceStorage.type
|
|
|
|
enum {
|
|
|
|
DEVICE_STORAGE_TYPE_DEFAULT = 0,
|
|
|
|
DEVICE_STORAGE_TYPE_SHARED,
|
2012-08-03 06:04:35 -07:00
|
|
|
DEVICE_STORAGE_TYPE_EXTERNAL
|
2012-08-01 23:29:34 -07:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|