DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "CallbackRunnables.h"
#include "mozilla/dom/Directory.h"
#include "mozilla/dom/DirectoryBinding.h"
#include "mozilla/dom/DOMException.h"
#include "mozilla/dom/File.h"
#include "mozilla/dom/FileBinding.h"
#include "mozilla/dom/FileSystemDirectoryReaderBinding.h"
#include "mozilla/dom/FileSystemFileEntry.h"
#include "mozilla/dom/FileSystemUtils.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/Unused.h"
#include "nsIGlobalObject.h"
#include "nsIFile.h"
#include "nsPIDOMWindow.h"

#include "../GetFileOrDirectoryTask.h"

namespace mozilla {
namespace dom {

EntryCallbackRunnable::EntryCallbackRunnable(FileSystemEntryCallback* aCallback,
                                             FileSystemEntry* aEntry)
    : Runnable("EntryCallbackRunnable"), mCallback(aCallback), mEntry(aEntry) {
  MOZ_ASSERT(aCallback);
  MOZ_ASSERT(aEntry);
}

NS_IMETHODIMP
EntryCallbackRunnable::Run() {
  mCallback->Call(*mEntry);
  return NS_OK;
}

ErrorCallbackRunnable::ErrorCallbackRunnable(nsIGlobalObject* aGlobalObject,
                                             ErrorCallback* aCallback,
                                             nsresult aError)
    : Runnable("ErrorCallbackRunnable"),
      mGlobal(aGlobalObject),
      mCallback(aCallback),
      mError(aError) {
  MOZ_ASSERT(aGlobalObject);
  MOZ_ASSERT(aCallback);
  MOZ_ASSERT(NS_FAILED(aError));
}

NS_IMETHODIMP
ErrorCallbackRunnable::Run() {
  RefPtr<DOMException> exception = DOMException::Create(mError);
  mCallback->Call(*exception);
  return NS_OK;
}

EmptyEntriesCallbackRunnable::EmptyEntriesCallbackRunnable(
    FileSystemEntriesCallback* aCallback)
    : Runnable("EmptyEntriesCallbackRunnable"), mCallback(aCallback) {
  MOZ_ASSERT(aCallback);
}

NS_IMETHODIMP
EmptyEntriesCallbackRunnable::Run() {
  Sequence<OwningNonNull<FileSystemEntry>> sequence;
  mCallback->Call(sequence);
  return NS_OK;
}

GetEntryHelper::GetEntryHelper(FileSystemDirectoryEntry* aParentEntry,
                               Directory* aDirectory,
                               nsTArray<nsString>& aParts,
                               FileSystem* aFileSystem,
                               FileSystemEntryCallback* aSuccessCallback,
                               ErrorCallback* aErrorCallback,
                               FileSystemDirectoryEntry::GetInternalType aType)
    : mParentEntry(aParentEntry),
      mDirectory(aDirectory),
      mParts(aParts),
      mFileSystem(aFileSystem),
      mSuccessCallback(aSuccessCallback),
      mErrorCallback(aErrorCallback),
      mType(aType) {
  MOZ_ASSERT(aParentEntry);
  MOZ_ASSERT(aDirectory);
  MOZ_ASSERT(!aParts.IsEmpty());
  MOZ_ASSERT(aFileSystem);
  MOZ_ASSERT(aSuccessCallback || aErrorCallback);
}

GetEntryHelper::~GetEntryHelper() {}

namespace {

nsresult DOMPathToRealPath(Directory* aDirectory, const nsAString& aPath,
                           nsIFile** aFile) {
  nsString relativePath;
  relativePath = aPath;

  // Trim white spaces.
  static const char kWhitespace[] = "\b\t\r\n ";
  relativePath.Trim(kWhitespace);

  nsTArray<nsString> parts;
  if (!FileSystemUtils::IsValidRelativeDOMPath(relativePath, parts)) {
    return NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR;
  }

  nsCOMPtr<nsIFile> file;
  nsresult rv = aDirectory->GetInternalNsIFile()->Clone(getter_AddRefs(file));
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  for (uint32_t i = 0; i < parts.Length(); ++i) {
    rv = file->AppendRelativePath(parts[i]);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return rv;
    }
  }

