1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/FileSystemUtils.h"
8 #include "nsCharSeparatedTokenizer.h"
9 #include "nsIEventTarget.h"
10 #include "nsThreadUtils.h"
12 namespace mozilla::dom
{
15 bool FileSystemUtils::IsDescendantPath(const nsAString
& aPath
,
16 const nsAString
& aDescendantPath
) {
17 // Check the sub-directory path to see if it has the parent path as prefix.
18 if (!aDescendantPath
.Equals(aPath
) &&
19 !StringBeginsWith(aDescendantPath
, aPath
)) {
27 bool FileSystemUtils::IsValidRelativeDOMPath(const nsAString
& aPath
,
28 nsTArray
<nsString
>& aParts
) {
29 // We don't allow empty relative path to access the root.
30 if (aPath
.IsEmpty()) {
34 // Leading and trailing "/" are not allowed.
35 if (aPath
.First() == FILESYSTEM_DOM_PATH_SEPARATOR_CHAR
||
36 aPath
.Last() == FILESYSTEM_DOM_PATH_SEPARATOR_CHAR
) {
40 constexpr auto kCurrentDir
= u
"."_ns
;
41 constexpr auto kParentDir
= u
".."_ns
;
43 // Split path and check each path component.
44 for (const nsAString
& pathComponent
:
45 nsCharSeparatedTokenizerTemplate
<NS_TokenizerIgnoreNothing
>{
46 aPath
, FILESYSTEM_DOM_PATH_SEPARATOR_CHAR
}
48 // The path containing empty components, such as "foo//bar", is invalid.
49 // We don't allow paths, such as "../foo", "foo/./bar" and "foo/../bar",
50 // to walk up the directory.
51 if (pathComponent
.IsEmpty() || pathComponent
.Equals(kCurrentDir
) ||
52 pathComponent
.Equals(kParentDir
)) {
56 aParts
.AppendElement(pathComponent
);
63 nsresult
FileSystemUtils::DispatchRunnable(
64 nsIGlobalObject
* aGlobal
, already_AddRefed
<nsIRunnable
>&& aRunnable
) {
65 nsCOMPtr
<nsIRunnable
> runnable
= aRunnable
;
67 nsCOMPtr
<nsIEventTarget
> target
;
69 target
= GetMainThreadSerialEventTarget();
71 target
= aGlobal
->SerialEventTarget();
76 nsresult rv
= target
->Dispatch(runnable
.forget(), NS_DISPATCH_NORMAL
);
77 if (NS_WARN_IF(NS_FAILED(rv
))) {
84 } // namespace mozilla::dom