no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / filesystem / FileSystemUtils.cpp
blob136f683e5e09eb7320edcf8b0aa26c8e9cde470e
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 {
14 /* static */
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)) {
20 return false;
23 return true;
26 /* static */
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()) {
31 return false;
34 // Leading and trailing "/" are not allowed.
35 if (aPath.First() == FILESYSTEM_DOM_PATH_SEPARATOR_CHAR ||
36 aPath.Last() == FILESYSTEM_DOM_PATH_SEPARATOR_CHAR) {
37 return false;
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}
47 .ToRange()) {
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)) {
53 return false;
56 aParts.AppendElement(pathComponent);
59 return true;
62 /* static */
63 nsresult FileSystemUtils::DispatchRunnable(
64 nsIGlobalObject* aGlobal, already_AddRefed<nsIRunnable>&& aRunnable) {
65 nsCOMPtr<nsIRunnable> runnable = aRunnable;
67 nsCOMPtr<nsIEventTarget> target;
68 if (!aGlobal) {
69 target = GetMainThreadSerialEventTarget();
70 } else {
71 target = aGlobal->SerialEventTarget();
74 MOZ_ASSERT(target);
76 nsresult rv = target->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
77 if (NS_WARN_IF(NS_FAILED(rv))) {
78 return rv;
81 return NS_OK;
84 } // namespace mozilla::dom