Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/filesystem/FileSystemUtils.cpp
Line
Count
Source (jump to first uncovered line)
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
TokenizerIgnoreNothing(char16_t /* aChar */)
16
0
{
17
0
  return false;
18
0
}
19
20
} // anonymous namespace
21
22
/* static */ bool
23
FileSystemUtils::IsDescendantPath(const nsAString& aPath,
24
                                  const nsAString& aDescendantPath)
25
0
{
26
0
  // 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
0
  }
31
0
32
0
  return true;
33
0
}
34
35
/* static */ bool
36
FileSystemUtils::IsValidRelativeDOMPath(const nsAString& aPath,
37
                                        nsTArray<nsString>& aParts)
38
0
{
39
0
  // We don't allow empty relative path to access the root.
40
0
  if (aPath.IsEmpty()) {
41
0
    return false;
42
0
  }
43
0
44
0
  // 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
0
  }
49
0
50
0
  NS_NAMED_LITERAL_STRING(kCurrentDir, ".");
51
0
  NS_NAMED_LITERAL_STRING(kParentDir, "..");
52
0
53
0
  // Split path and check each path component.
54
0
  nsCharSeparatedTokenizerTemplate<TokenizerIgnoreNothing>
55
0
    tokenizer(aPath, FILESYSTEM_DOM_PATH_SEPARATOR_CHAR);
56
0
57
0
  while (tokenizer.hasMoreTokens()) {
58
0
    nsDependentSubstring pathComponent = tokenizer.nextToken();
59
0
    // The path containing empty components, such as "foo//bar", is invalid.
60
0
    // We don't allow paths, such as "../foo", "foo/./bar" and "foo/../bar",
61
0
    // 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
0
    }
67
0
68
0
    aParts.AppendElement(pathComponent);
69
0
  }
70
0
71
0
  return true;
72
0
}
73
74
/* static */ nsresult
75
FileSystemUtils::DispatchRunnable(nsIGlobalObject* aGlobal,
76
                                  already_AddRefed<nsIRunnable>&& aRunnable)
77
0
{
78
0
  nsCOMPtr<nsIRunnable> runnable = aRunnable;
79
0
80
0
  nsCOMPtr<nsIEventTarget> target;
81
0
  if (!aGlobal) {
82
0
    target = SystemGroup::EventTargetFor(TaskCategory::Other);
83
0
  } else {
84
0
    target = aGlobal->EventTargetFor(TaskCategory::Other);
85
0
  }
86
0
87
0
  MOZ_ASSERT(target);
88
0
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
0
  }
93
0
94
0
  return NS_OK;
95
0
}
96
97
} // namespace dom
98
} // namespace mozilla