Bug 1866894 - Update failing subtest for content-visibility-auto-resize.html. r=fredw
[gecko.git] / dom / events / Clipboard.h
blobe4179c816fddd332366cdddc29c9f3b7f2b16157
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/. */
7 #ifndef mozilla_dom_Clipboard_h_
8 #define mozilla_dom_Clipboard_h_
10 #include "nsString.h"
11 #include "nsStringFwd.h"
12 #include "mozilla/DOMEventTargetHelper.h"
13 #include "mozilla/Logging.h"
14 #include "mozilla/RefPtr.h"
15 #include "mozilla/UniquePtr.h"
16 #include "mozilla/dom/DataTransfer.h"
18 namespace mozilla::dom {
20 class Promise;
21 class ClipboardItem;
23 // https://www.w3.org/TR/clipboard-apis/#clipboard-interface
24 class Clipboard : public DOMEventTargetHelper {
25 public:
26 NS_DECL_ISUPPORTS_INHERITED
27 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(Clipboard, DOMEventTargetHelper)
29 IMPL_EVENT_HANDLER(message)
30 IMPL_EVENT_HANDLER(messageerror)
32 explicit Clipboard(nsPIDOMWindowInner* aWindow);
33 already_AddRefed<Promise> Read(nsIPrincipal& aSubjectPrincipal,
34 ErrorResult& aRv);
35 already_AddRefed<Promise> ReadText(nsIPrincipal& aSubjectPrincipal,
36 ErrorResult& aRv);
37 already_AddRefed<Promise> Write(
38 const Sequence<OwningNonNull<ClipboardItem>>& aData,
39 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv);
40 already_AddRefed<Promise> WriteText(const nsAString& aData,
41 nsIPrincipal& aSubjectPrincipal,
42 ErrorResult& aRv);
44 // See documentation of the corresponding .webidl file.
45 void OnUserReactedToPasteMenuPopup(bool aAllowed);
47 static LogModule* GetClipboardLog();
49 // Check if the Clipboard.readText API should be enabled for this context.
50 // This API is only enabled for Extension and System contexts, as there is no
51 // way to request the required permission for web content. If the clipboard
52 // API testing pref is enabled, ReadText is enabled for web content for
53 // testing purposes.
54 static bool ReadTextEnabled(JSContext* aCx, JSObject* aGlobal);
56 virtual JSObject* WrapObject(JSContext* aCx,
57 JS::Handle<JSObject*> aGivenProto) override;
59 private:
60 enum class ReadRequestType {
61 eRead,
62 eReadText,
65 // Checks if dom.events.testing.asyncClipboard pref is enabled.
66 // The aforementioned pref allows automated tests to bypass the security
67 // checks when writing to
68 // or reading from the clipboard.
69 static bool IsTestingPrefEnabled();
71 static bool IsTestingPrefEnabledOrHasReadPermission(
72 nsIPrincipal& aSubjectPrincipal);
74 void CheckReadPermissionAndHandleRequest(Promise& aPromise,
75 nsIPrincipal& aSubjectPrincipal,
76 ReadRequestType aType);
78 void HandleReadRequestWhichRequiresPasteButton(Promise& aPromise,
79 ReadRequestType aType);
81 already_AddRefed<Promise> ReadHelper(nsIPrincipal& aSubjectPrincipal,
82 ReadRequestType aType, ErrorResult& aRv);
84 ~Clipboard();
86 class ReadRequest final {
87 public:
88 ReadRequest(Promise& aPromise, ReadRequestType aType,
89 nsPIDOMWindowInner& aOwner)
90 : mType(aType), mPromise(&aPromise), mOwner(&aOwner) {}
92 // Clears the request too.
93 void Answer();
95 void MaybeRejectWithNotAllowedError(const nsACString& aMessage);
97 private:
98 ReadRequestType mType;
99 // Not cycle-collected, because it's nulled when the request is answered or
100 // destructed.
101 RefPtr<Promise> mPromise;
102 RefPtr<nsPIDOMWindowInner> mOwner;
105 AutoTArray<UniquePtr<ReadRequest>, 1> mReadRequests;
107 class TransientUserPasteState final {
108 public:
109 enum class Value {
110 Initial,
111 WaitingForUserReactionToPasteMenuPopup,
112 TransientlyForbiddenByUser,
113 TransientlyAllowedByUser,
116 // @param aWindowContext requires valid transient user gesture activation.
117 Value RefreshAndGet(WindowContext& aWindowContext);
119 void OnStartWaitingForUserReactionToPasteMenuPopup(
120 const TimeStamp& aUserGestureStart);
121 void OnUserReactedToPasteMenuPopup(bool aAllowed);
123 private:
124 TimeStamp mUserGestureStart;
126 Value mValue = Value::Initial;
129 TransientUserPasteState mTransientUserPasteState;
132 } // namespace mozilla::dom
133 #endif // mozilla_dom_Clipboard_h_