2012-05-21 09:18:30 -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/. */
|
|
|
|
|
2012-12-14 15:58:45 -08:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
|
2012-09-20 00:55:36 -07:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
|
|
|
|
#include "nsDeviceStorage.h"
|
|
|
|
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2012-08-01 23:29:34 -07:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
|
|
|
#include "mozilla/dom/devicestorage/PDeviceStorageRequestChild.h"
|
2012-09-20 00:55:36 -07:00
|
|
|
#include "mozilla/dom/ipc/Blob.h"
|
|
|
|
#include "mozilla/dom/PBrowserChild.h"
|
2012-08-01 23:29:34 -07:00
|
|
|
#include "mozilla/dom/PContentPermissionRequestChild.h"
|
|
|
|
|
2012-09-13 22:37:00 -07:00
|
|
|
#include "nsAutoPtr.h"
|
2012-08-01 23:29:34 -07:00
|
|
|
#include "nsDOMEvent.h"
|
2012-05-21 09:18:30 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2012-06-05 19:08:30 -07:00
|
|
|
#include "nsIFile.h"
|
2012-05-21 09:18:30 -07:00
|
|
|
#include "nsIDirectoryEnumerator.h"
|
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
2012-06-19 16:14:39 -07:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
2012-05-21 09:18:30 -07:00
|
|
|
#include "nsIDOMFile.h"
|
|
|
|
#include "nsDOMBlobBuilder.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "nsJSUtils.h"
|
2012-06-10 22:50:44 -07:00
|
|
|
#include "DictionaryHelpers.h"
|
2012-06-18 18:21:14 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-07-01 16:45:59 -07:00
|
|
|
#include "nsContentUtils.h"
|
2012-06-19 16:14:39 -07:00
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#include "TabChild.h"
|
|
|
|
#include "DeviceStorageRequestChild.h"
|
2012-08-01 23:29:34 -07:00
|
|
|
#include "nsIDOMDeviceStorageChangeEvent.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "nsIObserverService.h"
|
2012-08-02 13:25:16 -07:00
|
|
|
#include "GeneratedEvents.h"
|
2012-07-30 07:58:26 -07:00
|
|
|
#include "mozilla/dom/PermissionMessageUtils.h"
|
2012-08-30 15:17:38 -07:00
|
|
|
#include "nsIMIMEService.h"
|
|
|
|
#include "nsCExternalHandlerService.h"
|
2012-09-18 13:46:52 -07:00
|
|
|
#include "nsIPermissionManager.h"
|
2012-09-05 14:30:36 -07:00
|
|
|
#include "nsIStringBundle.h"
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2012-09-05 14:30:36 -07:00
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
// Microsoft's API Name hackery sucks
|
|
|
|
#undef CreateEvent
|
2012-05-21 09:18:30 -07:00
|
|
|
|
2012-07-18 11:31:05 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2012-08-14 08:53:20 -07:00
|
|
|
#include "nsIVolume.h"
|
2012-07-18 11:31:05 -07:00
|
|
|
#include "nsIVolumeService.h"
|
|
|
|
#endif
|
|
|
|
|
2012-09-05 14:30:36 -07:00
|
|
|
#define DEVICESTORAGE_PROPERTIES "chrome://global/content/devicestorage.properties"
|
2012-09-13 22:37:00 -07:00
|
|
|
#define DEVICESTORAGE_PICTURES "pictures"
|
|
|
|
#define DEVICESTORAGE_VIDEOS "videos"
|
|
|
|
#define DEVICESTORAGE_MUSIC "music"
|
2012-09-18 13:46:52 -07:00
|
|
|
#define DEVICESTORAGE_APPS "apps"
|
2012-09-28 09:50:13 -07:00
|
|
|
#define DEVICESTORAGE_SDCARD "sdcard"
|
2012-08-30 15:17:38 -07:00
|
|
|
|
2012-09-20 00:55:36 -07:00
|
|
|
using namespace mozilla;
|
2012-05-21 09:18:30 -07:00
|
|
|
using namespace mozilla::dom;
|
2012-06-19 16:14:39 -07:00
|
|
|
using namespace mozilla::dom::devicestorage;
|
2012-05-21 09:18:30 -07:00
|
|
|
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
|
2012-09-13 22:37:00 -07:00
|
|
|
nsAutoPtr<DeviceStorageTypeChecker> DeviceStorageTypeChecker::sDeviceStorageTypeChecker;
|
|
|
|
|
|
|
|
DeviceStorageTypeChecker::DeviceStorageTypeChecker()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageTypeChecker::~DeviceStorageTypeChecker()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageTypeChecker*
|
|
|
|
DeviceStorageTypeChecker::CreateOrGet()
|
|
|
|
{
|
|
|
|
if (sDeviceStorageTypeChecker) {
|
|
|
|
return sDeviceStorageTypeChecker;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "This can only be created on the main thread!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundleService> stringService = mozilla::services::GetStringBundleService();
|
|
|
|
if (!stringService) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-13 22:37:00 -07:00
|
|
|
|
2012-09-13 22:37:00 -07:00
|
|
|
nsCOMPtr<nsIStringBundle> filterBundle;
|
|
|
|
if (NS_FAILED(stringService->CreateBundle(DEVICESTORAGE_PROPERTIES, getter_AddRefs(filterBundle)))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageTypeChecker* result = new DeviceStorageTypeChecker();
|
|
|
|
result->InitFromBundle(filterBundle);
|
|
|
|
|
|
|
|
sDeviceStorageTypeChecker = result;
|
|
|
|
ClearOnShutdown(&sDeviceStorageTypeChecker);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DeviceStorageTypeChecker::InitFromBundle(nsIStringBundle* aBundle)
|
|
|
|
{
|
|
|
|
aBundle->GetStringFromName(NS_ConvertASCIItoUTF16(DEVICESTORAGE_PICTURES).get(), getter_Copies(mPicturesExtensions));
|
|
|
|
aBundle->GetStringFromName(NS_ConvertASCIItoUTF16(DEVICESTORAGE_MUSIC).get(), getter_Copies(mMusicExtensions));
|
|
|
|
aBundle->GetStringFromName(NS_ConvertASCIItoUTF16(DEVICESTORAGE_VIDEOS).get(), getter_Copies(mVideosExtensions));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
DeviceStorageTypeChecker::Check(const nsAString& aType, nsIDOMBlob* aBlob)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aBlob, "Calling Check without a blob");
|
|
|
|
|
|
|
|
nsString mimeType;
|
|
|
|
if (NS_FAILED(aBlob->GetType(mimeType))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aType.EqualsLiteral(DEVICESTORAGE_PICTURES)) {
|
|
|
|
return StringBeginsWith(mimeType, NS_LITERAL_STRING("image/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aType.EqualsLiteral(DEVICESTORAGE_VIDEOS)) {
|
|
|
|
return StringBeginsWith(mimeType, NS_LITERAL_STRING("video/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aType.EqualsLiteral(DEVICESTORAGE_MUSIC)) {
|
|
|
|
return StringBeginsWith(mimeType, NS_LITERAL_STRING("audio/"));
|
|
|
|
}
|
|
|
|
|
2012-09-28 09:50:13 -07:00
|
|
|
if (aType.EqualsLiteral(DEVICESTORAGE_APPS) ||
|
|
|
|
aType.EqualsLiteral(DEVICESTORAGE_SDCARD)) {
|
|
|
|
// Apps and sdcard have no restriction on mime types
|
2012-09-18 13:46:52 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-13 22:37:00 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DeviceStorageTypeChecker::Check(const nsAString& aType, nsIFile* aFile)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aFile, "Calling Check without a file");
|
|
|
|
|
2012-09-28 09:50:13 -07:00
|
|
|
if (aType.EqualsLiteral(DEVICESTORAGE_APPS) ||
|
|
|
|
aType.EqualsLiteral(DEVICESTORAGE_SDCARD)) {
|
2012-09-18 13:46:52 -07:00
|
|
|
// apps have no restrictions on what file extensions used.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-13 22:37:00 -07:00
|
|
|
nsString path;
|
|
|
|
aFile->GetPath(path);
|
|
|
|
|
|
|
|
int32_t dotIdx = path.RFindChar(PRUnichar('.'));
|
|
|
|
if (dotIdx == kNotFound) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString extensionMatch;
|
|
|
|
extensionMatch.AssignLiteral("*");
|
|
|
|
extensionMatch.Append(Substring(path, dotIdx));
|
|
|
|
extensionMatch.AppendLiteral(";");
|
|
|
|
|
|
|
|
if (aType.EqualsLiteral(DEVICESTORAGE_PICTURES)) {
|
2012-09-20 14:08:50 -07:00
|
|
|
return CaseInsensitiveFindInReadable(extensionMatch, mPicturesExtensions);
|
2012-09-13 22:37:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aType.EqualsLiteral(DEVICESTORAGE_VIDEOS)) {
|
2012-09-20 14:08:50 -07:00
|
|
|
return CaseInsensitiveFindInReadable(extensionMatch, mVideosExtensions);
|
2012-09-13 22:37:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aType.EqualsLiteral(DEVICESTORAGE_MUSIC)) {
|
2012-09-20 14:08:50 -07:00
|
|
|
return CaseInsensitiveFindInReadable(extensionMatch, mMusicExtensions);
|
2012-09-13 22:37:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-13 22:37:00 -07:00
|
|
|
nsresult
|
|
|
|
DeviceStorageTypeChecker::GetPermissionForType(const nsAString& aType, nsACString& aPermissionResult)
|
|
|
|
{
|
|
|
|
if (!aType.EqualsLiteral(DEVICESTORAGE_PICTURES) &&
|
|
|
|
!aType.EqualsLiteral(DEVICESTORAGE_VIDEOS) &&
|
2012-09-18 13:46:52 -07:00
|
|
|
!aType.EqualsLiteral(DEVICESTORAGE_MUSIC) &&
|
2012-09-28 09:50:13 -07:00
|
|
|
!aType.EqualsLiteral(DEVICESTORAGE_APPS) &&
|
|
|
|
!aType.EqualsLiteral(DEVICESTORAGE_SDCARD)) {
|
2012-09-13 22:37:00 -07:00
|
|
|
// unknown type
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
aPermissionResult.AssignLiteral("device-storage:");
|
|
|
|
aPermissionResult.Append(NS_ConvertUTF16toUTF8(aType));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-13 16:06:42 -08:00
|
|
|
nsresult
|
|
|
|
DeviceStorageTypeChecker::GetAccessForRequest(const DeviceStorageRequestType aRequestType, nsACString& aAccessResult)
|
|
|
|
{
|
|
|
|
switch(aRequestType) {
|
|
|
|
case DEVICE_STORAGE_REQUEST_READ:
|
|
|
|
case DEVICE_STORAGE_REQUEST_WATCH:
|
2013-01-25 11:05:24 -08:00
|
|
|
case DEVICE_STORAGE_REQUEST_FREE_SPACE:
|
|
|
|
case DEVICE_STORAGE_REQUEST_USED_SPACE:
|
|
|
|
case DEVICE_STORAGE_REQUEST_AVAILABLE:
|
2012-11-13 16:06:42 -08:00
|
|
|
aAccessResult.AssignLiteral("read");
|
|
|
|
break;
|
|
|
|
case DEVICE_STORAGE_REQUEST_WRITE:
|
|
|
|
case DEVICE_STORAGE_REQUEST_DELETE:
|
|
|
|
aAccessResult.AssignLiteral("write");
|
|
|
|
break;
|
2012-11-19 12:31:21 -08:00
|
|
|
case DEVICE_STORAGE_REQUEST_CREATE:
|
|
|
|
aAccessResult.AssignLiteral("create");
|
|
|
|
break;
|
2012-11-13 16:06:42 -08:00
|
|
|
default:
|
|
|
|
aAccessResult.AssignLiteral("undefined");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-09 07:03:28 -08:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(FileUpdateDispatcher, nsIObserver)
|
|
|
|
|
|
|
|
mozilla::StaticRefPtr<FileUpdateDispatcher> FileUpdateDispatcher::sSingleton;
|
|
|
|
|
|
|
|
FileUpdateDispatcher*
|
|
|
|
FileUpdateDispatcher::GetSingleton()
|
|
|
|
{
|
|
|
|
if (sSingleton) {
|
|
|
|
return sSingleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
sSingleton = new FileUpdateDispatcher();
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
obs->AddObserver(sSingleton, "file-watcher-notify", false);
|
|
|
|
ClearOnShutdown(&sSingleton);
|
|
|
|
|
|
|
|
return sSingleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FileUpdateDispatcher::Observe(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
|
|
|
const PRUnichar *aData)
|
|
|
|
{
|
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
|
|
|
|
DeviceStorageFile* file = static_cast<DeviceStorageFile*>(aSubject);
|
|
|
|
if (!file || !file->mFile) {
|
|
|
|
NS_WARNING("Device storage file looks invalid!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString fullpath;
|
|
|
|
nsresult rv = file->mFile->GetPath(fullpath);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Could not get path from the nsIFile!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentChild::GetSingleton()->SendFilePathUpdateNotify(file->mStorageType,
|
|
|
|
fullpath,
|
|
|
|
NS_ConvertUTF16toUTF8(aData));
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
obs->NotifyObservers(aSubject, "file-watcher-update", aData);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-17 19:43:00 -07:00
|
|
|
class IOEventComplete : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2012-08-30 15:17:37 -07:00
|
|
|
IOEventComplete(DeviceStorageFile *aFile, const char *aType)
|
2012-08-17 19:43:00 -07:00
|
|
|
: mFile(aFile)
|
|
|
|
, mType(aType)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~IOEventComplete() {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
nsString data;
|
|
|
|
CopyASCIItoUTF16(mType, data);
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
2013-01-09 07:03:28 -08:00
|
|
|
|
|
|
|
obs->NotifyObservers(mFile, "file-watcher-notify", data.get());
|
2012-08-17 19:43:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-08-30 15:17:37 -07:00
|
|
|
nsRefPtr<DeviceStorageFile> mFile;
|
2012-08-17 19:43:00 -07:00
|
|
|
nsCString mType;
|
|
|
|
};
|
|
|
|
|
2012-09-05 14:30:36 -07:00
|
|
|
DeviceStorageFile::DeviceStorageFile(const nsAString& aStorageType,
|
2012-09-13 22:37:00 -07:00
|
|
|
nsIFile* aFile,
|
|
|
|
const nsAString& aPath)
|
2012-07-13 21:01:52 -07:00
|
|
|
: mPath(aPath)
|
2012-08-30 15:17:37 -07:00
|
|
|
, mStorageType(aStorageType)
|
2012-07-13 21:01:52 -07:00
|
|
|
, mEditable(false)
|
2012-06-19 16:14:39 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aFile, "Must not create a DeviceStorageFile with a null nsIFile");
|
|
|
|
// always take a clone
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
aFile->Clone(getter_AddRefs(mFile));
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
AppendRelativePath();
|
|
|
|
NormalizeFilePath();
|
2012-09-13 22:37:00 -07:00
|
|
|
|
2012-09-20 00:55:36 -07:00
|
|
|
DebugOnly<DeviceStorageTypeChecker*> typeChecker = DeviceStorageTypeChecker::CreateOrGet();
|
2012-09-13 22:37:00 -07:00
|
|
|
NS_ASSERTION(typeChecker, "DeviceStorageTypeChecker is null");
|
2012-06-19 16:14:39 -07:00
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-08-30 15:17:37 -07:00
|
|
|
DeviceStorageFile::DeviceStorageFile(const nsAString& aStorageType, nsIFile* aFile)
|
|
|
|
: mStorageType(aStorageType)
|
|
|
|
, mEditable(false)
|
2012-06-19 16:14:39 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aFile, "Must not create a DeviceStorageFile with a null nsIFile");
|
|
|
|
// always take a clone
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
aFile->Clone(getter_AddRefs(mFile));
|
2012-09-13 22:37:00 -07:00
|
|
|
|
2012-09-20 00:55:36 -07:00
|
|
|
DebugOnly<DeviceStorageTypeChecker*> typeChecker = DeviceStorageTypeChecker::CreateOrGet();
|
2012-09-13 22:37:00 -07:00
|
|
|
NS_ASSERTION(typeChecker, "DeviceStorageTypeChecker is null");
|
2012-06-19 16:14:39 -07:00
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
void
|
|
|
|
DeviceStorageFile::SetPath(const nsAString& aPath) {
|
|
|
|
mPath.Assign(aPath);
|
|
|
|
NormalizeFilePath();
|
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
void
|
|
|
|
DeviceStorageFile::SetEditable(bool aEditable) {
|
|
|
|
mEditable = aEditable;
|
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-06-19 16:14:39 -07: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
|
|
|
|
DeviceStorageFile::IsSafePath()
|
|
|
|
{
|
|
|
|
nsAString::const_iterator start, end;
|
|
|
|
mPath.BeginReading(start);
|
|
|
|
mPath.EndReading(end);
|
|
|
|
|
|
|
|
// if the path has a ~ or \ in it, return false.
|
|
|
|
NS_NAMED_LITERAL_STRING(tilde, "~");
|
|
|
|
NS_NAMED_LITERAL_STRING(bslash, "\\");
|
|
|
|
if (FindInReadable(tilde, start, end) ||
|
|
|
|
FindInReadable(bslash, start, end)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// split on /. if any token is "", ., or .., return false.
|
|
|
|
NS_ConvertUTF16toUTF8 cname(mPath);
|
|
|
|
char* buffer = cname.BeginWriting();
|
|
|
|
const char* token;
|
|
|
|
|
|
|
|
while ((token = nsCRT::strtok(buffer, "/", &buffer))) {
|
|
|
|
if (PL_strcmp(token, "") == 0 ||
|
2012-08-01 23:29:34 -07:00
|
|
|
PL_strcmp(token, ".") == 0 ||
|
|
|
|
PL_strcmp(token, "..") == 0 ) {
|
2012-07-13 21:01:52 -07:00
|
|
|
return false;
|
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
return true;
|
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
void
|
|
|
|
DeviceStorageFile::NormalizeFilePath() {
|
2012-07-13 21:01:52 -07:00
|
|
|
#if defined(XP_WIN)
|
2012-06-19 16:14:39 -07:00
|
|
|
PRUnichar* cur = mPath.BeginWriting();
|
|
|
|
PRUnichar* end = mPath.EndWriting();
|
|
|
|
for (; cur < end; ++cur) {
|
|
|
|
if (PRUnichar('\\') == *cur)
|
|
|
|
*cur = PRUnichar('/');
|
2012-06-19 16:14:39 -07:00
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
#endif
|
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
void
|
|
|
|
DeviceStorageFile::AppendRelativePath() {
|
2012-07-13 21:01:52 -07:00
|
|
|
#if defined(XP_WIN)
|
2012-06-19 16:14:39 -07:00
|
|
|
// replace forward slashes with backslashes,
|
|
|
|
// since nsLocalFileWin chokes on them
|
|
|
|
nsString temp;
|
|
|
|
temp.Assign(mPath);
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
PRUnichar* cur = temp.BeginWriting();
|
|
|
|
PRUnichar* end = temp.EndWriting();
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
for (; cur < end; ++cur) {
|
|
|
|
if (PRUnichar('/') == *cur)
|
|
|
|
*cur = PRUnichar('\\');
|
|
|
|
}
|
|
|
|
mFile->AppendRelativePath(temp);
|
2012-07-13 21:01:52 -07:00
|
|
|
#else
|
2012-06-19 16:14:39 -07:00
|
|
|
mFile->AppendRelativePath(mPath);
|
2012-07-13 21:01:52 -07:00
|
|
|
#endif
|
2012-06-19 16:14:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-09 15:41:18 -07:00
|
|
|
DeviceStorageFile::Write(nsIInputStream* aInputStream)
|
2012-06-19 16:14:39 -07:00
|
|
|
{
|
2012-08-09 15:41:18 -07:00
|
|
|
if (!aInputStream) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
nsresult rv = mFile->Create(nsIFile::NORMAL_FILE_TYPE, 00600);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2012-06-19 16:14:39 -07:00
|
|
|
}
|
2012-07-13 21:01:52 -07:00
|
|
|
|
2012-08-30 15:17:37 -07:00
|
|
|
nsCOMPtr<IOEventComplete> iocomplete = new IOEventComplete(this, "created");
|
2012-08-30 20:15:32 -07:00
|
|
|
NS_DispatchToMainThread(iocomplete);
|
2012-08-17 19:43:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t bufSize = 0;
|
2012-08-09 15:41:18 -07:00
|
|
|
aInputStream->Available(&bufSize);
|
2012-06-19 16:14:39 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStream> outputStream;
|
|
|
|
NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), mFile);
|
|
|
|
|
|
|
|
if (!outputStream) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStream> bufferedOutputStream;
|
2012-10-16 01:38:20 -07:00
|
|
|
rv = NS_NewBufferedOutputStream(getter_AddRefs(bufferedOutputStream),
|
|
|
|
outputStream,
|
|
|
|
4096*4);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-08-10 19:44:11 -07:00
|
|
|
while (bufSize) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t wrote;
|
2012-08-17 19:43:00 -07:00
|
|
|
rv = bufferedOutputStream->WriteFrom(aInputStream,
|
2013-01-15 04:22:03 -08:00
|
|
|
static_cast<uint32_t>(std::min<uint64_t>(bufSize, UINT32_MAX)),
|
2012-09-13 22:37:00 -07:00
|
|
|
&wrote);
|
2012-08-10 19:44:11 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bufSize -= wrote;
|
|
|
|
}
|
|
|
|
|
2012-08-30 15:17:37 -07:00
|
|
|
iocomplete = new IOEventComplete(this, "modified");
|
2012-08-17 19:43:00 -07:00
|
|
|
NS_DispatchToMainThread(iocomplete);
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
bufferedOutputStream->Close();
|
|
|
|
outputStream->Close();
|
2012-08-10 19:44:11 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2012-06-19 16:14:39 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
DeviceStorageFile::Write(InfallibleTArray<uint8_t>& aBits) {
|
2012-06-19 16:14:39 -07:00
|
|
|
|
|
|
|
nsresult rv = mFile->Create(nsIFile::NORMAL_FILE_TYPE, 00600);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-30 15:17:37 -07:00
|
|
|
nsCOMPtr<IOEventComplete> iocomplete = new IOEventComplete(this, "created");
|
2012-08-17 19:43:00 -07:00
|
|
|
NS_DispatchToMainThread(iocomplete);
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
nsCOMPtr<nsIOutputStream> outputStream;
|
|
|
|
NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), mFile);
|
|
|
|
|
|
|
|
if (!outputStream) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t wrote;
|
2012-06-19 16:14:39 -07:00
|
|
|
outputStream->Write((char*) aBits.Elements(), aBits.Length(), &wrote);
|
|
|
|
outputStream->Close();
|
|
|
|
|
2012-08-30 15:17:37 -07:00
|
|
|
iocomplete = new IOEventComplete(this, "modified");
|
2012-08-17 19:43:00 -07:00
|
|
|
NS_DispatchToMainThread(iocomplete);
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
if (aBits.Length() != wrote) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-17 19:43:00 -07:00
|
|
|
nsresult
|
|
|
|
DeviceStorageFile::Remove()
|
|
|
|
{
|
2012-08-28 16:42:40 -07:00
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
bool check;
|
|
|
|
nsresult rv = mFile->Exists(&check);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-09-05 14:30:36 -07:00
|
|
|
|
2012-08-28 16:42:40 -07:00
|
|
|
if (!check) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = mFile->Remove(true);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-30 15:17:37 -07:00
|
|
|
nsCOMPtr<IOEventComplete> iocomplete = new IOEventComplete(this, "deleted");
|
2012-08-28 16:42:40 -07:00
|
|
|
NS_DispatchToMainThread(iocomplete);
|
2012-08-17 19:43:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
void
|
|
|
|
DeviceStorageFile::CollectFiles(nsTArray<nsRefPtr<DeviceStorageFile> > &aFiles,
|
2012-09-10 23:52:47 -07:00
|
|
|
PRTime aSince)
|
2012-06-19 16:14:39 -07:00
|
|
|
{
|
|
|
|
nsString rootPath;
|
2012-08-01 23:29:34 -07:00
|
|
|
nsresult rv = mFile->GetPath(rootPath);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
|
|
|
return collectFilesInternal(aFiles, aSince, rootPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DeviceStorageFile::collectFilesInternal(nsTArray<nsRefPtr<DeviceStorageFile> > &aFiles,
|
2012-09-10 23:52:47 -07:00
|
|
|
PRTime aSince,
|
2012-08-01 23:29:34 -07:00
|
|
|
nsAString& aRootPath)
|
2012-06-19 16:14:39 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> e;
|
|
|
|
mFile->GetDirectoryEntries(getter_AddRefs(e));
|
|
|
|
|
|
|
|
if (!e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDirectoryEnumerator> files = do_QueryInterface(e);
|
|
|
|
nsCOMPtr<nsIFile> f;
|
|
|
|
|
|
|
|
while (NS_SUCCEEDED(files->GetNextFile(getter_AddRefs(f))) && f) {
|
|
|
|
|
2012-08-30 00:10:35 -07:00
|
|
|
PRTime msecs;
|
2012-06-19 16:14:39 -07:00
|
|
|
f->GetLastModifiedTime(&msecs);
|
|
|
|
|
|
|
|
if (msecs < aSince) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDir;
|
|
|
|
f->IsDirectory(&isDir);
|
|
|
|
|
|
|
|
bool isFile;
|
|
|
|
f->IsFile(&isFile);
|
|
|
|
|
|
|
|
nsString fullpath;
|
2012-08-01 23:29:34 -07:00
|
|
|
nsresult rv = f->GetPath(fullpath);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
|
|
|
if (!StringBeginsWith(fullpath, aRootPath)) {
|
|
|
|
NS_ERROR("collectFiles returned a path that does not belong!");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAString::size_type len = aRootPath.Length() + 1; // +1 for the trailing /
|
|
|
|
nsDependentSubstring newPath = Substring(fullpath, len);
|
|
|
|
|
|
|
|
if (isDir) {
|
2012-08-30 15:17:37 -07:00
|
|
|
DeviceStorageFile dsf(mStorageType, f);
|
2012-06-19 16:14:39 -07:00
|
|
|
dsf.SetPath(newPath);
|
|
|
|
dsf.collectFilesInternal(aFiles, aSince, aRootPath);
|
|
|
|
} else if (isFile) {
|
2012-08-30 15:17:37 -07:00
|
|
|
nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(mStorageType, f);
|
2012-06-19 16:14:39 -07:00
|
|
|
dsf->SetPath(newPath);
|
|
|
|
aFiles.AppendElement(dsf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-21 09:18:30 -07:00
|
|
|
|
2012-08-30 20:15:32 -07:00
|
|
|
void
|
2012-09-05 14:30:36 -07:00
|
|
|
DeviceStorageFile::DirectoryDiskUsage(nsIFile* aFile, uint64_t* aSoFar, const nsAString& aStorageType)
|
2012-07-31 12:28:23 -07:00
|
|
|
{
|
|
|
|
if (!aFile) {
|
2012-08-30 20:15:32 -07:00
|
|
|
return;
|
2012-07-31 12:28:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> e;
|
|
|
|
rv = aFile->GetDirectoryEntries(getter_AddRefs(e));
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) || !e) {
|
2012-08-30 20:15:32 -07:00
|
|
|
return;
|
2012-07-31 12:28:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDirectoryEnumerator> files = do_QueryInterface(e);
|
|
|
|
NS_ASSERTION(files, "GetDirectoryEntries must return a nsIDirectoryEnumerator");
|
|
|
|
|
2012-09-13 22:37:00 -07:00
|
|
|
DeviceStorageTypeChecker* typeChecker = DeviceStorageTypeChecker::CreateOrGet();
|
|
|
|
if (!typeChecker) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-31 12:28:23 -07:00
|
|
|
nsCOMPtr<nsIFile> f;
|
|
|
|
while (NS_SUCCEEDED(files->GetNextFile(getter_AddRefs(f))) && f) {
|
|
|
|
bool isDir;
|
|
|
|
rv = f->IsDirectory(&isDir);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isFile;
|
|
|
|
rv = f->IsFile(&isFile);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isLink;
|
|
|
|
rv = f->IsSymlink(&isLink);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-09-05 14:30:36 -07:00
|
|
|
|
2012-07-31 12:28:23 -07:00
|
|
|
if (isLink) {
|
|
|
|
// for now, lets just totally ignore symlinks.
|
|
|
|
NS_WARNING("DirectoryDiskUsage ignores symlinks");
|
|
|
|
} else if (isDir) {
|
2012-09-05 14:30:36 -07:00
|
|
|
DirectoryDiskUsage(f, aSoFar, aStorageType);
|
2012-07-31 12:28:23 -07:00
|
|
|
} else if (isFile) {
|
2012-09-05 14:30:36 -07:00
|
|
|
|
2012-09-13 22:37:00 -07:00
|
|
|
if (!typeChecker->Check(aStorageType, f)) {
|
2012-09-13 22:37:00 -07:00
|
|
|
continue;
|
2012-09-05 14:30:36 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t size;
|
2012-07-31 12:28:23 -07:00
|
|
|
rv = f->GetFileSize(&size);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-09-13 22:37:00 -07:00
|
|
|
*aSoFar += size;
|
2012-07-31 12:28:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-01 13:19:08 -07:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS0(DeviceStorageFile)
|
2012-05-21 09:18:30 -07:00
|
|
|
|
2012-08-09 15:41:18 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2012-08-14 08:53:20 -07:00
|
|
|
nsresult
|
|
|
|
GetSDCardStatus(nsAString& aState) {
|
|
|
|
nsCOMPtr<nsIVolumeService> vs = do_GetService(NS_VOLUMESERVICE_CONTRACTID);
|
|
|
|
if (!vs) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIVolume> vol;
|
|
|
|
vs->GetVolumeByName(NS_LITERAL_STRING("sdcard"), getter_AddRefs(vol));
|
|
|
|
if (!vol) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t state;
|
2012-08-14 08:53:20 -07:00
|
|
|
nsresult rv = vol->GetState(&state);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == nsIVolume::STATE_MOUNTED) {
|
2012-09-13 22:37:00 -07:00
|
|
|
aState.AssignLiteral("available");
|
2012-08-14 08:53:20 -07:00
|
|
|
} else if (state == nsIVolume::STATE_SHARED || state == nsIVolume::STATE_SHAREDMNT) {
|
2012-09-13 22:37:00 -07:00
|
|
|
aState.AssignLiteral("shared");
|
2012-08-14 08:53:20 -07:00
|
|
|
} else {
|
2012-09-13 22:37:00 -07:00
|
|
|
aState.AssignLiteral("unavailable");
|
2012-08-14 08:53:20 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-09 15:41:18 -07:00
|
|
|
static void
|
|
|
|
RegisterForSDCardChanges(nsIObserver* aObserver)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
obs->AddObserver(aObserver, NS_VOLUME_STATE_CHANGED, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
UnregisterForSDCardChanges(nsIObserver* aObserver)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
obs->RemoveObserver(aObserver, NS_VOLUME_STATE_CHANGED);
|
|
|
|
}
|
|
|
|
#endif
|
2012-05-21 09:18:30 -07:00
|
|
|
|
2012-08-01 23:32:08 -07:00
|
|
|
void
|
2012-08-30 15:17:37 -07:00
|
|
|
nsDOMDeviceStorage::SetRootDirectoryForType(const nsAString& aType)
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> f;
|
2012-05-21 09:18:30 -07:00
|
|
|
nsCOMPtr<nsIProperties> dirService = do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID);
|
|
|
|
NS_ASSERTION(dirService, "Must have directory service");
|
|
|
|
|
|
|
|
// Picture directory
|
2012-09-18 13:46:52 -07:00
|
|
|
if (aType.EqualsLiteral(DEVICESTORAGE_PICTURES)) {
|
2012-05-21 09:18:30 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2012-08-30 15:17:39 -07:00
|
|
|
NS_NewLocalFile(NS_LITERAL_STRING("/sdcard"), false, getter_AddRefs(f));
|
2012-05-21 09:18:30 -07:00
|
|
|
#elif defined (MOZ_WIDGET_COCOA)
|
2012-08-01 23:32:11 -07:00
|
|
|
dirService->Get(NS_OSX_PICTURE_DOCUMENTS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f));
|
2012-05-21 09:18:30 -07:00
|
|
|
#elif defined (XP_UNIX)
|
2012-08-01 23:32:11 -07:00
|
|
|
dirService->Get(NS_UNIX_XDG_PICTURES_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f));
|
2012-09-05 14:30:36 -07:00
|
|
|
#elif defined (XP_WIN)
|
2012-09-07 13:23:01 -07:00
|
|
|
dirService->Get(NS_WIN_PICTURES_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f));
|
2012-05-21 09:18:30 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-05-30 01:47:12 -07:00
|
|
|
// Video directory
|
2012-09-18 13:46:52 -07:00
|
|
|
else if (aType.EqualsLiteral(DEVICESTORAGE_VIDEOS)) {
|
2012-05-30 01:47:12 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2012-08-30 15:17:39 -07:00
|
|
|
NS_NewLocalFile(NS_LITERAL_STRING("/sdcard"), false, getter_AddRefs(f));
|
2012-05-30 01:47:12 -07:00
|
|
|
#elif defined (MOZ_WIDGET_COCOA)
|
2012-08-01 23:32:11 -07:00
|
|
|
dirService->Get(NS_OSX_MOVIE_DOCUMENTS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f));
|
2012-05-30 01:47:12 -07:00
|
|
|
#elif defined (XP_UNIX)
|
2012-08-01 23:32:11 -07:00
|
|
|
dirService->Get(NS_UNIX_XDG_VIDEOS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f));
|
2012-09-05 14:30:36 -07:00
|
|
|
#elif defined (XP_WIN)
|
2012-09-07 13:23:01 -07:00
|
|
|
dirService->Get(NS_WIN_VIDEOS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f));
|
2012-05-30 01:47:12 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Music directory
|
2012-09-18 13:46:52 -07:00
|
|
|
else if (aType.EqualsLiteral(DEVICESTORAGE_MUSIC)) {
|
2012-05-30 01:47:12 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2012-08-30 15:17:39 -07:00
|
|
|
NS_NewLocalFile(NS_LITERAL_STRING("/sdcard"), false, getter_AddRefs(f));
|
2012-05-30 01:47:12 -07:00
|
|
|
#elif defined (MOZ_WIDGET_COCOA)
|
2012-08-01 23:32:11 -07:00
|
|
|
dirService->Get(NS_OSX_MUSIC_DOCUMENTS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f));
|
2012-05-30 01:47:12 -07:00
|
|
|
#elif defined (XP_UNIX)
|
2012-08-01 23:32:11 -07:00
|
|
|
dirService->Get(NS_UNIX_XDG_MUSIC_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f));
|
2012-09-05 14:30:36 -07:00
|
|
|
#elif defined (XP_WIN)
|
2012-09-07 13:23:01 -07:00
|
|
|
dirService->Get(NS_WIN_MUSIC_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f));
|
2012-05-30 01:47:12 -07:00
|
|
|
#endif
|
|
|
|
}
|
2012-09-18 13:46:52 -07:00
|
|
|
|
|
|
|
// Apps directory
|
|
|
|
else if (aType.EqualsLiteral(DEVICESTORAGE_APPS)) {
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
NS_NewLocalFile(NS_LITERAL_STRING("/data"), false, getter_AddRefs(f));
|
|
|
|
#else
|
|
|
|
dirService->Get(NS_APP_USER_PROFILE_50_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f));
|
|
|
|
if (f) {
|
|
|
|
f->AppendRelativeNativePath(NS_LITERAL_CSTRING("webapps"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2012-05-30 01:47:12 -07:00
|
|
|
|
2012-09-28 09:50:13 -07:00
|
|
|
// default SDCard
|
|
|
|
else if (aType.EqualsLiteral(DEVICESTORAGE_SDCARD)) {
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
NS_NewLocalFile(NS_LITERAL_STRING("/sdcard"), false, getter_AddRefs(f));
|
|
|
|
#else
|
|
|
|
// Eventually, on desktop, we want to do something smarter -- for example,
|
|
|
|
// detect when an sdcard is inserted, and use that instead of this.
|
|
|
|
dirService->Get(NS_APP_USER_PROFILE_50_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f));
|
|
|
|
if (f) {
|
|
|
|
f->AppendRelativeNativePath(NS_LITERAL_CSTRING("fake-sdcard"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-09-05 14:30:36 -07:00
|
|
|
// in testing, we default all device storage types to a temp directory
|
|
|
|
if (f && mozilla::Preferences::GetBool("device.storage.testing", false)) {
|
|
|
|
dirService->Get(NS_OS_TEMP_DIR, NS_GET_IID(nsIFile), getter_AddRefs(f));
|
|
|
|
if (f) {
|
|
|
|
f->AppendRelativeNativePath(NS_LITERAL_CSTRING("device-storage-testing"));
|
|
|
|
f->Create(nsIFile::DIRECTORY_TYPE, 0777);
|
|
|
|
f->Normalize();
|
2012-05-21 09:18:30 -07:00
|
|
|
}
|
2012-08-30 15:17:37 -07:00
|
|
|
}
|
2012-05-21 09:18:30 -07:00
|
|
|
|
2012-08-09 15:41:18 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
RegisterForSDCardChanges(this);
|
|
|
|
#endif
|
2012-08-17 19:43:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
obs->AddObserver(this, "file-watcher-update", false);
|
2012-08-30 15:17:37 -07:00
|
|
|
mRootDirectory = f;
|
|
|
|
mStorageType = aType;
|
2012-05-21 09:18:30 -07:00
|
|
|
}
|
|
|
|
|
2012-07-31 12:28:23 -07:00
|
|
|
jsval InterfaceToJsval(nsPIDOMWindow* aWindow, nsISupports* aObject, const nsIID* aIID)
|
2012-06-19 16:14:39 -07:00
|
|
|
{
|
2012-05-21 09:18:30 -07:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aWindow);
|
|
|
|
if (!sgo) {
|
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
nsIScriptContext *scriptContext = sgo->GetScriptContext();
|
|
|
|
if (!scriptContext) {
|
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext *cx = scriptContext->GetNativeContext();
|
|
|
|
if (!cx) {
|
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-31 12:28:23 -07:00
|
|
|
jsval someJsVal;
|
2012-05-21 09:18:30 -07:00
|
|
|
nsresult rv = nsContentUtils::WrapNative(cx,
|
|
|
|
JS_GetGlobalObject(cx),
|
2012-07-31 12:28:23 -07:00
|
|
|
aObject,
|
|
|
|
aIID,
|
|
|
|
&someJsVal);
|
2012-05-21 09:18:30 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-31 12:28:23 -07:00
|
|
|
return someJsVal;
|
2012-05-21 09:18:30 -07:00
|
|
|
}
|
|
|
|
|
2012-07-31 12:28:23 -07:00
|
|
|
jsval nsIFileToJsval(nsPIDOMWindow* aWindow, DeviceStorageFile* aFile)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aWindow, "Null Window");
|
|
|
|
|
2012-10-04 13:40:45 -07:00
|
|
|
if (!aFile) {
|
2012-07-31 12:28:23 -07:00
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
|
|
|
|
2012-10-04 13:40:45 -07:00
|
|
|
if (aFile->mEditable) {
|
|
|
|
// TODO - needs janv's file handle support.
|
2012-07-31 12:28:23 -07:00
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
|
|
|
|
2012-11-27 03:48:15 -08:00
|
|
|
nsCOMPtr<nsIDOMBlob> blob = new nsDOMFileFile(aFile->mFile, aFile->mPath,
|
|
|
|
EmptyString());
|
2012-07-31 12:28:23 -07:00
|
|
|
return InterfaceToJsval(aWindow, blob, &NS_GET_IID(nsIDOMBlob));
|
2013-01-25 11:05:24 -08:00
|
|
|
}
|
2012-07-31 12:28:23 -07:00
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
jsval StringToJsval(nsPIDOMWindow* aWindow, nsAString& aString)
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aWindow, "Null Window");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aWindow);
|
|
|
|
if (!sgo) {
|
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
nsIScriptContext *scriptContext = sgo->GetScriptContext();
|
|
|
|
if (!scriptContext) {
|
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext *cx = scriptContext->GetNativeContext();
|
|
|
|
if (!cx) {
|
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
|
|
|
|
jsval result = JSVAL_NULL;
|
|
|
|
if (!xpc::StringToJsval(cx, aString, &result)) {
|
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
class DeviceStorageCursorRequest MOZ_FINAL
|
|
|
|
: public nsIContentPermissionRequest
|
|
|
|
, public PCOMContentPermissionRequestChild
|
2012-05-29 16:12:39 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(DeviceStorageCursorRequest, nsIContentPermissionRequest)
|
|
|
|
|
|
|
|
NS_FORWARD_NSICONTENTPERMISSIONREQUEST(mCursor->);
|
|
|
|
|
|
|
|
DeviceStorageCursorRequest(nsDOMDeviceStorageCursor* aCursor)
|
|
|
|
: mCursor(aCursor) { }
|
|
|
|
|
|
|
|
~DeviceStorageCursorRequest() {}
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
bool Recv__delete__(const bool& allow)
|
|
|
|
{
|
|
|
|
if (allow) {
|
|
|
|
Allow();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Cancel();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IPDLRelease()
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
2012-05-29 16:12:39 -07:00
|
|
|
private:
|
|
|
|
nsRefPtr<nsDOMDeviceStorageCursor> mCursor;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeviceStorageCursorRequest)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIContentPermissionRequest)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIContentPermissionRequest)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(DeviceStorageCursorRequest)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(DeviceStorageCursorRequest)
|
|
|
|
|
2012-12-02 00:54:37 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_1(DeviceStorageCursorRequest,
|
|
|
|
mCursor)
|
2012-05-29 16:12:39 -07:00
|
|
|
|
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
class PostErrorEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2012-10-13 08:20:14 -07:00
|
|
|
PostErrorEvent(nsRefPtr<DOMRequest>& aRequest, const char* aMessage)
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
|
|
|
mRequest.swap(aRequest);
|
2012-10-13 08:20:14 -07:00
|
|
|
CopyASCIItoUTF16(aMessage, mError);
|
2012-05-21 09:18:30 -07:00
|
|
|
}
|
|
|
|
|
2012-10-13 08:20:14 -07:00
|
|
|
PostErrorEvent(DOMRequest* aRequest, const char* aMessage)
|
2012-05-21 09:18:30 -07:00
|
|
|
: mRequest(aRequest)
|
|
|
|
{
|
2012-10-13 08:20:14 -07:00
|
|
|
CopyASCIItoUTF16(aMessage, mError);
|
2012-05-21 09:18:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
~PostErrorEvent() {}
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2012-05-21 09:18:30 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2012-08-08 14:07:39 -07:00
|
|
|
mRequest->FireError(mError);
|
2012-07-30 07:20:58 -07:00
|
|
|
mRequest = nullptr;
|
2012-05-21 09:18:30 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<DOMRequest> mRequest;
|
|
|
|
nsString mError;
|
|
|
|
};
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
ContinueCursorEvent::ContinueCursorEvent(nsRefPtr<DOMRequest>& aRequest)
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
2012-06-19 16:14:39 -07:00
|
|
|
mRequest.swap(aRequest);
|
|
|
|
}
|
2012-05-21 09:18:30 -07:00
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
ContinueCursorEvent::ContinueCursorEvent(DOMRequest* aRequest)
|
|
|
|
: mRequest(aRequest)
|
|
|
|
{
|
|
|
|
}
|
2012-05-21 09:18:30 -07:00
|
|
|
|
2012-10-04 13:40:45 -07:00
|
|
|
already_AddRefed<DeviceStorageFile>
|
|
|
|
ContinueCursorEvent::GetNextFile()
|
|
|
|
{
|
2012-06-19 16:14:39 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2012-05-21 09:18:30 -07:00
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
nsDOMDeviceStorageCursor* cursor = static_cast<nsDOMDeviceStorageCursor*>(mRequest.get());
|
2012-08-30 15:17:38 -07:00
|
|
|
nsString cursorStorageType;
|
|
|
|
cursor->GetStorageType(cursorStorageType);
|
|
|
|
|
2012-09-13 22:37:00 -07:00
|
|
|
DeviceStorageTypeChecker* typeChecker = DeviceStorageTypeChecker::CreateOrGet();
|
|
|
|
if (!typeChecker) {
|
2012-10-04 13:40:45 -07:00
|
|
|
return nullptr;
|
2012-09-13 22:37:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-30 15:17:38 -07:00
|
|
|
while (cursor->mFiles.Length() > 0) {
|
2012-06-19 16:14:39 -07:00
|
|
|
nsRefPtr<DeviceStorageFile> file = cursor->mFiles[0];
|
|
|
|
cursor->mFiles.RemoveElementAt(0);
|
2012-09-13 22:37:00 -07:00
|
|
|
if (!typeChecker->Check(cursorStorageType, file->mFile)) {
|
2012-08-30 15:17:38 -07:00
|
|
|
continue;
|
|
|
|
}
|
2012-10-04 13:40:45 -07:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ContinueCursorEvent::~ContinueCursorEvent() {}
|
|
|
|
|
|
|
|
void
|
|
|
|
ContinueCursorEvent::Continue()
|
|
|
|
{
|
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Default) {
|
|
|
|
NS_DispatchToMainThread(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<DeviceStorageFile> file = GetNextFile();
|
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
// done with enumeration.
|
|
|
|
NS_DispatchToMainThread(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString fullpath;
|
|
|
|
nsresult rv = file->mFile->GetPath(fullpath);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ASSERTION(false, "GetPath failed to return a valid path");
|
|
|
|
return;
|
2012-06-19 16:14:39 -07:00
|
|
|
}
|
2012-07-13 21:01:52 -07:00
|
|
|
|
2012-10-04 13:40:45 -07:00
|
|
|
nsDOMDeviceStorageCursor* cursor = static_cast<nsDOMDeviceStorageCursor*>(mRequest.get());
|
|
|
|
nsString cursorStorageType;
|
|
|
|
cursor->GetStorageType(cursorStorageType);
|
|
|
|
|
|
|
|
DeviceStorageRequestChild* child = new DeviceStorageRequestChild(mRequest, file);
|
|
|
|
child->SetCallback(cursor);
|
|
|
|
DeviceStorageGetParams params(cursorStorageType, file->mPath, fullpath);
|
|
|
|
ContentChild::GetSingleton()->SendPDeviceStorageRequestConstructor(child, params);
|
|
|
|
mRequest = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ContinueCursorEvent::Run()
|
|
|
|
{
|
|
|
|
nsRefPtr<DeviceStorageFile> file = GetNextFile();
|
|
|
|
|
|
|
|
nsDOMDeviceStorageCursor* cursor = static_cast<nsDOMDeviceStorageCursor*>(mRequest.get());
|
|
|
|
jsval val = nsIFileToJsval(cursor->GetOwner(), file);
|
|
|
|
|
|
|
|
cursor->mOkToCallContinue = true;
|
|
|
|
|
2012-08-08 14:07:39 -07:00
|
|
|
mRequest->FireSuccess(val);
|
2012-07-30 07:20:58 -07:00
|
|
|
mRequest = nullptr;
|
2012-06-19 16:14:39 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-05-21 09:18:30 -07:00
|
|
|
|
|
|
|
class InitCursorEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
InitCursorEvent(DOMRequest* aRequest, DeviceStorageFile* aFile)
|
|
|
|
: mFile(aFile)
|
|
|
|
, mRequest(aRequest)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~InitCursorEvent() {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
bool check;
|
|
|
|
mFile->mFile->IsDirectory(&check);
|
|
|
|
if (!check) {
|
2012-10-13 08:20:14 -07:00
|
|
|
nsCOMPtr<PostErrorEvent> event = new PostErrorEvent(mRequest, POST_ERROR_EVENT_FILE_NOT_ENUMERABLE);
|
2012-05-21 09:18:30 -07:00
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
nsDOMDeviceStorageCursor* cursor = static_cast<nsDOMDeviceStorageCursor*>(mRequest.get());
|
|
|
|
mFile->CollectFiles(cursor->mFiles, cursor->mSince);
|
2012-05-21 09:18:30 -07:00
|
|
|
|
|
|
|
nsCOMPtr<ContinueCursorEvent> event = new ContinueCursorEvent(mRequest);
|
2012-12-21 09:18:19 -08:00
|
|
|
event->Continue();
|
2012-05-21 09:18:30 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<DeviceStorageFile> mFile;
|
|
|
|
nsRefPtr<DOMRequest> mRequest;
|
|
|
|
};
|
|
|
|
|
|
|
|
DOMCI_DATA(DeviceStorageCursor, nsDOMDeviceStorageCursor)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsDOMDeviceStorageCursor)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMDeviceStorageCursor)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIContentPermissionRequest)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMDOMRequest)
|
2012-05-29 16:12:39 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMDeviceStorageCursor)
|
2012-05-21 09:18:30 -07:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(DeviceStorageCursor)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMRequest)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(nsDOMDeviceStorageCursor, DOMRequest)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsDOMDeviceStorageCursor, DOMRequest)
|
|
|
|
|
|
|
|
nsDOMDeviceStorageCursor::nsDOMDeviceStorageCursor(nsIDOMWindow* aWindow,
|
2012-07-30 07:58:26 -07:00
|
|
|
nsIPrincipal* aPrincipal,
|
2012-05-21 09:18:30 -07:00
|
|
|
DeviceStorageFile* aFile,
|
2012-09-10 23:52:47 -07:00
|
|
|
PRTime aSince)
|
2012-05-21 09:18:30 -07:00
|
|
|
: DOMRequest(aWindow)
|
|
|
|
, mOkToCallContinue(false)
|
2012-06-19 16:14:39 -07:00
|
|
|
, mSince(aSince)
|
2012-05-21 09:18:30 -07:00
|
|
|
, mFile(aFile)
|
2012-07-30 07:58:26 -07:00
|
|
|
, mPrincipal(aPrincipal)
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMDeviceStorageCursor::~nsDOMDeviceStorageCursor()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-30 15:17:38 -07:00
|
|
|
void
|
|
|
|
nsDOMDeviceStorageCursor::GetStorageType(nsAString & aType)
|
|
|
|
{
|
|
|
|
aType = mFile->mStorageType;
|
|
|
|
}
|
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDeviceStorageCursor::GetType(nsACString & aType)
|
|
|
|
{
|
2012-09-13 22:37:00 -07:00
|
|
|
return DeviceStorageTypeChecker::GetPermissionForType(mFile->mStorageType, aType);
|
2012-05-21 09:18:30 -07:00
|
|
|
}
|
|
|
|
|
2012-11-13 16:06:42 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDeviceStorageCursor::GetAccess(nsACString & aAccess)
|
|
|
|
{
|
|
|
|
aAccess = NS_LITERAL_CSTRING("read");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
NS_IMETHODIMP
|
2012-07-30 07:58:26 -07:00
|
|
|
nsDOMDeviceStorageCursor::GetPrincipal(nsIPrincipal * *aRequestingPrincipal)
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
2012-07-30 07:58:26 -07:00
|
|
|
NS_IF_ADDREF(*aRequestingPrincipal = mPrincipal);
|
2012-05-21 09:18:30 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDeviceStorageCursor::GetWindow(nsIDOMWindow * *aRequestingWindow)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aRequestingWindow = GetOwner());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDeviceStorageCursor::GetElement(nsIDOMElement * *aRequestingElement)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*aRequestingElement = nullptr;
|
2012-05-21 09:18:30 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDeviceStorageCursor::Cancel()
|
|
|
|
{
|
2012-10-13 08:20:14 -07:00
|
|
|
nsCOMPtr<PostErrorEvent> event = new PostErrorEvent(this, POST_ERROR_EVENT_PERMISSION_DENIED);
|
2012-05-21 09:18:30 -07:00
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDeviceStorageCursor::Allow()
|
|
|
|
{
|
2012-06-19 16:14:39 -07:00
|
|
|
if (!mFile->IsSafePath()) {
|
2012-10-13 08:20:14 -07:00
|
|
|
nsCOMPtr<nsIRunnable> r = new PostErrorEvent(this, POST_ERROR_EVENT_PERMISSION_DENIED);
|
2012-05-21 09:18:30 -07:00
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
|
|
|
|
nsString fullpath;
|
2012-08-01 23:29:34 -07:00
|
|
|
nsresult rv = mFile->mFile->GetPath(fullpath);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// just do nothing
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
|
|
|
PDeviceStorageRequestChild* child = new DeviceStorageRequestChild(this, mFile);
|
2012-08-30 15:17:37 -07:00
|
|
|
DeviceStorageEnumerationParams params(mFile->mStorageType, fullpath, mSince);
|
2012-06-19 16:14:39 -07:00
|
|
|
ContentChild::GetSingleton()->SendPDeviceStorageRequestConstructor(child, params);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
nsCOMPtr<nsIEventTarget> target = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
|
|
|
|
NS_ASSERTION(target, "Must have stream transport service");
|
|
|
|
|
|
|
|
nsCOMPtr<InitCursorEvent> event = new InitCursorEvent(this, mFile);
|
|
|
|
target->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDeviceStorageCursor::Continue()
|
|
|
|
{
|
|
|
|
if (!mOkToCallContinue) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mRooted) {
|
|
|
|
// We call onsuccess multiple times. clear the last
|
|
|
|
// rooted result.
|
|
|
|
mResult = JSVAL_VOID;
|
2012-11-27 17:37:57 -08:00
|
|
|
NS_DROP_JS_OBJECTS(this, nsDOMDeviceStorageCursor);
|
2012-05-21 09:18:30 -07:00
|
|
|
mDone = false;
|
|
|
|
mRooted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<ContinueCursorEvent> event = new ContinueCursorEvent(this);
|
2012-12-21 09:18:19 -08:00
|
|
|
event->Continue();
|
2012-05-21 09:18:30 -07:00
|
|
|
|
|
|
|
mOkToCallContinue = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
bool
|
|
|
|
nsDOMDeviceStorageCursor::Recv__delete__(const bool& allow)
|
|
|
|
{
|
|
|
|
if (allow) {
|
|
|
|
Allow();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Cancel();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMDeviceStorageCursor::IPDLRelease()
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
}
|
2012-05-21 09:18:30 -07:00
|
|
|
|
2012-10-04 13:40:45 -07:00
|
|
|
void
|
|
|
|
nsDOMDeviceStorageCursor::RequestComplete()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mOkToCallContinue, "mOkToCallContinue must be false");
|
|
|
|
mOkToCallContinue = true;
|
|
|
|
}
|
|
|
|
|
2013-01-25 11:05:24 -08:00
|
|
|
class PostAvailableResultEvent : public nsRunnable
|
2012-07-31 12:28:23 -07:00
|
|
|
{
|
|
|
|
public:
|
2013-01-25 11:05:24 -08:00
|
|
|
PostAvailableResultEvent(DOMRequest* aRequest)
|
|
|
|
: mRequest(aRequest)
|
|
|
|
{
|
|
|
|
}
|
2012-07-31 12:28:23 -07:00
|
|
|
|
2013-01-25 11:05:24 -08:00
|
|
|
~PostAvailableResultEvent() {}
|
2012-07-31 12:28:23 -07:00
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-08-16 16:03:06 -07:00
|
|
|
nsString state;
|
|
|
|
state.Assign(NS_LITERAL_STRING("available"));
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
nsresult rv = GetSDCardStatus(state);
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-08-30 20:15:32 -07:00
|
|
|
state.Assign(NS_LITERAL_STRING("unavailable"));
|
2012-08-16 16:03:06 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-01-25 11:05:24 -08:00
|
|
|
jsval result = StringToJsval(mRequest->GetOwner(), state);
|
2012-07-31 12:28:23 -07:00
|
|
|
mRequest->FireSuccess(result);
|
2012-08-12 03:43:47 -07:00
|
|
|
mRequest = nullptr;
|
2012-07-31 12:28:23 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<DOMRequest> mRequest;
|
|
|
|
};
|
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
class PostResultEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2012-06-27 13:47:35 -07:00
|
|
|
PostResultEvent(nsRefPtr<DOMRequest>& aRequest, DeviceStorageFile* aFile)
|
|
|
|
: mFile(aFile)
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
|
|
|
mRequest.swap(aRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
PostResultEvent(nsRefPtr<DOMRequest>& aRequest, const nsAString & aPath)
|
|
|
|
: mPath(aPath)
|
|
|
|
{
|
|
|
|
mRequest.swap(aRequest);
|
|
|
|
}
|
|
|
|
|
2013-01-25 11:05:24 -08:00
|
|
|
PostResultEvent(nsRefPtr<DOMRequest>& aRequest, const int64_t aValue)
|
|
|
|
: mValue(aValue)
|
|
|
|
{
|
|
|
|
mRequest.swap(aRequest);
|
|
|
|
}
|
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
~PostResultEvent() {}
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
NS_IMETHOD Run()
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
jsval result = JSVAL_NULL;
|
2013-01-25 11:05:24 -08:00
|
|
|
nsPIDOMWindow* window = mRequest->GetOwner();
|
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
if (mFile) {
|
2013-01-25 11:05:24 -08:00
|
|
|
result = nsIFileToJsval(window, mFile);
|
|
|
|
} else if (mPath.Length()) {
|
|
|
|
result = StringToJsval(window, mPath);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = JS_NumberValue(double(mValue));
|
2012-05-21 09:18:30 -07:00
|
|
|
}
|
|
|
|
|
2012-08-08 14:07:39 -07:00
|
|
|
mRequest->FireSuccess(result);
|
2012-07-30 07:20:58 -07:00
|
|
|
mRequest = nullptr;
|
2012-05-21 09:18:30 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<DeviceStorageFile> mFile;
|
|
|
|
nsString mPath;
|
2013-01-25 11:05:24 -08:00
|
|
|
int64_t mValue;
|
2012-05-21 09:18:30 -07:00
|
|
|
nsRefPtr<DOMRequest> mRequest;
|
|
|
|
};
|
|
|
|
|
|
|
|
class WriteFileEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2012-06-19 16:14:39 -07:00
|
|
|
WriteFileEvent(nsIDOMBlob* aBlob,
|
2012-05-21 09:18:30 -07:00
|
|
|
DeviceStorageFile *aFile,
|
|
|
|
nsRefPtr<DOMRequest>& aRequest)
|
|
|
|
: mBlob(aBlob)
|
|
|
|
, mFile(aFile)
|
|
|
|
{
|
|
|
|
mRequest.swap(aRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
~WriteFileEvent() {}
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
NS_IMETHOD Run()
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-08-09 15:41:18 -07:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
mBlob->GetInternalStream(getter_AddRefs(stream));
|
|
|
|
|
2012-10-13 08:20:14 -07:00
|
|
|
bool check = false;
|
|
|
|
mFile->mFile->Exists(&check);
|
|
|
|
if (check) {
|
2012-10-13 08:20:14 -07:00
|
|
|
nsCOMPtr<PostErrorEvent> event = new PostErrorEvent(mRequest, POST_ERROR_EVENT_FILE_EXISTS);
|
2012-10-13 08:20:14 -07:00
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-09 15:41:18 -07:00
|
|
|
nsresult rv = mFile->Write(stream);
|
2012-07-13 21:01:52 -07:00
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-06-19 16:14:39 -07:00
|
|
|
mFile->mFile->Remove(false);
|
2012-07-13 21:01:52 -07:00
|
|
|
|
2012-10-13 08:20:14 -07:00
|
|
|
nsCOMPtr<PostErrorEvent> event = new PostErrorEvent(mRequest, POST_ERROR_EVENT_UNKNOWN);
|
2012-06-19 16:14:39 -07:00
|
|
|
NS_DispatchToMainThread(event);
|
2012-07-13 21:01:52 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-17 19:43:00 -07:00
|
|
|
nsCOMPtr<PostResultEvent> event = new PostResultEvent(mRequest, mFile->mPath);
|
2012-05-21 09:18:30 -07:00
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIDOMBlob> mBlob;
|
|
|
|
nsRefPtr<DeviceStorageFile> mFile;
|
|
|
|
nsRefPtr<DOMRequest> mRequest;
|
|
|
|
};
|
2012-08-17 19:43:00 -07:00
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
class ReadFileEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ReadFileEvent(DeviceStorageFile* aFile,
|
|
|
|
nsRefPtr<DOMRequest>& aRequest)
|
|
|
|
: mFile(aFile)
|
|
|
|
{
|
|
|
|
mRequest.swap(aRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
~ReadFileEvent() {}
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
NS_IMETHOD Run()
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsRefPtr<nsRunnable> r;
|
2012-06-27 13:47:35 -07:00
|
|
|
if (!mFile->mEditable) {
|
2012-05-21 09:18:30 -07:00
|
|
|
bool check = false;
|
|
|
|
mFile->mFile->Exists(&check);
|
|
|
|
if (!check) {
|
2012-10-13 08:20:14 -07:00
|
|
|
r = new PostErrorEvent(mRequest, POST_ERROR_EVENT_FILE_DOES_NOT_EXIST);
|
2012-05-21 09:18:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!r) {
|
2012-06-27 13:47:35 -07:00
|
|
|
r = new PostResultEvent(mRequest, mFile);
|
2012-05-21 09:18:30 -07:00
|
|
|
}
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<DeviceStorageFile> mFile;
|
|
|
|
nsRefPtr<DOMRequest> mRequest;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DeleteFileEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DeleteFileEvent(DeviceStorageFile* aFile,
|
|
|
|
nsRefPtr<DOMRequest>& aRequest)
|
|
|
|
: mFile(aFile)
|
|
|
|
{
|
|
|
|
mRequest.swap(aRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
~DeleteFileEvent() {}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
2012-08-17 19:43:00 -07:00
|
|
|
mFile->Remove();
|
2012-06-01 13:19:08 -07:00
|
|
|
|
|
|
|
nsRefPtr<nsRunnable> r;
|
|
|
|
bool check = false;
|
|
|
|
mFile->mFile->Exists(&check);
|
|
|
|
if (check) {
|
2012-10-13 08:20:14 -07:00
|
|
|
r = new PostErrorEvent(mRequest, POST_ERROR_EVENT_FILE_DOES_NOT_EXIST);
|
2012-06-01 13:19:08 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
r = new PostResultEvent(mRequest, mFile->mPath);
|
|
|
|
}
|
|
|
|
NS_DispatchToMainThread(r);
|
2012-05-21 09:18:30 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<DeviceStorageFile> mFile;
|
|
|
|
nsRefPtr<DOMRequest> mRequest;
|
|
|
|
};
|
|
|
|
|
2013-01-25 11:05:24 -08:00
|
|
|
class UsedSpaceFileEvent : public nsRunnable
|
2012-07-31 12:28:23 -07:00
|
|
|
{
|
|
|
|
public:
|
2013-01-25 11:05:24 -08:00
|
|
|
UsedSpaceFileEvent(DeviceStorageFile* aFile, nsRefPtr<DOMRequest>& aRequest)
|
2012-07-31 12:28:23 -07:00
|
|
|
: mFile(aFile)
|
|
|
|
{
|
|
|
|
mRequest.swap(aRequest);
|
|
|
|
}
|
|
|
|
|
2013-01-25 11:05:24 -08:00
|
|
|
~UsedSpaceFileEvent() {}
|
2012-07-31 12:28:23 -07:00
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
nsCOMPtr<nsIRunnable> r;
|
2012-08-30 20:15:32 -07:00
|
|
|
uint64_t diskUsage = 0;
|
2012-09-05 14:30:36 -07:00
|
|
|
DeviceStorageFile::DirectoryDiskUsage(mFile->mFile, &diskUsage, mFile->mStorageType);
|
2013-01-25 11:05:24 -08:00
|
|
|
|
|
|
|
r = new PostResultEvent(mRequest, diskUsage);
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<DeviceStorageFile> mFile;
|
|
|
|
nsRefPtr<DOMRequest> mRequest;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FreeSpaceFileEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FreeSpaceFileEvent(DeviceStorageFile* aFile, nsRefPtr<DOMRequest>& aRequest)
|
|
|
|
: mFile(aFile)
|
|
|
|
{
|
|
|
|
mRequest.swap(aRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
~FreeSpaceFileEvent() {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
nsCOMPtr<nsIRunnable> r;
|
2012-08-30 20:15:32 -07:00
|
|
|
int64_t freeSpace = 0;
|
2012-07-31 12:28:23 -07:00
|
|
|
nsresult rv = mFile->mFile->GetDiskSpaceAvailable(&freeSpace);
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-08-16 16:14:38 -07:00
|
|
|
freeSpace = 0;
|
2012-07-31 12:28:23 -07:00
|
|
|
}
|
2012-08-16 16:03:06 -07:00
|
|
|
|
2013-01-25 11:05:24 -08:00
|
|
|
r = new PostResultEvent(mRequest, freeSpace);
|
2012-07-31 12:28:23 -07:00
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<DeviceStorageFile> mFile;
|
|
|
|
nsRefPtr<DOMRequest> mRequest;
|
|
|
|
};
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
class DeviceStorageRequest MOZ_FINAL
|
|
|
|
: public nsIContentPermissionRequest
|
|
|
|
, public nsIRunnable
|
|
|
|
, public PCOMContentPermissionRequestChild
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2012-08-01 23:29:34 -07:00
|
|
|
DeviceStorageRequest(const DeviceStorageRequestType aRequestType,
|
|
|
|
nsPIDOMWindow *aWindow,
|
2012-07-30 07:58:26 -07:00
|
|
|
nsIPrincipal *aPrincipal,
|
2012-08-01 23:29:34 -07:00
|
|
|
DeviceStorageFile *aFile,
|
|
|
|
DOMRequest* aRequest,
|
|
|
|
nsDOMDeviceStorage *aDeviceStorage,
|
|
|
|
nsIDOMEventListener *aListener)
|
|
|
|
: mRequestType(aRequestType)
|
|
|
|
, mWindow(aWindow)
|
2012-07-30 07:58:26 -07:00
|
|
|
, mPrincipal(aPrincipal)
|
2012-08-01 23:29:34 -07:00
|
|
|
, mFile(aFile)
|
|
|
|
, mRequest(aRequest)
|
|
|
|
, mDeviceStorage(aDeviceStorage)
|
2012-07-30 07:58:26 -07:00
|
|
|
, mListener(aListener) {}
|
2012-08-01 23:29:34 -07:00
|
|
|
|
|
|
|
DeviceStorageRequest(const DeviceStorageRequestType aRequestType,
|
2012-05-21 09:18:30 -07:00
|
|
|
nsPIDOMWindow *aWindow,
|
2012-07-30 07:58:26 -07:00
|
|
|
nsIPrincipal *aPrincipal,
|
2012-05-21 09:18:30 -07:00
|
|
|
DeviceStorageFile *aFile,
|
|
|
|
DOMRequest* aRequest,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIDOMBlob *aBlob = nullptr)
|
2012-08-01 23:29:34 -07:00
|
|
|
: mRequestType(aRequestType)
|
|
|
|
, mWindow(aWindow)
|
2012-07-30 07:58:26 -07:00
|
|
|
, mPrincipal(aPrincipal)
|
2012-08-01 23:29:34 -07:00
|
|
|
, mFile(aFile)
|
|
|
|
, mRequest(aRequest)
|
|
|
|
, mBlob(aBlob) {}
|
2012-05-21 09:18:30 -07:00
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(DeviceStorageRequest, nsIContentPermissionRequest)
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
|
|
|
|
if (mozilla::Preferences::GetBool("device.storage.prompt.testing", false)) {
|
|
|
|
Allow();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
|
|
|
|
|
|
|
// because owner implements nsITabChild, we can assume that it is
|
|
|
|
// the one and only TabChild.
|
|
|
|
TabChild* child = GetTabChildFrom(mWindow->GetDocShell());
|
2012-08-01 23:29:34 -07:00
|
|
|
if (!child) {
|
2012-07-27 06:59:29 -07:00
|
|
|
return NS_OK;
|
2012-08-01 23:29:34 -07:00
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
|
|
|
// Retain a reference so the object isn't deleted without IPDL's knowledge.
|
|
|
|
// Corresponding release occurs in DeallocPContentPermissionRequest.
|
|
|
|
AddRef();
|
|
|
|
|
2012-09-13 22:37:00 -07:00
|
|
|
nsCString type;
|
|
|
|
nsresult rv = DeviceStorageTypeChecker::GetPermissionForType(mFile->mStorageType, type);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-11-13 16:06:42 -08:00
|
|
|
nsCString access;
|
|
|
|
rv = DeviceStorageTypeChecker::GetAccessForRequest(DeviceStorageRequestType(mRequestType), access);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
child->SendPContentPermissionRequestConstructor(this, type, access, IPC::Principal(mPrincipal));
|
2012-06-19 16:14:39 -07:00
|
|
|
|
|
|
|
Sendprompt();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-31 10:06:42 -07:00
|
|
|
nsCOMPtr<nsIContentPermissionPrompt> prompt = do_CreateInstance(NS_CONTENT_PERMISSION_PROMPT_CONTRACTID);
|
2012-05-21 09:18:30 -07:00
|
|
|
if (prompt) {
|
|
|
|
prompt->Prompt(this);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD GetType(nsACString & aType)
|
|
|
|
{
|
2012-09-13 22:37:00 -07:00
|
|
|
nsCString type;
|
|
|
|
nsresult rv = DeviceStorageTypeChecker::GetPermissionForType(mFile->mStorageType, aType);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-05-21 09:18:30 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-13 16:06:42 -08:00
|
|
|
NS_IMETHOD GetAccess(nsACString & aAccess)
|
|
|
|
{
|
|
|
|
nsresult rv = DeviceStorageTypeChecker::GetAccessForRequest(DeviceStorageRequestType(mRequestType), aAccess);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:58:26 -07:00
|
|
|
NS_IMETHOD GetPrincipal(nsIPrincipal * *aRequestingPrincipal)
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
2012-07-30 07:58:26 -07:00
|
|
|
NS_IF_ADDREF(*aRequestingPrincipal = mPrincipal);
|
2012-05-21 09:18:30 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD GetWindow(nsIDOMWindow * *aRequestingWindow)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aRequestingWindow = mWindow);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD GetElement(nsIDOMElement * *aRequestingElement)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*aRequestingElement = nullptr;
|
2012-05-21 09:18:30 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Cancel()
|
|
|
|
{
|
2012-10-13 08:20:14 -07:00
|
|
|
nsCOMPtr<PostErrorEvent> event = new PostErrorEvent(mRequest, POST_ERROR_EVENT_PERMISSION_DENIED);
|
2012-05-21 09:18:30 -07:00
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Allow()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> r;
|
|
|
|
|
|
|
|
if (!mRequest) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
nsString fullpath;
|
2012-08-01 23:29:34 -07:00
|
|
|
nsresult rv = mFile->mFile->GetPath(fullpath);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// just do nothing
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
switch(mRequestType) {
|
2012-11-19 12:31:21 -08:00
|
|
|
case DEVICE_STORAGE_REQUEST_CREATE:
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
|
|
|
if (!mBlob) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-01-09 05:43:59 -08:00
|
|
|
DeviceStorageTypeChecker* typeChecker = DeviceStorageTypeChecker::CreateOrGet();
|
|
|
|
if (!typeChecker) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!typeChecker->Check(mFile->mStorageType, mFile->mFile) ||
|
|
|
|
!typeChecker->Check(mFile->mStorageType, mBlob)) {
|
|
|
|
r = new PostErrorEvent(mRequest, POST_ERROR_EVENT_ILLEGAL_TYPE);
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-01 23:29:34 -07:00
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-09-13 22:37:00 -07:00
|
|
|
BlobChild* actor = ContentChild::GetSingleton()->GetOrCreateActorForBlob(mBlob);
|
|
|
|
if (!actor) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-08-09 15:41:18 -07:00
|
|
|
DeviceStorageAddParams params;
|
2012-09-13 22:37:00 -07:00
|
|
|
params.blobChild() = actor;
|
|
|
|
params.type() = mFile->mStorageType;
|
|
|
|
params.name() = mFile->mPath;
|
|
|
|
params.fullpath() = fullpath;
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-08-09 15:41:18 -07:00
|
|
|
PDeviceStorageRequestChild* child = new DeviceStorageRequestChild(mRequest, mFile);
|
2012-08-01 23:29:34 -07:00
|
|
|
ContentChild::GetSingleton()->SendPDeviceStorageRequestConstructor(child, params);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
r = new WriteFileEvent(mBlob, mFile, mRequest);
|
2012-05-21 09:18:30 -07:00
|
|
|
break;
|
|
|
|
}
|
2012-08-01 23:29:34 -07:00
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
case DEVICE_STORAGE_REQUEST_READ:
|
2012-11-19 12:31:21 -08:00
|
|
|
case DEVICE_STORAGE_REQUEST_WRITE:
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
2013-01-09 05:43:59 -08:00
|
|
|
DeviceStorageTypeChecker* typeChecker = DeviceStorageTypeChecker::CreateOrGet();
|
|
|
|
if (!typeChecker) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!typeChecker->Check(mFile->mStorageType, mFile->mFile)) {
|
|
|
|
r = new PostErrorEvent(mRequest, POST_ERROR_EVENT_ILLEGAL_TYPE);
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-01 23:29:34 -07:00
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
PDeviceStorageRequestChild* child = new DeviceStorageRequestChild(mRequest, mFile);
|
2012-08-30 15:17:37 -07:00
|
|
|
DeviceStorageGetParams params(mFile->mStorageType, mFile->mPath, fullpath);
|
2012-08-01 23:29:34 -07:00
|
|
|
ContentChild::GetSingleton()->SendPDeviceStorageRequestConstructor(child, params);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-06-27 13:47:35 -07:00
|
|
|
r = new ReadFileEvent(mFile, mRequest);
|
2012-05-21 09:18:30 -07:00
|
|
|
break;
|
|
|
|
}
|
2012-08-01 23:29:34 -07:00
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
case DEVICE_STORAGE_REQUEST_DELETE:
|
|
|
|
{
|
2013-01-09 05:43:59 -08:00
|
|
|
DeviceStorageTypeChecker* typeChecker = DeviceStorageTypeChecker::CreateOrGet();
|
|
|
|
if (!typeChecker) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!typeChecker->Check(mFile->mStorageType, mFile->mFile)) {
|
|
|
|
r = new PostErrorEvent(mRequest, POST_ERROR_EVENT_ILLEGAL_TYPE);
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-01 23:29:34 -07:00
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
PDeviceStorageRequestChild* child = new DeviceStorageRequestChild(mRequest, mFile);
|
2012-08-30 15:17:37 -07:00
|
|
|
DeviceStorageDeleteParams params(mFile->mStorageType, fullpath);
|
2012-08-01 23:29:34 -07:00
|
|
|
ContentChild::GetSingleton()->SendPDeviceStorageRequestConstructor(child, params);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-05-21 09:18:30 -07:00
|
|
|
r = new DeleteFileEvent(mFile, mRequest);
|
|
|
|
break;
|
|
|
|
}
|
2012-08-01 23:29:34 -07:00
|
|
|
|
2013-01-25 11:05:24 -08:00
|
|
|
case DEVICE_STORAGE_REQUEST_FREE_SPACE:
|
|
|
|
{
|
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
PDeviceStorageRequestChild* child = new DeviceStorageRequestChild(mRequest, mFile);
|
|
|
|
DeviceStorageFreeSpaceParams params(mFile->mStorageType, fullpath);
|
|
|
|
ContentChild::GetSingleton()->SendPDeviceStorageRequestConstructor(child, params);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
r = new FreeSpaceFileEvent(mFile, mRequest);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DEVICE_STORAGE_REQUEST_USED_SPACE:
|
2012-07-31 12:28:23 -07:00
|
|
|
{
|
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
PDeviceStorageRequestChild* child = new DeviceStorageRequestChild(mRequest, mFile);
|
2013-01-25 11:05:24 -08:00
|
|
|
DeviceStorageUsedSpaceParams params(mFile->mStorageType, fullpath);
|
2012-07-31 12:28:23 -07:00
|
|
|
ContentChild::GetSingleton()->SendPDeviceStorageRequestConstructor(child, params);
|
2012-09-13 22:37:00 -07:00
|
|
|
return NS_OK;
|
2012-07-31 12:28:23 -07:00
|
|
|
}
|
2013-01-25 11:05:24 -08:00
|
|
|
r = new UsedSpaceFileEvent(mFile, mRequest);
|
2012-07-31 12:28:23 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-01-25 11:05:24 -08:00
|
|
|
case DEVICE_STORAGE_REQUEST_AVAILABLE:
|
|
|
|
{
|
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
PDeviceStorageRequestChild* child = new DeviceStorageRequestChild(mRequest, mFile);
|
|
|
|
DeviceStorageAvailableParams params(mFile->mStorageType);
|
|
|
|
ContentChild::GetSingleton()->SendPDeviceStorageRequestConstructor(child, params);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
r = new PostAvailableResultEvent(mRequest);
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-01 23:29:34 -07:00
|
|
|
case DEVICE_STORAGE_REQUEST_WATCH:
|
|
|
|
{
|
2012-09-13 22:37:00 -07:00
|
|
|
mDeviceStorage->mAllowedToWatchFile = true;
|
|
|
|
return NS_OK;
|
2012-08-01 23:29:34 -07:00
|
|
|
}
|
2012-05-21 09:18:30 -07:00
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
if (r) {
|
|
|
|
nsCOMPtr<nsIEventTarget> target = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
|
|
|
|
NS_ASSERTION(target, "Must have stream transport service");
|
|
|
|
target->Dispatch(r, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
bool Recv__delete__(const bool& allow)
|
|
|
|
{
|
|
|
|
if (allow) {
|
|
|
|
Allow();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Cancel();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IPDLRelease()
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
private:
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mRequestType;
|
2012-05-21 09:18:30 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> mWindow;
|
2012-07-30 07:58:26 -07:00
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
2012-05-21 09:18:30 -07:00
|
|
|
nsRefPtr<DeviceStorageFile> mFile;
|
|
|
|
|
|
|
|
nsRefPtr<DOMRequest> mRequest;
|
|
|
|
nsCOMPtr<nsIDOMBlob> mBlob;
|
2012-08-01 23:29:34 -07:00
|
|
|
nsRefPtr<nsDOMDeviceStorage> mDeviceStorage;
|
|
|
|
nsCOMPtr<nsIDOMEventListener> mListener;
|
2012-05-21 09:18:30 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeviceStorageRequest)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIContentPermissionRequest)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIContentPermissionRequest)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRunnable)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(DeviceStorageRequest)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(DeviceStorageRequest)
|
2012-12-02 00:54:37 -08:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_5(DeviceStorageRequest,
|
|
|
|
mRequest,
|
|
|
|
mWindow,
|
|
|
|
mBlob,
|
|
|
|
mDeviceStorage,
|
|
|
|
mListener)
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_0(nsDOMDeviceStorage, nsDOMEventTargetHelper)
|
2012-08-01 23:29:34 -07:00
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
DOMCI_DATA(DeviceStorage, nsDOMDeviceStorage)
|
|
|
|
|
2012-08-01 23:29:34 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsDOMDeviceStorage)
|
2012-05-21 09:18:30 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMDeviceStorage)
|
2012-08-01 23:32:04 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
2012-05-21 09:18:30 -07:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(DeviceStorage)
|
2012-08-01 23:29:34 -07:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
2012-05-21 09:18:30 -07:00
|
|
|
|
2012-08-01 23:29:34 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(nsDOMDeviceStorage, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsDOMDeviceStorage, nsDOMEventTargetHelper)
|
2012-05-21 09:18:30 -07:00
|
|
|
|
|
|
|
nsDOMDeviceStorage::nsDOMDeviceStorage()
|
2012-08-01 23:32:08 -07:00
|
|
|
: mIsWatchingFile(false)
|
2012-08-17 19:43:00 -07:00
|
|
|
, mAllowedToWatchFile(false)
|
2012-08-01 23:29:34 -07:00
|
|
|
{ }
|
2012-05-21 09:18:30 -07:00
|
|
|
|
|
|
|
nsresult
|
2012-08-01 23:32:11 -07:00
|
|
|
nsDOMDeviceStorage::Init(nsPIDOMWindow* aWindow, const nsAString &aType)
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aWindow, "Must have a content dom");
|
|
|
|
|
2012-08-30 15:17:37 -07:00
|
|
|
SetRootDirectoryForType(aType);
|
|
|
|
if (!mRootDirectory) {
|
2012-05-21 09:18:30 -07:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2012-08-01 23:29:34 -07:00
|
|
|
BindToOwner(aWindow);
|
2012-05-21 09:18:30 -07:00
|
|
|
|
2012-07-30 07:58:26 -07:00
|
|
|
// Grab the principal of the document
|
2012-05-21 09:18:30 -07:00
|
|
|
nsCOMPtr<nsIDOMDocument> domdoc;
|
|
|
|
aWindow->GetDocument(getter_AddRefs(domdoc));
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc);
|
|
|
|
if (!doc) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-07-30 07:58:26 -07:00
|
|
|
mPrincipal = doc->NodePrincipal();
|
2012-09-18 13:46:52 -07:00
|
|
|
|
|
|
|
// the 'apps' type is special. We only want this exposed
|
|
|
|
// if the caller has the "webapps-manage" permission.
|
|
|
|
if (aType.EqualsLiteral("apps")) {
|
|
|
|
nsCOMPtr<nsIPermissionManager> permissionManager = do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(permissionManager, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
uint32_t permission;
|
|
|
|
nsresult rv = permissionManager->TestPermissionFromPrincipal(mPrincipal,
|
|
|
|
"webapps-manage",
|
|
|
|
&permission);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) || permission != nsIPermissionManager::ALLOW_ACTION) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMDeviceStorage::~nsDOMDeviceStorage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-01 23:29:34 -07:00
|
|
|
void
|
|
|
|
nsDOMDeviceStorage::Shutdown()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-08-09 15:41:18 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
UnregisterForSDCardChanges(this);
|
|
|
|
#endif
|
2012-08-17 19:43:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
obs->RemoveObserver(this, "file-watcher-update");
|
2012-08-01 23:29:34 -07:00
|
|
|
}
|
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
void
|
|
|
|
nsDOMDeviceStorage::CreateDeviceStoragesFor(nsPIDOMWindow* aWin,
|
|
|
|
const nsAString &aType,
|
2012-08-01 23:32:11 -07:00
|
|
|
nsDOMDeviceStorage** aStore)
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
2012-08-01 23:32:11 -07:00
|
|
|
nsRefPtr<nsDOMDeviceStorage> storage = new nsDOMDeviceStorage();
|
|
|
|
if (NS_SUCCEEDED(storage->Init(aWin, aType))) {
|
|
|
|
NS_ADDREF(*aStore = storage);
|
2012-05-21 09:18:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-07-06 13:14:07 -07:00
|
|
|
nsDOMDeviceStorage::Add(nsIDOMBlob *aBlob, nsIDOMDOMRequest * *_retval)
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
2012-09-05 14:30:36 -07:00
|
|
|
if (!aBlob) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIMIMEService> mimeSvc = do_GetService(NS_MIMESERVICE_CONTRACTID);
|
|
|
|
if (!mimeSvc) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if mimeType isn't set, we will not get a correct
|
|
|
|
// extension, and AddNamed() will fail. This will post an
|
|
|
|
// onerror to the requestee.
|
|
|
|
nsString mimeType;
|
|
|
|
aBlob->GetType(mimeType);
|
|
|
|
|
|
|
|
nsCString extension;
|
|
|
|
mimeSvc->GetPrimaryExtension(NS_LossyConvertUTF16toASCII(mimeType), EmptyCString(), extension);
|
|
|
|
// if extension is null here, we will ignore it for now.
|
|
|
|
// AddNamed() will check the file path and fail. This
|
|
|
|
// will post an onerror to the requestee.
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
// possible race here w/ unique filename
|
2012-05-21 09:18:30 -07:00
|
|
|
char buffer[128];
|
2012-09-05 14:30:36 -07:00
|
|
|
NS_MakeRandomString(buffer, ArrayLength(buffer));
|
2012-05-21 09:18:30 -07:00
|
|
|
|
2012-09-05 14:30:36 -07:00
|
|
|
nsAutoCString path;
|
|
|
|
path.Assign(nsDependentCString(buffer));
|
|
|
|
path.Append(".");
|
|
|
|
path.Append(extension);
|
2012-05-21 09:18:30 -07:00
|
|
|
|
2012-09-05 14:30:36 -07:00
|
|
|
return AddNamed(aBlob, NS_ConvertASCIItoUTF16(path), _retval);
|
2012-05-21 09:18:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDeviceStorage::AddNamed(nsIDOMBlob *aBlob,
|
|
|
|
const nsAString & aPath,
|
2012-07-06 13:14:07 -07:00
|
|
|
nsIDOMDOMRequest * *_retval)
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
|
|
|
// if the blob is null here, bail
|
2012-07-30 07:20:58 -07:00
|
|
|
if (aBlob == nullptr)
|
2012-05-21 09:18:30 -07:00
|
|
|
return NS_OK;
|
|
|
|
|
2012-08-01 23:29:34 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
2012-05-21 09:18:30 -07:00
|
|
|
if (!win) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-09-13 22:37:00 -07:00
|
|
|
DeviceStorageTypeChecker* typeChecker = DeviceStorageTypeChecker::CreateOrGet();
|
|
|
|
if (!typeChecker) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
|
|
|
NS_ADDREF(*_retval = request);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> r;
|
|
|
|
|
2012-08-30 15:17:37 -07:00
|
|
|
nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(mStorageType, mRootDirectory, aPath);
|
2012-10-13 08:20:14 -07:00
|
|
|
if (!dsf->IsSafePath()) {
|
|
|
|
r = new PostErrorEvent(request, POST_ERROR_EVENT_PERMISSION_DENIED);
|
|
|
|
} else if (!typeChecker->Check(mStorageType, dsf->mFile) ||
|
2012-09-13 22:37:00 -07:00
|
|
|
!typeChecker->Check(mStorageType, aBlob)) {
|
2012-10-13 08:20:14 -07:00
|
|
|
r = new PostErrorEvent(request, POST_ERROR_EVENT_ILLEGAL_TYPE);
|
2012-05-21 09:18:30 -07:00
|
|
|
}
|
|
|
|
else {
|
2012-11-19 12:31:21 -08:00
|
|
|
r = new DeviceStorageRequest(DEVICE_STORAGE_REQUEST_CREATE,
|
2012-09-13 22:37:00 -07:00
|
|
|
win, mPrincipal, dsf, request, aBlob);
|
2012-05-21 09:18:30 -07:00
|
|
|
}
|
2012-09-05 14:30:36 -07:00
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-08 21:33:16 -07:00
|
|
|
nsDOMDeviceStorage::Get(const JS::Value & aPath,
|
2012-05-21 09:18:30 -07:00
|
|
|
JSContext* aCx,
|
2012-07-06 13:14:07 -07:00
|
|
|
nsIDOMDOMRequest * *_retval)
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
|
|
|
return GetInternal(aPath, aCx, _retval, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-08 21:33:16 -07:00
|
|
|
nsDOMDeviceStorage::GetEditable(const JS::Value & aPath,
|
2012-05-21 09:18:30 -07:00
|
|
|
JSContext* aCx,
|
2012-07-06 13:14:07 -07:00
|
|
|
nsIDOMDOMRequest * *_retval)
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
|
|
|
return GetInternal(aPath, aCx, _retval, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-06-08 21:33:16 -07:00
|
|
|
nsDOMDeviceStorage::GetInternal(const JS::Value & aPath,
|
2012-05-21 09:18:30 -07:00
|
|
|
JSContext* aCx,
|
2012-07-06 13:14:07 -07:00
|
|
|
nsIDOMDOMRequest * *_retval,
|
2012-05-21 09:18:30 -07:00
|
|
|
bool aEditable)
|
|
|
|
{
|
2012-08-01 23:29:34 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
2012-05-21 09:18:30 -07:00
|
|
|
if (!win) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
|
|
|
NS_ADDREF(*_retval = request);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> r;
|
|
|
|
|
|
|
|
JSString* jsstr = JS_ValueToString(aCx, aPath);
|
|
|
|
nsDependentJSString path;
|
|
|
|
if (!path.init(aCx, jsstr)) {
|
2012-10-13 08:20:14 -07:00
|
|
|
r = new PostErrorEvent(request, POST_ERROR_EVENT_UNKNOWN);
|
2012-06-01 13:19:08 -07:00
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-05-21 09:18:30 -07:00
|
|
|
|
2012-08-30 15:17:37 -07:00
|
|
|
nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(mStorageType, mRootDirectory, path);
|
2012-06-19 16:14:39 -07:00
|
|
|
dsf->SetEditable(aEditable);
|
|
|
|
if (!dsf->IsSafePath()) {
|
2012-10-13 08:20:14 -07:00
|
|
|
r = new PostErrorEvent(request, POST_ERROR_EVENT_PERMISSION_DENIED);
|
2012-06-01 13:19:08 -07:00
|
|
|
} else {
|
2012-11-19 12:31:21 -08:00
|
|
|
r = new DeviceStorageRequest(aEditable ? DEVICE_STORAGE_REQUEST_WRITE : DEVICE_STORAGE_REQUEST_READ,
|
2012-07-30 07:58:26 -07:00
|
|
|
win, mPrincipal, dsf, request);
|
2012-05-21 09:18:30 -07:00
|
|
|
}
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-07-06 13:14:07 -07:00
|
|
|
nsDOMDeviceStorage::Delete(const JS::Value & aPath, JSContext* aCx, nsIDOMDOMRequest * *_retval)
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> r;
|
|
|
|
|
2012-08-01 23:29:34 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
2012-05-21 09:18:30 -07:00
|
|
|
if (!win) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
|
|
|
NS_ADDREF(*_retval = request);
|
|
|
|
|
|
|
|
JSString* jsstr = JS_ValueToString(aCx, aPath);
|
|
|
|
nsDependentJSString path;
|
|
|
|
if (!path.init(aCx, jsstr)) {
|
2012-10-13 08:20:14 -07:00
|
|
|
r = new PostErrorEvent(request, POST_ERROR_EVENT_UNKNOWN);
|
2012-06-01 13:19:08 -07:00
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-30 15:17:37 -07:00
|
|
|
nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(mStorageType, mRootDirectory, path);
|
2012-06-01 13:19:08 -07:00
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
if (!dsf->IsSafePath()) {
|
2012-10-13 08:20:14 -07:00
|
|
|
r = new PostErrorEvent(request, POST_ERROR_EVENT_PERMISSION_DENIED);
|
2012-05-21 09:18:30 -07:00
|
|
|
}
|
|
|
|
else {
|
2012-11-13 16:06:42 -08:00
|
|
|
r = new DeviceStorageRequest(DEVICE_STORAGE_REQUEST_DELETE,
|
2012-07-30 07:58:26 -07:00
|
|
|
win, mPrincipal, dsf, request);
|
2012-05-21 09:18:30 -07:00
|
|
|
}
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-31 12:28:23 -07:00
|
|
|
NS_IMETHODIMP
|
2013-01-25 11:05:24 -08:00
|
|
|
nsDOMDeviceStorage::FreeSpace(nsIDOMDOMRequest** aRetval)
|
2012-07-31 12:28:23 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
|
|
|
NS_ADDREF(*aRetval = request);
|
|
|
|
|
2012-08-30 15:17:37 -07:00
|
|
|
nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(mStorageType, mRootDirectory);
|
2013-01-25 11:05:24 -08:00
|
|
|
nsCOMPtr<nsIRunnable> r = new DeviceStorageRequest(DEVICE_STORAGE_REQUEST_FREE_SPACE,
|
|
|
|
win,
|
|
|
|
mPrincipal,
|
|
|
|
dsf,
|
|
|
|
request);
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDeviceStorage::UsedSpace(nsIDOMDOMRequest** aRetval)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
|
|
|
NS_ADDREF(*aRetval = request);
|
|
|
|
|
|
|
|
nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(mStorageType, mRootDirectory);
|
|
|
|
nsCOMPtr<nsIRunnable> r = new DeviceStorageRequest(DEVICE_STORAGE_REQUEST_USED_SPACE,
|
|
|
|
win,
|
|
|
|
mPrincipal,
|
|
|
|
dsf,
|
|
|
|
request);
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDeviceStorage::Available(nsIDOMDOMRequest** aRetval)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
|
|
|
NS_ADDREF(*aRetval = request);
|
|
|
|
|
|
|
|
nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(mStorageType, mRootDirectory);
|
|
|
|
nsCOMPtr<nsIRunnable> r = new DeviceStorageRequest(DEVICE_STORAGE_REQUEST_AVAILABLE,
|
2012-09-13 22:37:00 -07:00
|
|
|
win,
|
|
|
|
mPrincipal,
|
|
|
|
dsf,
|
|
|
|
request);
|
2012-07-31 12:28:23 -07:00
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-28 22:30:52 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDeviceStorage::GetRootDirectory(nsIFile** aRootDirectory)
|
|
|
|
{
|
|
|
|
if (!mRootDirectory) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return mRootDirectory->Clone(aRootDirectory);
|
|
|
|
}
|
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
NS_IMETHODIMP
|
2012-06-10 22:50:44 -07:00
|
|
|
nsDOMDeviceStorage::Enumerate(const JS::Value & aName,
|
|
|
|
const JS::Value & aOptions,
|
|
|
|
JSContext* aCx,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t aArgc,
|
2012-06-10 22:50:44 -07:00
|
|
|
nsIDOMDeviceStorageCursor** aRetval)
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
2012-06-10 22:50:44 -07:00
|
|
|
return EnumerateInternal(aName, aOptions, aCx, aArgc, false, aRetval);
|
2012-05-21 09:18:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-10 22:50:44 -07:00
|
|
|
nsDOMDeviceStorage::EnumerateEditable(const JS::Value & aName,
|
|
|
|
const JS::Value & aOptions,
|
|
|
|
JSContext* aCx,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t aArgc,
|
2012-06-10 22:50:44 -07:00
|
|
|
nsIDOMDeviceStorageCursor** aRetval)
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
2012-06-10 22:50:44 -07:00
|
|
|
return EnumerateInternal(aName, aOptions, aCx, aArgc, true, aRetval);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PRTime
|
|
|
|
ExtractDateFromOptions(JSContext* aCx, const JS::Value& aOptions)
|
|
|
|
{
|
|
|
|
PRTime result = 0;
|
|
|
|
DeviceStorageEnumerationParameters params;
|
|
|
|
if (!JSVAL_IS_VOID(aOptions) && !aOptions.isNull()) {
|
|
|
|
nsresult rv = params.Init(aCx, &aOptions);
|
|
|
|
if (NS_SUCCEEDED(rv) && !JSVAL_IS_VOID(params.since) && !params.since.isNull() && params.since.isObject()) {
|
|
|
|
JSObject* obj = JSVAL_TO_OBJECT(params.since);
|
2012-09-20 21:41:49 -07:00
|
|
|
if (JS_ObjectIsDate(aCx, obj) && js_DateIsValid(obj)) {
|
|
|
|
result = js_DateGetMsecSinceEpoch(obj);
|
2012-06-10 22:50:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2012-05-21 09:18:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-06-10 22:50:44 -07:00
|
|
|
nsDOMDeviceStorage::EnumerateInternal(const JS::Value & aName,
|
|
|
|
const JS::Value & aOptions,
|
|
|
|
JSContext* aCx,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t aArgc,
|
2012-06-10 22:50:44 -07:00
|
|
|
bool aEditable,
|
|
|
|
nsIDOMDeviceStorageCursor** aRetval)
|
2012-05-21 09:18:30 -07:00
|
|
|
{
|
2012-08-01 23:29:34 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
2012-05-21 09:18:30 -07:00
|
|
|
if (!win)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2012-06-10 22:50:44 -07:00
|
|
|
PRTime since = 0;
|
|
|
|
nsString path;
|
|
|
|
path.SetIsVoid(true);
|
|
|
|
|
|
|
|
if (aArgc > 0) {
|
|
|
|
// inspect the first value to see if it is a string
|
|
|
|
if (JSVAL_IS_STRING(aName)) {
|
|
|
|
JSString* jsstr = JS_ValueToString(aCx, aName);
|
|
|
|
nsDependentJSString jspath;
|
|
|
|
jspath.init(aCx, jsstr);
|
|
|
|
path.Assign(jspath);
|
|
|
|
} else if (!JSVAL_IS_PRIMITIVE(aName)) {
|
|
|
|
// it also might be an options object
|
|
|
|
since = ExtractDateFromOptions(aCx, aName);
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-06-10 22:50:44 -07:00
|
|
|
if (aArgc == 2 && (JSVAL_IS_VOID(aOptions) || aOptions.isNull() || !aOptions.isObject())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
since = ExtractDateFromOptions(aCx, aOptions);
|
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-08-30 15:17:37 -07:00
|
|
|
nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(mStorageType, mRootDirectory, path);
|
2012-06-19 16:14:39 -07:00
|
|
|
dsf->SetEditable(aEditable);
|
2012-06-27 13:47:35 -07:00
|
|
|
|
2012-07-30 07:58:26 -07:00
|
|
|
nsRefPtr<nsDOMDeviceStorageCursor> cursor = new nsDOMDeviceStorageCursor(win, mPrincipal,
|
|
|
|
dsf, since);
|
2012-06-08 21:33:16 -07:00
|
|
|
nsRefPtr<DeviceStorageCursorRequest> r = new DeviceStorageCursorRequest(cursor);
|
2012-06-08 20:15:04 -07:00
|
|
|
|
2012-06-10 22:50:44 -07:00
|
|
|
NS_ADDREF(*aRetval = cursor);
|
|
|
|
|
2012-05-29 16:12:39 -07:00
|
|
|
if (mozilla::Preferences::GetBool("device.storage.prompt.testing", false)) {
|
|
|
|
r->Allow();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
|
|
|
// because owner implements nsITabChild, we can assume that it is
|
|
|
|
// the one and only TabChild.
|
|
|
|
TabChild* child = GetTabChildFrom(win->GetDocShell());
|
|
|
|
if (!child)
|
2012-07-27 06:59:29 -07:00
|
|
|
return NS_OK;
|
2012-06-19 16:14:39 -07:00
|
|
|
|
|
|
|
// Retain a reference so the object isn't deleted without IPDL's knowledge.
|
|
|
|
// Corresponding release occurs in DeallocPContentPermissionRequest.
|
|
|
|
r->AddRef();
|
|
|
|
|
2012-09-13 22:37:00 -07:00
|
|
|
nsCString type;
|
|
|
|
nsresult rv = DeviceStorageTypeChecker::GetPermissionForType(mStorageType, type);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-11-13 16:06:42 -08:00
|
|
|
child->SendPContentPermissionRequestConstructor(r, type, NS_LITERAL_CSTRING("read"), IPC::Principal(mPrincipal));
|
2012-06-19 16:14:39 -07:00
|
|
|
|
|
|
|
r->Sendprompt();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-31 10:06:42 -07:00
|
|
|
nsCOMPtr<nsIContentPermissionPrompt> prompt = do_CreateInstance(NS_CONTENT_PERMISSION_PROMPT_CONTRACTID);
|
2012-05-29 16:12:39 -07:00
|
|
|
if (prompt) {
|
|
|
|
prompt->Prompt(r);
|
|
|
|
}
|
|
|
|
|
2012-05-21 09:18:30 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-08-01 23:29:34 -07:00
|
|
|
|
2012-08-09 15:41:18 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
void
|
2012-08-14 08:53:44 -07:00
|
|
|
nsDOMDeviceStorage::DispatchMountChangeEvent(nsAString& aType)
|
2012-08-09 15:41:18 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
NS_NewDOMDeviceStorageChangeEvent(getter_AddRefs(event), nullptr, nullptr);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDeviceStorageChangeEvent> ce = do_QueryInterface(event);
|
|
|
|
nsresult rv = ce->InitDeviceStorageChangeEvent(NS_LITERAL_STRING("change"),
|
|
|
|
true, false,
|
2012-08-30 20:15:32 -07:00
|
|
|
NS_LITERAL_STRING(""),
|
2012-08-14 08:53:44 -07:00
|
|
|
aType);
|
2012-08-09 15:41:18 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ignore;
|
|
|
|
DispatchEvent(ce, &ignore);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-01 23:32:04 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDeviceStorage::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData)
|
|
|
|
{
|
2012-08-09 15:41:18 -07:00
|
|
|
if (!strcmp(aTopic, "file-watcher-update")) {
|
2012-08-01 23:32:04 -07:00
|
|
|
|
2012-08-30 15:17:37 -07:00
|
|
|
DeviceStorageFile* file = static_cast<DeviceStorageFile*>(aSubject);
|
2012-08-17 19:43:00 -07:00
|
|
|
Notify(NS_ConvertUTF16toUTF8(aData).get(), file);
|
2012-08-09 15:41:18 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2012-08-17 19:43:00 -07:00
|
|
|
else if (!strcmp(aTopic, NS_VOLUME_STATE_CHANGED)) {
|
2012-08-09 15:41:18 -07:00
|
|
|
nsCOMPtr<nsIVolume> vol = do_QueryInterface(aSubject);
|
|
|
|
if (!vol) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsString volName;
|
|
|
|
vol->GetName(volName);
|
2012-09-18 13:46:52 -07:00
|
|
|
if (!volName.EqualsLiteral("sdcard")) {
|
2012-08-09 15:41:18 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t state;
|
2012-08-09 15:41:18 -07:00
|
|
|
nsresult rv = vol->GetState(&state);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-14 08:53:44 -07:00
|
|
|
nsString type;
|
|
|
|
if (state == nsIVolume::STATE_MOUNTED) {
|
|
|
|
type.Assign(NS_LITERAL_STRING("available"));
|
|
|
|
} else if (state == nsIVolume::STATE_SHARED || state == nsIVolume::STATE_SHAREDMNT) {
|
|
|
|
type.Assign(NS_LITERAL_STRING("shared"));
|
|
|
|
} else if (state == nsIVolume::STATE_NOMEDIA || state == nsIVolume::STATE_UNMOUNTING) {
|
|
|
|
type.Assign(NS_LITERAL_STRING("unavailable"));
|
|
|
|
} else {
|
|
|
|
// ignore anything else.
|
2012-08-09 15:41:18 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-08-14 08:53:44 -07:00
|
|
|
DispatchMountChangeEvent(type);
|
2012-08-09 15:41:18 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
#endif
|
2012-08-01 23:32:04 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-17 19:43:00 -07:00
|
|
|
nsresult
|
2012-08-30 15:17:37 -07:00
|
|
|
nsDOMDeviceStorage::Notify(const char* aReason, DeviceStorageFile* aFile)
|
2012-08-01 23:29:34 -07:00
|
|
|
{
|
2012-08-17 19:43:00 -07:00
|
|
|
if (!mAllowedToWatchFile) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-30 15:17:37 -07:00
|
|
|
if (!mStorageType.Equals(aFile->mStorageType)) {
|
|
|
|
// Ignore this
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mRootDirectory) {
|
2012-08-17 19:43:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-08-01 23:29:34 -07:00
|
|
|
nsString rootpath;
|
2012-08-30 15:17:37 -07:00
|
|
|
nsresult rv = mRootDirectory->GetPath(rootpath);
|
2012-08-01 23:29:34 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-08-30 20:15:32 -07:00
|
|
|
|
2012-08-01 23:29:34 -07:00
|
|
|
nsString fullpath;
|
2012-08-30 15:17:37 -07:00
|
|
|
rv = aFile->mFile->GetPath(fullpath);
|
2012-08-01 23:29:34 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(fullpath.Length() >= rootpath.Length(), "Root path longer than full path!");
|
|
|
|
|
|
|
|
nsAString::size_type len = rootpath.Length() + 1; // +1 for the trailing /
|
|
|
|
nsDependentSubstring newPath (fullpath, len, fullpath.Length() - len);
|
|
|
|
|
2012-08-02 13:25:16 -07:00
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
NS_NewDOMDeviceStorageChangeEvent(getter_AddRefs(event), nullptr, nullptr);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDeviceStorageChangeEvent> ce = do_QueryInterface(event);
|
|
|
|
|
2012-08-01 23:29:34 -07:00
|
|
|
nsString reason;
|
|
|
|
reason.AssignWithConversion(aReason);
|
2012-08-02 13:25:16 -07:00
|
|
|
rv = ce->InitDeviceStorageChangeEvent(NS_LITERAL_STRING("change"), true, false, newPath, reason);
|
2012-08-01 23:29:34 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
bool ignore;
|
2012-08-02 13:25:16 -07:00
|
|
|
DispatchEvent(ce, &ignore);
|
2012-08-01 23:29:34 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDeviceStorage::AddEventListener(const nsAString & aType,
|
|
|
|
nsIDOMEventListener *aListener,
|
|
|
|
bool aUseCapture,
|
|
|
|
bool aWantsUntrusted,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t aArgc)
|
2012-08-01 23:29:34 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
2012-08-30 15:17:37 -07:00
|
|
|
nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(mStorageType, mRootDirectory);
|
2012-11-13 16:06:42 -08:00
|
|
|
nsCOMPtr<nsIRunnable> r = new DeviceStorageRequest(DEVICE_STORAGE_REQUEST_WATCH,
|
2012-07-30 07:58:26 -07:00
|
|
|
win, mPrincipal, dsf, request, this, aListener);
|
2012-08-01 23:29:34 -07:00
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return nsDOMEventTargetHelper::AddEventListener(aType, aListener, aUseCapture, aWantsUntrusted, aArgc);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDeviceStorage::AddSystemEventListener(const nsAString & aType,
|
|
|
|
nsIDOMEventListener *aListener,
|
|
|
|
bool aUseCapture,
|
|
|
|
bool aWantsUntrusted,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t aArgc)
|
2012-08-01 23:29:34 -07:00
|
|
|
{
|
2012-08-17 19:43:00 -07:00
|
|
|
if (!mIsWatchingFile) {
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
obs->AddObserver(this, "file-watcher-update", false);
|
|
|
|
mIsWatchingFile = true;
|
|
|
|
}
|
|
|
|
|
2012-08-01 23:29:34 -07:00
|
|
|
return nsDOMDeviceStorage::AddEventListener(aType,aListener,aUseCapture,aWantsUntrusted, aArgc);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDeviceStorage::RemoveEventListener(const nsAString & aType,
|
|
|
|
nsIDOMEventListener *aListener,
|
|
|
|
bool aUseCapture)
|
|
|
|
{
|
|
|
|
nsDOMEventTargetHelper::RemoveEventListener(aType, aListener, false);
|
|
|
|
|
2012-10-28 21:33:51 -07:00
|
|
|
if (mIsWatchingFile && !HasListenersFor(nsGkAtoms::onchange)) {
|
2012-08-17 19:43:00 -07:00
|
|
|
mIsWatchingFile = false;
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
obs->RemoveObserver(this, "file-watcher-update");
|
2012-08-01 23:29:34 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDeviceStorage::RemoveSystemEventListener(const nsAString & aType,
|
|
|
|
nsIDOMEventListener *aListener,
|
|
|
|
bool aUseCapture)
|
|
|
|
{
|
|
|
|
return nsDOMDeviceStorage::RemoveEventListener(aType, aListener, aUseCapture);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDeviceStorage::DispatchEvent(nsIDOMEvent *aEvt,
|
|
|
|
bool *aRetval)
|
|
|
|
{
|
|
|
|
return nsDOMEventTargetHelper::DispatchEvent(aEvt, aRetval);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDOMEventTarget *
|
|
|
|
nsDOMDeviceStorage::GetTargetForDOMEvent()
|
|
|
|
{
|
|
|
|
return nsDOMEventTargetHelper::GetTargetForDOMEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDOMEventTarget *
|
|
|
|
nsDOMDeviceStorage::GetTargetForEventTargetChain()
|
|
|
|
{
|
|
|
|
return nsDOMEventTargetHelper::GetTargetForEventTargetChain();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMDeviceStorage::PreHandleEvent(nsEventChainPreVisitor & aVisitor)
|
|
|
|
{
|
|
|
|
return nsDOMEventTargetHelper::PreHandleEvent(aVisitor);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMDeviceStorage::WillHandleEvent(nsEventChainPostVisitor & aVisitor)
|
|
|
|
{
|
|
|
|
return nsDOMEventTargetHelper::WillHandleEvent(aVisitor);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMDeviceStorage::PostHandleEvent(nsEventChainPostVisitor & aVisitor)
|
|
|
|
{
|
|
|
|
return nsDOMEventTargetHelper::PostHandleEvent(aVisitor);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMDeviceStorage::DispatchDOMEvent(nsEvent *aEvent,
|
|
|
|
nsIDOMEvent *aDOMEvent,
|
|
|
|
nsPresContext *aPresContext,
|
|
|
|
nsEventStatus *aEventStatus)
|
|
|
|
{
|
|
|
|
return nsDOMEventTargetHelper::DispatchDOMEvent(aEvent,
|
|
|
|
aDOMEvent,
|
|
|
|
aPresContext,
|
|
|
|
aEventStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventListenerManager *
|
|
|
|
nsDOMDeviceStorage::GetListenerManager(bool aMayCreate)
|
|
|
|
{
|
|
|
|
return nsDOMEventTargetHelper::GetListenerManager(aMayCreate);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIScriptContext *
|
|
|
|
nsDOMDeviceStorage::GetContextForEventHandlers(nsresult *aRv)
|
|
|
|
{
|
|
|
|
return nsDOMEventTargetHelper::GetContextForEventHandlers(aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext *
|
|
|
|
nsDOMDeviceStorage::GetJSContextForEventHandlers()
|
|
|
|
{
|
|
|
|
return nsDOMEventTargetHelper::GetJSContextForEventHandlers();
|
|
|
|
}
|
|
|
|
|
2012-09-05 14:30:36 -07:00
|
|
|
NS_IMPL_EVENT_HANDLER(nsDOMDeviceStorage, change)
|