Commit ffd18bce authored by Neil Deakin's avatar Neil Deakin
Browse files

Bug 1746052, add a means to get the filename that should be used for saving an...

Bug 1746052, add a means to get the filename that should be used for saving an image to disk, r=aosmond

Differential Revision: https://phabricator.services.mozilla.com/D135950
parent 2b3fdc36
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -100,6 +100,14 @@ 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()
+26 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@
#include "nsIScriptSecurityManager.h"
#include "nsComponentManagerUtils.h"
#include "nsContentUtils.h"
#include "nsEscape.h"

#include "plstr.h"   // PL_strcasestr(...)
#include "prtime.h"  // for PR_Now
@@ -38,6 +39,7 @@
#include "nsIProtocolHandler.h"
#include "imgIRequest.h"
#include "nsProperties.h"
#include "nsIURL.h"

#include "mozilla/IntegerPrintfMacros.h"
#include "mozilla/SizeOfState.h"
@@ -461,6 +463,30 @@ already_AddRefed<image::Image> imgRequest::GetImage() const {
  return image.forget();
}

void imgRequest::GetFileName(nsACString& aFileName) {
  nsAutoString fileName;

  nsCOMPtr<nsISupportsCString> supportscstr;
  if (NS_SUCCEEDED(mProperties->Get("content-disposition",
                                    NS_GET_IID(nsISupportsCString),
                                    getter_AddRefs(supportscstr))) &&
      supportscstr) {
    nsAutoCString cdHeader;
    supportscstr->GetData(cdHeader);
    NS_GetFilenameFromDisposition(fileName, cdHeader);
  }

  if (fileName.IsEmpty()) {
    nsCOMPtr<nsIURL> imgUrl(do_QueryInterface(mURI));
    if (imgUrl) {
      imgUrl->GetFileName(aFileName);
      NS_UnescapeURL(aFileName);
    }
  } else {
    aFileName = NS_ConvertUTF16toUTF8(fileName);
  }
}

int32_t imgRequest::Priority() const {
  int32_t priority = nsISupportsPriority::PRIORITY_NORMAL;
  nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(mRequest);
+2 −0
Original line number Diff line number Diff line
@@ -151,6 +151,8 @@ class imgRequest final : public nsIStreamListener,
  /// Returns a non-owning pointer to this imgRequest's MIME type.
  const char* GetMimeType() const { return mContentType.get(); }

  void GetFileName(nsACString& aFileName);

  /// @return the priority of the underlying network request, or
  /// PRIORITY_NORMAL if it doesn't support nsISupportsPriority.
  int32_t Priority() const;
+10 −0
Original line number Diff line number Diff line
@@ -747,6 +747,16 @@ imgRequestProxy::GetMimeType(char** aMimeType) {
  return NS_OK;
}

NS_IMETHODIMP
imgRequestProxy::GetFileName(nsACString& aFileName) {
  if (!GetOwner()) {
    return NS_ERROR_FAILURE;
  }

  GetOwner()->GetFileName(aFileName);
  return NS_OK;
}

imgRequestProxy* imgRequestProxy::NewClonedProxy() {
  return new imgRequestProxy();
}