widget/xpwidgets/nsTransferable.cpp

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
parent 8
97036ab72558
child 10
ac0c01689b40
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 Notes to self:
michael@0 8
michael@0 9 - at some point, strings will be accessible from JS, so we won't have to wrap
michael@0 10 flavors in an nsISupportsCString. Until then, we're kinda stuck with
michael@0 11 this crappy API of nsISupportsArrays.
michael@0 12
michael@0 13 */
michael@0 14
michael@0 15
michael@0 16 #include "nsTransferable.h"
michael@0 17 #include "nsString.h"
michael@0 18 #include "nsReadableUtils.h"
michael@0 19 #include "nsTArray.h"
michael@0 20 #include "nsIFormatConverter.h"
michael@0 21 #include "nsIComponentManager.h"
michael@0 22 #include "nsCOMPtr.h"
michael@0 23 #include "nsXPCOM.h"
michael@0 24 #include "nsISupportsPrimitives.h"
michael@0 25 #include "nsMemory.h"
michael@0 26 #include "nsPrimitiveHelpers.h"
michael@0 27 #include "nsXPIDLString.h"
michael@0 28 #include "nsDirectoryServiceDefs.h"
michael@0 29 #include "nsDirectoryService.h"
michael@0 30 #include "nsCRT.h"
michael@0 31 #include "nsNetUtil.h"
michael@0 32 #include "nsIOutputStream.h"
michael@0 33 #include "nsIInputStream.h"
michael@0 34 #include "nsIFile.h"
michael@0 35 #include "nsILoadContext.h"
michael@0 36 #include "nsAutoPtr.h"
michael@0 37
michael@0 38 NS_IMPL_ISUPPORTS(nsTransferable, nsITransferable)
michael@0 39
michael@0 40 uint32_t GetDataForFlavor (const nsTArray<DataStruct>& aArray,
michael@0 41 const char* aDataFlavor)
michael@0 42 {
michael@0 43 for (uint32_t i = 0 ; i < aArray.Length () ; ++i) {
michael@0 44 if (aArray[i].GetFlavor().Equals (aDataFlavor))
michael@0 45 return i;
michael@0 46 }
michael@0 47
michael@0 48 return aArray.NoIndex;
michael@0 49 }
michael@0 50
michael@0 51 //-------------------------------------------------------------------------
michael@0 52 DataStruct::~DataStruct()
michael@0 53 {
michael@0 54 if (mCacheFileName) free(mCacheFileName);
michael@0 55 }
michael@0 56
michael@0 57 //-------------------------------------------------------------------------
michael@0 58 void
michael@0 59 DataStruct::SetData ( nsISupports* aData, uint32_t aDataLen )
michael@0 60 {
michael@0 61 // Now, check to see if we consider the data to be "too large"
michael@9 62 if (aDataLen > kLargeDatasetSize) {
michael@0 63 // if so, cache it to disk instead of memory
michael@0 64 if ( NS_SUCCEEDED(WriteCache(aData, aDataLen)) )
michael@0 65 return;
michael@0 66 else
michael@0 67 NS_WARNING("Oh no, couldn't write data to the cache file");
michael@0 68 }
michael@0 69
michael@0 70 mData = aData;
michael@0 71 mDataLen = aDataLen;
michael@0 72 }
michael@0 73
michael@0 74
michael@0 75 //-------------------------------------------------------------------------
michael@0 76 void
michael@0 77 DataStruct::GetData ( nsISupports** aData, uint32_t *aDataLen )
michael@0 78 {
michael@0 79 // check here to see if the data is cached on disk
michael@0 80 if ( !mData && mCacheFileName ) {
michael@0 81 // if so, read it in and pass it back
michael@0 82 // ReadCache creates memory and copies the data into it.
michael@0 83 if ( NS_SUCCEEDED(ReadCache(aData, aDataLen)) )
michael@0 84 return;
michael@0 85 else {
michael@0 86 // oh shit, something went horribly wrong here.
michael@0 87 NS_WARNING("Oh no, couldn't read data in from the cache file");
michael@0 88 *aData = nullptr;
michael@0 89 *aDataLen = 0;
michael@0 90 return;
michael@0 91 }
michael@0 92 }
michael@0 93
michael@0 94 *aData = mData;
michael@0 95 if ( mData )
michael@0 96 NS_ADDREF(*aData);
michael@0 97 *aDataLen = mDataLen;
michael@0 98 }
michael@0 99
michael@0 100
michael@0 101 //-------------------------------------------------------------------------
michael@0 102 already_AddRefed<nsIFile>
michael@0 103 DataStruct::GetFileSpec(const char* aFileName)
michael@0 104 {
michael@0 105 nsCOMPtr<nsIFile> cacheFile;
michael@0 106 NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(cacheFile));
michael@0 107
michael@0 108 if (!cacheFile)
michael@0 109 return nullptr;
michael@0 110
michael@0 111 // if the param aFileName contains a name we should use that
michael@0 112 // because the file probably already exists
michael@0 113 // otherwise create a unique name
michael@0 114 if (!aFileName) {
michael@0 115 cacheFile->AppendNative(NS_LITERAL_CSTRING("clipboardcache"));
michael@0 116 cacheFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
michael@0 117 } else {
michael@0 118 cacheFile->AppendNative(nsDependentCString(aFileName));
michael@0 119 }
michael@0 120
michael@0 121 return cacheFile.forget();
michael@0 122 }
michael@0 123
michael@0 124
michael@0 125 //-------------------------------------------------------------------------
michael@0 126 nsresult
michael@0 127 DataStruct::WriteCache(nsISupports* aData, uint32_t aDataLen)
michael@0 128 {
michael@0 129 // Get a new path and file to the temp directory
michael@0 130 nsCOMPtr<nsIFile> cacheFile = GetFileSpec(mCacheFileName);
michael@0 131 if (cacheFile) {
michael@0 132 // remember the file name
michael@0 133 if (!mCacheFileName) {
michael@0 134 nsXPIDLCString fName;
michael@0 135 cacheFile->GetNativeLeafName(fName);
michael@0 136 mCacheFileName = strdup(fName);
michael@0 137 }
michael@0 138
michael@0 139 // write out the contents of the clipboard
michael@0 140 // to the file
michael@0 141 //uint32_t bytes;
michael@0 142 nsCOMPtr<nsIOutputStream> outStr;
michael@0 143
michael@0 144 NS_NewLocalFileOutputStream(getter_AddRefs(outStr),
michael@0 145 cacheFile);
michael@0 146
michael@0 147 if (!outStr) return NS_ERROR_FAILURE;
michael@0 148
michael@0 149 void* buff = nullptr;
michael@0 150 nsPrimitiveHelpers::CreateDataFromPrimitive ( mFlavor.get(), aData, &buff, aDataLen );
michael@0 151 if ( buff ) {
michael@0 152 uint32_t ignored;
michael@0 153 outStr->Write(reinterpret_cast<char*>(buff), aDataLen, &ignored);
michael@0 154 nsMemory::Free(buff);
michael@0 155 return NS_OK;
michael@0 156 }
michael@0 157 }
michael@0 158 return NS_ERROR_FAILURE;
michael@0 159 }
michael@0 160
michael@0 161
michael@0 162 //-------------------------------------------------------------------------
michael@0 163 nsresult
michael@0 164 DataStruct::ReadCache(nsISupports** aData, uint32_t* aDataLen)
michael@0 165 {
michael@0 166 // if we don't have a cache filename we are out of luck
michael@0 167 if (!mCacheFileName)
michael@0 168 return NS_ERROR_FAILURE;
michael@0 169
michael@0 170 // get the path and file name
michael@0 171 nsCOMPtr<nsIFile> cacheFile = GetFileSpec(mCacheFileName);
michael@0 172 bool exists;
michael@0 173 if ( cacheFile && NS_SUCCEEDED(cacheFile->Exists(&exists)) && exists ) {
michael@0 174 // get the size of the file
michael@0 175 int64_t fileSize;
michael@0 176 int64_t max32 = 0xFFFFFFFF;
michael@0 177 cacheFile->GetFileSize(&fileSize);
michael@0 178 if (fileSize > max32)
michael@0 179 return NS_ERROR_OUT_OF_MEMORY;
michael@0 180
michael@0 181 uint32_t size = uint32_t(fileSize);
michael@0 182 // create new memory for the large clipboard data
michael@0 183 nsAutoArrayPtr<char> data(new char[size]);
michael@0 184 if ( !data )
michael@0 185 return NS_ERROR_OUT_OF_MEMORY;
michael@0 186
michael@0 187 // now read it all in
michael@0 188 nsCOMPtr<nsIInputStream> inStr;
michael@0 189 NS_NewLocalFileInputStream( getter_AddRefs(inStr),
michael@0 190 cacheFile);
michael@0 191
michael@0 192 if (!cacheFile) return NS_ERROR_FAILURE;
michael@0 193
michael@0 194 nsresult rv = inStr->Read(data, fileSize, aDataLen);
michael@0 195
michael@0 196 // make sure we got all the data ok
michael@0 197 if (NS_SUCCEEDED(rv) && *aDataLen == size) {
michael@0 198 nsPrimitiveHelpers::CreatePrimitiveForData ( mFlavor.get(), data, fileSize, aData );
michael@0 199 return *aData ? NS_OK : NS_ERROR_FAILURE;
michael@0 200 }
michael@0 201
michael@0 202 // zero the return params
michael@0 203 *aData = nullptr;
michael@0 204 *aDataLen = 0;
michael@0 205 }
michael@0 206
michael@0 207 return NS_ERROR_FAILURE;
michael@0 208 }
michael@0 209
michael@0 210
michael@0 211 //-------------------------------------------------------------------------
michael@0 212 //
michael@0 213 // Transferable constructor
michael@0 214 //
michael@0 215 //-------------------------------------------------------------------------
michael@0 216 nsTransferable::nsTransferable()
michael@0 217 : mPrivateData(false)
michael@0 218 #ifdef DEBUG
michael@0 219 , mInitialized(false)
michael@0 220 #endif
michael@0 221 {
michael@0 222 }
michael@0 223
michael@0 224 //-------------------------------------------------------------------------
michael@0 225 //
michael@0 226 // Transferable destructor
michael@0 227 //
michael@0 228 //-------------------------------------------------------------------------
michael@0 229 nsTransferable::~nsTransferable()
michael@0 230 {
michael@0 231 }
michael@0 232
michael@0 233
michael@0 234 NS_IMETHODIMP
michael@0 235 nsTransferable::Init(nsILoadContext* aContext)
michael@0 236 {
michael@0 237 MOZ_ASSERT(!mInitialized);
michael@0 238
michael@0 239 if (aContext) {
michael@0 240 mPrivateData = aContext->UsePrivateBrowsing();
michael@0 241 }
michael@0 242 #ifdef DEBUG
michael@0 243 mInitialized = true;
michael@0 244 #endif
michael@0 245 return NS_OK;
michael@0 246 }
michael@0 247
michael@0 248 //
michael@0 249 // GetTransferDataFlavors
michael@0 250 //
michael@0 251 // Returns a copy of the internal list of flavors. This does NOT take into
michael@0 252 // account any converter that may be registered. This list consists of
michael@0 253 // nsISupportsCString objects so that the flavor list can be accessed from JS.
michael@0 254 //
michael@0 255 nsresult
michael@0 256 nsTransferable::GetTransferDataFlavors(nsISupportsArray ** aDataFlavorList)
michael@0 257 {
michael@0 258 MOZ_ASSERT(mInitialized);
michael@0 259
michael@0 260 nsresult rv = NS_NewISupportsArray ( aDataFlavorList );
michael@0 261 if (NS_FAILED(rv)) return rv;
michael@0 262
michael@0 263 for ( uint32_t i=0; i<mDataArray.Length(); ++i ) {
michael@0 264 DataStruct& data = mDataArray.ElementAt(i);
michael@0 265 nsCOMPtr<nsISupportsCString> flavorWrapper = do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID);
michael@0 266 if ( flavorWrapper ) {
michael@0 267 flavorWrapper->SetData ( data.GetFlavor() );
michael@0 268 nsCOMPtr<nsISupports> genericWrapper ( do_QueryInterface(flavorWrapper) );
michael@0 269 (*aDataFlavorList)->AppendElement( genericWrapper );
michael@0 270 }
michael@0 271 }
michael@0 272
michael@0 273 return NS_OK;
michael@0 274 }
michael@0 275
michael@0 276
michael@0 277 //
michael@0 278 // GetTransferData
michael@0 279 //
michael@0 280 // Returns the data of the requested flavor, obtained from either having the data on hand or
michael@0 281 // using a converter to get it. The data is wrapped in a nsISupports primitive so that it is
michael@0 282 // accessible from JS.
michael@0 283 //
michael@0 284 NS_IMETHODIMP
michael@0 285 nsTransferable::GetTransferData(const char *aFlavor, nsISupports **aData, uint32_t *aDataLen)
michael@0 286 {
michael@0 287 MOZ_ASSERT(mInitialized);
michael@0 288
michael@0 289 NS_ENSURE_ARG_POINTER(aFlavor && aData && aDataLen);
michael@0 290
michael@0 291 nsresult rv = NS_OK;
michael@0 292 nsCOMPtr<nsISupports> savedData;
michael@0 293
michael@0 294 // first look and see if the data is present in one of the intrinsic flavors
michael@0 295 uint32_t i;
michael@0 296 for (i = 0; i < mDataArray.Length(); ++i ) {
michael@0 297 DataStruct& data = mDataArray.ElementAt(i);
michael@0 298 if ( data.GetFlavor().Equals(aFlavor) ) {
michael@0 299 nsCOMPtr<nsISupports> dataBytes;
michael@0 300 uint32_t len;
michael@0 301 data.GetData(getter_AddRefs(dataBytes), &len);
michael@0 302 if (len == kFlavorHasDataProvider && dataBytes) {
michael@0 303 // do we have a data provider?
michael@0 304 nsCOMPtr<nsIFlavorDataProvider> dataProvider = do_QueryInterface(dataBytes);
michael@0 305 if (dataProvider) {
michael@0 306 rv = dataProvider->GetFlavorData(this, aFlavor,
michael@0 307 getter_AddRefs(dataBytes), &len);
michael@0 308 if (NS_FAILED(rv))
michael@0 309 break; // the provider failed. fall into the converter code below.
michael@0 310 }
michael@0 311 }
michael@0 312 if (dataBytes && len > 0) { // XXXmats why is zero length not ok?
michael@0 313 *aDataLen = len;
michael@0 314 dataBytes.forget(aData);
michael@0 315 return NS_OK;
michael@0 316 }
michael@0 317 savedData = dataBytes; // return this if format converter fails
michael@0 318 break;
michael@0 319 }
michael@0 320 }
michael@0 321
michael@0 322 bool found = false;
michael@0 323
michael@0 324 // if not, try using a format converter to get the requested flavor
michael@0 325 if ( mFormatConv ) {
michael@0 326 for (i = 0; i < mDataArray.Length(); ++i) {
michael@0 327 DataStruct& data = mDataArray.ElementAt(i);
michael@0 328 bool canConvert = false;
michael@0 329 mFormatConv->CanConvert(data.GetFlavor().get(), aFlavor, &canConvert);
michael@0 330 if ( canConvert ) {
michael@0 331 nsCOMPtr<nsISupports> dataBytes;
michael@0 332 uint32_t len;
michael@0 333 data.GetData(getter_AddRefs(dataBytes), &len);
michael@0 334 if (len == kFlavorHasDataProvider && dataBytes) {
michael@0 335 // do we have a data provider?
michael@0 336 nsCOMPtr<nsIFlavorDataProvider> dataProvider = do_QueryInterface(dataBytes);
michael@0 337 if (dataProvider) {
michael@0 338 rv = dataProvider->GetFlavorData(this, aFlavor,
michael@0 339 getter_AddRefs(dataBytes), &len);
michael@0 340 if (NS_FAILED(rv))
michael@0 341 break; // give up
michael@0 342 }
michael@0 343 }
michael@0 344 mFormatConv->Convert(data.GetFlavor().get(), dataBytes, len, aFlavor, aData, aDataLen);
michael@0 345 found = true;
michael@0 346 break;
michael@0 347 }
michael@0 348 }
michael@0 349 }
michael@0 350
michael@0 351 // for backward compatibility
michael@0 352 if (!found) {
michael@0 353 savedData.forget(aData);
michael@0 354 *aDataLen = 0;
michael@0 355 }
michael@0 356
michael@0 357 return found ? NS_OK : NS_ERROR_FAILURE;
michael@0 358 }
michael@0 359
michael@0 360
michael@0 361 //
michael@0 362 // GetAnyTransferData
michael@0 363 //
michael@0 364 // Returns the data of the first flavor found. Caller is responsible for deleting the
michael@0 365 // flavor string.
michael@0 366 //
michael@0 367 NS_IMETHODIMP
michael@0 368 nsTransferable::GetAnyTransferData(char **aFlavor, nsISupports **aData, uint32_t *aDataLen)
michael@0 369 {
michael@0 370 MOZ_ASSERT(mInitialized);
michael@0 371
michael@0 372 NS_ENSURE_ARG_POINTER(aFlavor && aData && aDataLen);
michael@0 373
michael@0 374 for ( uint32_t i=0; i < mDataArray.Length(); ++i ) {
michael@0 375 DataStruct& data = mDataArray.ElementAt(i);
michael@0 376 if (data.IsDataAvailable()) {
michael@0 377 *aFlavor = ToNewCString(data.GetFlavor());
michael@0 378 data.GetData(aData, aDataLen);
michael@0 379 return NS_OK;
michael@0 380 }
michael@0 381 }
michael@0 382
michael@0 383 return NS_ERROR_FAILURE;
michael@0 384 }
michael@0 385
michael@0 386
michael@0 387 //
michael@0 388 // SetTransferData
michael@0 389 //
michael@0 390 //
michael@0 391 //
michael@0 392 NS_IMETHODIMP
michael@0 393 nsTransferable::SetTransferData(const char *aFlavor, nsISupports *aData, uint32_t aDataLen)
michael@0 394 {
michael@0 395 MOZ_ASSERT(mInitialized);
michael@0 396
michael@0 397 NS_ENSURE_ARG(aFlavor);
michael@0 398
michael@0 399 // first check our intrinsic flavors to see if one has been registered.
michael@0 400 uint32_t i = 0;
michael@0 401 for (i = 0; i < mDataArray.Length(); ++i) {
michael@0 402 DataStruct& data = mDataArray.ElementAt(i);
michael@0 403 if ( data.GetFlavor().Equals(aFlavor) ) {
michael@0 404 data.SetData ( aData, aDataLen );
michael@0 405 return NS_OK;
michael@0 406 }
michael@0 407 }
michael@0 408
michael@0 409 // if not, try using a format converter to find a flavor to put the data in
michael@0 410 if ( mFormatConv ) {
michael@0 411 for (i = 0; i < mDataArray.Length(); ++i) {
michael@0 412 DataStruct& data = mDataArray.ElementAt(i);
michael@0 413 bool canConvert = false;
michael@0 414 mFormatConv->CanConvert(aFlavor, data.GetFlavor().get(), &canConvert);
michael@0 415
michael@0 416 if ( canConvert ) {
michael@0 417 nsCOMPtr<nsISupports> ConvertedData;
michael@0 418 uint32_t ConvertedLen;
michael@0 419 mFormatConv->Convert(aFlavor, aData, aDataLen, data.GetFlavor().get(), getter_AddRefs(ConvertedData), &ConvertedLen);
michael@0 420 data.SetData(ConvertedData, ConvertedLen);
michael@0 421 return NS_OK;
michael@0 422 }
michael@0 423 }
michael@0 424 }
michael@0 425
michael@0 426 // Can't set data neither directly nor through converter. Just add this flavor and try again
michael@0 427 nsresult result = NS_ERROR_FAILURE;
michael@0 428 if ( NS_SUCCEEDED(AddDataFlavor(aFlavor)) )
michael@0 429 result = SetTransferData (aFlavor, aData, aDataLen);
michael@0 430
michael@0 431 return result;
michael@0 432 }
michael@0 433
michael@0 434
michael@0 435 //
michael@0 436 // AddDataFlavor
michael@0 437 //
michael@0 438 // Adds a data flavor to our list with no data. Error if it already exists.
michael@0 439 //
michael@0 440 NS_IMETHODIMP
michael@0 441 nsTransferable::AddDataFlavor(const char *aDataFlavor)
michael@0 442 {
michael@0 443 MOZ_ASSERT(mInitialized);
michael@0 444
michael@0 445 if (GetDataForFlavor (mDataArray, aDataFlavor) != mDataArray.NoIndex)
michael@0 446 return NS_ERROR_FAILURE;
michael@0 447
michael@0 448 // Create a new "slot" for the data
michael@0 449 mDataArray.AppendElement(DataStruct ( aDataFlavor ));
michael@0 450
michael@0 451 return NS_OK;
michael@0 452 }
michael@0 453
michael@0 454
michael@0 455 //
michael@0 456 // RemoveDataFlavor
michael@0 457 //
michael@0 458 // Removes a data flavor (and causes the data to be destroyed). Error if
michael@0 459 // the requested flavor is not present.
michael@0 460 //
michael@0 461 NS_IMETHODIMP
michael@0 462 nsTransferable::RemoveDataFlavor(const char *aDataFlavor)
michael@0 463 {
michael@0 464 MOZ_ASSERT(mInitialized);
michael@0 465
michael@0 466 uint32_t idx;
michael@0 467 if ((idx = GetDataForFlavor(mDataArray, aDataFlavor)) != mDataArray.NoIndex) {
michael@0 468 mDataArray.RemoveElementAt (idx);
michael@0 469 return NS_OK;
michael@0 470 }
michael@0 471 return NS_ERROR_FAILURE;
michael@0 472 }
michael@0 473
michael@0 474
michael@0 475 /**
michael@0 476 *
michael@0 477 *
michael@0 478 */
michael@0 479 NS_IMETHODIMP
michael@0 480 nsTransferable::IsLargeDataSet(bool *_retval)
michael@0 481 {
michael@0 482 MOZ_ASSERT(mInitialized);
michael@0 483
michael@0 484 NS_ENSURE_ARG_POINTER(_retval);
michael@0 485 *_retval = false;
michael@0 486 return NS_OK;
michael@0 487 }
michael@0 488
michael@0 489
michael@0 490 /**
michael@0 491 *
michael@0 492 *
michael@0 493 */
michael@0 494 NS_IMETHODIMP nsTransferable::SetConverter(nsIFormatConverter * aConverter)
michael@0 495 {
michael@0 496 MOZ_ASSERT(mInitialized);
michael@0 497
michael@0 498 mFormatConv = aConverter;
michael@0 499 return NS_OK;
michael@0 500 }
michael@0 501
michael@0 502
michael@0 503 /**
michael@0 504 *
michael@0 505 *
michael@0 506 */
michael@0 507 NS_IMETHODIMP nsTransferable::GetConverter(nsIFormatConverter * *aConverter)
michael@0 508 {
michael@0 509 MOZ_ASSERT(mInitialized);
michael@0 510
michael@0 511 NS_ENSURE_ARG_POINTER(aConverter);
michael@0 512 *aConverter = mFormatConv;
michael@0 513 NS_IF_ADDREF(*aConverter);
michael@0 514 return NS_OK;
michael@0 515 }
michael@0 516
michael@0 517
michael@0 518 //
michael@0 519 // FlavorsTransferableCanImport
michael@0 520 //
michael@0 521 // Computes a list of flavors that the transferable can accept into it, either through
michael@0 522 // intrinsic knowledge or input data converters.
michael@0 523 //
michael@0 524 NS_IMETHODIMP
michael@0 525 nsTransferable::FlavorsTransferableCanImport(nsISupportsArray **_retval)
michael@0 526 {
michael@0 527 MOZ_ASSERT(mInitialized);
michael@0 528
michael@0 529 NS_ENSURE_ARG_POINTER(_retval);
michael@0 530
michael@0 531 // Get the flavor list, and on to the end of it, append the list of flavors we
michael@0 532 // can also get to through a converter. This is so that we can just walk the list
michael@0 533 // in one go, looking for the desired flavor.
michael@0 534 GetTransferDataFlavors(_retval); // addrefs
michael@0 535 nsCOMPtr<nsIFormatConverter> converter;
michael@0 536 GetConverter(getter_AddRefs(converter));
michael@0 537 if ( converter ) {
michael@0 538 nsCOMPtr<nsISupportsArray> convertedList;
michael@0 539 converter->GetInputDataFlavors(getter_AddRefs(convertedList));
michael@0 540
michael@0 541 if ( convertedList ) {
michael@0 542 uint32_t importListLen;
michael@0 543 convertedList->Count(&importListLen);
michael@0 544
michael@0 545 for ( uint32_t i=0; i < importListLen; ++i ) {
michael@0 546 nsCOMPtr<nsISupports> genericFlavor;
michael@0 547 convertedList->GetElementAt ( i, getter_AddRefs(genericFlavor) );
michael@0 548
michael@0 549 nsCOMPtr<nsISupportsCString> flavorWrapper ( do_QueryInterface (genericFlavor) );
michael@0 550 nsAutoCString flavorStr;
michael@0 551 flavorWrapper->GetData( flavorStr );
michael@0 552
michael@0 553 if (GetDataForFlavor (mDataArray, flavorStr.get())
michael@0 554 == mDataArray.NoIndex) // Don't append if already in intrinsic list
michael@0 555 (*_retval)->AppendElement (genericFlavor);
michael@0 556 } // foreach flavor that can be converted to
michael@0 557 }
michael@0 558 } // if a converter exists
michael@0 559
michael@0 560 return NS_OK;
michael@0 561 } // FlavorsTransferableCanImport
michael@0 562
michael@0 563
michael@0 564 //
michael@0 565 // FlavorsTransferableCanExport
michael@0 566 //
michael@0 567 // Computes a list of flavors that the transferable can export, either through
michael@0 568 // intrinsic knowledge or output data converters.
michael@0 569 //
michael@0 570 NS_IMETHODIMP
michael@0 571 nsTransferable::FlavorsTransferableCanExport(nsISupportsArray **_retval)
michael@0 572 {
michael@0 573 MOZ_ASSERT(mInitialized);
michael@0 574
michael@0 575 NS_ENSURE_ARG_POINTER(_retval);
michael@0 576
michael@0 577 // Get the flavor list, and on to the end of it, append the list of flavors we
michael@0 578 // can also get to through a converter. This is so that we can just walk the list
michael@0 579 // in one go, looking for the desired flavor.
michael@0 580 GetTransferDataFlavors(_retval); // addrefs
michael@0 581 nsCOMPtr<nsIFormatConverter> converter;
michael@0 582 GetConverter(getter_AddRefs(converter));
michael@0 583 if ( converter ) {
michael@0 584 nsCOMPtr<nsISupportsArray> convertedList;
michael@0 585 converter->GetOutputDataFlavors(getter_AddRefs(convertedList));
michael@0 586
michael@0 587 if ( convertedList ) {
michael@0 588 uint32_t importListLen;
michael@0 589 convertedList->Count(&importListLen);
michael@0 590
michael@0 591 for ( uint32_t i=0; i < importListLen; ++i ) {
michael@0 592 nsCOMPtr<nsISupports> genericFlavor;
michael@0 593 convertedList->GetElementAt ( i, getter_AddRefs(genericFlavor) );
michael@0 594
michael@0 595 nsCOMPtr<nsISupportsCString> flavorWrapper ( do_QueryInterface (genericFlavor) );
michael@0 596 nsAutoCString flavorStr;
michael@0 597 flavorWrapper->GetData( flavorStr );
michael@0 598
michael@0 599 if (GetDataForFlavor (mDataArray, flavorStr.get())
michael@0 600 == mDataArray.NoIndex) // Don't append if already in intrinsic list
michael@0 601 (*_retval)->AppendElement (genericFlavor);
michael@0 602 } // foreach flavor that can be converted to
michael@0 603 }
michael@0 604 } // if a converter exists
michael@0 605
michael@0 606 return NS_OK;
michael@0 607 } // FlavorsTransferableCanExport
michael@0 608
michael@0 609 NS_IMETHODIMP
michael@0 610 nsTransferable::GetIsPrivateData(bool *aIsPrivateData)
michael@0 611 {
michael@0 612 MOZ_ASSERT(mInitialized);
michael@0 613
michael@0 614 NS_ENSURE_ARG_POINTER(aIsPrivateData);
michael@0 615
michael@0 616 *aIsPrivateData = mPrivateData;
michael@0 617
michael@0 618 return NS_OK;
michael@0 619 }
michael@0 620
michael@0 621 NS_IMETHODIMP
michael@0 622 nsTransferable::SetIsPrivateData(bool aIsPrivateData)
michael@0 623 {
michael@0 624 MOZ_ASSERT(mInitialized);
michael@0 625
michael@0 626 mPrivateData = aIsPrivateData;
michael@0 627
michael@0 628 return NS_OK;
michael@0 629 }
michael@0 630

mercurial