Merge mozilla-central to autoland. CLOSED TREE
[gecko.git] / widget / nsClipboardHelper.cpp
blob87e697a4c640548e9b07183a45ad2efc4546a59e
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(
40 const nsAString& aString, int32_t aClipboardID,
41 mozilla::dom::WindowContext* aSettingWindowContext,
42 SensitiveData aSensitive) {
43 nsresult rv;
45 // get the clipboard
46 nsCOMPtr<nsIClipboard> clipboard(
47 do_GetService("@mozilla.org/widget/clipboard;1", &rv));
48 NS_ENSURE_SUCCESS(rv, rv);
49 NS_ENSURE_TRUE(clipboard, NS_ERROR_FAILURE);
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 !clipboard->IsClipboardTypeSupported(nsIClipboard::kSelectionClipboard)) {
55 return NS_ERROR_FAILURE;
58 // don't go any further if they're asking for the find clipboard on a platform
59 // which doesn't support it (i.e., non-osx)
60 if (nsIClipboard::kFindClipboard == aClipboardID &&
61 !clipboard->IsClipboardTypeSupported(nsIClipboard::kFindClipboard)) {
62 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);
72 if (aSensitive == SensitiveData::Sensitive) {
73 trans->SetIsPrivateData(true);
76 // Add the text data flavor to the transferable
77 rv = trans->AddDataFlavor(kTextMime);
78 NS_ENSURE_SUCCESS(rv, rv);
80 // get wStrings to hold clip data
81 nsCOMPtr<nsISupportsString> data(
82 do_CreateInstance("@mozilla.org/supports-string;1", &rv));
83 NS_ENSURE_SUCCESS(rv, rv);
84 NS_ENSURE_TRUE(data, NS_ERROR_FAILURE);
86 // populate the string
87 rv = data->SetData(aString);
88 NS_ENSURE_SUCCESS(rv, rv);
90 // Pass the data object as |nsISupports| so that when the transferable holds
91 // onto it, it will addref the correct interface.
92 rv = trans->SetTransferData(kTextMime, ToSupports(data));
93 NS_ENSURE_SUCCESS(rv, rv);
95 // put the transferable on the clipboard
96 rv = clipboard->SetData(trans, nullptr, aClipboardID, aSettingWindowContext);
97 NS_ENSURE_SUCCESS(rv, rv);
99 return NS_OK;
102 NS_IMETHODIMP
103 nsClipboardHelper::CopyString(
104 const nsAString& aString,
105 mozilla::dom::WindowContext* aSettingWindowContext,
106 SensitiveData aSensitive) {
107 nsresult rv;
109 // copy to the global clipboard. it's bad if this fails in any way.
110 rv = CopyStringToClipboard(aString, nsIClipboard::kGlobalClipboard,
111 aSettingWindowContext, aSensitive);
112 NS_ENSURE_SUCCESS(rv, rv);
114 // unix also needs us to copy to the selection clipboard. this will
115 // fail in CopyStringToClipboard if we're not on a platform that
116 // supports the selection clipboard. (this could have been #ifdef
117 // XP_UNIX, but using the IsClipboardTypeSupported call is the more correct
118 // thing to do.
120 // if this fails in any way other than "not being unix", we'll get
121 // the assertion we need in CopyStringToClipboard, and we needn't
122 // assert again here.
123 CopyStringToClipboard(aString, nsIClipboard::kSelectionClipboard,
124 aSettingWindowContext, aSensitive);
126 return NS_OK;