Commit 7896e7e1 authored by Csoregi Natalia's avatar Csoregi Natalia
Browse files

Backed out 13 changesets (bug 1746052) for causing failures on...

Backed out 13 changesets (bug 1746052) for causing failures on uriloader/exthandler/tests/. CLOSED TREE

Backed out changeset d19cc58e3cab (bug 1746052)
Backed out changeset 229edc158a2b (bug 1746052)
Backed out changeset b0ef7c68abcf (bug 1746052)
Backed out changeset 30de4b77f242 (bug 1746052)
Backed out changeset ebc6720fdab3 (bug 1746052)
Backed out changeset daccb796a093 (bug 1746052)
Backed out changeset 5b76d8d76b2b (bug 1746052)
Backed out changeset a698068d078f (bug 1746052)
Backed out changeset 73d17535d8d1 (bug 1746052)
Backed out changeset 94c95f004221 (bug 1746052)
Backed out changeset 1a389759585a (bug 1746052)
Backed out changeset c91230a8ea90 (bug 1746052)
Backed out changeset 7665f02c114e (bug 1746052)
parent a3e1d0e5
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -100,7 +100,7 @@ function test() {
      aCallback();
    };

    launcherDialog.promptForSaveToFileAsync(launcher, aWin, "", "", false);
    launcherDialog.promptForSaveToFileAsync(launcher, aWin, null, null, null);
  }

  testOnWindow(false, function(win, downloadDir) {
+68 −22
Original line number Diff line number Diff line
@@ -261,23 +261,42 @@ nsContentAreaDragDropDataProvider::GetFlavorData(nsITransferable* aTransferable,
      supportsString = do_QueryInterface(tmp);
      if (!supportsString) return NS_ERROR_FAILURE;

      nsAutoString contentType;
      supportsString->GetData(contentType);
      nsAutoString imageRequestMime;
      supportsString->GetData(imageRequestMime);

      nsCOMPtr<nsIMIMEService> mimeService =
          do_GetService("@mozilla.org/mime;1");
      if (NS_WARN_IF(!mimeService)) {
        return NS_ERROR_FAILURE;
      }
      // If we have a MIME type, check the extension is compatible
      if (!imageRequestMime.IsEmpty()) {
        // Build a URL to get the filename extension
        nsCOMPtr<nsIURL> imageURL = do_QueryInterface(sourceURI, &rv);
        NS_ENSURE_SUCCESS(rv, rv);

      mimeService->ValidateFileNameForSaving(
          targetFilename, NS_ConvertUTF16toUTF8(contentType),
          nsIMIMEService::VALIDATE_DEFAULT, targetFilename);
    } else {
        nsAutoCString extension;
        rv = imageURL->GetFileExtension(extension);
        NS_ENSURE_SUCCESS(rv, rv);

        NS_ConvertUTF16toUTF8 mimeCString(imageRequestMime);
        bool isValidExtension;
        nsAutoCString primaryExtension;
        rv = CheckAndGetExtensionForMime(extension, mimeCString,
                                         &isValidExtension, &primaryExtension);
        NS_ENSURE_SUCCESS(rv, rv);

        if (!isValidExtension && !primaryExtension.IsEmpty()) {
          // The filename extension is missing or incompatible
          // with the MIME type, replace it with the primary
          // extension.
          nsAutoCString newFileName;
          rv = imageURL->GetFileBaseName(newFileName);
          NS_ENSURE_SUCCESS(rv, rv);
          newFileName.Append(".");
          newFileName.Append(primaryExtension);
          CopyUTF8toUTF16(newFileName, targetFilename);
        }
      }
    }
    // make the filename safe for the filesystem
    targetFilename.ReplaceChar(FILE_PATH_SEPARATOR FILE_ILLEGAL_CHARACTERS,
                               '-');
    }
#endif /* defined(XP_MACOSX) */

    // get the target directory from the kFilePromiseDirectoryMime
@@ -417,28 +436,55 @@ nsresult DragDataProducer::GetImageData(imgIContainer* aImage,
    nsCString mimeType;
    aRequest->GetMimeType(getter_Copies(mimeType));

    nsAutoCString fileName;
    aRequest->GetFileName(fileName);

#if defined(XP_MACOSX)
    // Save the MIME type so we can make sure the extension
    // is compatible (and replace it if it isn't) when the
    // image is dropped. On Mac, we need to get the OS MIME
    // handler information in the parent due to sandboxing.
    CopyUTF8toUTF16(mimeType, mImageRequestMime);
    CopyUTF8toUTF16(fileName, mImageDestFileName);
#else
    nsCOMPtr<nsIMIMEService> mimeService = do_GetService("@mozilla.org/mime;1");
    if (NS_WARN_IF(!mimeService)) {
      return NS_ERROR_FAILURE;
    }

    CopyUTF8toUTF16(fileName, mImageDestFileName);
    mimeService->ValidateFileNameForSaving(mImageDestFileName, mimeType,
                                           nsIMIMEService::VALIDATE_DEFAULT,
                                           mImageDestFileName);
    nsCOMPtr<nsIMIMEInfo> mimeInfo;
    mimeService->GetFromTypeAndExtension(mimeType, ""_ns,
                                         getter_AddRefs(mimeInfo));
    if (mimeInfo) {
      nsAutoCString extension;
      imgUrl->GetFileExtension(extension);

      bool validExtension;
      if (extension.IsEmpty() ||
          NS_FAILED(mimeInfo->ExtensionExists(extension, &validExtension)) ||
          !validExtension) {
        // Fix the file extension in the URL
        nsAutoCString primaryExtension;
        mimeInfo->GetPrimaryExtension(primaryExtension);
        if (!primaryExtension.IsEmpty()) {
          rv = NS_MutateURI(imgUrl)
                   .Apply(&nsIURLMutator::SetFileExtension, primaryExtension,
                          nullptr)
                   .Finalize(imgUrl);
          NS_ENSURE_SUCCESS(rv, rv);
        }
      }
    }
#endif /* defined(XP_MACOSX) */

    nsAutoCString fileName;
    imgUrl->GetFileName(fileName);

    NS_UnescapeURL(fileName);

#if !defined(XP_MACOSX)
    // make the filename safe for the filesystem
    fileName.ReplaceChar(FILE_PATH_SEPARATOR FILE_ILLEGAL_CHARACTERS, '-');
#endif

    CopyUTF8toUTF16(fileName, mImageDestFileName);

    // and the image object
    mImage = aImage;
  }
+47 −14
Original line number Diff line number Diff line
@@ -32,6 +32,8 @@
#include "nsHTMLDocument.h"
#include "nsGkAtoms.h"
#include "nsIFrame.h"
#include "nsIURI.h"
#include "nsGenericHTMLElement.h"

// image copy stuff
#include "nsIImageLoadingContent.h"
@@ -602,39 +604,70 @@ static nsresult AppendImagePromise(nsITransferable* aTransferable,
  nsCOMPtr<nsINode> node = do_QueryInterface(aImageElement, &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIMIMEService> mimeService = do_GetService("@mozilla.org/mime;1");
  if (NS_WARN_IF(!mimeService)) {
    return NS_ERROR_FAILURE;
  }
  // Fix the file extension in the URL if necessary
  nsCOMPtr<nsIMIMEService> mimeService =
      do_GetService(NS_MIMESERVICE_CONTRACTID);
  NS_ENSURE_TRUE(mimeService, NS_OK);

  nsCOMPtr<nsIURI> imgUri;
  rv = aImgRequest->GetFinalURI(getter_AddRefs(imgUri));
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIURL> imgUrl = do_QueryInterface(imgUri);
  NS_ENSURE_TRUE(imgUrl, NS_OK);

  nsAutoCString extension;
  rv = imgUrl->GetFileExtension(extension);
  NS_ENSURE_SUCCESS(rv, rv);

  nsCString mimeType;
  rv = aImgRequest->GetMimeType(getter_Copies(mimeType));
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIMIMEInfo> mimeInfo;
  mimeService->GetFromTypeAndExtension(mimeType, ""_ns,
                                       getter_AddRefs(mimeInfo));
  NS_ENSURE_TRUE(mimeInfo, NS_OK);

  nsAutoCString spec;
  rv = imgUri->GetSpec(spec);
  rv = imgUrl->GetSpec(spec);
  NS_ENSURE_SUCCESS(rv, rv);

  // pass out the image source string
  nsString imageSourceString;
  CopyUTF8toUTF16(spec, imageSourceString);

  nsCString mimeType;
  rv = aImgRequest->GetMimeType(getter_Copies(mimeType));
  bool validExtension;
  if (extension.IsEmpty() ||
      NS_FAILED(mimeInfo->ExtensionExists(extension, &validExtension)) ||
      !validExtension) {
    // Fix the file extension in the URL
    nsAutoCString primaryExtension;
    mimeInfo->GetPrimaryExtension(primaryExtension);
    if (!primaryExtension.IsEmpty()) {
      rv = NS_MutateURI(imgUri)
               .Apply(&nsIURLMutator::SetFileExtension, primaryExtension,
                      nullptr)
               .Finalize(imgUrl);
      NS_ENSURE_SUCCESS(rv, rv);
    }
  }

  nsAutoCString fileName;
  rv = aImgRequest->GetFileName(fileName);
  NS_ENSURE_SUCCESS(rv, rv);
  imgUrl->GetFileName(fileName);

  NS_UnescapeURL(fileName);

  // make the filename safe for the filesystem
  fileName.ReplaceChar(FILE_PATH_SEPARATOR FILE_ILLEGAL_CHARACTERS, '-');

  nsAutoString validFileName = NS_ConvertUTF8toUTF16(fileName);
  mimeService->ValidateFileNameForSaving(
      validFileName, mimeType, nsIMIMEService::VALIDATE_DEFAULT, validFileName);
  nsString imageDestFileName;
  CopyUTF8toUTF16(fileName, imageDestFileName);

  rv = AppendString(aTransferable, imageSourceString, kFilePromiseURLMime);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = AppendString(aTransferable, validFileName, kFilePromiseDestFilename);
  rv = AppendString(aTransferable, imageDestFileName, kFilePromiseDestFilename);
  NS_ENSURE_SUCCESS(rv, rv);

  aTransferable->SetRequestingPrincipal(node->NodePrincipal());
+69 −17
Original line number Diff line number Diff line
@@ -2152,28 +2152,80 @@ nsresult nsWebBrowserPersist::CalculateAndAppendFileExt(
    mMIMEService->GetTypeFromURI(uri, contentType);
  }

  // Validate the filename
  // Append the extension onto the file
  if (!contentType.IsEmpty()) {
    nsAutoString newFileName;
    if (NS_SUCCEEDED(mMIMEService->GetValidFileName(
            aChannel, contentType, aOriginalURIWithExtension,
            nsIMIMEService::VALIDATE_DEFAULT, newFileName))) {
    nsCOMPtr<nsIMIMEInfo> mimeInfo;
    mMIMEService->GetFromTypeAndExtension(contentType, ""_ns,
                                          getter_AddRefs(mimeInfo));

    nsCOMPtr<nsIFile> localFile;
    GetLocalFileFromURI(aURI, getter_AddRefs(localFile));

    if (mimeInfo) {
      nsCOMPtr<nsIURL> url(do_QueryInterface(aURI));
      NS_ENSURE_TRUE(url, NS_ERROR_FAILURE);

      nsAutoCString newFileName;
      url->GetFileName(newFileName);

      // Test if the current extension is current for the mime type
      bool hasExtension = false;
      int32_t ext = newFileName.RFind(".");
      if (ext != -1) {
        mimeInfo->ExtensionExists(Substring(newFileName, ext + 1),
                                  &hasExtension);
      }

      // Append the mime file extension
      nsAutoCString fileExt;
      if (!hasExtension) {
        // Test if previous extension is acceptable
        nsCOMPtr<nsIURL> oldurl(do_QueryInterface(aOriginalURIWithExtension));
        NS_ENSURE_TRUE(oldurl, NS_ERROR_FAILURE);
        oldurl->GetFileExtension(fileExt);
        bool useOldExt = false;
        if (!fileExt.IsEmpty()) {
          mimeInfo->ExtensionExists(fileExt, &useOldExt);
        }

        // If the url doesn't have an extension, or we don't know the extension,
        // try to use the primary extension for the type. If we don't know the
        // primary extension for the type, just continue with the url extension.
        if (!useOldExt) {
          nsAutoCString primaryExt;
          mimeInfo->GetPrimaryExtension(primaryExt);
          if (!primaryExt.IsEmpty()) {
            fileExt = primaryExt;
          }
        }

        if (!fileExt.IsEmpty()) {
          uint32_t newLength = newFileName.Length() + fileExt.Length() + 1;
          if (newLength > kDefaultMaxFilenameLength) {
            if (fileExt.Length() > kDefaultMaxFilenameLength / 2)
              fileExt.Truncate(kDefaultMaxFilenameLength / 2);

            uint32_t diff = kDefaultMaxFilenameLength - 1 - fileExt.Length();
            if (newFileName.Length() > diff) newFileName.Truncate(diff);
          }
          newFileName.Append('.');
          newFileName.Append(fileExt);
        }

        if (localFile) {
        localFile->SetLeafName(newFileName);
          localFile->SetLeafName(NS_ConvertUTF8toUTF16(newFileName));

          // Resync the URI with the file after the extension has been appended
        return NS_MutateURI(aURI)
          return NS_MutateURI(url)
              .Apply(&nsIFileURLMutator::SetFile, localFile)
              .Finalize(aOutURI);
        }
      return NS_MutateURI(aURI)
          .Apply(&nsIURLMutator::SetFileName,
                 NS_ConvertUTF16toUTF8(newFileName), nullptr)
        return NS_MutateURI(url)
            .Apply(&nsIURLMutator::SetFileName, newFileName, nullptr)
            .Finalize(aOutURI);
      }
    }
  }

  // TODO (:valentin) This method should always clone aURI
  aOutURI = aURI;
+0 −8
Original line number Diff line number Diff line
@@ -100,14 +100,6 @@ interface imgIRequest : nsIRequest

  readonly attribute string mimeType;

  /**
   * The filename that should be used when saving the image. This is determined
   * from the Content-Disposition, if present, or the uri of the image. This
   * filename should be validated using nsIMIMEService::GetValidFilenameForSaving
   * before creating the file.
   */
  readonly attribute ACString fileName;

  /**
   * Clone this request; the returned request will have aObserver as the
   * observer.  aObserver will be notified synchronously (before the clone()
Loading