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 "mozilla/DOMEventTargetHelper.h"
12 #include "mozilla/Logging.h"
13 #include "mozilla/dom/DataTransfer.h"
15 namespace mozilla::dom
{
17 enum ClipboardReadType
{
25 // https://www.w3.org/TR/clipboard-apis/#clipboard-interface
26 class Clipboard
: public DOMEventTargetHelper
{
28 NS_DECL_ISUPPORTS_INHERITED
29 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(Clipboard
, DOMEventTargetHelper
)
31 IMPL_EVENT_HANDLER(message
)
32 IMPL_EVENT_HANDLER(messageerror
)
34 explicit Clipboard(nsPIDOMWindowInner
* aWindow
);
35 already_AddRefed
<Promise
> Read(nsIPrincipal
& aSubjectPrincipal
,
37 already_AddRefed
<Promise
> ReadText(nsIPrincipal
& aSubjectPrincipal
,
39 already_AddRefed
<Promise
> Write(
40 const Sequence
<OwningNonNull
<ClipboardItem
>>& aData
,
41 nsIPrincipal
& aSubjectPrincipal
, ErrorResult
& aRv
);
42 already_AddRefed
<Promise
> WriteText(const nsAString
& aData
,
43 nsIPrincipal
& aSubjectPrincipal
,
46 static LogModule
* GetClipboardLog();
48 // Check if the Clipboard.readText API should be enabled for this context.
49 // This API is only enabled for Extension and System contexts, as there is no
50 // way to request the required permission for web content. If the clipboard
51 // API testing pref is enabled, ReadText is enabled for web content for
53 static bool ReadTextEnabled(JSContext
* aCx
, JSObject
* aGlobal
);
55 virtual JSObject
* WrapObject(JSContext
* aCx
,
56 JS::Handle
<JSObject
*> aGivenProto
) override
;
59 // Checks if dom.events.testing.asyncClipboard pref is enabled.
60 // The aforementioned pref allows automated tests to bypass the security
61 // checks when writing to
62 // or reading from the clipboard.
63 static bool IsTestingPrefEnabled();
65 already_AddRefed
<Promise
> ReadHelper(nsIPrincipal
& aSubjectPrincipal
,
66 ClipboardReadType aClipboardReadType
,
69 // If necessary, fill the data transfer with data from the clipboard and
70 // resolve a promise with the appropriate object based on aClipboardReadType
71 static void ProcessDataTransfer(DataTransfer
& aDataTransfer
,
73 ClipboardReadType aClipboardReadType
,
74 nsPIDOMWindowInner
& aOwner
,
75 nsIPrincipal
& aSubjectPrincipal
,
81 } // namespace mozilla::dom
82 #endif // mozilla_dom_Clipboard_h_