dom/filesystem/CreateDirectoryTask.cpp

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

     1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
     2 /* vim: set ts=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/. */
     7 #include "CreateDirectoryTask.h"
     9 #include "DOMError.h"
    10 #include "mozilla/dom/Directory.h"
    11 #include "mozilla/dom/FileSystemBase.h"
    12 #include "mozilla/dom/FileSystemUtils.h"
    13 #include "mozilla/dom/Promise.h"
    14 #include "nsIFile.h"
    15 #include "nsStringGlue.h"
    17 namespace mozilla {
    18 namespace dom {
    20 CreateDirectoryTask::CreateDirectoryTask(FileSystemBase* aFileSystem,
    21                                          const nsAString& aPath)
    22   : FileSystemTaskBase(aFileSystem)
    23   , mTargetRealPath(aPath)
    24 {
    25   MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
    26   MOZ_ASSERT(aFileSystem);
    27   nsCOMPtr<nsIGlobalObject> globalObject =
    28     do_QueryInterface(aFileSystem->GetWindow());
    29   if (!globalObject) {
    30     return;
    31   }
    32   mPromise = new Promise(globalObject);
    33 }
    35 CreateDirectoryTask::CreateDirectoryTask(
    36   FileSystemBase* aFileSystem,
    37   const FileSystemCreateDirectoryParams& aParam,
    38   FileSystemRequestParent* aParent)
    39   : FileSystemTaskBase(aFileSystem, aParam, aParent)
    40 {
    41   MOZ_ASSERT(FileSystemUtils::IsParentProcess(),
    42              "Only call from parent process!");
    43   MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
    44   MOZ_ASSERT(aFileSystem);
    45   mTargetRealPath = aParam.realPath();
    46 }
    48 CreateDirectoryTask::~CreateDirectoryTask()
    49 {
    50   MOZ_ASSERT(!mPromise || NS_IsMainThread(),
    51              "mPromise should be released on main thread!");
    52 }
    54 already_AddRefed<Promise>
    55 CreateDirectoryTask::GetPromise()
    56 {
    57   MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
    58   return nsRefPtr<Promise>(mPromise).forget();
    59 }
    61 FileSystemParams
    62 CreateDirectoryTask::GetRequestParams(const nsString& aFileSystem) const
    63 {
    64   MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
    65   return FileSystemCreateDirectoryParams(aFileSystem, mTargetRealPath);
    66 }
    68 FileSystemResponseValue
    69 CreateDirectoryTask::GetSuccessRequestResult() const
    70 {
    71   MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
    72   return FileSystemDirectoryResponse(mTargetRealPath);
    73 }
    75 void
    76 CreateDirectoryTask::SetSuccessRequestResult(const FileSystemResponseValue& aValue)
    77 {
    78   MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
    79   FileSystemDirectoryResponse r = aValue;
    80   mTargetRealPath = r.realPath();
    81 }
    83 nsresult
    84 CreateDirectoryTask::Work()
    85 {
    86   MOZ_ASSERT(FileSystemUtils::IsParentProcess(),
    87              "Only call from parent process!");
    88   MOZ_ASSERT(!NS_IsMainThread(), "Only call on worker thread!");
    90   if (mFileSystem->IsShutdown()) {
    91     return NS_ERROR_FAILURE;
    92   }
    94   nsCOMPtr<nsIFile> file = mFileSystem->GetLocalFile(mTargetRealPath);
    95   if (!file) {
    96     return NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR;
    97   }
    99   bool fileExists;
   100   nsresult rv = file->Exists(&fileExists);
   101   if (NS_WARN_IF(NS_FAILED(rv))) {
   102     return rv;
   103   }
   105   if (fileExists) {
   106     return NS_ERROR_DOM_FILESYSTEM_PATH_EXISTS_ERR;
   107   }
   109   rv = file->Create(nsIFile::DIRECTORY_TYPE, 0770);
   110   return rv;
   111 }
   113 void
   114 CreateDirectoryTask::HandlerCallback()
   115 {
   116   MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
   117   if (mFileSystem->IsShutdown()) {
   118     mPromise = nullptr;
   119     return;
   120   }
   122   if (HasError()) {
   123     nsRefPtr<DOMError> domError = new DOMError(mFileSystem->GetWindow(),
   124       mErrorValue);
   125     mPromise->MaybeReject(domError);
   126     mPromise = nullptr;
   127     return;
   128   }
   129   nsRefPtr<Directory> dir = new Directory(mFileSystem, mTargetRealPath);
   130   mPromise->MaybeResolve(dir);
   131   mPromise = nullptr;
   132 }
   134 void
   135 CreateDirectoryTask::GetPermissionAccessType(nsCString& aAccess) const
   136 {
   137   aAccess.AssignLiteral("create");
   138 }
   140 } // namespace dom
   141 } // namespace mozilla

mercurial