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"
|
|
|
|
|
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-04-18 07:13:23 -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-05-11 02:10:18 -07:00
|
|
|
static already_AddRefed<DeviceStorageFile> CreateUnique(nsAString& aFileName,
|
|
|
|
uint32_t aFileType,
|
|
|
|
uint32_t aFileAttributes);
|
|
|
|
|
2012-12-04 18:00:39 -08:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2013-05-11 02:10:18 -07:00
|
|
|
bool IsAvailable();
|
|
|
|
bool IsComposite();
|
|
|
|
void GetCompositePath(nsAString& aCompositePath);
|
|
|
|
|
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);
|
|
|
|
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 GetStatusInternal(nsAString& aStorageName, nsAString& aStatus);
|
|
|
|
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
|
|
|
{
|
|
|
|
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-04-16 14:16:08 -07:00
|
|
|
virtual void AddEventListener(const nsAString& aType,
|
|
|
|
nsIDOMEventListener* aListener,
|
|
|
|
bool aUseCapture,
|
|
|
|
const mozilla::dom::Nullable<bool>& aWantsUntrusted,
|
|
|
|
mozilla::ErrorResult& aRv) MOZ_OVERRIDE;
|
|
|
|
virtual void RemoveEventListener(const nsAString& aType,
|
|
|
|
nsIDOMEventListener* aListener,
|
|
|
|
bool aUseCapture,
|
|
|
|
mozilla::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,
|
|
|
|
nsTArray<nsRefPtr<nsDOMDeviceStorage> >& aStores);
|
|
|
|
nsresult Init(nsPIDOMWindow* aWindow, const nsAString& aType,
|
|
|
|
const nsAString& aVolName);
|
|
|
|
|
|
|
|
bool IsAvailable();
|
2012-08-01 23:29:34 -07:00
|
|
|
|
2013-05-11 02:10:18 -07:00
|
|
|
void SetRootDirectoryForType(const nsAString& aType, const nsAString& aVolName);
|
2013-04-02 12:52:21 -07:00
|
|
|
|
2013-05-11 02:10:18 -07:00
|
|
|
static void CreateDeviceStorageFor(nsPIDOMWindow* aWin,
|
|
|
|
const nsAString& aType,
|
|
|
|
nsDOMDeviceStorage** aStore);
|
2012-08-01 23:29:34 -07:00
|
|
|
|
|
|
|
static void CreateDeviceStoragesFor(nsPIDOMWindow* aWin,
|
2013-05-11 02:10:18 -07:00
|
|
|
const nsAString& aType,
|
2013-05-16 11:11:58 -07:00
|
|
|
nsTArray<nsRefPtr<nsDOMDeviceStorage> >& aStores,
|
|
|
|
bool aCompositeComponent);
|
2012-08-01 23:29:34 -07:00
|
|
|
void Shutdown();
|
|
|
|
|
2013-05-11 02:10:18 -07:00
|
|
|
static void GetOrderedVolumeNames(nsTArray<nsString>& aVolumeNames);
|
|
|
|
|
|
|
|
static void GetWritableStorageName(const nsAString& aStorageType,
|
|
|
|
nsAString &aStorageName);
|
|
|
|
|
|
|
|
static bool ParseCompositePath(const nsAString& aCompositePath,
|
|
|
|
nsAString& aOutStorageName,
|
|
|
|
nsAString& aOutStoragePath);
|
2012-08-01 23:29:34 -07:00
|
|
|
private:
|
|
|
|
~nsDOMDeviceStorage();
|
|
|
|
|
2013-05-31 23:54:40 -07:00
|
|
|
nsresult GetInternal(const nsAString& aName,
|
2012-08-01 23:29:34 -07:00
|
|
|
nsIDOMDOMRequest** aRetval,
|
|
|
|
bool aEditable);
|
|
|
|
|
2013-05-11 02:10:18 -07:00
|
|
|
nsresult GetInternal(nsPIDOMWindow* aWin,
|
|
|
|
const nsAString& aPath,
|
|
|
|
mozilla::dom::DOMRequest* aRequest,
|
|
|
|
bool aEditable);
|
|
|
|
|
|
|
|
nsresult DeleteInternal(nsPIDOMWindow* aWin,
|
|
|
|
const nsAString& aPath,
|
|
|
|
mozilla::dom::DOMRequest* aRequest);
|
|
|
|
|
|
|
|
nsresult EnumerateInternal(const JS::Value& aName,
|
|
|
|
const JS::Value& aOptions,
|
2012-08-01 23:29:34 -07:00
|
|
|
JSContext* aCx,
|
2012-11-29 21:41:40 -08:00
|
|
|
uint8_t aArgc,
|
|
|
|
bool aEditable,
|
2013-03-01 16:21:01 -08:00
|
|
|
nsIDOMDOMCursor** aRetval);
|
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-05-16 11:11:58 -07:00
|
|
|
bool mCompositeComponent;
|
|
|
|
|
|
|
|
// A composite device storage object is one which front-ends for multiple
|
|
|
|
// real storage objects. The real storage objects will each be stored in
|
|
|
|
// mStores and will each have a unique mStorageName. The composite storage
|
|
|
|
// object will have mStorageName == "", and mRootDirectory will be null.
|
|
|
|
//
|
|
|
|
// Note that on desktop (or other non-gonk), composite storage areas
|
|
|
|
// don't exist, and mStorageName will also be "".
|
|
|
|
//
|
|
|
|
// A device storage object which is stored in mStores is considered to be
|
|
|
|
// a composite component.
|
2013-05-11 02:10:18 -07:00
|
|
|
|
|
|
|
bool IsComposite() { return mStores.Length() > 0; }
|
2013-05-16 11:11:58 -07:00
|
|
|
bool IsCompositeComponent() { return mCompositeComponent; }
|
2013-05-11 02:10:18 -07:00
|
|
|
nsTArray<nsRefPtr<nsDOMDeviceStorage> > mStores;
|
|
|
|
already_AddRefed<nsDOMDeviceStorage> GetStorage(const nsAString& aCompositePath,
|
|
|
|
nsAString& aOutStoragePath);
|
|
|
|
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-05-11 02:10:18 -07:00
|
|
|
void DispatchMountChangeEvent(nsAString& aVolumeName,
|
|
|
|
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
|