FilePickerParent.h (3391B)
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 #ifndef mozilla_dom_FilePickerParent_h 8 #define mozilla_dom_FilePickerParent_h 9 10 #include "mozilla/dom/BrowsingContext.h" 11 #include "mozilla/dom/File.h" 12 #include "mozilla/dom/PFilePickerParent.h" 13 #include "nsCOMArray.h" 14 #include "nsIEventTarget.h" 15 #include "nsIFilePicker.h" 16 #include "nsThreadUtils.h" 17 18 class nsIFile; 19 20 namespace mozilla::dom { 21 22 class FilePickerParent : public PFilePickerParent { 23 public: 24 FilePickerParent(const nsString& aTitle, const nsIFilePicker::Mode& aMode, 25 BrowsingContext* aBrowsingContext) 26 : mTitle(aTitle), 27 mMode(aMode), 28 mBrowsingContext(aBrowsingContext), 29 mResult(nsIFilePicker::returnOK) {} 30 31 private: 32 virtual ~FilePickerParent(); 33 34 public: 35 NS_INLINE_DECL_REFCOUNTING(FilePickerParent, final) 36 37 void Done(nsIFilePicker::ResultCode aResult); 38 39 struct BlobImplOrString { 40 RefPtr<BlobImpl> mBlobImpl; 41 nsString mDirectoryPath; 42 43 enum { eBlobImpl, eDirectoryPath } mType; 44 }; 45 46 void SendFilesOrDirectories( 47 const nsTArray<BlobImplOrString>& aData, 48 const nsTArray<RefPtr<BlobImpl>>& aFilesInWebKitDirectory); 49 50 mozilla::ipc::IPCResult RecvOpen( 51 const int16_t& aSelectedType, const bool& aAddToRecentDocs, 52 const nsString& aDefaultFile, const nsString& aDefaultExtension, 53 nsTArray<nsString>&& aFilters, nsTArray<nsString>&& aFilterNames, 54 nsTArray<nsString>&& aRawFilters, const nsString& aDisplayDirectory, 55 const nsString& aDisplaySpecialDirectory, const nsString& aOkButtonLabel, 56 const nsIFilePicker::CaptureTarget& aCapture); 57 58 virtual void ActorDestroy(ActorDestroyReason aWhy) override; 59 60 class FilePickerShownCallback : public nsIFilePickerShownCallback { 61 public: 62 explicit FilePickerShownCallback(FilePickerParent* aFilePickerParent) 63 : mFilePickerParent(aFilePickerParent) {} 64 65 NS_DECL_ISUPPORTS 66 NS_DECL_NSIFILEPICKERSHOWNCALLBACK 67 68 void Destroy(); 69 70 private: 71 virtual ~FilePickerShownCallback() = default; 72 RefPtr<FilePickerParent> mFilePickerParent; 73 }; 74 75 private: 76 bool CreateFilePicker(); 77 78 // This runnable is used to do some I/O operation on a separate thread. 79 class IORunnable : public Runnable { 80 RefPtr<FilePickerParent> mFilePickerParent; 81 nsTArray<nsCOMPtr<nsIFile>> mFiles; 82 nsTArray<BlobImplOrString> mResults; 83 nsCOMPtr<nsIEventTarget> mEventTarget; 84 nsTArray<RefPtr<BlobImpl>> mFilesInWebKitDirectory; 85 bool mIsDirectory; 86 87 public: 88 IORunnable(FilePickerParent* aFPParent, 89 nsTArray<nsCOMPtr<nsIFile>>&& aFiles, 90 nsTArray<RefPtr<BlobImpl>>&& aFilesInWebKitDirectory, 91 bool aIsDirectory); 92 93 bool Dispatch(); 94 NS_IMETHOD Run() override; 95 void Destroy(); 96 }; 97 98 RefPtr<IORunnable> mRunnable; 99 RefPtr<FilePickerShownCallback> mCallback; 100 nsCOMPtr<nsIFilePicker> mFilePicker; 101 102 nsString mTitle; 103 nsIFilePicker::Mode mMode; 104 RefPtr<mozilla::dom::BrowsingContext> mBrowsingContext; 105 nsIFilePicker::ResultCode mResult; 106 }; 107 108 } // namespace mozilla::dom 109 110 #endif // mozilla_dom_FilePickerParent_h