Line data Source code
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/. */
6 :
7 : #include "mozilla/dom/FileSystemUtils.h"
8 :
9 : namespace mozilla {
10 : namespace dom {
11 :
12 : namespace {
13 :
14 : bool
15 0 : TokenizerIgnoreNothing(char16_t /* aChar */)
16 : {
17 0 : return false;
18 : }
19 :
20 : } // anonymous namespace
21 :
22 : /* static */ bool
23 0 : FileSystemUtils::IsDescendantPath(const nsAString& aPath,
24 : const nsAString& aDescendantPath)
25 : {
26 : // Check the sub-directory path to see if it has the parent path as prefix.
27 0 : if (!aDescendantPath.Equals(aPath) &&
28 0 : !StringBeginsWith(aDescendantPath, aPath)) {
29 0 : return false;
30 : }
31 :
32 0 : return true;
33 : }
34 :
35 : /* static */ bool
36 0 : FileSystemUtils::IsValidRelativeDOMPath(const nsAString& aPath,
37 : nsTArray<nsString>& aParts)
38 : {
39 : // We don't allow empty relative path to access the root.
40 0 : if (aPath.IsEmpty()) {
41 0 : return false;
42 : }
43 :
44 : // Leading and trailing "/" are not allowed.
45 0 : if (aPath.First() == FILESYSTEM_DOM_PATH_SEPARATOR_CHAR ||
46 0 : aPath.Last() == FILESYSTEM_DOM_PATH_SEPARATOR_CHAR) {
47 0 : return false;
48 : }
49 :
50 0 : NS_NAMED_LITERAL_STRING(kCurrentDir, ".");
51 0 : NS_NAMED_LITERAL_STRING(kParentDir, "..");
52 :
53 : // Split path and check each path component.
54 : nsCharSeparatedTokenizerTemplate<TokenizerIgnoreNothing>
55 0 : tokenizer(aPath, FILESYSTEM_DOM_PATH_SEPARATOR_CHAR);
56 :
57 0 : while (tokenizer.hasMoreTokens()) {
58 0 : nsDependentSubstring pathComponent = tokenizer.nextToken();
59 : // The path containing empty components, such as "foo//bar", is invalid.
60 : // We don't allow paths, such as "../foo", "foo/./bar" and "foo/../bar",
61 : // to walk up the directory.
62 0 : if (pathComponent.IsEmpty() ||
63 0 : pathComponent.Equals(kCurrentDir) ||
64 0 : pathComponent.Equals(kParentDir)) {
65 0 : return false;
66 : }
67 :
68 0 : aParts.AppendElement(pathComponent);
69 : }
70 :
71 0 : return true;
72 : }
73 :
74 : /* static */ nsresult
75 0 : FileSystemUtils::DispatchRunnable(nsIGlobalObject* aGlobal,
76 : already_AddRefed<nsIRunnable>&& aRunnable)
77 : {
78 0 : nsCOMPtr<nsIRunnable> runnable = aRunnable;
79 :
80 0 : nsCOMPtr<nsIEventTarget> target;
81 0 : if (!aGlobal) {
82 0 : target = SystemGroup::EventTargetFor(TaskCategory::Other);
83 : } else {
84 0 : target = aGlobal->EventTargetFor(TaskCategory::Other);
85 : }
86 :
87 0 : MOZ_ASSERT(target);
88 :
89 0 : nsresult rv = target->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
90 0 : if (NS_WARN_IF(NS_FAILED(rv))) {
91 0 : return rv;
92 : }
93 :
94 0 : return NS_OK;
95 : }
96 :
97 : } // namespace dom
98 : } // namespace mozilla
|