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_
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
{
24 // https://www.w3.org/TR/clipboard-apis/#clipboard-interface
25 class Clipboard
: public DOMEventTargetHelper
{
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
,
36 already_AddRefed
<Promise
> ReadText(nsIPrincipal
& aSubjectPrincipal
,
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
,
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
52 static bool ReadTextEnabled(JSContext
* aCx
, JSObject
* aGlobal
);
54 virtual JSObject
* WrapObject(JSContext
* aCx
,
55 JS::Handle
<JSObject
*> aGivenProto
) override
;
58 enum class ReadRequestType
{
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
);
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_