Bug 1792034 [wpt PR 36019] - Make location.search always expect UTF-8, a=testonly
[gecko.git] / widget / nsClipboardHelper.cpp
blob901af7159f2a74e1667fffbf25d022fef2a879df
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 bool clipboardSupported;
51 // don't go any further if they're asking for the selection
52 // clipboard on a platform which doesn't support it (i.e., unix)
53 if (nsIClipboard::kSelectionClipboard == aClipboardID) {
54 rv = clipboard->SupportsSelectionClipboard(&clipboardSupported);
55 NS_ENSURE_SUCCESS(rv, rv);
56 if (!clipboardSupported) return NS_ERROR_FAILURE;
59 // don't go any further if they're asking for the find clipboard on a platform
60 // which doesn't support it (i.e., non-osx)
61 if (nsIClipboard::kFindClipboard == aClipboardID) {
62 rv = clipboard->SupportsFindClipboard(&clipboardSupported);
63 NS_ENSURE_SUCCESS(rv, rv);
64 if (!clipboardSupported) return NS_ERROR_FAILURE;
67 // create a transferable for putting data on the clipboard
68 nsCOMPtr<nsITransferable> trans(
69 do_CreateInstance("@mozilla.org/widget/transferable;1", &rv));
70 NS_ENSURE_SUCCESS(rv, rv);
71 NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE);
73 trans->Init(nullptr);
74 if (aSensitive == SensitiveData::Sensitive) {
75 trans->SetIsPrivateData(true);
78 // Add the text data flavor to the transferable
79 rv = trans->AddDataFlavor(kUnicodeMime);
80 NS_ENSURE_SUCCESS(rv, rv);
82 // get wStrings to hold clip data
83 nsCOMPtr<nsISupportsString> data(
84 do_CreateInstance("@mozilla.org/supports-string;1", &rv));
85 NS_ENSURE_SUCCESS(rv, rv);
86 NS_ENSURE_TRUE(data, NS_ERROR_FAILURE);
88 // populate the string
89 rv = data->SetData(aString);
90 NS_ENSURE_SUCCESS(rv, rv);
92 // qi the data object an |nsISupports| so that when the transferable holds
93 // onto it, it will addref the correct interface.
94 nsCOMPtr<nsISupports> genericData(do_QueryInterface(data, &rv));
95 NS_ENSURE_SUCCESS(rv, rv);
96 NS_ENSURE_TRUE(genericData, NS_ERROR_FAILURE);
98 // set the transfer data
99 rv = trans->SetTransferData(kUnicodeMime, genericData);
100 NS_ENSURE_SUCCESS(rv, rv);
102 // put the transferable on the clipboard
103 rv = clipboard->SetData(trans, nullptr, aClipboardID);
104 NS_ENSURE_SUCCESS(rv, rv);
106 return NS_OK;
109 NS_IMETHODIMP
110 nsClipboardHelper::CopyString(const nsAString& aString,
111 SensitiveData aSensitive) {
112 nsresult rv;
114 // copy to the global clipboard. it's bad if this fails in any way.
115 rv = CopyStringToClipboard(aString, nsIClipboard::kGlobalClipboard,
116 aSensitive);
117 NS_ENSURE_SUCCESS(rv, rv);
119 // unix also needs us to copy to the selection clipboard. this will
120 // fail in CopyStringToClipboard if we're not on a platform that
121 // supports the selection clipboard. (this could have been #ifdef
122 // XP_UNIX, but using the SupportsSelectionClipboard call is the
123 // more correct thing to do.
125 // if this fails in any way other than "not being unix", we'll get
126 // the assertion we need in CopyStringToClipboard, and we needn't
127 // assert again here.
128 CopyStringToClipboard(aString, nsIClipboard::kSelectionClipboard, aSensitive);
130 return NS_OK;