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 "nsStringFwd.h"
17 class nsIImageLoadingContent
;
18 class nsITransferable
;
28 } // namespace mozilla
31 // class of static helper functions for copy support
33 static nsresult
ClearSelectionCache();
36 * @param aDoc Needs to be not nullptr.
38 static nsresult
EncodeDocumentWithContextAndPutToClipboard(
39 mozilla::dom::Selection
* aSel
, mozilla::dom::Document
* aDoc
,
40 int16_t aClipboardID
, bool aWithRubyAnnotation
);
42 // Get the selection, or entire document, in the format specified by the mime
43 // type (text/html or text/plain). If aSel is non-null, use it, otherwise get
45 static nsresult
GetContents(const nsACString
& aMimeType
, uint32_t aFlags
,
46 mozilla::dom::Selection
* aSel
,
47 mozilla::dom::Document
* aDoc
, nsAString
& outdata
);
49 static nsresult
ImageCopy(nsIImageLoadingContent
* aImageElement
,
50 nsILoadContext
* aLoadContext
, int32_t aCopyFlags
,
51 mozilla::dom::WindowContext
* aSettingWindowContext
);
53 // Get the selection as a transferable.
54 // @param aSelection Can be nullptr.
55 // @param aDocument Needs to be not nullptr.
56 // @param aTransferable Needs to be not nullptr.
57 static nsresult
GetTransferableForSelection(
58 mozilla::dom::Selection
* aSelection
, mozilla::dom::Document
* aDocument
,
59 nsITransferable
** aTransferable
);
61 // Same as GetTransferableForSelection, but doesn't skip invisible content.
62 // @param aNode Needs to be not nullptr.
63 // @param aDoc Needs to be not nullptr.
64 // @param aTransferable Needs to be not nullptr.
65 MOZ_CAN_RUN_SCRIPT_BOUNDARY
66 static nsresult
GetTransferableForNode(nsINode
* aNode
,
67 mozilla::dom::Document
* aDoc
,
68 nsITransferable
** aTransferable
);
70 * Retrieve the selection for the given document. If the current focus
71 * within the document has its own selection, aSelection will be set to it
72 * and this focused content node returned. Otherwise, aSelection will be
73 * set to the document's selection and null will be returned.
75 static already_AddRefed
<mozilla::dom::Selection
> GetSelectionForCopy(
76 mozilla::dom::Document
* aDocument
);
79 * Returns true if a copy operation is currently permitted based on the
80 * current focus and selection within the specified document.
82 static bool CanCopy(mozilla::dom::Document
* aDocument
);
85 * Fires a cut, copy or paste event, on the given presshell, depending
86 * on the value of aEventMessage, which should be either eCut, eCopy or
87 * ePaste, and perform the default copy action if the event was not
90 * If aSelection is specified, then this selection is used as the target
91 * of the operation. Otherwise, GetSelectionForCopy is used to retrieve
92 * the current selection.
94 * This will fire a cut, copy or paste event at the node at the start
95 * point of the selection. If a cut or copy event is not cancelled, the
96 * selection is copied to the clipboard and true is returned. Paste events
97 * have no default behaviour but true will be returned. It is expected
98 * that the caller will execute any needed default paste behaviour. Also,
99 * note that this method only copies text to the clipboard, the caller is
100 * responsible for removing the content during a cut operation if true is
103 * aClipboardType specifies which clipboard to use, from nsIClipboard.
105 * If aActionTaken is non-NULL, it will be set to true if an action was
106 * taken, whether it be the default action or the default being prevented.
108 * If the event is cancelled or an error occurs, false will be returned.
110 MOZ_CAN_RUN_SCRIPT_BOUNDARY
111 static bool FireClipboardEvent(mozilla::EventMessage aEventMessage
,
112 int32_t aClipboardType
,
113 mozilla::PresShell
* aPresShell
,
114 mozilla::dom::Selection
* aSelection
,
115 bool* aActionTaken
= nullptr);