/src/mozilla-central/widget/nsClipboardHelper.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
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/. */ |
6 | | |
7 | | #include "nsClipboardHelper.h" |
8 | | |
9 | | // basics |
10 | | #include "nsCOMPtr.h" |
11 | | #include "nsXPCOM.h" |
12 | | #include "nsISupportsPrimitives.h" |
13 | | #include "nsIServiceManager.h" |
14 | | |
15 | | // helpers |
16 | | #include "nsIClipboard.h" |
17 | | #include "nsIDocument.h" |
18 | | #include "nsITransferable.h" |
19 | | #include "nsReadableUtils.h" |
20 | | |
21 | | NS_IMPL_ISUPPORTS(nsClipboardHelper, nsIClipboardHelper) |
22 | | |
23 | | /***************************************************************************** |
24 | | * nsClipboardHelper ctor / dtor |
25 | | *****************************************************************************/ |
26 | | |
27 | | nsClipboardHelper::nsClipboardHelper() |
28 | 0 | { |
29 | 0 | } |
30 | | |
31 | | nsClipboardHelper::~nsClipboardHelper() |
32 | 0 | { |
33 | 0 | // no members, nothing to destroy |
34 | 0 | } |
35 | | |
36 | | /***************************************************************************** |
37 | | * nsIClipboardHelper methods |
38 | | *****************************************************************************/ |
39 | | |
40 | | NS_IMETHODIMP |
41 | | nsClipboardHelper::CopyStringToClipboard(const nsAString& aString, |
42 | | int32_t aClipboardID) |
43 | 0 | { |
44 | 0 | nsresult rv; |
45 | 0 |
|
46 | 0 | // get the clipboard |
47 | 0 | nsCOMPtr<nsIClipboard> |
48 | 0 | clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv)); |
49 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
50 | 0 | NS_ENSURE_TRUE(clipboard, NS_ERROR_FAILURE); |
51 | 0 |
|
52 | 0 | bool clipboardSupported; |
53 | 0 | // don't go any further if they're asking for the selection |
54 | 0 | // clipboard on a platform which doesn't support it (i.e., unix) |
55 | 0 | if (nsIClipboard::kSelectionClipboard == aClipboardID) { |
56 | 0 | rv = clipboard->SupportsSelectionClipboard(&clipboardSupported); |
57 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
58 | 0 | if (!clipboardSupported) |
59 | 0 | return NS_ERROR_FAILURE; |
60 | 0 | } |
61 | 0 | |
62 | 0 | // don't go any further if they're asking for the find clipboard on a platform |
63 | 0 | // which doesn't support it (i.e., non-osx) |
64 | 0 | if (nsIClipboard::kFindClipboard == aClipboardID) { |
65 | 0 | rv = clipboard->SupportsFindClipboard(&clipboardSupported); |
66 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
67 | 0 | if (!clipboardSupported) |
68 | 0 | return NS_ERROR_FAILURE; |
69 | 0 | } |
70 | 0 | |
71 | 0 | // create a transferable for putting data on the clipboard |
72 | 0 | nsCOMPtr<nsITransferable> |
73 | 0 | trans(do_CreateInstance("@mozilla.org/widget/transferable;1", &rv)); |
74 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
75 | 0 | NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE); |
76 | 0 |
|
77 | 0 | trans->Init(nullptr); |
78 | 0 |
|
79 | 0 | // Add the text data flavor to the transferable |
80 | 0 | rv = trans->AddDataFlavor(kUnicodeMime); |
81 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
82 | 0 |
|
83 | 0 | // get wStrings to hold clip data |
84 | 0 | nsCOMPtr<nsISupportsString> |
85 | 0 | data(do_CreateInstance("@mozilla.org/supports-string;1", &rv)); |
86 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
87 | 0 | NS_ENSURE_TRUE(data, NS_ERROR_FAILURE); |
88 | 0 |
|
89 | 0 | // populate the string |
90 | 0 | rv = data->SetData(aString); |
91 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
92 | 0 |
|
93 | 0 | // qi the data object an |nsISupports| so that when the transferable holds |
94 | 0 | // onto it, it will addref the correct interface. |
95 | 0 | nsCOMPtr<nsISupports> genericData(do_QueryInterface(data, &rv)); |
96 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
97 | 0 | NS_ENSURE_TRUE(genericData, NS_ERROR_FAILURE); |
98 | 0 |
|
99 | 0 | // set the transfer data |
100 | 0 | rv = trans->SetTransferData(kUnicodeMime, genericData, |
101 | 0 | aString.Length() * 2); |
102 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
103 | 0 |
|
104 | 0 | // put the transferable on the clipboard |
105 | 0 | rv = clipboard->SetData(trans, nullptr, aClipboardID); |
106 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
107 | 0 |
|
108 | 0 | return NS_OK; |
109 | 0 | } |
110 | | |
111 | | NS_IMETHODIMP |
112 | | nsClipboardHelper::CopyString(const nsAString& aString) |
113 | 0 | { |
114 | 0 | nsresult rv; |
115 | 0 |
|
116 | 0 | // copy to the global clipboard. it's bad if this fails in any way. |
117 | 0 | rv = CopyStringToClipboard(aString, nsIClipboard::kGlobalClipboard); |
118 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
119 | 0 |
|
120 | 0 | // unix also needs us to copy to the selection clipboard. this will |
121 | 0 | // fail in CopyStringToClipboard if we're not on a platform that |
122 | 0 | // supports the selection clipboard. (this could have been #ifdef |
123 | 0 | // XP_UNIX, but using the SupportsSelectionClipboard call is the |
124 | 0 | // more correct thing to do. |
125 | 0 | // |
126 | 0 | // if this fails in any way other than "not being unix", we'll get |
127 | 0 | // the assertion we need in CopyStringToClipboard, and we needn't |
128 | 0 | // assert again here. |
129 | 0 | CopyStringToClipboard(aString, nsIClipboard::kSelectionClipboard); |
130 | 0 |
|
131 | 0 | return NS_OK; |
132 | 0 | } |