dom/filesystem/CreateDirectoryTask.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/filesystem/CreateDirectoryTask.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,141 @@
     1.4 +/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "CreateDirectoryTask.h"
    1.11 +
    1.12 +#include "DOMError.h"
    1.13 +#include "mozilla/dom/Directory.h"
    1.14 +#include "mozilla/dom/FileSystemBase.h"
    1.15 +#include "mozilla/dom/FileSystemUtils.h"
    1.16 +#include "mozilla/dom/Promise.h"
    1.17 +#include "nsIFile.h"
    1.18 +#include "nsStringGlue.h"
    1.19 +
    1.20 +namespace mozilla {
    1.21 +namespace dom {
    1.22 +
    1.23 +CreateDirectoryTask::CreateDirectoryTask(FileSystemBase* aFileSystem,
    1.24 +                                         const nsAString& aPath)
    1.25 +  : FileSystemTaskBase(aFileSystem)
    1.26 +  , mTargetRealPath(aPath)
    1.27 +{
    1.28 +  MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
    1.29 +  MOZ_ASSERT(aFileSystem);
    1.30 +  nsCOMPtr<nsIGlobalObject> globalObject =
    1.31 +    do_QueryInterface(aFileSystem->GetWindow());
    1.32 +  if (!globalObject) {
    1.33 +    return;
    1.34 +  }
    1.35 +  mPromise = new Promise(globalObject);
    1.36 +}
    1.37 +
    1.38 +CreateDirectoryTask::CreateDirectoryTask(
    1.39 +  FileSystemBase* aFileSystem,
    1.40 +  const FileSystemCreateDirectoryParams& aParam,
    1.41 +  FileSystemRequestParent* aParent)
    1.42 +  : FileSystemTaskBase(aFileSystem, aParam, aParent)
    1.43 +{
    1.44 +  MOZ_ASSERT(FileSystemUtils::IsParentProcess(),
    1.45 +             "Only call from parent process!");
    1.46 +  MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
    1.47 +  MOZ_ASSERT(aFileSystem);
    1.48 +  mTargetRealPath = aParam.realPath();
    1.49 +}
    1.50 +
    1.51 +CreateDirectoryTask::~CreateDirectoryTask()
    1.52 +{
    1.53 +  MOZ_ASSERT(!mPromise || NS_IsMainThread(),
    1.54 +             "mPromise should be released on main thread!");
    1.55 +}
    1.56 +
    1.57 +already_AddRefed<Promise>
    1.58 +CreateDirectoryTask::GetPromise()
    1.59 +{
    1.60 +  MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
    1.61 +  return nsRefPtr<Promise>(mPromise).forget();
    1.62 +}
    1.63 +
    1.64 +FileSystemParams
    1.65 +CreateDirectoryTask::GetRequestParams(const nsString& aFileSystem) const
    1.66 +{
    1.67 +  MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
    1.68 +  return FileSystemCreateDirectoryParams(aFileSystem, mTargetRealPath);
    1.69 +}
    1.70 +
    1.71 +FileSystemResponseValue
    1.72 +CreateDirectoryTask::GetSuccessRequestResult() const
    1.73 +{
    1.74 +  MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
    1.75 +  return FileSystemDirectoryResponse(mTargetRealPath);
    1.76 +}
    1.77 +
    1.78 +void
    1.79 +CreateDirectoryTask::SetSuccessRequestResult(const FileSystemResponseValue& aValue)
    1.80 +{
    1.81 +  MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
    1.82 +  FileSystemDirectoryResponse r = aValue;
    1.83 +  mTargetRealPath = r.realPath();
    1.84 +}
    1.85 +
    1.86 +nsresult
    1.87 +CreateDirectoryTask::Work()
    1.88 +{
    1.89 +  MOZ_ASSERT(FileSystemUtils::IsParentProcess(),
    1.90 +             "Only call from parent process!");
    1.91 +  MOZ_ASSERT(!NS_IsMainThread(), "Only call on worker thread!");
    1.92 +
    1.93 +  if (mFileSystem->IsShutdown()) {
    1.94 +    return NS_ERROR_FAILURE;
    1.95 +  }
    1.96 +
    1.97 +  nsCOMPtr<nsIFile> file = mFileSystem->GetLocalFile(mTargetRealPath);
    1.98 +  if (!file) {
    1.99 +    return NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR;
   1.100 +  }
   1.101 +
   1.102 +  bool fileExists;
   1.103 +  nsresult rv = file->Exists(&fileExists);
   1.104 +  if (NS_WARN_IF(NS_FAILED(rv))) {
   1.105 +    return rv;
   1.106 +  }
   1.107 +
   1.108 +  if (fileExists) {
   1.109 +    return NS_ERROR_DOM_FILESYSTEM_PATH_EXISTS_ERR;
   1.110 +  }
   1.111 +
   1.112 +  rv = file->Create(nsIFile::DIRECTORY_TYPE, 0770);
   1.113 +  return rv;
   1.114 +}
   1.115 +
   1.116 +void
   1.117 +CreateDirectoryTask::HandlerCallback()
   1.118 +{
   1.119 +  MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
   1.120 +  if (mFileSystem->IsShutdown()) {
   1.121 +    mPromise = nullptr;
   1.122 +    return;
   1.123 +  }
   1.124 +
   1.125 +  if (HasError()) {
   1.126 +    nsRefPtr<DOMError> domError = new DOMError(mFileSystem->GetWindow(),
   1.127 +      mErrorValue);
   1.128 +    mPromise->MaybeReject(domError);
   1.129 +    mPromise = nullptr;
   1.130 +    return;
   1.131 +  }
   1.132 +  nsRefPtr<Directory> dir = new Directory(mFileSystem, mTargetRealPath);
   1.133 +  mPromise->MaybeResolve(dir);
   1.134 +  mPromise = nullptr;
   1.135 +}
   1.136 +
   1.137 +void
   1.138 +CreateDirectoryTask::GetPermissionAccessType(nsCString& aAccess) const
   1.139 +{
   1.140 +  aAccess.AssignLiteral("create");
   1.141 +}
   1.142 +
   1.143 +} // namespace dom
   1.144 +} // namespace mozilla

mercurial