2014-03-05 07:26:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-05-03 22:32:37 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-03-05 07:26:39 +04: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/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/Directory.h"
|
|
|
|
|
2015-06-23 02:31:34 +03:00
|
|
|
#include "GetDirectoryListingTask.h"
|
2016-04-13 14:15:56 +03:00
|
|
|
#include "GetFilesTask.h"
|
2014-03-05 07:25:40 +04:00
|
|
|
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "nsIFile.h"
|
2014-03-05 07:26:39 +04:00
|
|
|
#include "nsString.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/dom/BlobImpl.h"
|
2014-03-05 07:26:39 +04:00
|
|
|
#include "mozilla/dom/DirectoryBinding.h"
|
2014-03-05 07:25:40 +04:00
|
|
|
#include "mozilla/dom/FileSystemBase.h"
|
|
|
|
#include "mozilla/dom/FileSystemUtils.h"
|
2016-03-20 13:56:10 +03:00
|
|
|
#include "mozilla/dom/OSFileSystem.h"
|
2018-01-31 10:25:30 +03:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
2014-03-05 07:26:39 +04:00
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom {
|
2014-03-05 07:26:39 +04:00
|
|
|
|
2015-12-30 11:53:38 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(Directory)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Directory)
|
2016-03-20 13:56:10 +03:00
|
|
|
if (tmp->mFileSystem) {
|
|
|
|
tmp->mFileSystem->Unlink();
|
|
|
|
tmp->mFileSystem = nullptr;
|
|
|
|
}
|
2019-10-21 08:33:33 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mGlobal)
|
2015-12-30 11:53:38 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Directory)
|
2016-03-20 13:56:10 +03:00
|
|
|
if (tmp->mFileSystem) {
|
|
|
|
tmp->mFileSystem->Traverse(cb);
|
|
|
|
}
|
2019-10-21 08:33:33 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGlobal)
|
2015-12-30 11:53:38 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(Directory)
|
|
|
|
|
2014-03-05 07:26:39 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Directory)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Directory)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Directory)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<Directory> Directory::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aRealPath,
|
|
|
|
ErrorResult& aRv) {
|
2016-06-01 13:29:00 +03:00
|
|
|
nsCOMPtr<nsIFile> path;
|
2017-01-18 06:48:29 +03:00
|
|
|
aRv = NS_NewLocalFile(aRealPath, true, getter_AddRefs(path));
|
2016-06-01 13:29:00 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-21 08:33:33 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (NS_WARN_IF(!global)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Create(global, path);
|
2016-06-01 13:29:00 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
2019-10-21 08:33:33 +03:00
|
|
|
already_AddRefed<Directory> Directory::Create(nsIGlobalObject* aGlobal,
|
2019-02-26 01:05:29 +03:00
|
|
|
nsIFile* aFile,
|
|
|
|
FileSystemBase* aFileSystem) {
|
2019-10-21 08:33:33 +03:00
|
|
|
MOZ_ASSERT(aGlobal);
|
2016-03-20 13:56:10 +03:00
|
|
|
MOZ_ASSERT(aFile);
|
|
|
|
|
2019-10-21 08:33:33 +03:00
|
|
|
RefPtr<Directory> directory = new Directory(aGlobal, aFile, aFileSystem);
|
2016-03-20 13:56:10 +03:00
|
|
|
return directory.forget();
|
|
|
|
}
|
|
|
|
|
2019-10-21 08:33:33 +03:00
|
|
|
Directory::Directory(nsIGlobalObject* aGlobal, nsIFile* aFile,
|
2016-03-20 13:56:10 +03:00
|
|
|
FileSystemBase* aFileSystem)
|
2019-10-21 08:33:33 +03:00
|
|
|
: mGlobal(aGlobal), mFile(aFile) {
|
2016-03-20 13:56:10 +03:00
|
|
|
MOZ_ASSERT(aFile);
|
|
|
|
|
|
|
|
// aFileSystem can be null. In this case we create a OSFileSystem when needed.
|
2016-03-20 13:58:01 +03:00
|
|
|
if (aFileSystem) {
|
|
|
|
// More likely, this is a OSFileSystem. This object keeps a reference of
|
2019-10-21 08:33:33 +03:00
|
|
|
// mGlobal but it's not cycle collectable and to avoid manual
|
2016-03-20 13:58:01 +03:00
|
|
|
// addref/release, it's better to have 1 object per directory. For this
|
|
|
|
// reason we clone it here.
|
|
|
|
mFileSystem = aFileSystem->Clone();
|
|
|
|
}
|
2014-03-05 07:26:39 +04:00
|
|
|
}
|
|
|
|
|
2020-02-20 19:01:35 +03:00
|
|
|
Directory::~Directory() = default;
|
2014-03-05 07:26:39 +04:00
|
|
|
|
2019-10-21 08:33:33 +03:00
|
|
|
nsIGlobalObject* Directory::GetParentObject() const { return mGlobal; }
|
2014-03-05 07:26:39 +04:00
|
|
|
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
JSObject* Directory::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return Directory_Binding::Wrap(aCx, this, aGivenProto);
|
2014-03-05 07:26:39 +04:00
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
void Directory::GetName(nsAString& aRetval, ErrorResult& aRv) {
|
2014-03-05 07:26:39 +04:00
|
|
|
aRetval.Truncate();
|
2014-03-05 07:25:40 +04:00
|
|
|
|
2016-04-18 10:32:30 +03:00
|
|
|
RefPtr<FileSystemBase> fs = GetFileSystem(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2014-03-05 07:25:40 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-18 10:32:30 +03:00
|
|
|
fs->GetDirectoryName(mFile, aRetval, aRv);
|
2014-03-05 07:26:39 +04:00
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
void Directory::GetPath(nsAString& aRetval, ErrorResult& aRv) {
|
2016-03-30 09:17:56 +03:00
|
|
|
// This operation is expensive. Better to cache the result.
|
|
|
|
if (mPath.IsEmpty()) {
|
|
|
|
RefPtr<FileSystemBase> fs = GetFileSystem(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-18 10:32:30 +03:00
|
|
|
fs->GetDOMPath(mFile, mPath, aRv);
|
2016-03-30 09:17:56 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-20 13:56:10 +03:00
|
|
|
}
|
2016-03-30 09:17:56 +03:00
|
|
|
|
|
|
|
aRetval = mPath;
|
2016-03-20 13:56:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Directory::GetFullRealPath(nsAString& aPath) {
|
|
|
|
nsresult rv = mFile->GetPath(aPath);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2015-06-23 02:31:34 +03:00
|
|
|
}
|
2016-03-20 13:56:10 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2015-06-23 02:31:34 +03:00
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
already_AddRefed<Promise> Directory::GetFilesAndDirectories(ErrorResult& aRv) {
|
|
|
|
RefPtr<FileSystemBase> fs = GetFileSystem(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-09 21:17:28 +03:00
|
|
|
RefPtr<GetDirectoryListingTaskChild> task =
|
2016-05-13 14:11:38 +03:00
|
|
|
GetDirectoryListingTaskChild::Create(fs, this, mFile, mFilters, aRv);
|
2016-03-20 13:56:10 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2015-06-23 02:31:34 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-02-01 23:56:43 +03:00
|
|
|
|
2017-03-08 22:15:45 +03:00
|
|
|
task->Start();
|
|
|
|
|
2015-06-23 02:31:34 +03:00
|
|
|
return task->GetPromise();
|
|
|
|
}
|
|
|
|
|
2016-04-13 14:15:56 +03:00
|
|
|
already_AddRefed<Promise> Directory::GetFiles(bool aRecursiveFlag,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
ErrorResult rv;
|
|
|
|
RefPtr<FileSystemBase> fs = GetFileSystem(rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<GetFilesTaskChild> task =
|
2016-05-13 14:11:38 +03:00
|
|
|
GetFilesTaskChild::Create(fs, this, mFile, aRecursiveFlag, rv);
|
2016-04-13 14:15:56 +03:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-03-08 22:15:45 +03:00
|
|
|
task->Start();
|
|
|
|
|
2016-04-13 14:15:56 +03:00
|
|
|
return task->GetPromise();
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:59:40 +03:00
|
|
|
void Directory::SetContentFilters(const nsAString& aFilters) {
|
|
|
|
mFilters = aFilters;
|
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
FileSystemBase* Directory::GetFileSystem(ErrorResult& aRv) {
|
|
|
|
if (!mFileSystem) {
|
|
|
|
nsAutoString path;
|
2016-03-30 09:17:15 +03:00
|
|
|
aRv = mFile->GetPath(path);
|
2016-03-20 13:56:10 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<OSFileSystem> fs = new OSFileSystem(path);
|
2019-10-21 08:33:33 +03:00
|
|
|
fs->Init(mGlobal);
|
2016-03-20 13:56:10 +03:00
|
|
|
|
|
|
|
mFileSystem = fs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mFileSystem;
|
2014-03-12 10:30:21 +04:00
|
|
|
}
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom
|