  file.forget(aFile);
  return NS_OK;
}

}  // namespace

void GetEntryHelper::Run() {
  MOZ_ASSERT(!mParts.IsEmpty());

  nsCOMPtr<nsIFile> realPath;
  nsresult error =
      DOMPathToRealPath(mDirectory, mParts[0], getter_AddRefs(realPath));

  ErrorResult rv;
  RefPtr<FileSystemBase> fs = mDirectory->GetFileSystem(rv);
  if (NS_WARN_IF(rv.Failed())) {
    rv.SuppressException();
    Error(NS_ERROR_DOM_INVALID_STATE_ERR);
    return;
  }

  RefPtr<GetFileOrDirectoryTaskChild> task =
      GetFileOrDirectoryTaskChild::Create(fs, realPath, rv);
  if (NS_WARN_IF(rv.Failed())) {
    rv.SuppressException();
    Error(NS_ERROR_DOM_INVALID_STATE_ERR);
    return;
  }

  task->SetError(error);
  task->Start();

  RefPtr<Promise> promise = task->GetPromise();

  mParts.RemoveElementAt(0);
  promise->AppendNativeHandler(this);
}

void GetEntryHelper::ResolvedCallback(JSContext* aCx,
                                      JS::Handle<JS::Value> aValue) {
  if (NS_WARN_IF(!aValue.isObject())) {
    return;
  }

  JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());

  // This is not the last part of the path.
  if (!mParts.IsEmpty()) {
    ContinueRunning(obj);
    return;
  }

  CompleteOperation(obj);
}

void GetEntryHelper::CompleteOperation(JSObject* aObj) {
  MOZ_ASSERT(mParts.IsEmpty());

  if (mType == FileSystemDirectoryEntry::eGetFile) {
    RefPtr<File> file;
    if (NS_FAILED(UNWRAP_OBJECT(File, aObj, file))) {
      Error(NS_ERROR_DOM_TYPE_MISMATCH_ERR);
      return;
    }

    RefPtr<FileSystemFileEntry> entry = new FileSystemFileEntry(
        mParentEntry->GetParentObject(), file, mParentEntry, mFileSystem);
    mSuccessCallback->Call(*entry);
    return;
  }

  MOZ_ASSERT(mType == FileSystemDirectoryEntry::eGetDirectory);

  RefPtr<Directory> directory;
  if (NS_FAILED(UNWRAP_OBJECT(Directory, aObj, directory))) {
    Error(NS_ERROR_DOM_TYPE_MISMATCH_ERR);
    return;
  }

  RefPtr<FileSystemDirectoryEntry> entry = new FileSystemDirectoryEntry(
      mParentEntry->GetParentObject(), directory, mParentEntry, mFileSystem);
  mSuccessCallback->Call(*entry);
}

void GetEntryHelper::ContinueRunning(JSObject* aObj) {
  MOZ_ASSERT(!mParts.IsEmpty());

  RefPtr<Directory> directory;
  if (NS_FAILED(UNWRAP_OBJECT(Directory, aObj, directory))) {
    Error(NS_ERROR_DOM_TYPE_MISMATCH_ERR);
    return;
  }

  RefPtr<FileSystemDirectoryEntry> entry = new FileSystemDirectoryEntry(
      mParentEntry->GetParentObject(), directory, mParentEntry, mFileSystem);

  // Update the internal values.
  mParentEntry = entry;
  mDirectory = directory;

  Run();
}

void GetEntryHelper::RejectedCallback(JSContext* aCx,
                                      JS::Handle<JS::Value> aValue) {
  Error(NS_ERROR_DOM_NOT_FOUND_ERR);
}

void GetEntryHelper::Error(nsresult aError) {
  MOZ_ASSERT(NS_FAILED(aError));

  if (mErrorCallback) {
    RefPtr<ErrorCallbackRunnable> runnable = new ErrorCallbackRunnable(
        mParentEntry->GetParentObject(), mErrorCallback, aError);

    FileSystemUtils::DispatchRunnable(mParentEntry->GetParentObject(),
                                      runnable.forget());
  }
}

NS_IMPL_ISUPPORTS0(GetEntryHelper);

/* static */
void FileSystemEntryCallbackHelper::Call(
    nsIGlobalObject* aGlobalObject,
    const Optional<OwningNonNull<FileSystemEntryCallback>>& aEntryCallback,
    FileSystemEntry* aEntry) {
  MOZ_ASSERT(aGlobalObject);
  MOZ_ASSERT(aEntry);

  if (aEntryCallback.WasPassed()) {
    RefPtr<EntryCallbackRunnable> runnable =
        new EntryCallbackRunnable(&aEntryCallback.Value(), aEntry);

    FileSystemUtils::DispatchRunnable(aGlobalObject, runnable.forget());
  }
}

/* static */
void ErrorCallbackHelper::Call(
    nsIGlobalObject* aGlobal,
    const Optional<OwningNonNull<ErrorCallback>>& aErrorCallback,
    nsresult aError) {
  MOZ_ASSERT(aGlobal);
  MOZ_ASSERT(NS_FAILED(aError));

  if (aErrorCallback.WasPassed()) {
    RefPtr<ErrorCallbackRunnable> runnable =
        new ErrorCallbackRunnable(aGlobal, &aErrorCallback.Value(), aError);

    FileSystemUtils::DispatchRunnable(aGlobal, runnable.forget());
  }
}

}  // namespace dom
}  // namespace mozilla