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
|
|
|
|
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2014-03-05 07:26:39 +04:00
|
|
|
#include "nsString.h"
|
|
|
|
#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
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
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;
|
|
|
|
}
|
2016-03-20 13:56:10 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent)
|
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);
|
|
|
|
}
|
2016-03-20 13:56:10 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent)
|
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
|
|
|
|
|
2016-06-01 13:29:00 +03:00
|
|
|
/* static */ already_AddRefed<Directory>
|
|
|
|
Directory::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aRealPath,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Create(aGlobal.GetAsSupports(), path);
|
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
/* static */ already_AddRefed<Directory>
|
2016-03-20 13:56:10 +03:00
|
|
|
Directory::Create(nsISupports* aParent, nsIFile* aFile,
|
2016-04-18 10:32:30 +03:00
|
|
|
FileSystemBase* aFileSystem)
|
2014-03-05 07:26:39 +04:00
|
|
|
{
|
2016-03-20 13:56:10 +03:00
|
|
|
MOZ_ASSERT(aParent);
|
2016-03-20 13:56:10 +03:00
|
|
|
MOZ_ASSERT(aFile);
|
|
|
|
|
2016-04-18 10:32:30 +03:00
|
|
|
RefPtr<Directory> directory = new Directory(aParent, aFile, aFileSystem);
|
2016-03-20 13:56:10 +03:00
|
|
|
return directory.forget();
|
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
Directory::Directory(nsISupports* aParent,
|
2016-03-20 13:56:10 +03:00
|
|
|
nsIFile* aFile,
|
|
|
|
FileSystemBase* aFileSystem)
|
2016-03-20 13:56:10 +03:00
|
|
|
: mParent(aParent)
|
2016-03-20 13:56:10 +03:00
|
|
|
, mFile(aFile)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
// mParent but it's not cycle collectable and to avoid manual
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
Directory::~Directory()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
nsISupports*
|
2014-03-05 07:26:39 +04:00
|
|
|
Directory::GetParentObject() const
|
|
|
|
{
|
2016-03-20 13:56:10 +03:00
|
|
|
return mParent;
|
2014-03-05 07:26:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
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
|
|
|
Directory::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
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
|
|
|
return DirectoryBinding::Wrap(aCx, this, aGivenProto);
|
2014-03-05 07:26:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-03-20 13:56:10 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-06-23 02:31:34 +03:00
|
|
|
void
|
2016-03-20 13:56:10 +03:00
|
|
|
Directory::GetPath(nsAString& aRetval, ErrorResult& aRv)
|
2015-06-23 02:31:34 +03:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2016-03-20 13:56:10 +03:00
|
|
|
Directory::GetFilesAndDirectories(ErrorResult& aRv)
|
2015-06-23 02:31:34 +03:00
|
|
|
{
|
2016-03-20 13:56:10 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-03-12 10:30:21 +04:00
|
|
|
FileSystemBase*
|
2016-03-20 13:56:10 +03:00
|
|
|
Directory::GetFileSystem(ErrorResult& aRv)
|
2014-03-12 10:30:21 +04:00
|
|
|
{
|
2016-03-20 13:56:10 +03:00
|
|
|
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);
|
2016-03-20 13:56:10 +03:00
|
|
|
fs->Init(mParent);
|
2016-03-20 13:56:10 +03:00
|
|
|
|
|
|
|
mFileSystem = fs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mFileSystem;
|
2014-03-12 10:30:21 +04:00
|
|
|
}
|
|
|
|
|
2014-03-05 07:26:39 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|