Bug 1700051: part 28) Refactor `WordSplitState<T>::GetDOMWordSeparatorOffset` to...
[gecko.git] / widget / nsClipboardHelper.cpp
blob3f42800a28fd1bbc511397e1cbe806c9e35c9f0a
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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 #include "nsClipboardHelper.h"
9 // basics
10 #include "nsCOMPtr.h"
11 #include "nsXPCOM.h"
12 #include "nsISupportsPrimitives.h"
13 #include "nsServiceManagerUtils.h"
15 // helpers
16 #include "nsIClipboard.h"
17 #include "mozilla/dom/Document.h"
18 #include "nsITransferable.h"
19 #include "nsReadableUtils.h"
21 NS_IMPL_ISUPPORTS(nsClipboardHelper, nsIClipboardHelper)
23 /*****************************************************************************
24 * nsClipboardHelper ctor / dtor
25 *****************************************************************************/
27 nsClipboardHelper::nsClipboardHelper() = default;
29 nsClipboardHelper::~nsClipboardHelper() {
30 // no members, nothing to destroy
33 /*****************************************************************************
34 * nsIClipboardHelper methods
35 *****************************************************************************/
37 NS_IMETHODIMP
38 nsClipboardHelper::CopyStringToClipboard(const nsAString& aString,
39 int32_t aClipboardID) {
40 nsresult rv;
42 // get the clipboard
43 nsCOMPtr<nsIClipboard> clipboard(
44 do_GetService("@mozilla.org/widget/clipboard;1", &rv));
45 NS_ENSURE_SUCCESS(rv, rv);
46 NS_ENSURE_TRUE(clipboard, NS_ERROR_FAILURE);
48 bool clipboardSupported;
49 // don't go any further if they're asking for the selection
50 // clipboard on a platform which doesn't support it (i.e., unix)
51 if (nsIClipboard::kSelectionClipboard == aClipboardID) {
52 rv = clipboard->SupportsSelectionClipboard(&clipboardSupported);
53 NS_ENSURE_SUCCESS(rv, rv);
54 if (!clipboardSupported) return NS_ERROR_FAILURE;
57 // don't go any further if they're asking for the find clipboard on a platform
58 // which doesn't support it (i.e., non-osx)
59 if (nsIClipboard::kFindClipboard == aClipboardID) {
60 rv = clipboard->SupportsFindClipboard(&clipboardSupported);
61 NS_ENSURE_SUCCESS(rv, rv);
62 if (!clipboardSupported) return NS_ERROR_FAILURE;
65 // create a transferable for putting data on the clipboard
66 nsCOMPtr<nsITransferable> trans(
67 do_CreateInstance("@mozilla.org/widget/transferable;1", &rv));
68 NS_ENSURE_SUCCESS(rv, rv);
69 NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE);
71 trans->Init(nullptr);
73 // Add the text data flavor to the transferable
74 rv = trans->AddDataFlavor(kUnicodeMime);
75 NS_ENSURE_SUCCESS(rv, rv);
77 // get wStrings to hold clip data
78 nsCOMPtr<nsISupportsString> data(
79 do_CreateInstance("@mozilla.org/supports-string;1", &rv));
80 NS_ENSURE_SUCCESS(rv, rv);
81 NS_ENSURE_TRUE(data, NS_ERROR_FAILURE);
83 // populate the string
84 rv = data->SetData(aString);
85 NS_ENSURE_SUCCESS(rv, rv);
87 // qi the data object an |nsISupports| so that when the transferable holds
88 // onto it, it will addref the correct interface.
89 nsCOMPtr<nsISupports> genericData(do_QueryInterface(data, &rv));
90 NS_ENSURE_SUCCESS(rv, rv);
91 NS_ENSURE_TRUE(genericData, NS_ERROR_FAILURE);
93 // set the transfer data
94 rv = trans->SetTransferData(kUnicodeMime, genericData);
95 NS_ENSURE_SUCCESS(rv, rv);
97 // put the transferable on the clipboard
98 rv = clipboard->SetData(trans, nullptr, aClipboardID);
99 NS_ENSURE_SUCCESS(rv, rv);
101 return NS_OK;
104 NS_IMETHODIMP
105 nsClipboardHelper::CopyString(const nsAString& aString) {
106 nsresult rv;
108 // copy to the global clipboard. it's bad if this fails in any way.
109 rv = CopyStringToClipboard(aString, nsIClipboard::kGlobalClipboard);
110 NS_ENSURE_SUCCESS(rv, rv);
112 // unix also needs us to copy to the selection clipboard. this will
113 // fail in CopyStringToClipboard if we're not on a platform that
114 // supports the selection clipboard. (this could have been #ifdef
115 // XP_UNIX, but using the SupportsSelectionClipboard call is the
116 // more correct thing to do.
118 // if this fails in any way other than "not being unix", we'll get
119 // the assertion we need in CopyStringToClipboard, and we needn't
120 // assert again here.
121 CopyStringToClipboard(aString, nsIClipboard::kSelectionClipboard);
123 return NS_OK;