Bug 1887677: Correctly compute inClass predicate when validating unbound private...
[gecko.git] / widget / nsClipboardHelper.cpp
blob3439adef48370c9b2aa7b41af5ab78639325b04f
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 // Pass the data object as |nsISupports| so that when the transferable holds
90 // onto it, it will addref the correct interface.
91 rv = trans->SetTransferData(kTextMime, ToSupports(data));
92 NS_ENSURE_SUCCESS(rv, rv);
94 // put the transferable on the clipboard
95 rv = clipboard->SetData(trans, nullptr, aClipboardID);
96 NS_ENSURE_SUCCESS(rv, rv);
98 return NS_OK;
101 NS_IMETHODIMP
102 nsClipboardHelper::CopyString(const nsAString& aString,
103 SensitiveData aSensitive) {
104 nsresult rv;
106 // copy to the global clipboard. it's bad if this fails in any way.
107 rv = CopyStringToClipboard(aString, nsIClipboard::kGlobalClipboard,
108 aSensitive);
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 IsClipboardTypeSupported call is the more correct
115 // 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, aSensitive);
122 return NS_OK;