Blame dom/filesystem/FileSystemUtils.cpp

Packit f0b94e
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
Packit f0b94e
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
Packit f0b94e
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit f0b94e
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
Packit f0b94e
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit f0b94e
Packit f0b94e
#include "mozilla/dom/FileSystemUtils.h"
Packit f0b94e
Packit f0b94e
namespace mozilla {
Packit f0b94e
namespace dom {
Packit f0b94e
Packit f0b94e
namespace {
Packit f0b94e
Packit f0b94e
bool TokenizerIgnoreNothing(char16_t /* aChar */) { return false; }
Packit f0b94e
Packit f0b94e
}  // anonymous namespace
Packit f0b94e
Packit f0b94e
/* static */ bool FileSystemUtils::IsDescendantPath(
Packit f0b94e
    const nsAString& aPath, const nsAString& aDescendantPath) {
Packit f0b94e
  // Check the sub-directory path to see if it has the parent path as prefix.
Packit f0b94e
  if (!aDescendantPath.Equals(aPath) &&
Packit f0b94e
      !StringBeginsWith(aDescendantPath, aPath)) {
Packit f0b94e
    return false;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  return true;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
/* static */ bool FileSystemUtils::IsValidRelativeDOMPath(
Packit f0b94e
    const nsAString& aPath, nsTArray<nsString>& aParts) {
Packit f0b94e
  // We don't allow empty relative path to access the root.
Packit f0b94e
  if (aPath.IsEmpty()) {
Packit f0b94e
    return false;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  // Leading and trailing "/" are not allowed.
Packit f0b94e
  if (aPath.First() == FILESYSTEM_DOM_PATH_SEPARATOR_CHAR ||
Packit f0b94e
      aPath.Last() == FILESYSTEM_DOM_PATH_SEPARATOR_CHAR) {
Packit f0b94e
    return false;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  NS_NAMED_LITERAL_STRING(kCurrentDir, ".");
Packit f0b94e
  NS_NAMED_LITERAL_STRING(kParentDir, "..");
Packit f0b94e
Packit f0b94e
  // Split path and check each path component.
Packit f0b94e
  nsCharSeparatedTokenizerTemplate<TokenizerIgnoreNothing> tokenizer(
Packit f0b94e
      aPath, FILESYSTEM_DOM_PATH_SEPARATOR_CHAR);
Packit f0b94e
Packit f0b94e
  while (tokenizer.hasMoreTokens()) {
Packit f0b94e
    nsDependentSubstring pathComponent = tokenizer.nextToken();
Packit f0b94e
    // The path containing empty components, such as "foo//bar", is invalid.
Packit f0b94e
    // We don't allow paths, such as "../foo", "foo/./bar" and "foo/../bar",
Packit f0b94e
    // to walk up the directory.
Packit f0b94e
    if (pathComponent.IsEmpty() || pathComponent.Equals(kCurrentDir) ||
Packit f0b94e
        pathComponent.Equals(kParentDir)) {
Packit f0b94e
      return false;
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    aParts.AppendElement(pathComponent);
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  return true;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
/* static */ nsresult FileSystemUtils::DispatchRunnable(
Packit f0b94e
    nsIGlobalObject* aGlobal, already_AddRefed<nsIRunnable>&& aRunnable) {
Packit f0b94e
  nsCOMPtr<nsIRunnable> runnable = aRunnable;
Packit f0b94e
Packit f0b94e
  nsCOMPtr<nsIEventTarget> target;
Packit f0b94e
  if (!aGlobal) {
Packit f0b94e
    target = SystemGroup::EventTargetFor(TaskCategory::Other);
Packit f0b94e
  } else {
Packit f0b94e
    target = aGlobal->EventTargetFor(TaskCategory::Other);
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  MOZ_ASSERT(target);
Packit f0b94e
Packit f0b94e
  nsresult rv = target->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
Packit f0b94e
  if (NS_WARN_IF(NS_FAILED(rv))) {
Packit f0b94e
    return rv;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  return NS_OK;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
}  // namespace dom
Packit f0b94e
}  // namespace mozilla