Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / events / Clipboard.h
bloba9952e60520b0b1ccd2e41d7fa7fdd8385416a0e
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"
17 class nsIAsyncGetClipboardData;
19 namespace mozilla::dom {
21 class Promise;
22 class ClipboardItem;
24 // https://www.w3.org/TR/clipboard-apis/#clipboard-interface
25 class Clipboard : public DOMEventTargetHelper {
26 public:
27 NS_DECL_ISUPPORTS_INHERITED
28 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(Clipboard, DOMEventTargetHelper)
30 IMPL_EVENT_HANDLER(message)
31 IMPL_EVENT_HANDLER(messageerror)
33 explicit Clipboard(nsPIDOMWindowInner* aWindow);
34 already_AddRefed<Promise> Read(nsIPrincipal& aSubjectPrincipal,
35 ErrorResult& aRv);
36 already_AddRefed<Promise> ReadText(nsIPrincipal& aSubjectPrincipal,
37 ErrorResult& aRv);
38 already_AddRefed<Promise> Write(
39 const Sequence<OwningNonNull<ClipboardItem>>& aData,
40 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv);
41 already_AddRefed<Promise> WriteText(const nsAString& aData,
42 nsIPrincipal& aSubjectPrincipal,
43 ErrorResult& aRv);
45 static LogModule* GetClipboardLog();
47 // Check if the Clipboard.readText API should be enabled for this context.
48 // This API is only enabled for Extension and System contexts, as there is no
49 // way to request the required permission for web content. If the clipboard
50 // API testing pref is enabled, ReadText is enabled for web content for
51 // testing purposes.
52 static bool ReadTextEnabled(JSContext* aCx, JSObject* aGlobal);
54 virtual JSObject* WrapObject(JSContext* aCx,
55 JS::Handle<JSObject*> aGivenProto) override;
57 private:
58 enum class ReadRequestType {
59 eRead,
60 eReadText,
63 // Checks if dom.events.testing.asyncClipboard pref is enabled.
64 // The aforementioned pref allows automated tests to bypass the security
65 // checks when writing to
66 // or reading from the clipboard.
67 static bool IsTestingPrefEnabled();
69 static bool IsTestingPrefEnabledOrHasReadPermission(
70 nsIPrincipal& aSubjectPrincipal);
72 already_AddRefed<Promise> ReadHelper(nsIPrincipal& aSubjectPrincipal,
73 ReadRequestType aType, ErrorResult& aRv);
75 ~Clipboard();
77 void RequestRead(Promise* aPromise, ReadRequestType aType,
78 nsPIDOMWindowInner* aOwner, nsIPrincipal& aPrincipal);
80 void RequestRead(Promise& aPromise, const ReadRequestType& aType,
81 nsPIDOMWindowInner& aOwner, nsIPrincipal& aSubjectPrincipal,
82 nsIAsyncGetClipboardData& aRequest);
85 } // namespace mozilla::dom
86 #endif // mozilla_dom_Clipboard_h_