summaryrefslogtreecommitdiffstats
path: root/widget/windows/filedialog/WinFileDialogParent.h
blob: a1eb54744434d89ef20d3fc5e76addd9c8df7944 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* -*- 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/. */

#ifndef widget_windows_filedialog_WinFileDialogParent_h__
#define widget_windows_filedialog_WinFileDialogParent_h__

#include "mozilla/Assertions.h"
#include "mozilla/Maybe.h"
#include "mozilla/MozPromise.h"
#include "mozilla/ProcInfo.h"
#include "mozilla/Result.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "mozilla/dom/ChromeUtilsBinding.h"
#include "mozilla/ipc/UtilityProcessParent.h"
#include "mozilla/widget/filedialog/PWinFileDialogParent.h"
#include "nsISupports.h"
#include "nsStringFwd.h"

namespace mozilla::widget::filedialog {

class WinFileDialogParent final : private PWinFileDialogParent {
 public:
  using UtilityActorName = ::mozilla::UtilityActorName;
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WinFileDialogParent, override);

  using ShowFileDialogPromise = Promise<Maybe<Results>>;
  using ShowFolderDialogPromise = Promise<Maybe<nsString>>;

 public:
  WinFileDialogParent();
  nsresult BindToUtilityProcess(
      mozilla::ipc::UtilityProcessParent* aUtilityParent);

  UtilityActorName GetActorName() {
    return UtilityActorName::WindowsFileDialog;
  }

  bool CanSend() const { return PWinFileDialogParent::CanSend(); }
  void Close() { return PWinFileDialogParent::Close(); }

  [[nodiscard]] RefPtr<ShowFileDialogPromise> ShowFileDialogImpl(
      HWND parent, const FileDialogType& type,
      mozilla::Span<Command const> commands);

  [[nodiscard]] RefPtr<ShowFolderDialogPromise> ShowFolderDialogImpl(
      HWND parent, mozilla::Span<Command const> commands);

 private:
  ~WinFileDialogParent();

  void ProcessingError(Result aCode, const char* aReason) override;
};

// Proxy for the WinFileDialog process and actor.
//
// The IPC subsystem holds a strong reference to all IPC actors, so releasing
// the last RefPtr for such an actor does not actually cause the actor to be
// destroyed. Similarly, the UtilityProcessManager owns the host process for an
// actor, and merely destroying all actors within that host process will not
// cause it to be reaped.
//
// This object, then, acts as a proxy for those objects' lifetimes: when the
// last reference to `Contents` is released, the necessary explicit cleanup of
// the actor (and, if possible, the host process) will be performed.
class ProcessProxy {
 public:
  using WFDP = WinFileDialogParent;

  explicit ProcessProxy(RefPtr<WFDP>&& obj);
  ~ProcessProxy() = default;

  explicit operator bool() const { return data->ptr && data->ptr->CanSend(); }
  bool operator!() const { return !bool(*this); }

  WFDP& operator*() const { return *data->ptr; }
  WFDP* operator->() const { return data->ptr; }
  WFDP* get() const { return data->ptr; }

  ProcessProxy(ProcessProxy const& that) = default;
  ProcessProxy(ProcessProxy&&) = default;

 private:
  struct Contents {
    NS_INLINE_DECL_REFCOUNTING(Contents);

   public:
    explicit Contents(RefPtr<WFDP>&& obj);
    RefPtr<WFDP> const ptr;

   private:
    ~Contents();
    void StopProcess();
  };
  // guaranteed nonnull
  RefPtr<Contents> data;
};

}  // namespace mozilla::widget::filedialog

#endif  // widget_windows_filedialog_WinFileDialogParent_h__