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