Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include <QGuiApplication> |
michael@0 | 6 | #include <QMimeData> |
michael@0 | 7 | #include <QString> |
michael@0 | 8 | #include <QStringList> |
michael@0 | 9 | #include <QByteArray> |
michael@0 | 10 | #include <QImage> |
michael@0 | 11 | #include <QImageWriter> |
michael@0 | 12 | #include <QBuffer> |
michael@0 | 13 | |
michael@0 | 14 | #include "gfxPlatform.h" |
michael@0 | 15 | #include "mozilla/ArrayUtils.h" |
michael@0 | 16 | #include "mozilla/gfx/2D.h" |
michael@0 | 17 | |
michael@0 | 18 | #include "nsClipboard.h" |
michael@0 | 19 | #include "nsISupportsPrimitives.h" |
michael@0 | 20 | #include "nsXPIDLString.h" |
michael@0 | 21 | #include "nsPrimitiveHelpers.h" |
michael@0 | 22 | #include "nsIInputStream.h" |
michael@0 | 23 | #include "nsReadableUtils.h" |
michael@0 | 24 | #include "nsStringStream.h" |
michael@0 | 25 | #include "nsComponentManagerUtils.h" |
michael@0 | 26 | |
michael@0 | 27 | #include "imgIContainer.h" |
michael@0 | 28 | #include "gfxImageSurface.h" |
michael@0 | 29 | |
michael@0 | 30 | using namespace mozilla; |
michael@0 | 31 | using namespace mozilla::gfx; |
michael@0 | 32 | |
michael@0 | 33 | NS_IMPL_ISUPPORTS(nsClipboard, nsIClipboard) |
michael@0 | 34 | |
michael@0 | 35 | //------------------------------------------------------------------------- |
michael@0 | 36 | // |
michael@0 | 37 | // nsClipboard constructor |
michael@0 | 38 | // |
michael@0 | 39 | //------------------------------------------------------------------------- |
michael@0 | 40 | nsClipboard::nsClipboard() : nsIClipboard(), |
michael@0 | 41 | mSelectionOwner(nullptr), |
michael@0 | 42 | mGlobalOwner(nullptr), |
michael@0 | 43 | mSelectionTransferable(nullptr), |
michael@0 | 44 | mGlobalTransferable(nullptr) |
michael@0 | 45 | { |
michael@0 | 46 | // No implementation needed |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | //------------------------------------------------------------------------- |
michael@0 | 50 | // |
michael@0 | 51 | // nsClipboard destructor |
michael@0 | 52 | // |
michael@0 | 53 | //------------------------------------------------------------------------- |
michael@0 | 54 | nsClipboard::~nsClipboard() |
michael@0 | 55 | { |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | static inline QImage::Format |
michael@0 | 59 | _moz2dformat_to_qformat(SurfaceFormat aFormat) |
michael@0 | 60 | { |
michael@0 | 61 | switch (aFormat) { |
michael@0 | 62 | case SurfaceFormat::B8G8R8A8: |
michael@0 | 63 | return QImage::Format_ARGB32_Premultiplied; |
michael@0 | 64 | case SurfaceFormat::B8G8R8X8: |
michael@0 | 65 | return QImage::Format_ARGB32; |
michael@0 | 66 | case SurfaceFormat::R5G6B5: |
michael@0 | 67 | return QImage::Format_RGB16; |
michael@0 | 68 | default: |
michael@0 | 69 | return QImage::Format_Invalid; |
michael@0 | 70 | } |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | // nsClipboard::SetNativeClipboardData ie. Copy |
michael@0 | 74 | |
michael@0 | 75 | NS_IMETHODIMP |
michael@0 | 76 | nsClipboard::SetNativeClipboardData( nsITransferable *aTransferable, |
michael@0 | 77 | QClipboard::Mode clipboardMode ) |
michael@0 | 78 | { |
michael@0 | 79 | if (nullptr == aTransferable) |
michael@0 | 80 | { |
michael@0 | 81 | NS_WARNING("nsClipboard::SetNativeClipboardData(): no transferable!"); |
michael@0 | 82 | return NS_ERROR_FAILURE; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | // get flavor list that includes all flavors that can be written (including |
michael@0 | 86 | // ones obtained through conversion) |
michael@0 | 87 | nsCOMPtr<nsISupportsArray> flavorList; |
michael@0 | 88 | nsresult rv = aTransferable->FlavorsTransferableCanExport( getter_AddRefs(flavorList) ); |
michael@0 | 89 | |
michael@0 | 90 | if (NS_FAILED(rv)) |
michael@0 | 91 | { |
michael@0 | 92 | NS_WARNING("nsClipboard::SetNativeClipboardData(): no FlavorsTransferable !"); |
michael@0 | 93 | return NS_ERROR_FAILURE; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | QClipboard *cb = QGuiApplication::clipboard(); |
michael@0 | 97 | QMimeData *mimeData = new QMimeData; |
michael@0 | 98 | |
michael@0 | 99 | uint32_t flavorCount = 0; |
michael@0 | 100 | flavorList->Count(&flavorCount); |
michael@0 | 101 | bool imageAdded = false; |
michael@0 | 102 | |
michael@0 | 103 | for (uint32_t i = 0; i < flavorCount; ++i) |
michael@0 | 104 | { |
michael@0 | 105 | nsCOMPtr<nsISupports> genericFlavor; |
michael@0 | 106 | flavorList->GetElementAt(i,getter_AddRefs(genericFlavor)); |
michael@0 | 107 | nsCOMPtr<nsISupportsCString> currentFlavor(do_QueryInterface(genericFlavor)); |
michael@0 | 108 | |
michael@0 | 109 | if (currentFlavor) |
michael@0 | 110 | { |
michael@0 | 111 | // flavorStr is the mime type |
michael@0 | 112 | nsXPIDLCString flavorStr; |
michael@0 | 113 | currentFlavor->ToString(getter_Copies(flavorStr)); |
michael@0 | 114 | |
michael@0 | 115 | // Clip is the data which will be sent to the clipboard |
michael@0 | 116 | nsCOMPtr<nsISupports> clip; |
michael@0 | 117 | // len is the length of the data |
michael@0 | 118 | uint32_t len; |
michael@0 | 119 | |
michael@0 | 120 | // Unicode text? |
michael@0 | 121 | if (!strcmp(flavorStr.get(), kUnicodeMime)) |
michael@0 | 122 | { |
michael@0 | 123 | rv = aTransferable->GetTransferData(flavorStr,getter_AddRefs(clip),&len); |
michael@0 | 124 | nsCOMPtr<nsISupportsString> wideString; |
michael@0 | 125 | wideString = do_QueryInterface(clip); |
michael@0 | 126 | if (!wideString || NS_FAILED(rv)) |
michael@0 | 127 | continue; |
michael@0 | 128 | |
michael@0 | 129 | nsAutoString utf16string; |
michael@0 | 130 | wideString->GetData(utf16string); |
michael@0 | 131 | QString str = QString::fromUtf16((const ushort*)utf16string.get()); |
michael@0 | 132 | |
michael@0 | 133 | // Add text to the mimeData |
michael@0 | 134 | mimeData->setText(str); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | // html? |
michael@0 | 138 | else if (!strcmp(flavorStr.get(), kHTMLMime)) |
michael@0 | 139 | { |
michael@0 | 140 | rv = aTransferable->GetTransferData(flavorStr,getter_AddRefs(clip),&len); |
michael@0 | 141 | nsCOMPtr<nsISupportsString> wideString; |
michael@0 | 142 | wideString = do_QueryInterface(clip); |
michael@0 | 143 | if (!wideString || NS_FAILED(rv)) |
michael@0 | 144 | continue; |
michael@0 | 145 | |
michael@0 | 146 | nsAutoString utf16string; |
michael@0 | 147 | wideString->GetData(utf16string); |
michael@0 | 148 | QString str = QString::fromUtf16((const ushort*)utf16string.get()); |
michael@0 | 149 | |
michael@0 | 150 | // Add html to the mimeData |
michael@0 | 151 | mimeData->setHtml(str); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | // image? |
michael@0 | 155 | else if (!imageAdded // image is added only once to the clipboard |
michael@0 | 156 | && (!strcmp(flavorStr.get(), kNativeImageMime) |
michael@0 | 157 | || !strcmp(flavorStr.get(), kPNGImageMime) |
michael@0 | 158 | || !strcmp(flavorStr.get(), kJPEGImageMime) |
michael@0 | 159 | || !strcmp(flavorStr.get(), kJPGImageMime) |
michael@0 | 160 | || !strcmp(flavorStr.get(), kGIFImageMime)) |
michael@0 | 161 | ) |
michael@0 | 162 | { |
michael@0 | 163 | // Look through our transfer data for the image |
michael@0 | 164 | static const char* const imageMimeTypes[] = { |
michael@0 | 165 | kNativeImageMime, kPNGImageMime, kJPEGImageMime, kJPGImageMime, kGIFImageMime }; |
michael@0 | 166 | nsCOMPtr<nsISupportsInterfacePointer> ptrPrimitive; |
michael@0 | 167 | for (uint32_t i = 0; !ptrPrimitive && i < ArrayLength(imageMimeTypes); i++) |
michael@0 | 168 | { |
michael@0 | 169 | aTransferable->GetTransferData(imageMimeTypes[i], getter_AddRefs(clip), &len); |
michael@0 | 170 | ptrPrimitive = do_QueryInterface(clip); |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | if (!ptrPrimitive) |
michael@0 | 174 | continue; |
michael@0 | 175 | |
michael@0 | 176 | nsCOMPtr<nsISupports> primitiveData; |
michael@0 | 177 | ptrPrimitive->GetData(getter_AddRefs(primitiveData)); |
michael@0 | 178 | nsCOMPtr<imgIContainer> image(do_QueryInterface(primitiveData)); |
michael@0 | 179 | if (!image) // Not getting an image for an image mime type!? |
michael@0 | 180 | continue; |
michael@0 | 181 | |
michael@0 | 182 | RefPtr<SourceSurface> surface = |
michael@0 | 183 | image->GetFrame(imgIContainer::FRAME_CURRENT, |
michael@0 | 184 | imgIContainer::FLAG_SYNC_DECODE); |
michael@0 | 185 | if (!surface) |
michael@0 | 186 | continue; |
michael@0 | 187 | |
michael@0 | 188 | RefPtr<DataSourceSurface> dataSurface = |
michael@0 | 189 | surface->GetDataSurface(); |
michael@0 | 190 | if (!dataSurface) |
michael@0 | 191 | continue; |
michael@0 | 192 | |
michael@0 | 193 | DataSourceSurface::MappedSurface map; |
michael@0 | 194 | if (!dataSurface->Map(DataSourceSurface::MapType::READ, &map)) |
michael@0 | 195 | continue; |
michael@0 | 196 | |
michael@0 | 197 | QImage qImage(map.mData, |
michael@0 | 198 | dataSurface->GetSize().width, |
michael@0 | 199 | dataSurface->GetSize().height, |
michael@0 | 200 | map.mStride, |
michael@0 | 201 | _moz2dformat_to_qformat(dataSurface->GetFormat())); |
michael@0 | 202 | |
michael@0 | 203 | dataSurface->Unmap(); |
michael@0 | 204 | |
michael@0 | 205 | // Add image to the mimeData |
michael@0 | 206 | mimeData->setImageData(qImage); |
michael@0 | 207 | imageAdded = true; |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | // Other flavors, adding data to clipboard "as is" |
michael@0 | 211 | else |
michael@0 | 212 | { |
michael@0 | 213 | rv = aTransferable->GetTransferData(flavorStr.get(), getter_AddRefs(clip), &len); |
michael@0 | 214 | // nothing found? |
michael@0 | 215 | if (!clip || NS_FAILED(rv)) |
michael@0 | 216 | continue; |
michael@0 | 217 | |
michael@0 | 218 | void *primitive_data = nullptr; |
michael@0 | 219 | nsPrimitiveHelpers::CreateDataFromPrimitive(flavorStr.get(), clip, |
michael@0 | 220 | &primitive_data, len); |
michael@0 | 221 | |
michael@0 | 222 | if (primitive_data) |
michael@0 | 223 | { |
michael@0 | 224 | QByteArray data ((const char *)primitive_data, len); |
michael@0 | 225 | // Add data to the mimeData |
michael@0 | 226 | mimeData->setData(flavorStr.get(), data); |
michael@0 | 227 | nsMemory::Free(primitive_data); |
michael@0 | 228 | } |
michael@0 | 229 | } |
michael@0 | 230 | } |
michael@0 | 231 | } |
michael@0 | 232 | |
michael@0 | 233 | // If we have some mime data, add it to the clipboard |
michael@0 | 234 | if(!mimeData->formats().isEmpty()) |
michael@0 | 235 | cb->setMimeData(mimeData, clipboardMode); |
michael@0 | 236 | else |
michael@0 | 237 | delete mimeData; |
michael@0 | 238 | |
michael@0 | 239 | return NS_OK; |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | // nsClipboard::GetNativeClipboardData ie. Paste |
michael@0 | 243 | // |
michael@0 | 244 | NS_IMETHODIMP |
michael@0 | 245 | nsClipboard::GetNativeClipboardData(nsITransferable *aTransferable, |
michael@0 | 246 | QClipboard::Mode clipboardMode) |
michael@0 | 247 | { |
michael@0 | 248 | if (nullptr == aTransferable) |
michael@0 | 249 | { |
michael@0 | 250 | NS_WARNING("GetNativeClipboardData: Transferable is null!"); |
michael@0 | 251 | return NS_ERROR_FAILURE; |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | // get flavor list that includes all acceptable flavors (including |
michael@0 | 255 | // ones obtained through conversion) |
michael@0 | 256 | nsCOMPtr<nsISupportsArray> flavorList; |
michael@0 | 257 | nsresult errCode = aTransferable->FlavorsTransferableCanImport( |
michael@0 | 258 | getter_AddRefs(flavorList)); |
michael@0 | 259 | |
michael@0 | 260 | if (NS_FAILED(errCode)) |
michael@0 | 261 | { |
michael@0 | 262 | NS_WARNING("nsClipboard::GetNativeClipboardData(): no FlavorsTransferable!"); |
michael@0 | 263 | return NS_ERROR_FAILURE; |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | QClipboard *cb = QGuiApplication::clipboard(); |
michael@0 | 267 | const QMimeData *mimeData = cb->mimeData(clipboardMode); |
michael@0 | 268 | |
michael@0 | 269 | // Walk through flavors and see which flavor matches the one being pasted |
michael@0 | 270 | uint32_t flavorCount; |
michael@0 | 271 | flavorList->Count(&flavorCount); |
michael@0 | 272 | nsAutoCString foundFlavor; |
michael@0 | 273 | |
michael@0 | 274 | for (uint32_t i = 0; i < flavorCount; ++i) |
michael@0 | 275 | { |
michael@0 | 276 | nsCOMPtr<nsISupports> genericFlavor; |
michael@0 | 277 | flavorList->GetElementAt(i,getter_AddRefs(genericFlavor)); |
michael@0 | 278 | nsCOMPtr<nsISupportsCString> currentFlavor(do_QueryInterface( genericFlavor) ); |
michael@0 | 279 | |
michael@0 | 280 | if (currentFlavor) |
michael@0 | 281 | { |
michael@0 | 282 | nsXPIDLCString flavorStr; |
michael@0 | 283 | currentFlavor->ToString(getter_Copies(flavorStr)); |
michael@0 | 284 | |
michael@0 | 285 | // Ok, so which flavor the data being pasted could be? |
michael@0 | 286 | // Text? |
michael@0 | 287 | if (!strcmp(flavorStr.get(), kUnicodeMime) && mimeData->hasText()) |
michael@0 | 288 | { |
michael@0 | 289 | // Clipboard has text and flavor accepts text, so lets |
michael@0 | 290 | // handle the data as text |
michael@0 | 291 | foundFlavor = nsAutoCString(flavorStr); |
michael@0 | 292 | |
michael@0 | 293 | // Get the text data from clipboard |
michael@0 | 294 | QString text = mimeData->text(); |
michael@0 | 295 | const QChar *unicode = text.unicode(); |
michael@0 | 296 | // Is there a more correct way to get the size in UTF16? |
michael@0 | 297 | uint32_t len = (uint32_t) 2*text.size(); |
michael@0 | 298 | |
michael@0 | 299 | // And then to genericDataWrapper |
michael@0 | 300 | nsCOMPtr<nsISupports> genericDataWrapper; |
michael@0 | 301 | nsPrimitiveHelpers::CreatePrimitiveForData( |
michael@0 | 302 | foundFlavor.get(), |
michael@0 | 303 | (void*)unicode, |
michael@0 | 304 | len, |
michael@0 | 305 | getter_AddRefs(genericDataWrapper)); |
michael@0 | 306 | // Data is good, set it to the transferable |
michael@0 | 307 | aTransferable->SetTransferData(foundFlavor.get(), |
michael@0 | 308 | genericDataWrapper,len); |
michael@0 | 309 | // And thats all |
michael@0 | 310 | break; |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | // html? |
michael@0 | 314 | if (!strcmp(flavorStr.get(), kHTMLMime) && mimeData->hasHtml()) |
michael@0 | 315 | { |
michael@0 | 316 | // Clipboard has text/html and flavor accepts text/html, so lets |
michael@0 | 317 | // handle the data as text/html |
michael@0 | 318 | foundFlavor = nsAutoCString(flavorStr); |
michael@0 | 319 | |
michael@0 | 320 | // Get the text data from clipboard |
michael@0 | 321 | QString html = mimeData->html(); |
michael@0 | 322 | const QChar *unicode = html.unicode(); |
michael@0 | 323 | // Is there a more correct way to get the size in UTF16? |
michael@0 | 324 | uint32_t len = (uint32_t) 2*html.size(); |
michael@0 | 325 | |
michael@0 | 326 | // And then to genericDataWrapper |
michael@0 | 327 | nsCOMPtr<nsISupports> genericDataWrapper; |
michael@0 | 328 | nsPrimitiveHelpers::CreatePrimitiveForData( |
michael@0 | 329 | foundFlavor.get(), |
michael@0 | 330 | (void*)unicode, |
michael@0 | 331 | len, |
michael@0 | 332 | getter_AddRefs(genericDataWrapper)); |
michael@0 | 333 | // Data is good, set it to the transferable |
michael@0 | 334 | aTransferable->SetTransferData(foundFlavor.get(), |
michael@0 | 335 | genericDataWrapper,len); |
michael@0 | 336 | // And thats all |
michael@0 | 337 | break; |
michael@0 | 338 | } |
michael@0 | 339 | |
michael@0 | 340 | // Image? |
michael@0 | 341 | if (( !strcmp(flavorStr.get(), kJPEGImageMime) |
michael@0 | 342 | || !strcmp(flavorStr.get(), kJPGImageMime) |
michael@0 | 343 | || !strcmp(flavorStr.get(), kPNGImageMime) |
michael@0 | 344 | || !strcmp(flavorStr.get(), kGIFImageMime)) |
michael@0 | 345 | && mimeData->hasImage()) |
michael@0 | 346 | { |
michael@0 | 347 | // Try to retrieve an image from clipboard |
michael@0 | 348 | QImage image = cb->image(); |
michael@0 | 349 | if(image.isNull()) |
michael@0 | 350 | continue; |
michael@0 | 351 | |
michael@0 | 352 | // Lets set the image format |
michael@0 | 353 | QByteArray imageFormat; |
michael@0 | 354 | if (!strcmp(flavorStr.get(), kJPEGImageMime) || !strcmp(flavorStr.get(), kJPGImageMime)) |
michael@0 | 355 | imageFormat = "jpeg"; |
michael@0 | 356 | else if (!strcmp(flavorStr.get(), kPNGImageMime)) |
michael@0 | 357 | imageFormat = "png"; |
michael@0 | 358 | else if (!strcmp(flavorStr.get(), kGIFImageMime)) |
michael@0 | 359 | imageFormat = "gif"; |
michael@0 | 360 | else |
michael@0 | 361 | continue; |
michael@0 | 362 | |
michael@0 | 363 | // Write image from clippboard to a QByteArrayBuffer |
michael@0 | 364 | QByteArray imageData; |
michael@0 | 365 | QBuffer imageBuffer(&imageData); |
michael@0 | 366 | QImageWriter imageWriter(&imageBuffer, imageFormat); |
michael@0 | 367 | if(!imageWriter.write(image)) |
michael@0 | 368 | continue; |
michael@0 | 369 | |
michael@0 | 370 | // Add the data to inputstream |
michael@0 | 371 | nsCOMPtr<nsIInputStream> byteStream; |
michael@0 | 372 | NS_NewByteInputStream(getter_AddRefs(byteStream), imageData.constData(), |
michael@0 | 373 | imageData.size(), NS_ASSIGNMENT_COPY); |
michael@0 | 374 | // Data is good, set it to the transferable |
michael@0 | 375 | aTransferable->SetTransferData(flavorStr, byteStream, sizeof(nsIInputStream*)); |
michael@0 | 376 | |
michael@0 | 377 | imageBuffer.close(); |
michael@0 | 378 | |
michael@0 | 379 | // And thats all |
michael@0 | 380 | break; |
michael@0 | 381 | } |
michael@0 | 382 | |
michael@0 | 383 | // Other mimetype? |
michael@0 | 384 | // Trying to forward the data "as is" |
michael@0 | 385 | if(mimeData->hasFormat(flavorStr.get())) |
michael@0 | 386 | { |
michael@0 | 387 | // get the data from the clipboard |
michael@0 | 388 | QByteArray clipboardData = mimeData->data(flavorStr.get()); |
michael@0 | 389 | // And add it to genericDataWrapper |
michael@0 | 390 | nsCOMPtr<nsISupports> genericDataWrapper; |
michael@0 | 391 | nsPrimitiveHelpers::CreatePrimitiveForData( |
michael@0 | 392 | foundFlavor.get(), |
michael@0 | 393 | (void*) clipboardData.data(), |
michael@0 | 394 | clipboardData.size(), |
michael@0 | 395 | getter_AddRefs(genericDataWrapper)); |
michael@0 | 396 | |
michael@0 | 397 | // Data is good, set it to the transferable |
michael@0 | 398 | aTransferable->SetTransferData(foundFlavor.get(), |
michael@0 | 399 | genericDataWrapper,clipboardData.size()); |
michael@0 | 400 | // And thats all |
michael@0 | 401 | break; |
michael@0 | 402 | } |
michael@0 | 403 | } |
michael@0 | 404 | } |
michael@0 | 405 | |
michael@0 | 406 | return NS_OK; |
michael@0 | 407 | } |
michael@0 | 408 | |
michael@0 | 409 | NS_IMETHODIMP |
michael@0 | 410 | nsClipboard::HasDataMatchingFlavors(const char** aFlavorList, uint32_t aLength, |
michael@0 | 411 | int32_t aWhichClipboard, bool *_retval) |
michael@0 | 412 | { |
michael@0 | 413 | *_retval = false; |
michael@0 | 414 | if (aWhichClipboard != kGlobalClipboard) |
michael@0 | 415 | return NS_OK; |
michael@0 | 416 | |
michael@0 | 417 | // Which kind of data in the clipboard |
michael@0 | 418 | QClipboard *cb = QGuiApplication::clipboard(); |
michael@0 | 419 | const QMimeData *mimeData = cb->mimeData(); |
michael@0 | 420 | const char *flavor=nullptr; |
michael@0 | 421 | QStringList formats = mimeData->formats(); |
michael@0 | 422 | for (uint32_t i = 0; i < aLength; ++i) |
michael@0 | 423 | { |
michael@0 | 424 | flavor = aFlavorList[i]; |
michael@0 | 425 | if (flavor) |
michael@0 | 426 | { |
michael@0 | 427 | QString qflavor(flavor); |
michael@0 | 428 | |
michael@0 | 429 | if (strcmp(flavor,kTextMime) == 0) |
michael@0 | 430 | { |
michael@0 | 431 | NS_WARNING("DO NOT USE THE text/plain DATA FLAVOR ANY MORE. USE text/unicode INSTEAD"); |
michael@0 | 432 | } |
michael@0 | 433 | |
michael@0 | 434 | // QClipboard says it has text/plain, mozilla wants to |
michael@0 | 435 | // know if the data is text/unicode -> interpret text/plain to text/unicode |
michael@0 | 436 | if (formats.contains(qflavor) || |
michael@0 | 437 | strcmp(flavor, kUnicodeMime) == 0) |
michael@0 | 438 | { |
michael@0 | 439 | // A match has been found, return' |
michael@0 | 440 | *_retval = true; |
michael@0 | 441 | break; |
michael@0 | 442 | } |
michael@0 | 443 | } |
michael@0 | 444 | } |
michael@0 | 445 | return NS_OK; |
michael@0 | 446 | } |
michael@0 | 447 | |
michael@0 | 448 | /** |
michael@0 | 449 | * Sets the transferable object |
michael@0 | 450 | */ |
michael@0 | 451 | NS_IMETHODIMP |
michael@0 | 452 | nsClipboard::SetData(nsITransferable *aTransferable, |
michael@0 | 453 | nsIClipboardOwner *aOwner, |
michael@0 | 454 | int32_t aWhichClipboard) |
michael@0 | 455 | { |
michael@0 | 456 | // See if we can short cut |
michael@0 | 457 | if ( |
michael@0 | 458 | (aWhichClipboard == kGlobalClipboard |
michael@0 | 459 | && aTransferable == mGlobalTransferable.get() |
michael@0 | 460 | && aOwner == mGlobalOwner.get() |
michael@0 | 461 | ) |
michael@0 | 462 | || |
michael@0 | 463 | (aWhichClipboard == kSelectionClipboard |
michael@0 | 464 | && aTransferable == mSelectionTransferable.get() |
michael@0 | 465 | && aOwner == mSelectionOwner.get() |
michael@0 | 466 | ) |
michael@0 | 467 | ) |
michael@0 | 468 | { |
michael@0 | 469 | return NS_OK; |
michael@0 | 470 | } |
michael@0 | 471 | |
michael@0 | 472 | nsresult rv; |
michael@0 | 473 | if (!mPrivacyHandler) { |
michael@0 | 474 | rv = NS_NewClipboardPrivacyHandler(getter_AddRefs(mPrivacyHandler)); |
michael@0 | 475 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 476 | } |
michael@0 | 477 | rv = mPrivacyHandler->PrepareDataForClipboard(aTransferable); |
michael@0 | 478 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 479 | |
michael@0 | 480 | EmptyClipboard(aWhichClipboard); |
michael@0 | 481 | |
michael@0 | 482 | QClipboard::Mode mode; |
michael@0 | 483 | |
michael@0 | 484 | if (kGlobalClipboard == aWhichClipboard) |
michael@0 | 485 | { |
michael@0 | 486 | mGlobalOwner = aOwner; |
michael@0 | 487 | mGlobalTransferable = aTransferable; |
michael@0 | 488 | |
michael@0 | 489 | mode = QClipboard::Clipboard; |
michael@0 | 490 | } |
michael@0 | 491 | else |
michael@0 | 492 | { |
michael@0 | 493 | mSelectionOwner = aOwner; |
michael@0 | 494 | mSelectionTransferable = aTransferable; |
michael@0 | 495 | |
michael@0 | 496 | mode = QClipboard::Selection; |
michael@0 | 497 | } |
michael@0 | 498 | return SetNativeClipboardData( aTransferable, mode ); |
michael@0 | 499 | } |
michael@0 | 500 | |
michael@0 | 501 | /** |
michael@0 | 502 | * Gets the transferable object |
michael@0 | 503 | */ |
michael@0 | 504 | NS_IMETHODIMP |
michael@0 | 505 | nsClipboard::GetData(nsITransferable *aTransferable, int32_t aWhichClipboard) |
michael@0 | 506 | { |
michael@0 | 507 | if (nullptr != aTransferable) |
michael@0 | 508 | { |
michael@0 | 509 | QClipboard::Mode mode; |
michael@0 | 510 | if (kGlobalClipboard == aWhichClipboard) |
michael@0 | 511 | { |
michael@0 | 512 | mode = QClipboard::Clipboard; |
michael@0 | 513 | } |
michael@0 | 514 | else |
michael@0 | 515 | { |
michael@0 | 516 | mode = QClipboard::Selection; |
michael@0 | 517 | } |
michael@0 | 518 | return GetNativeClipboardData(aTransferable, mode); |
michael@0 | 519 | } |
michael@0 | 520 | else |
michael@0 | 521 | { |
michael@0 | 522 | NS_WARNING("nsClipboard::GetData(), aTransferable is NULL."); |
michael@0 | 523 | } |
michael@0 | 524 | return NS_ERROR_FAILURE; |
michael@0 | 525 | } |
michael@0 | 526 | |
michael@0 | 527 | NS_IMETHODIMP |
michael@0 | 528 | nsClipboard::EmptyClipboard(int32_t aWhichClipboard) |
michael@0 | 529 | { |
michael@0 | 530 | if (aWhichClipboard == kSelectionClipboard) |
michael@0 | 531 | { |
michael@0 | 532 | if (mSelectionOwner) |
michael@0 | 533 | { |
michael@0 | 534 | mSelectionOwner->LosingOwnership(mSelectionTransferable); |
michael@0 | 535 | mSelectionOwner = nullptr; |
michael@0 | 536 | } |
michael@0 | 537 | mSelectionTransferable = nullptr; |
michael@0 | 538 | } |
michael@0 | 539 | else |
michael@0 | 540 | { |
michael@0 | 541 | if (mGlobalOwner) |
michael@0 | 542 | { |
michael@0 | 543 | mGlobalOwner->LosingOwnership(mGlobalTransferable); |
michael@0 | 544 | mGlobalOwner = nullptr; |
michael@0 | 545 | } |
michael@0 | 546 | mGlobalTransferable = nullptr; |
michael@0 | 547 | } |
michael@0 | 548 | |
michael@0 | 549 | return NS_OK; |
michael@0 | 550 | } |
michael@0 | 551 | |
michael@0 | 552 | NS_IMETHODIMP |
michael@0 | 553 | nsClipboard::SupportsSelectionClipboard(bool *_retval) |
michael@0 | 554 | { |
michael@0 | 555 | NS_ENSURE_ARG_POINTER(_retval); |
michael@0 | 556 | |
michael@0 | 557 | QClipboard *cb = QGuiApplication::clipboard(); |
michael@0 | 558 | if (cb->supportsSelection()) |
michael@0 | 559 | { |
michael@0 | 560 | *_retval = true; // we support the selection clipboard |
michael@0 | 561 | } |
michael@0 | 562 | else |
michael@0 | 563 | { |
michael@0 | 564 | *_retval = false; |
michael@0 | 565 | } |
michael@0 | 566 | |
michael@0 | 567 | return NS_OK; |
michael@0 | 568 | } |
michael@0 | 569 | |
michael@0 | 570 | NS_IMETHODIMP |
michael@0 | 571 | nsClipboard::SupportsFindClipboard(bool* _retval) |
michael@0 | 572 | { |
michael@0 | 573 | NS_ENSURE_ARG_POINTER(_retval); |
michael@0 | 574 | |
michael@0 | 575 | *_retval = false; |
michael@0 | 576 | return NS_OK; |
michael@0 | 577 | } |