2020-11-05 21:08:38 +03:00
|
|
|
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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 "PathUtils.h"
|
|
|
|
|
2020-11-10 15:08:53 +03:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
|
|
|
#include "mozilla/DataMutex.h"
|
2020-11-05 21:08:38 +03:00
|
|
|
#include "mozilla/ErrorNames.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
2020-11-10 15:08:53 +03:00
|
|
|
#include "mozilla/Maybe.h"
|
|
|
|
#include "mozilla/MozPromise.h"
|
2020-11-05 21:08:38 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2020-11-10 15:08:53 +03:00
|
|
|
#include "mozilla/Result.h"
|
|
|
|
#include "mozilla/ResultExtensions.h"
|
2020-11-05 21:08:38 +03:00
|
|
|
#include "mozilla/Span.h"
|
|
|
|
#include "mozilla/dom/DOMParser.h"
|
|
|
|
#include "mozilla/dom/PathUtilsBinding.h"
|
2020-11-10 15:08:53 +03:00
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2022-01-24 23:48:10 +03:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
2020-11-10 15:08:53 +03:00
|
|
|
#include "nsDirectoryServiceUtils.h"
|
2020-11-05 21:08:38 +03:00
|
|
|
#include "nsIFile.h"
|
2020-11-10 15:08:53 +03:00
|
|
|
#include "nsIGlobalObject.h"
|
2020-11-05 21:08:38 +03:00
|
|
|
#include "nsLocalFile.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsString.h"
|
2021-11-23 20:27:20 +03:00
|
|
|
#include "xpcpublic.h"
|
2020-11-05 21:08:38 +03:00
|
|
|
|
2022-05-09 23:41:17 +03:00
|
|
|
namespace mozilla::dom {
|
2020-11-05 21:08:38 +03:00
|
|
|
|
|
|
|
static constexpr auto ERROR_EMPTY_PATH =
|
|
|
|
"PathUtils does not support empty paths"_ns;
|
|
|
|
static constexpr auto ERROR_INITIALIZE_PATH = "Could not initialize path"_ns;
|
|
|
|
static constexpr auto ERROR_GET_PARENT = "Could not get parent path"_ns;
|
2020-12-18 02:55:45 +03:00
|
|
|
static constexpr auto ERROR_JOIN = "Could not append to path"_ns;
|
2020-11-05 21:08:38 +03:00
|
|
|
|
2022-02-01 07:06:13 +03:00
|
|
|
static constexpr auto COLON = ": "_ns;
|
|
|
|
|
2020-11-05 21:08:38 +03:00
|
|
|
static void ThrowError(ErrorResult& aErr, const nsresult aResult,
|
|
|
|
const nsCString& aMessage) {
|
|
|
|
nsAutoCStringN<32> errName;
|
|
|
|
GetErrorName(aResult, errName);
|
|
|
|
|
|
|
|
nsAutoCStringN<256> formattedMsg;
|
|
|
|
formattedMsg.Append(aMessage);
|
2022-02-01 07:06:13 +03:00
|
|
|
formattedMsg.Append(COLON);
|
2020-11-05 21:08:38 +03:00
|
|
|
formattedMsg.Append(errName);
|
|
|
|
|
|
|
|
switch (aResult) {
|
|
|
|
case NS_ERROR_FILE_UNRECOGNIZED_PATH:
|
|
|
|
aErr.ThrowOperationError(formattedMsg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NS_ERROR_FILE_ACCESS_DENIED:
|
|
|
|
aErr.ThrowInvalidAccessError(formattedMsg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NS_ERROR_FAILURE:
|
|
|
|
default:
|
|
|
|
aErr.ThrowUnknownError(formattedMsg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 20:27:20 +03:00
|
|
|
static bool DoWindowsPathCheck() {
|
|
|
|
#ifdef XP_WIN
|
|
|
|
# ifdef DEBUG
|
|
|
|
return true;
|
2022-04-27 12:35:44 +03:00
|
|
|
# else // DEBUG
|
2021-11-23 20:27:20 +03:00
|
|
|
return xpc::IsInAutomation();
|
Bug 1766359 - Fix unreachable-code-return warnings in Windows builds. r=emilio,mhowell,nika,rkraesig
dom/system/PathUtils.cpp(77,10): error: 'return' will never be executed [-Werror,-Wunreachable-code-return]
return false;
^~~~~
ipc/chromium/src/chrome/common/ipc_channel_win.cc(479,10): error: 'return' will never be executed [-Werror,-Wunreachable-code-return]
return true;
^~~~
mozglue/misc/PreXULSkeletonUI.cpp(1263,10): error: 'return' will never be executed [-Werror,-Wunreachable-code-return]
return 0;
^
mozglue/tests/TestPEExportSection.cpp(348,12): error: 'return' will never be executed [-Werror,-Wunreachable-code-return]
return 0;
^
security/manager/ssl/OSReauthenticator.cpp(428,10): error: 'return' will never be executed [-Werror,-Wunreachable-code-return]
return NS_OK;
^~~~~
toolkit/components/maintenanceservice/maintenanceservice.cpp(214,10): error: 'return' will never be executed [-Werror,-Wunreachable-code-return]
return 0;
^
widget/windows/WindowsUIUtils.cpp(383,10): error: 'return' will never be executed [-Werror,-Wunreachable-code-return]
return false;
^~~~~
Differential Revision: https://phabricator.services.mozilla.com/D144661
2022-04-27 00:44:54 +03:00
|
|
|
# endif // DEBUG
|
2022-04-27 12:35:44 +03:00
|
|
|
#else // XP_WIN
|
2021-11-23 20:27:20 +03:00
|
|
|
return false;
|
2022-04-27 12:35:44 +03:00
|
|
|
#endif // XP_WIN
|
2021-11-23 20:27:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
nsresult PathUtils::InitFileWithPath(nsIFile* aFile, const nsAString& aPath) {
|
|
|
|
if (DoWindowsPathCheck()) {
|
|
|
|
MOZ_RELEASE_ASSERT(!aPath.Contains(u'/'),
|
|
|
|
"Windows paths cannot include forward slashes");
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aFile);
|
|
|
|
return aFile->InitWithPath(aPath);
|
|
|
|
}
|
|
|
|
|
2020-11-10 15:08:53 +03:00
|
|
|
StaticDataMutex<Maybe<PathUtils::DirectoryCache>> PathUtils::sDirCache{
|
|
|
|
"sDirCache"};
|
|
|
|
|
2020-11-05 21:08:38 +03:00
|
|
|
/**
|
|
|
|
* Return the leaf name, including leading path separators in the case of
|
|
|
|
* Windows UNC drive paths.
|
|
|
|
*
|
|
|
|
* @param aFile The file whose leaf name is to be returned.
|
|
|
|
* @param aResult The string to hold the resulting leaf name.
|
|
|
|
* @param aParent The pre-computed parent of |aFile|. If not provided, it will
|
|
|
|
* be computed.
|
|
|
|
*/
|
|
|
|
static nsresult GetLeafNamePreservingRoot(nsIFile* aFile, nsString& aResult,
|
|
|
|
nsIFile* aParent = nullptr) {
|
|
|
|
MOZ_ASSERT(aFile);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> parent = aParent;
|
|
|
|
if (!parent) {
|
|
|
|
MOZ_TRY(aFile->GetParent(getter_AddRefs(parent)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent) {
|
|
|
|
return aFile->GetLeafName(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have reached the root path. On Windows, the leafname for a UNC path
|
|
|
|
// will not have the leading backslashes, so we need to use the entire path
|
|
|
|
// here:
|
|
|
|
//
|
|
|
|
// * for a UNIX root path (/) this will be /;
|
|
|
|
// * for a Windows drive path (e.g., C:), this will be the drive path (C:);
|
|
|
|
// and
|
|
|
|
// * for a Windows UNC server path (e.g., \\\\server), this will be the full
|
|
|
|
// server path (\\\\server).
|
|
|
|
return aFile->GetPath(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathUtils::Filename(const GlobalObject&, const nsAString& aPath,
|
|
|
|
nsString& aResult, ErrorResult& aErr) {
|
|
|
|
if (aPath.IsEmpty()) {
|
2021-01-12 23:44:46 +03:00
|
|
|
aErr.ThrowNotAllowedError(ERROR_EMPTY_PATH);
|
2020-11-05 21:08:38 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> path = new nsLocalFile();
|
2021-11-23 20:27:20 +03:00
|
|
|
if (nsresult rv = InitFileWithPath(path, aPath); NS_FAILED(rv)) {
|
2020-11-05 21:08:38 +03:00
|
|
|
ThrowError(aErr, rv, ERROR_INITIALIZE_PATH);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nsresult rv = GetLeafNamePreservingRoot(path, aResult); NS_FAILED(rv)) {
|
|
|
|
ThrowError(aErr, rv, "Could not get leaf name of path"_ns);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathUtils::Parent(const GlobalObject&, const nsAString& aPath,
|
2022-01-25 15:16:16 +03:00
|
|
|
const int32_t aDepth, nsString& aResult,
|
|
|
|
ErrorResult& aErr) {
|
2020-11-05 21:08:38 +03:00
|
|
|
if (aPath.IsEmpty()) {
|
|
|
|
aErr.ThrowNotAllowedError(ERROR_EMPTY_PATH);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> path = new nsLocalFile();
|
2021-11-23 20:27:20 +03:00
|
|
|
if (nsresult rv = InitFileWithPath(path, aPath); NS_FAILED(rv)) {
|
2020-11-05 21:08:38 +03:00
|
|
|
ThrowError(aErr, rv, ERROR_INITIALIZE_PATH);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-25 15:16:16 +03:00
|
|
|
if (aDepth <= 0) {
|
|
|
|
aErr.ThrowNotSupportedError("A depth of at least 1 is required");
|
2022-01-19 07:16:46 +03:00
|
|
|
return;
|
2022-01-19 04:17:46 +03:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:16:16 +03:00
|
|
|
nsCOMPtr<nsIFile> parent;
|
|
|
|
for (int32_t i = 0; path && i < aDepth; i++) {
|
|
|
|
if (nsresult rv = path->GetParent(getter_AddRefs(parent)); NS_FAILED(rv)) {
|
|
|
|
ThrowError(aErr, rv, ERROR_GET_PARENT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
path = parent;
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:08:38 +03:00
|
|
|
if (parent) {
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(parent->GetPath(aResult));
|
|
|
|
} else {
|
|
|
|
aResult = VoidString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathUtils::Join(const GlobalObject&, const Sequence<nsString>& aComponents,
|
|
|
|
nsString& aResult, ErrorResult& aErr) {
|
2022-11-03 01:46:56 +03:00
|
|
|
nsCOMPtr<nsIFile> path = Join(Span(aComponents), aErr);
|
|
|
|
if (aErr.Failed()) {
|
2020-11-05 21:08:38 +03:00
|
|
|
return;
|
|
|
|
}
|
2022-11-03 01:46:56 +03:00
|
|
|
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(path->GetPath(aResult));
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIFile> PathUtils::Join(
|
|
|
|
const Span<const nsString>& aComponents, ErrorResult& aErr) {
|
|
|
|
if (aComponents.IsEmpty() || aComponents[0].IsEmpty()) {
|
2020-11-05 21:08:38 +03:00
|
|
|
aErr.ThrowNotAllowedError(ERROR_EMPTY_PATH);
|
2022-11-03 01:46:56 +03:00
|
|
|
return nullptr;
|
2020-11-05 21:08:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> path = new nsLocalFile();
|
2021-11-23 20:27:20 +03:00
|
|
|
if (nsresult rv = InitFileWithPath(path, aComponents[0]); NS_FAILED(rv)) {
|
2020-11-05 21:08:38 +03:00
|
|
|
ThrowError(aErr, rv, ERROR_INITIALIZE_PATH);
|
2022-11-03 01:46:56 +03:00
|
|
|
return nullptr;
|
2020-11-05 21:08:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto components = Span<const nsString>(aComponents).Subspan(1);
|
|
|
|
for (const auto& component : components) {
|
|
|
|
if (nsresult rv = path->Append(component); NS_FAILED(rv)) {
|
2020-12-18 02:55:45 +03:00
|
|
|
ThrowError(aErr, rv, ERROR_JOIN);
|
2022-11-03 01:46:56 +03:00
|
|
|
return nullptr;
|
2020-11-05 21:08:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 01:46:56 +03:00
|
|
|
return path.forget();
|
2020-11-05 21:08:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-18 02:55:45 +03:00
|
|
|
void PathUtils::JoinRelative(const GlobalObject&, const nsAString& aBasePath,
|
|
|
|
const nsAString& aRelativePath, nsString& aResult,
|
|
|
|
ErrorResult& aErr) {
|
|
|
|
if (aRelativePath.IsEmpty()) {
|
|
|
|
aResult = aBasePath;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> path = new nsLocalFile();
|
2021-11-23 20:27:20 +03:00
|
|
|
if (nsresult rv = InitFileWithPath(path, aBasePath); NS_FAILED(rv)) {
|
2020-12-18 02:55:45 +03:00
|
|
|
ThrowError(aErr, rv, ERROR_INITIALIZE_PATH);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nsresult rv = path->AppendRelativePath(aRelativePath); NS_FAILED(rv)) {
|
|
|
|
ThrowError(aErr, rv, ERROR_JOIN);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(path->GetPath(aResult));
|
|
|
|
}
|
|
|
|
|
2021-01-30 03:50:47 +03:00
|
|
|
void PathUtils::ToExtendedWindowsPath(const GlobalObject&,
|
|
|
|
const nsAString& aPath, nsString& aResult,
|
|
|
|
ErrorResult& aErr) {
|
|
|
|
#ifndef XP_WIN
|
|
|
|
aErr.ThrowNotAllowedError("Operation is windows specific"_ns);
|
|
|
|
return;
|
|
|
|
#else
|
|
|
|
if (aPath.IsEmpty()) {
|
|
|
|
aErr.ThrowNotAllowedError(ERROR_EMPTY_PATH);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsAString& str1 = Substring(aPath, 1, 1);
|
|
|
|
const nsAString& str2 = Substring(aPath, 2, aPath.Length() - 2);
|
|
|
|
|
|
|
|
bool isUNC = aPath.Length() >= 2 &&
|
|
|
|
(aPath.First() == '\\' || aPath.First() == '/') &&
|
|
|
|
(str1.EqualsLiteral("\\") || str1.EqualsLiteral("/"));
|
|
|
|
|
|
|
|
constexpr auto pathPrefix = u"\\\\?\\"_ns;
|
|
|
|
const nsAString& uncPath = pathPrefix + u"UNC\\"_ns + str2;
|
|
|
|
const nsAString& normalPath = pathPrefix + aPath;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> path = new nsLocalFile();
|
2021-11-23 20:27:20 +03:00
|
|
|
if (nsresult rv = InitFileWithPath(path, isUNC ? uncPath : normalPath);
|
2021-01-30 03:50:47 +03:00
|
|
|
NS_FAILED(rv)) {
|
|
|
|
ThrowError(aErr, rv, ERROR_INITIALIZE_PATH);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(path->GetPath(aResult));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:08:38 +03:00
|
|
|
void PathUtils::Normalize(const GlobalObject&, const nsAString& aPath,
|
|
|
|
nsString& aResult, ErrorResult& aErr) {
|
|
|
|
if (aPath.IsEmpty()) {
|
|
|
|
aErr.ThrowNotAllowedError(ERROR_EMPTY_PATH);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> path = new nsLocalFile();
|
2021-11-23 20:27:20 +03:00
|
|
|
if (nsresult rv = InitFileWithPath(path, aPath); NS_FAILED(rv)) {
|
2020-11-05 21:08:38 +03:00
|
|
|
ThrowError(aErr, rv, ERROR_INITIALIZE_PATH);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nsresult rv = path->Normalize(); NS_FAILED(rv)) {
|
|
|
|
ThrowError(aErr, rv, "Could not normalize path"_ns);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(path->GetPath(aResult));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathUtils::Split(const GlobalObject&, const nsAString& aPath,
|
|
|
|
nsTArray<nsString>& aResult, ErrorResult& aErr) {
|
|
|
|
if (aPath.IsEmpty()) {
|
|
|
|
aErr.ThrowNotAllowedError(ERROR_EMPTY_PATH);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> path = new nsLocalFile();
|
2021-11-23 20:27:20 +03:00
|
|
|
if (nsresult rv = InitFileWithPath(path, aPath); NS_FAILED(rv)) {
|
2020-11-05 21:08:38 +03:00
|
|
|
ThrowError(aErr, rv, ERROR_INITIALIZE_PATH);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (path) {
|
|
|
|
auto* component = aResult.EmplaceBack(fallible);
|
|
|
|
if (!component) {
|
|
|
|
aErr.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> parent;
|
|
|
|
if (nsresult rv = path->GetParent(getter_AddRefs(parent)); NS_FAILED(rv)) {
|
|
|
|
ThrowError(aErr, rv, ERROR_GET_PARENT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLeafPreservingRoot cannot fail if we pass it a parent path.
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(GetLeafNamePreservingRoot(path, *component, parent));
|
|
|
|
|
|
|
|
path = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
aResult.Reverse();
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:55:40 +03:00
|
|
|
void PathUtils::SplitRelative(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aPath,
|
|
|
|
const SplitRelativeOptions& aOptions,
|
|
|
|
nsTArray<nsString>& aResult, ErrorResult& aErr) {
|
|
|
|
if (aPath.IsEmpty()) {
|
|
|
|
aErr.ThrowNotAllowedError(ERROR_EMPTY_PATH);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DoWindowsPathCheck()) {
|
|
|
|
MOZ_RELEASE_ASSERT(!aPath.Contains(u'/'),
|
|
|
|
"Windows paths cannot include forward slashes");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsAbsolute(aGlobal, aPath)) {
|
|
|
|
aErr.ThrowNotAllowedError(
|
|
|
|
"PathUtils.splitRelative requires a relative path"_ns);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
constexpr auto SEPARATOR = u'\\';
|
|
|
|
#else
|
|
|
|
constexpr auto SEPARATOR = u'/';
|
|
|
|
#endif
|
|
|
|
|
|
|
|
constexpr auto PARENT = u".."_ns;
|
|
|
|
constexpr auto CURRENT = u"."_ns;
|
|
|
|
|
|
|
|
for (const nsAString& pathComponent :
|
|
|
|
nsCharSeparatedTokenizerTemplate<NS_TokenizerIgnoreNothing>{aPath,
|
|
|
|
SEPARATOR}
|
|
|
|
.ToRange()) {
|
|
|
|
if (!aOptions.mAllowEmpty && pathComponent.IsEmpty()) {
|
|
|
|
aErr.ThrowNotAllowedError(
|
|
|
|
"PathUtils.splitRelative: Empty directory components (\"\") not "
|
|
|
|
"allowed by options");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aOptions.mAllowParentDir && pathComponent == PARENT) {
|
|
|
|
aErr.ThrowNotAllowedError(
|
|
|
|
"PathUtils.splitRelative: Parent directory components (\"..\") not "
|
|
|
|
"allowed by options");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aOptions.mAllowCurrentDir && pathComponent == CURRENT) {
|
|
|
|
aErr.ThrowNotAllowedError(
|
|
|
|
"PathUtils.splitRelative: Current directory components (\".\") not "
|
|
|
|
"allowed by options");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aResult.AppendElement(pathComponent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:08:38 +03:00
|
|
|
void PathUtils::ToFileURI(const GlobalObject&, const nsAString& aPath,
|
|
|
|
nsCString& aResult, ErrorResult& aErr) {
|
|
|
|
if (aPath.IsEmpty()) {
|
|
|
|
aErr.ThrowNotAllowedError(ERROR_EMPTY_PATH);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> path = new nsLocalFile();
|
2021-11-23 20:27:20 +03:00
|
|
|
if (nsresult rv = InitFileWithPath(path, aPath); NS_FAILED(rv)) {
|
2020-11-05 21:08:38 +03:00
|
|
|
ThrowError(aErr, rv, ERROR_INITIALIZE_PATH);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
if (nsresult rv = NS_NewFileURI(getter_AddRefs(uri), path); NS_FAILED(rv)) {
|
|
|
|
ThrowError(aErr, rv, "Could not initialize File URI"_ns);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nsresult rv = uri->GetSpec(aResult); NS_FAILED(rv)) {
|
|
|
|
ThrowError(aErr, rv, "Could not retrieve URI spec"_ns);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 19:10:12 +03:00
|
|
|
bool PathUtils::IsAbsolute(const GlobalObject&, const nsAString& aPath) {
|
|
|
|
nsCOMPtr<nsIFile> path = new nsLocalFile();
|
|
|
|
nsresult rv = InitFileWithPath(path, aPath);
|
|
|
|
return NS_SUCCEEDED(rv);
|
|
|
|
}
|
|
|
|
|
2022-02-01 07:06:13 +03:00
|
|
|
void PathUtils::GetProfileDirSync(const GlobalObject&, nsString& aResult,
|
|
|
|
ErrorResult& aErr) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
auto guard = sDirCache.Lock();
|
|
|
|
DirectoryCache::Ensure(guard.ref())
|
|
|
|
.GetDirectorySync(aResult, aErr, DirectoryCache::Directory::Profile);
|
|
|
|
}
|
|
|
|
void PathUtils::GetLocalProfileDirSync(const GlobalObject&, nsString& aResult,
|
|
|
|
ErrorResult& aErr) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
auto guard = sDirCache.Lock();
|
|
|
|
DirectoryCache::Ensure(guard.ref())
|
|
|
|
.GetDirectorySync(aResult, aErr, DirectoryCache::Directory::LocalProfile);
|
|
|
|
}
|
|
|
|
void PathUtils::GetTempDirSync(const GlobalObject&, nsString& aResult,
|
|
|
|
ErrorResult& aErr) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
auto guard = sDirCache.Lock();
|
|
|
|
DirectoryCache::Ensure(guard.ref())
|
|
|
|
.GetDirectorySync(aResult, aErr, DirectoryCache::Directory::Temp);
|
|
|
|
}
|
|
|
|
|
2022-09-03 00:16:50 +03:00
|
|
|
void PathUtils::GetXulLibraryPathSync(const GlobalObject&, nsString& aResult,
|
|
|
|
ErrorResult& aErr) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
auto guard = sDirCache.Lock();
|
|
|
|
DirectoryCache::Ensure(guard.ref())
|
|
|
|
.GetDirectorySync(aResult, aErr, DirectoryCache::Directory::XulLibrary);
|
|
|
|
}
|
|
|
|
|
2022-02-01 07:06:13 +03:00
|
|
|
already_AddRefed<Promise> PathUtils::GetProfileDirAsync(
|
|
|
|
const GlobalObject& aGlobal, ErrorResult& aErr) {
|
2022-03-23 22:34:27 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2022-02-01 07:06:13 +03:00
|
|
|
|
2020-11-10 15:08:53 +03:00
|
|
|
auto guard = sDirCache.Lock();
|
|
|
|
return DirectoryCache::Ensure(guard.ref())
|
2022-02-01 07:06:13 +03:00
|
|
|
.GetDirectoryAsync(aGlobal, aErr, DirectoryCache::Directory::Profile);
|
2020-11-10 15:08:53 +03:00
|
|
|
}
|
|
|
|
|
2022-02-01 07:06:13 +03:00
|
|
|
already_AddRefed<Promise> PathUtils::GetLocalProfileDirAsync(
|
2020-11-10 15:08:53 +03:00
|
|
|
const GlobalObject& aGlobal, ErrorResult& aErr) {
|
2022-03-23 22:34:27 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2022-02-01 07:06:13 +03:00
|
|
|
|
2020-11-10 15:08:53 +03:00
|
|
|
auto guard = sDirCache.Lock();
|
|
|
|
return DirectoryCache::Ensure(guard.ref())
|
2022-02-01 07:06:13 +03:00
|
|
|
.GetDirectoryAsync(aGlobal, aErr,
|
|
|
|
DirectoryCache::Directory::LocalProfile);
|
2020-11-10 15:08:53 +03:00
|
|
|
}
|
|
|
|
|
2022-02-01 07:06:13 +03:00
|
|
|
already_AddRefed<Promise> PathUtils::GetTempDirAsync(
|
|
|
|
const GlobalObject& aGlobal, ErrorResult& aErr) {
|
2022-03-23 22:34:27 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2022-02-01 07:06:13 +03:00
|
|
|
|
2020-11-10 15:08:53 +03:00
|
|
|
auto guard = sDirCache.Lock();
|
|
|
|
return DirectoryCache::Ensure(guard.ref())
|
2022-02-01 07:06:13 +03:00
|
|
|
.GetDirectoryAsync(aGlobal, aErr, DirectoryCache::Directory::Temp);
|
2020-11-10 15:08:53 +03:00
|
|
|
}
|
|
|
|
|
2022-09-03 00:16:50 +03:00
|
|
|
already_AddRefed<Promise> PathUtils::GetXulLibraryPathAsync(
|
|
|
|
const GlobalObject& aGlobal, ErrorResult& aErr) {
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
auto guard = sDirCache.Lock();
|
|
|
|
return DirectoryCache::Ensure(guard.ref())
|
|
|
|
.GetDirectoryAsync(aGlobal, aErr, DirectoryCache::Directory::XulLibrary);
|
|
|
|
}
|
|
|
|
|
2020-11-10 15:08:53 +03:00
|
|
|
PathUtils::DirectoryCache::DirectoryCache() {
|
2022-01-24 23:48:10 +03:00
|
|
|
for (auto& dir : mDirectories) {
|
|
|
|
dir.SetIsVoid(true);
|
|
|
|
}
|
2020-11-10 15:08:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PathUtils::DirectoryCache& PathUtils::DirectoryCache::Ensure(
|
|
|
|
Maybe<PathUtils::DirectoryCache>& aCache) {
|
|
|
|
if (aCache.isNothing()) {
|
|
|
|
aCache.emplace();
|
|
|
|
|
2021-01-12 23:38:28 +03:00
|
|
|
auto clearAtShutdown = []() {
|
|
|
|
RunOnShutdown([]() {
|
|
|
|
auto cache = PathUtils::sDirCache.Lock();
|
|
|
|
cache->reset();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
clearAtShutdown();
|
|
|
|
} else {
|
|
|
|
NS_DispatchToMainThread(
|
|
|
|
NS_NewRunnableFunction(__func__, std::move(clearAtShutdown)));
|
|
|
|
}
|
2020-11-10 15:08:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return aCache.ref();
|
|
|
|
}
|
|
|
|
|
2022-02-01 07:06:13 +03:00
|
|
|
void PathUtils::DirectoryCache::GetDirectorySync(
|
|
|
|
nsString& aResult, ErrorResult& aErr, const Directory aRequestedDir) {
|
|
|
|
MOZ_RELEASE_ASSERT(aRequestedDir < Directory::Count);
|
|
|
|
|
|
|
|
if (nsresult rv = PopulateDirectoriesImpl(aRequestedDir); NS_FAILED(rv)) {
|
|
|
|
nsAutoCStringN<32> errorName;
|
|
|
|
GetErrorName(rv, errorName);
|
|
|
|
|
|
|
|
nsAutoCStringN<256> msg;
|
|
|
|
msg.Append("Could not retrieve directory "_ns);
|
|
|
|
msg.Append(kDirectoryNames[aRequestedDir]);
|
|
|
|
msg.Append(COLON);
|
|
|
|
msg.Append(errorName);
|
|
|
|
|
|
|
|
aErr.ThrowUnknownError(msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aResult = mDirectories[aRequestedDir];
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise> PathUtils::DirectoryCache::GetDirectoryAsync(
|
2020-11-10 15:08:53 +03:00
|
|
|
const GlobalObject& aGlobal, ErrorResult& aErr,
|
|
|
|
const Directory aRequestedDir) {
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
RefPtr<Promise> promise = Promise::Create(global, aErr);
|
|
|
|
if (aErr.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RefPtr<PopulateDirectoriesPromise> p =
|
|
|
|
PopulateDirectories(aRequestedDir)) {
|
|
|
|
p->Then(
|
|
|
|
GetCurrentSerialEventTarget(), __func__,
|
|
|
|
[promise, aRequestedDir](const Ok&) {
|
|
|
|
auto cache = PathUtils::sDirCache.Lock();
|
|
|
|
cache.ref()->ResolveWithDirectory(promise, aRequestedDir);
|
|
|
|
},
|
|
|
|
[promise](const nsresult& aRv) { promise->MaybeReject(aRv); });
|
|
|
|
} else {
|
|
|
|
ResolveWithDirectory(promise, aRequestedDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathUtils::DirectoryCache::ResolveWithDirectory(
|
|
|
|
Promise* aPromise, const Directory aRequestedDir) {
|
2022-01-24 23:48:10 +03:00
|
|
|
MOZ_RELEASE_ASSERT(aRequestedDir < Directory::Count);
|
|
|
|
MOZ_RELEASE_ASSERT(!mDirectories[aRequestedDir].IsVoid());
|
|
|
|
aPromise->MaybeResolve(mDirectories[aRequestedDir]);
|
2020-11-10 15:08:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<PathUtils::DirectoryCache::PopulateDirectoriesPromise>
|
|
|
|
PathUtils::DirectoryCache::PopulateDirectories(
|
|
|
|
const PathUtils::DirectoryCache::Directory aRequestedDir) {
|
2022-01-24 23:48:10 +03:00
|
|
|
MOZ_RELEASE_ASSERT(aRequestedDir < Directory::Count);
|
|
|
|
|
2020-11-10 15:08:53 +03:00
|
|
|
// If we have already resolved the requested directory, we can return
|
|
|
|
// immediately.
|
2022-03-25 17:42:44 +03:00
|
|
|
// Otherwise, if we have already fired off a request to populate the entry,
|
|
|
|
// so we can return the corresponding promise immediately. caller will queue
|
|
|
|
// a Thenable onto that promise to resolve/reject the request.
|
2022-01-24 23:48:10 +03:00
|
|
|
if (!mDirectories[aRequestedDir].IsVoid()) {
|
2020-11-10 15:08:53 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2022-01-24 23:48:10 +03:00
|
|
|
if (!mPromises[aRequestedDir].IsEmpty()) {
|
|
|
|
return mPromises[aRequestedDir].Ensure(__func__);
|
2020-11-10 15:08:53 +03:00
|
|
|
}
|
|
|
|
|
2022-01-24 23:48:10 +03:00
|
|
|
RefPtr<PopulateDirectoriesPromise> promise =
|
|
|
|
mPromises[aRequestedDir].Ensure(__func__);
|
2020-11-10 15:08:53 +03:00
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
nsresult rv = PopulateDirectoriesImpl(aRequestedDir);
|
|
|
|
ResolvePopulateDirectoriesPromise(rv, aRequestedDir);
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
NS_NewRunnableFunction(__func__, [aRequestedDir]() {
|
|
|
|
auto cache = PathUtils::sDirCache.Lock();
|
|
|
|
nsresult rv = cache.ref()->PopulateDirectoriesImpl(aRequestedDir);
|
|
|
|
cache.ref()->ResolvePopulateDirectoriesPromise(rv, aRequestedDir);
|
|
|
|
});
|
|
|
|
NS_DispatchToMainThread(runnable.forget());
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathUtils::DirectoryCache::ResolvePopulateDirectoriesPromise(
|
|
|
|
nsresult aRv, const PathUtils::DirectoryCache::Directory aRequestedDir) {
|
2022-01-24 23:48:10 +03:00
|
|
|
MOZ_RELEASE_ASSERT(aRequestedDir < Directory::Count);
|
|
|
|
|
2020-11-10 15:08:53 +03:00
|
|
|
if (NS_SUCCEEDED(aRv)) {
|
2022-01-24 23:48:10 +03:00
|
|
|
mPromises[aRequestedDir].Resolve(Ok{}, __func__);
|
2020-11-10 15:08:53 +03:00
|
|
|
} else {
|
2022-01-24 23:48:10 +03:00
|
|
|
mPromises[aRequestedDir].Reject(aRv, __func__);
|
2020-11-10 15:08:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult PathUtils::DirectoryCache::PopulateDirectoriesImpl(
|
|
|
|
const PathUtils::DirectoryCache::Directory aRequestedDir) {
|
|
|
|
MOZ_RELEASE_ASSERT(NS_IsMainThread());
|
2022-01-24 23:48:10 +03:00
|
|
|
MOZ_RELEASE_ASSERT(aRequestedDir < Directory::Count);
|
2020-11-10 15:08:53 +03:00
|
|
|
|
2022-02-01 07:06:13 +03:00
|
|
|
if (!mDirectories[aRequestedDir].IsVoid()) {
|
|
|
|
// In between when this promise was dispatched to the main thread and now,
|
2022-03-25 17:42:44 +03:00
|
|
|
// the directory cache has had this entry populated (via the
|
|
|
|
// on-main-thread sync method).
|
2022-02-01 07:06:13 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2020-11-10 15:08:53 +03:00
|
|
|
|
2022-01-24 23:48:10 +03:00
|
|
|
nsCOMPtr<nsIFile> path;
|
2022-01-22 05:04:25 +03:00
|
|
|
|
2022-01-24 23:48:10 +03:00
|
|
|
MOZ_TRY(NS_GetSpecialDirectory(kDirectoryNames[aRequestedDir],
|
|
|
|
getter_AddRefs(path)));
|
|
|
|
MOZ_TRY(path->GetPath(mDirectories[aRequestedDir]));
|
2020-11-10 15:08:53 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2022-05-09 23:41:17 +03:00
|
|
|
} // namespace mozilla::dom
|