1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsCopySupport_h__
7 #define nsCopySupport_h__
10 #include "ErrorList.h"
11 #include "mozilla/AlreadyAddRefed.h"
12 #include "mozilla/Attributes.h"
13 #include "mozilla/BasicEvents.h"
14 #include "mozilla/Maybe.h"
15 #include "nsIClipboard.h"
16 #include "nsStringFwd.h"
19 class nsIImageLoadingContent
;
20 class nsITransferable
;
31 } // namespace mozilla
34 // class of static helper functions for copy support
36 static nsresult
ClearSelectionCache();
39 * @param aDoc Needs to be not nullptr.
41 static nsresult
EncodeDocumentWithContextAndPutToClipboard(
42 mozilla::dom::Selection
* aSel
, mozilla::dom::Document
* aDoc
,
43 nsIClipboard::ClipboardType aClipboardID
, bool aWithRubyAnnotation
);
45 // Get the selection, or entire document, in the format specified by the mime
46 // type (text/html or text/plain). If aSel is non-null, use it, otherwise get
48 static nsresult
GetContents(const nsACString
& aMimeType
, uint32_t aFlags
,
49 mozilla::dom::Selection
* aSel
,
50 mozilla::dom::Document
* aDoc
, nsAString
& outdata
);
52 static nsresult
ImageCopy(nsIImageLoadingContent
* aImageElement
,
53 nsILoadContext
* aLoadContext
, int32_t aCopyFlags
,
54 mozilla::dom::WindowContext
* aSettingWindowContext
);
56 // Get the selection as a transferable.
57 // @param aSelection Can be nullptr.
58 // @param aDocument Needs to be not nullptr.
59 // @param aTransferable Needs to be not nullptr.
60 static nsresult
GetTransferableForSelection(
61 mozilla::dom::Selection
* aSelection
, mozilla::dom::Document
* aDocument
,
62 nsITransferable
** aTransferable
);
64 // Same as GetTransferableForSelection, but doesn't skip invisible content.
65 // @param aNode Needs to be not nullptr.
66 // @param aDoc Needs to be not nullptr.
67 // @param aTransferable Needs to be not nullptr.
68 MOZ_CAN_RUN_SCRIPT_BOUNDARY
69 static nsresult
GetTransferableForNode(nsINode
* aNode
,
70 mozilla::dom::Document
* aDoc
,
71 nsITransferable
** aTransferable
);
73 * Retrieve the selection for the given document. If the current focus
74 * within the document has its own selection, aSelection will be set to it
75 * and this focused content node returned. Otherwise, aSelection will be
76 * set to the document's selection and null will be returned.
78 static already_AddRefed
<mozilla::dom::Selection
> GetSelectionForCopy(
79 mozilla::dom::Document
* aDocument
);
82 * Returns true if a copy operation is currently permitted based on the
83 * current focus and selection within the specified document.
85 static bool CanCopy(mozilla::dom::Document
* aDocument
);
88 * Fires a cut, copy or paste event, on the given presshell, depending
89 * on the value of aEventMessage, which should be either eCut, eCopy or
90 * ePaste, and perform the default copy action if the event was not
93 * If aSelection is specified, then this selection is used as the target
94 * of the operation. Otherwise, GetSelectionForCopy is used to retrieve
95 * the current selection.
97 * This will fire a cut, copy or paste event at the node at the start
98 * point of the selection. If a cut or copy event is not cancelled, the
99 * selection is copied to the clipboard and true is returned. Paste events
100 * have no default behaviour but true will be returned. It is expected
101 * that the caller will execute any needed default paste behaviour. Also,
102 * note that this method only copies text to the clipboard, the caller is
103 * responsible for removing the content during a cut operation if true is
106 * aClipboardType specifies which clipboard to use, from nsIClipboard.
108 * If aDataTransfer is non-NULL, that data will be used to fire the clipboard
109 * event, and the caller is responsible for calling Disconnect(). (and
110 * possibly ClearAll())
112 * If aActionTaken is non-NULL, it will be set to true if an action was
113 * taken, whether it be the default action or the default being prevented.
115 * If the event is cancelled or an error occurs, false will be returned.
117 MOZ_CAN_RUN_SCRIPT_BOUNDARY
118 static bool FireClipboardEvent(
119 mozilla::EventMessage aEventMessage
,
120 mozilla::Maybe
<nsIClipboard::ClipboardType
> aClipboardType
,
121 mozilla::PresShell
* aPresShell
, mozilla::dom::Selection
* aSelection
,
122 mozilla::dom::DataTransfer
* aDataTransfer
, bool* aActionTaken
= nullptr);