1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "FileCreatorHelper.h"
8
9 #include "mozilla/dom/BindingDeclarations.h"
10 #include "mozilla/dom/FileBinding.h"
11 #include "mozilla/dom/FileCreatorChild.h"
12 #include "mozilla/ipc/BackgroundChild.h"
13 #include "mozilla/ipc/PBackgroundChild.h"
14 #include "mozilla/dom/File.h"
15 #include "mozilla/dom/Promise.h"
16 #include "nsContentUtils.h"
17 #include "nsPIDOMWindow.h"
18 #include "nsProxyRelease.h"
19 #include "nsIFile.h"
20
21 // Undefine the macro of CreateFile to avoid FileCreatorHelper#CreateFile being
22 // replaced by FileCreatorHelper#CreateFileW.
23 #ifdef CreateFile
24 # undef CreateFile
25 #endif
26
27 namespace mozilla {
28 namespace dom {
29
30 /* static */
CreateFile(nsIGlobalObject * aGlobalObject,nsIFile * aFile,const ChromeFilePropertyBag & aBag,bool aIsFromNsIFile,ErrorResult & aRv)31 already_AddRefed<Promise> FileCreatorHelper::CreateFile(
32 nsIGlobalObject* aGlobalObject, nsIFile* aFile,
33 const ChromeFilePropertyBag& aBag, bool aIsFromNsIFile, ErrorResult& aRv) {
34 MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
35
36 RefPtr<Promise> promise = Promise::Create(aGlobalObject, aRv);
37 if (NS_WARN_IF(aRv.Failed())) {
38 return nullptr;
39 }
40
41 nsAutoString path;
42 aRv = aFile->GetPath(path);
43 if (NS_WARN_IF(aRv.Failed())) {
44 return nullptr;
45 }
46
47 // Register this component to PBackground.
48 mozilla::ipc::PBackgroundChild* actorChild =
49 mozilla::ipc::BackgroundChild::GetOrCreateForCurrentThread();
50 if (NS_WARN_IF(!actorChild)) {
51 aRv.Throw(NS_ERROR_FAILURE);
52 return nullptr;
53 }
54
55 Maybe<int64_t> lastModified;
56 if (aBag.mLastModified.WasPassed()) {
57 lastModified.emplace(aBag.mLastModified.Value());
58 }
59
60 PFileCreatorChild* actor = actorChild->SendPFileCreatorConstructor(
61 path, aBag.mType, aBag.mName, lastModified, aBag.mExistenceCheck,
62 aIsFromNsIFile);
63
64 static_cast<FileCreatorChild*>(actor)->SetPromise(promise);
65 return promise.forget();
66 }
67
68 } // namespace dom
69 } // namespace mozilla
70