|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "mozilla/dom/ContentChild.h" |
|
6 #include "nsClipboard.h" |
|
7 #include "nsISupportsPrimitives.h" |
|
8 #include "nsCOMPtr.h" |
|
9 #include "nsComponentManagerUtils.h" |
|
10 #include "nsXULAppAPI.h" |
|
11 |
|
12 using namespace mozilla; |
|
13 using mozilla::dom::ContentChild; |
|
14 |
|
15 #define LOG_TAG "Clipboard" |
|
16 #define LOGI(args...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, ## args) |
|
17 #define LOGE(args...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, ## args) |
|
18 |
|
19 NS_IMPL_ISUPPORTS(nsClipboard, nsIClipboard) |
|
20 |
|
21 nsClipboard::nsClipboard() |
|
22 { |
|
23 } |
|
24 |
|
25 NS_IMETHODIMP |
|
26 nsClipboard::SetData(nsITransferable *aTransferable, |
|
27 nsIClipboardOwner *anOwner, int32_t aWhichClipboard) |
|
28 { |
|
29 if (aWhichClipboard != kGlobalClipboard) { |
|
30 return NS_ERROR_NOT_IMPLEMENTED; |
|
31 } |
|
32 |
|
33 nsCOMPtr<nsISupports> tmp; |
|
34 uint32_t len; |
|
35 nsresult rv = aTransferable->GetTransferData(kUnicodeMime, getter_AddRefs(tmp), |
|
36 &len); |
|
37 if (NS_WARN_IF(NS_FAILED(rv))) { |
|
38 return rv; |
|
39 } |
|
40 nsCOMPtr<nsISupportsString> supportsString = do_QueryInterface(tmp); |
|
41 // No support for non-text data |
|
42 if (NS_WARN_IF(!supportsString)) { |
|
43 LOGE("No support for non-text data. See bug 952456."); |
|
44 return NS_ERROR_NOT_IMPLEMENTED; |
|
45 } |
|
46 nsAutoString buffer; |
|
47 supportsString->GetData(buffer); |
|
48 |
|
49 if (XRE_GetProcessType() == GeckoProcessType_Default) { |
|
50 mClipboard = buffer; |
|
51 } else { |
|
52 bool isPrivateData = false; |
|
53 aTransferable->GetIsPrivateData(&isPrivateData); |
|
54 ContentChild::GetSingleton()->SendSetClipboardText(buffer, isPrivateData, |
|
55 aWhichClipboard); |
|
56 } |
|
57 |
|
58 return NS_OK; |
|
59 } |
|
60 |
|
61 NS_IMETHODIMP |
|
62 nsClipboard::GetData(nsITransferable *aTransferable, int32_t aWhichClipboard) |
|
63 { |
|
64 if (aWhichClipboard != kGlobalClipboard) { |
|
65 return NS_ERROR_NOT_IMPLEMENTED; |
|
66 } |
|
67 |
|
68 nsAutoString buffer; |
|
69 if (XRE_GetProcessType() == GeckoProcessType_Default) { |
|
70 buffer = mClipboard; |
|
71 } else { |
|
72 ContentChild::GetSingleton()->SendGetClipboardText(aWhichClipboard, &buffer); |
|
73 } |
|
74 |
|
75 nsresult rv; |
|
76 nsCOMPtr<nsISupportsString> dataWrapper = |
|
77 do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv); |
|
78 if (NS_WARN_IF(NS_FAILED(rv))) { |
|
79 return rv; |
|
80 } |
|
81 |
|
82 rv = dataWrapper->SetData(buffer); |
|
83 if (NS_WARN_IF(NS_FAILED(rv))) { |
|
84 return rv; |
|
85 } |
|
86 |
|
87 // If our data flavor has already been added, this will fail. But we don't care |
|
88 aTransferable->AddDataFlavor(kUnicodeMime); |
|
89 |
|
90 nsCOMPtr<nsISupports> nsisupportsDataWrapper = |
|
91 do_QueryInterface(dataWrapper); |
|
92 rv = aTransferable->SetTransferData(kUnicodeMime, nsisupportsDataWrapper, |
|
93 buffer.Length() * sizeof(PRUnichar)); |
|
94 if (NS_WARN_IF(NS_FAILED(rv))) { |
|
95 return rv; |
|
96 } |
|
97 |
|
98 return NS_OK; |
|
99 } |
|
100 |
|
101 NS_IMETHODIMP |
|
102 nsClipboard::EmptyClipboard(int32_t aWhichClipboard) |
|
103 { |
|
104 if (aWhichClipboard != kGlobalClipboard) { |
|
105 return NS_ERROR_NOT_IMPLEMENTED; |
|
106 } |
|
107 if (XRE_GetProcessType() == GeckoProcessType_Default) { |
|
108 mClipboard.Truncate(0); |
|
109 } else { |
|
110 ContentChild::GetSingleton()->SendEmptyClipboard(aWhichClipboard); |
|
111 } |
|
112 |
|
113 return NS_OK; |
|
114 } |
|
115 |
|
116 NS_IMETHODIMP |
|
117 nsClipboard::HasDataMatchingFlavors(const char **aFlavorList, |
|
118 uint32_t aLength, int32_t aWhichClipboard, |
|
119 bool *aHasText) |
|
120 { |
|
121 *aHasText = false; |
|
122 if (aWhichClipboard != kGlobalClipboard) { |
|
123 return NS_ERROR_NOT_IMPLEMENTED; |
|
124 } |
|
125 if (XRE_GetProcessType() == GeckoProcessType_Default) { |
|
126 *aHasText = !mClipboard.IsEmpty(); |
|
127 } else { |
|
128 ContentChild::GetSingleton()->SendClipboardHasText(aWhichClipboard, aHasText); |
|
129 } |
|
130 return NS_OK; |
|
131 } |
|
132 |
|
133 NS_IMETHODIMP |
|
134 nsClipboard::SupportsSelectionClipboard(bool *aIsSupported) |
|
135 { |
|
136 *aIsSupported = false; |
|
137 return NS_OK; |
|
138 } |
|
139 |
|
140 NS_IMETHODIMP |
|
141 nsClipboard::SupportsFindClipboard(bool* _retval) |
|
142 { |
|
143 NS_ENSURE_ARG_POINTER(_retval); |
|
144 |
|
145 *_retval = false; |
|
146 return NS_OK; |
|
147 } |
|
148 |