widget/xpwidgets/nsTransferable.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
parent 3
141e0f1194b1
child 9
a63d609f5ebe
permissions
-rw-r--r--

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

mercurial