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