nsCopySupport.h (5065B)
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/. */ 5 6 #ifndef nsCopySupport_h__ 7 #define nsCopySupport_h__ 8 9 #include <cstdint> 10 11 #include "ErrorList.h" 12 #include "mozilla/AlreadyAddRefed.h" 13 #include "mozilla/Attributes.h" 14 #include "mozilla/BasicEvents.h" 15 #include "mozilla/Maybe.h" 16 #include "nsIClipboard.h" 17 #include "nsStringFwd.h" 18 19 class nsINode; 20 class nsIImageLoadingContent; 21 class nsITransferable; 22 class nsILoadContext; 23 24 namespace mozilla { 25 class PresShell; 26 namespace dom { 27 class DataTransfer; 28 class Document; 29 class Selection; 30 class WindowContext; 31 } // namespace dom 32 } // namespace mozilla 33 34 class nsCopySupport { 35 // class of static helper functions for copy support 36 public: 37 static nsresult ClearSelectionCache(); 38 39 /** 40 * @param aDoc Needs to be not nullptr. 41 */ 42 static nsresult EncodeDocumentWithContextAndPutToClipboard( 43 mozilla::dom::Selection* aSel, mozilla::dom::Document* aDoc, 44 nsIClipboard::ClipboardType aClipboardID, bool aWithRubyAnnotation); 45 46 // Get the selection, or entire document, in the format specified by the mime 47 // type (text/html or text/plain). If aSel is non-null, use it, otherwise get 48 // the entire doc. 49 static nsresult GetContents(const nsACString& aMimeType, uint32_t aFlags, 50 mozilla::dom::Selection* aSel, 51 mozilla::dom::Document* aDoc, nsAString& outdata); 52 53 static nsresult ImageCopy(nsIImageLoadingContent* aImageElement, 54 nsILoadContext* aLoadContext, int32_t aCopyFlags, 55 mozilla::dom::WindowContext* aSettingWindowContext); 56 57 // Get the selection as a transferable. 58 // @param aSelection Can be nullptr. 59 // @param aDocument Needs to be not nullptr. 60 // @param aTransferable Needs to be not nullptr. 61 static nsresult GetTransferableForSelection( 62 mozilla::dom::Selection* aSelection, mozilla::dom::Document* aDocument, 63 nsITransferable** aTransferable); 64 65 // Same as GetTransferableForSelection, but doesn't skip invisible content. 66 // @param aNode Needs to be not nullptr. 67 // @param aDoc Needs to be not nullptr. 68 // @param aTransferable Needs to be not nullptr. 69 MOZ_CAN_RUN_SCRIPT_BOUNDARY 70 static nsresult GetTransferableForNode(nsINode* aNode, 71 mozilla::dom::Document* aDoc, 72 nsITransferable** aTransferable); 73 /** 74 * Retrieve the selection for the given document. If the current focus 75 * within the document has its own selection, aSelection will be set to it 76 * and this focused content node returned. Otherwise, aSelection will be 77 * set to the document's selection and null will be returned. 78 */ 79 static already_AddRefed<mozilla::dom::Selection> GetSelectionForCopy( 80 mozilla::dom::Document* aDocument); 81 82 /** 83 * Returns true if a copy operation is currently permitted based on the 84 * current focus and selection within the specified document. 85 */ 86 static bool CanCopy(mozilla::dom::Document* aDocument); 87 88 /** 89 * Fires a cut, copy or paste event, on the given presshell, depending 90 * on the value of aEventMessage, which should be either eCut, eCopy or 91 * ePaste, and perform the default copy action if the event was not 92 * cancelled. 93 * 94 * If aSelection is specified, then this selection is used as the target 95 * of the operation. Otherwise, GetSelectionForCopy is used to retrieve 96 * the current selection. 97 * 98 * This will fire a cut, copy or paste event at the node at the start 99 * point of the selection. If a cut or copy event is not cancelled, the 100 * selection is copied to the clipboard and true is returned. Paste events 101 * have no default behaviour but true will be returned. It is expected 102 * that the caller will execute any needed default paste behaviour. Also, 103 * note that this method only copies text to the clipboard, the caller is 104 * responsible for removing the content during a cut operation if true is 105 * returned. 106 * 107 * aClipboardType specifies which clipboard to use, from nsIClipboard. 108 * 109 * If aDataTransfer is non-NULL, that data will be used to fire the clipboard 110 * event, and the caller is responsible for calling Disconnect(). (and 111 * possibly ClearAll()) 112 * 113 * If aActionTaken is non-NULL, it will be set to true if an action was 114 * taken, whether it be the default action or the default being prevented. 115 * 116 * If the event is cancelled or an error occurs, false will be returned. 117 */ 118 MOZ_CAN_RUN_SCRIPT_BOUNDARY 119 static bool FireClipboardEvent( 120 mozilla::EventMessage aEventMessage, 121 mozilla::Maybe<nsIClipboard::ClipboardType> aClipboardType, 122 mozilla::PresShell* aPresShell, mozilla::dom::Selection* aSelection, 123 mozilla::dom::DataTransfer* aDataTransfer, bool* aActionTaken = nullptr); 124 }; 125 126 #endif