Bug 1826326 [wpt PR 39360] - Get rid of ResourceWarnings produced by the tests, a...
[gecko.git] / widget / nsClipboardHelper.cpp
blob9d7078b857129b6cd5ae9282f335b644a9bd6451
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 "nsComponentManagerUtils.h"
11 #include "nsCOMPtr.h"
12 #include "nsXPCOM.h"
13 #include "nsISupportsPrimitives.h"
14 #include "nsServiceManagerUtils.h"
16 // helpers
17 #include "nsIClipboard.h"
18 #include "mozilla/dom/Document.h"
19 #include "nsITransferable.h"
20 #include "nsReadableUtils.h"
22 NS_IMPL_ISUPPORTS(nsClipboardHelper, nsIClipboardHelper)
24 /*****************************************************************************
25 * nsClipboardHelper ctor / dtor
26 *****************************************************************************/
28 nsClipboardHelper::nsClipboardHelper() = default;
30 nsClipboardHelper::~nsClipboardHelper() {
31 // no members, nothing to destroy
34 /*****************************************************************************
35 * nsIClipboardHelper methods
36 *****************************************************************************/
38 NS_IMETHODIMP
39 nsClipboardHelper::CopyStringToClipboard(const nsAString& aString,
40 int32_t aClipboardID,
41 SensitiveData aSensitive) {
42 nsresult rv;
44 // get the clipboard
45 nsCOMPtr<nsIClipboard> clipboard(
46 do_GetService("@mozilla.org/widget/clipboard;1", &rv));
47 NS_ENSURE_SUCCESS(rv, rv);
48 NS_ENSURE_TRUE(clipboard, NS_ERROR_FAILURE);
50 // don't go any further if they're asking for the selection
51 // clipboard on a platform which doesn't support it (i.e., unix)
52 if (nsIClipboard::kSelectionClipboard == aClipboardID &&
53 !clipboard->IsClipboardTypeSupported(nsIClipboard::kSelectionClipboard)) {
54 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 !clipboard->IsClipboardTypeSupported(nsIClipboard::kFindClipboard)) {
61 return NS_ERROR_FAILURE;
64 // create a transferable for putting data on the clipboard
65 nsCOMPtr<nsITransferable> trans(
66 do_CreateInstance("@mozilla.org/widget/transferable;1", &rv));
67 NS_ENSURE_SUCCESS(rv, rv);
68 NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE);
70 trans->Init(nullptr);
71 if (aSensitive == SensitiveData::Sensitive) {
72 trans->SetIsPrivateData(true);
75 // Add the text data flavor to the transferable
76 rv = trans->AddDataFlavor(kTextMime);
77 NS_ENSURE_SUCCESS(rv, rv);
79 // get wStrings to hold clip data
80 nsCOMPtr<nsISupportsString> data(
81 do_CreateInstance("@mozilla.org/supports-string;1", &rv));
82 NS_ENSURE_SUCCESS(rv, rv);
83 NS_ENSURE_TRUE(data, NS_ERROR_FAILURE);
85 // populate the string
86 rv = data->SetData(aString);
87 NS_ENSURE_SUCCESS(rv, rv);
89 // qi the data object an |nsISupports| so that when the transferable holds
90 // onto it, it will addref the correct interface.
91 nsCOMPtr<nsISupports> genericData(do_QueryInterface(data, &rv));
92 NS_ENSURE_SUCCESS(rv, rv);
93 NS_ENSURE_TRUE(genericData, NS_ERROR_FAILURE);
95 // set the transfer data
96 rv = trans->SetTransferData(kTextMime, genericData);
97 NS_ENSURE_SUCCESS(rv, rv);
99 // put the transferable on the clipboard
100 rv = clipboard->SetData(trans, nullptr, aClipboardID);
101 NS_ENSURE_SUCCESS(rv, rv);
103 return NS_OK;
106 NS_IMETHODIMP
107 nsClipboardHelper::CopyString(const nsAString& aString,
108 SensitiveData aSensitive) {
109 nsresult rv;
111 // copy to the global clipboard. it's bad if this fails in any way.
112 rv = CopyStringToClipboard(aString, nsIClipboard::kGlobalClipboard,
113 aSensitive);
114 NS_ENSURE_SUCCESS(rv, rv);
116 // unix also needs us to copy to the selection clipboard. this will
117 // fail in CopyStringToClipboard if we're not on a platform that
118 // supports the selection clipboard. (this could have been #ifdef
119 // XP_UNIX, but using the IsClipboardTypeSupported call is the more correct
120 // thing to do.
122 // if this fails in any way other than "not being unix", we'll get
123 // the assertion we need in CopyStringToClipboard, and we needn't
124 // assert again here.
125 CopyStringToClipboard(aString, nsIClipboard::kSelectionClipboard, aSensitive);
127 return NS_OK;