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"
12 #include "nsISupportsPrimitives.h"
13 #include "nsIServiceManager.h"
16 #include "nsIClipboard.h"
17 #include "nsIDocument.h"
18 #include "nsIDOMDocument.h"
19 #include "nsITransferable.h"
20 #include "nsReadableUtils.h"
22 NS_IMPL_ISUPPORTS(nsClipboardHelper
, nsIClipboardHelper
)
24 /*****************************************************************************
25 * nsClipboardHelper ctor / dtor
26 *****************************************************************************/
28 nsClipboardHelper::nsClipboardHelper()
32 nsClipboardHelper::~nsClipboardHelper()
34 // no members, nothing to destroy
37 /*****************************************************************************
38 * nsIClipboardHelper methods
39 *****************************************************************************/
42 nsClipboardHelper::CopyStringToClipboard(const nsAString
& aString
,
44 nsIDOMDocument
* aDocument
)
49 nsCOMPtr
<nsIClipboard
>
50 clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv
));
51 NS_ENSURE_SUCCESS(rv
, rv
);
52 NS_ENSURE_TRUE(clipboard
, NS_ERROR_FAILURE
);
54 bool clipboardSupported
;
55 // don't go any further if they're asking for the selection
56 // clipboard on a platform which doesn't support it (i.e., unix)
57 if (nsIClipboard::kSelectionClipboard
== aClipboardID
) {
58 rv
= clipboard
->SupportsSelectionClipboard(&clipboardSupported
);
59 NS_ENSURE_SUCCESS(rv
, rv
);
60 if (!clipboardSupported
)
61 return NS_ERROR_FAILURE
;
64 // don't go any further if they're asking for the find clipboard on a platform
65 // which doesn't support it (i.e., non-osx)
66 if (nsIClipboard::kFindClipboard
== aClipboardID
) {
67 rv
= clipboard
->SupportsFindClipboard(&clipboardSupported
);
68 NS_ENSURE_SUCCESS(rv
, rv
);
69 if (!clipboardSupported
)
70 return NS_ERROR_FAILURE
;
73 // create a transferable for putting data on the clipboard
74 nsCOMPtr
<nsITransferable
>
75 trans(do_CreateInstance("@mozilla.org/widget/transferable;1", &rv
));
76 NS_ENSURE_SUCCESS(rv
, rv
);
77 NS_ENSURE_TRUE(trans
, NS_ERROR_FAILURE
);
79 nsCOMPtr
<nsIDocument
> doc
= do_QueryInterface(aDocument
);
80 nsILoadContext
* loadContext
= doc
? doc
->GetLoadContext() : nullptr;
81 trans
->Init(loadContext
);
83 // Add the text data flavor to the transferable
84 rv
= trans
->AddDataFlavor(kUnicodeMime
);
85 NS_ENSURE_SUCCESS(rv
, rv
);
87 // get wStrings to hold clip data
88 nsCOMPtr
<nsISupportsString
>
89 data(do_CreateInstance("@mozilla.org/supports-string;1", &rv
));
90 NS_ENSURE_SUCCESS(rv
, rv
);
91 NS_ENSURE_TRUE(data
, NS_ERROR_FAILURE
);
93 // populate the string
94 rv
= data
->SetData(aString
);
95 NS_ENSURE_SUCCESS(rv
, rv
);
97 // qi the data object an |nsISupports| so that when the transferable holds
98 // onto it, it will addref the correct interface.
99 nsCOMPtr
<nsISupports
> genericData(do_QueryInterface(data
, &rv
));
100 NS_ENSURE_SUCCESS(rv
, rv
);
101 NS_ENSURE_TRUE(genericData
, NS_ERROR_FAILURE
);
103 // set the transfer data
104 rv
= trans
->SetTransferData(kUnicodeMime
, genericData
,
105 aString
.Length() * 2);
106 NS_ENSURE_SUCCESS(rv
, rv
);
108 // put the transferable on the clipboard
109 rv
= clipboard
->SetData(trans
, nullptr, aClipboardID
);
110 NS_ENSURE_SUCCESS(rv
, rv
);
116 nsClipboardHelper::CopyString(const nsAString
& aString
, nsIDOMDocument
* aDocument
)
120 // copy to the global clipboard. it's bad if this fails in any way.
121 rv
= CopyStringToClipboard(aString
, nsIClipboard::kGlobalClipboard
, aDocument
);
122 NS_ENSURE_SUCCESS(rv
, rv
);
124 // unix also needs us to copy to the selection clipboard. this will
125 // fail in CopyStringToClipboard if we're not on a platform that
126 // supports the selection clipboard. (this could have been #ifdef
127 // XP_UNIX, but using the SupportsSelectionClipboard call is the
128 // more correct thing to do.
130 // if this fails in any way other than "not being unix", we'll get
131 // the assertion we need in CopyStringToClipboard, and we needn't
132 // assert again here.
133 CopyStringToClipboard(aString
, nsIClipboard::kSelectionClipboard
, aDocument
);