widget/xpwidgets/nsTransferable.cpp

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
parent 10
ac0c01689b40
child 12
7540298fafa1
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

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

mercurial