Bug 1626988 [wpt PR 22658] - wake lock: Remove WakeLockPermissionDescriptor, use...
[gecko.git] / widget / nsClipboardHelper.cpp
blobfa4a9c9ab97b9530b9e8e13347d1deceb09ca325
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"
14 // helpers
15 #include "nsIClipboard.h"
16 #include "mozilla/dom/Document.h"
17 #include "nsITransferable.h"
18 #include "nsReadableUtils.h"
20 NS_IMPL_ISUPPORTS(nsClipboardHelper, nsIClipboardHelper)
22 /*****************************************************************************
23 * nsClipboardHelper ctor / dtor
24 *****************************************************************************/
26 nsClipboardHelper::nsClipboardHelper() = default;
28 nsClipboardHelper::~nsClipboardHelper() {
29 // no members, nothing to destroy
32 /*****************************************************************************
33 * nsIClipboardHelper methods
34 *****************************************************************************/
36 NS_IMETHODIMP
37 nsClipboardHelper::CopyStringToClipboard(const nsAString& aString,
38 int32_t aClipboardID) {
39 nsresult rv;
41 // get the clipboard
42 nsCOMPtr<nsIClipboard> clipboard(
43 do_GetService("@mozilla.org/widget/clipboard;1", &rv));
44 NS_ENSURE_SUCCESS(rv, rv);
45 NS_ENSURE_TRUE(clipboard, NS_ERROR_FAILURE);
47 bool clipboardSupported;
48 // don't go any further if they're asking for the selection
49 // clipboard on a platform which doesn't support it (i.e., unix)
50 if (nsIClipboard::kSelectionClipboard == aClipboardID) {
51 rv = clipboard->SupportsSelectionClipboard(&clipboardSupported);
52 NS_ENSURE_SUCCESS(rv, rv);
53 if (!clipboardSupported) return NS_ERROR_FAILURE;
56 // don't go any further if they're asking for the find clipboard on a platform
57 // which doesn't support it (i.e., non-osx)
58 if (nsIClipboard::kFindClipboard == aClipboardID) {
59 rv = clipboard->SupportsFindClipboard(&clipboardSupported);
60 NS_ENSURE_SUCCESS(rv, rv);
61 if (!clipboardSupported) 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);
72 // Add the text data flavor to the transferable
73 rv = trans->AddDataFlavor(kUnicodeMime);
74 NS_ENSURE_SUCCESS(rv, rv);
76 // get wStrings to hold clip data
77 nsCOMPtr<nsISupportsString> data(
78 do_CreateInstance("@mozilla.org/supports-string;1", &rv));
79 NS_ENSURE_SUCCESS(rv, rv);
80 NS_ENSURE_TRUE(data, NS_ERROR_FAILURE);
82 // populate the string
83 rv = data->SetData(aString);
84 NS_ENSURE_SUCCESS(rv, rv);
86 // qi the data object an |nsISupports| so that when the transferable holds
87 // onto it, it will addref the correct interface.
88 nsCOMPtr<nsISupports> genericData(do_QueryInterface(data, &rv));
89 NS_ENSURE_SUCCESS(rv, rv);
90 NS_ENSURE_TRUE(genericData, NS_ERROR_FAILURE);
92 // set the transfer data
93 rv = trans->SetTransferData(kUnicodeMime, genericData);
94 NS_ENSURE_SUCCESS(rv, rv);
96 // put the transferable on the clipboard
97 rv = clipboard->SetData(trans, nullptr, aClipboardID);
98 NS_ENSURE_SUCCESS(rv, rv);
100 return NS_OK;
103 NS_IMETHODIMP
104 nsClipboardHelper::CopyString(const nsAString& aString) {
105 nsresult rv;
107 // copy to the global clipboard. it's bad if this fails in any way.
108 rv = CopyStringToClipboard(aString, nsIClipboard::kGlobalClipboard);
109 NS_ENSURE_SUCCESS(rv, rv);
111 // unix also needs us to copy to the selection clipboard. this will
112 // fail in CopyStringToClipboard if we're not on a platform that
113 // supports the selection clipboard. (this could have been #ifdef
114 // XP_UNIX, but using the SupportsSelectionClipboard call is the
115 // more correct thing to do.
117 // if this fails in any way other than "not being unix", we'll get
118 // the assertion we need in CopyStringToClipboard, and we needn't
119 // assert again here.
120 CopyStringToClipboard(aString, nsIClipboard::kSelectionClipboard);
122 return NS_OK;