FilePickerParent.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_FilePickerParent_h
  6. #define mozilla_dom_FilePickerParent_h
  7. #include "nsIEventTarget.h"
  8. #include "nsIFilePicker.h"
  9. #include "nsCOMArray.h"
  10. #include "nsThreadUtils.h"
  11. #include "mozilla/dom/File.h"
  12. #include "mozilla/dom/PFilePickerParent.h"
  13. class nsIFile;
  14. namespace mozilla {
  15. namespace dom {
  16. class FilePickerParent : public PFilePickerParent
  17. {
  18. public:
  19. FilePickerParent(const nsString& aTitle,
  20. const int16_t& aMode)
  21. : mTitle(aTitle)
  22. , mMode(aMode)
  23. {}
  24. virtual ~FilePickerParent();
  25. void Done(int16_t aResult);
  26. struct BlobImplOrString
  27. {
  28. RefPtr<BlobImpl> mBlobImpl;
  29. nsString mDirectoryPath;
  30. enum {
  31. eBlobImpl,
  32. eDirectoryPath
  33. } mType;
  34. };
  35. void SendFilesOrDirectories(const nsTArray<BlobImplOrString>& aData);
  36. virtual bool RecvOpen(const int16_t& aSelectedType,
  37. const bool& aAddToRecentDocs,
  38. const nsString& aDefaultFile,
  39. const nsString& aDefaultExtension,
  40. InfallibleTArray<nsString>&& aFilters,
  41. InfallibleTArray<nsString>&& aFilterNames,
  42. const nsString& aDisplayDirectory,
  43. const nsString& aOkButtonLabel) override;
  44. virtual void ActorDestroy(ActorDestroyReason aWhy) override;
  45. class FilePickerShownCallback : public nsIFilePickerShownCallback
  46. {
  47. public:
  48. explicit FilePickerShownCallback(FilePickerParent* aFilePickerParent)
  49. : mFilePickerParent(aFilePickerParent)
  50. { }
  51. NS_DECL_ISUPPORTS
  52. NS_DECL_NSIFILEPICKERSHOWNCALLBACK
  53. void Destroy();
  54. private:
  55. virtual ~FilePickerShownCallback() {}
  56. FilePickerParent* mFilePickerParent;
  57. };
  58. private:
  59. bool CreateFilePicker();
  60. // This runnable is used to do some I/O operation on a separate thread.
  61. class IORunnable : public Runnable
  62. {
  63. FilePickerParent* mFilePickerParent;
  64. nsTArray<nsCOMPtr<nsIFile>> mFiles;
  65. nsTArray<BlobImplOrString> mResults;
  66. nsCOMPtr<nsIEventTarget> mEventTarget;
  67. bool mIsDirectory;
  68. public:
  69. IORunnable(FilePickerParent *aFPParent,
  70. nsTArray<nsCOMPtr<nsIFile>>& aFiles,
  71. bool aIsDirectory);
  72. bool Dispatch();
  73. NS_IMETHOD Run();
  74. void Destroy();
  75. };
  76. RefPtr<IORunnable> mRunnable;
  77. RefPtr<FilePickerShownCallback> mCallback;
  78. nsCOMPtr<nsIFilePicker> mFilePicker;
  79. nsString mTitle;
  80. int16_t mMode;
  81. int16_t mResult;
  82. };
  83. } // namespace dom
  84. } // namespace mozilla
  85. #endif // mozilla_dom_FilePickerParent_h