2017-02-08 12:19:01 +03:00
|
|
|
/* -*- 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 "FileCreatorHelper.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
|
|
|
#include "mozilla/dom/FileBinding.h"
|
2019-04-26 21:00:33 +03:00
|
|
|
#include "mozilla/dom/FileCreatorChild.h"
|
2019-04-26 20:53:07 +03:00
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundChild.h"
|
2017-02-08 12:19:01 +03:00
|
|
|
#include "mozilla/dom/File.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2019-04-26 20:53:07 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2017-02-08 12:19:01 +03:00
|
|
|
#include "nsIFile.h"
|
|
|
|
|
|
|
|
// Undefine the macro of CreateFile to avoid FileCreatorHelper#CreateFile being
|
|
|
|
// replaced by FileCreatorHelper#CreateFileW.
|
|
|
|
#ifdef CreateFile
|
|
|
|
# undef CreateFile
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<Promise> FileCreatorHelper::CreateFile(
|
2017-02-08 16:34:42 +03:00
|
|
|
nsIGlobalObject* aGlobalObject, nsIFile* aFile,
|
2017-02-08 12:19:01 +03:00
|
|
|
const ChromeFilePropertyBag& aBag, bool aIsFromNsIFile, ErrorResult& aRv) {
|
2017-02-23 05:23:51 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
|
2017-02-08 12:19:01 +03:00
|
|
|
|
2017-02-08 16:34:42 +03:00
|
|
|
RefPtr<Promise> promise = Promise::Create(aGlobalObject, aRv);
|
2017-02-08 12:19:01 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-04-25 12:36:51 +03:00
|
|
|
nsAutoString path;
|
|
|
|
aRv = aFile->GetPath(path);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2019-04-26 20:53:07 +03:00
|
|
|
return nullptr;
|
2019-04-25 12:36:51 +03:00
|
|
|
}
|
|
|
|
|
2019-04-26 20:53:07 +03:00
|
|
|
// Register this component to PBackground.
|
|
|
|
mozilla::ipc::PBackgroundChild* actorChild =
|
|
|
|
mozilla::ipc::BackgroundChild::GetOrCreateForCurrentThread();
|
|
|
|
if (NS_WARN_IF(!actorChild)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
2019-04-25 12:36:51 +03:00
|
|
|
}
|
|
|
|
|
2019-04-26 20:53:07 +03:00
|
|
|
Maybe<int64_t> lastModified;
|
|
|
|
if (aBag.mLastModified.WasPassed()) {
|
|
|
|
lastModified.emplace(aBag.mLastModified.Value());
|
2019-04-25 12:36:51 +03:00
|
|
|
}
|
|
|
|
|
2019-04-26 21:00:33 +03:00
|
|
|
PFileCreatorChild* actor = actorChild->SendPFileCreatorConstructor(
|
|
|
|
path, aBag.mType, aBag.mName, lastModified, aBag.mExistenceCheck,
|
|
|
|
aIsFromNsIFile);
|
2019-04-25 12:36:51 +03:00
|
|
|
|
2019-04-26 20:53:07 +03:00
|
|
|
static_cast<FileCreatorChild*>(actor)->SetPromise(promise);
|
|
|
|
return promise.forget();
|
2017-02-08 12:19:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|