Skip to content

Commit

Permalink
Bug 1746052, add a means to get the filename that should be used for …
Browse files Browse the repository at this point in the history
…saving an image to disk, r=aosmond

Differential Revision: https://phabricator.services.mozilla.com/D135950
  • Loading branch information
EnnDeakin2 committed May 3, 2022
1 parent 30d6445 commit 976c5b2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions image/imgIRequest.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
26 changes: 26 additions & 0 deletions image/imgRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
#include "nsIScriptSecurityManager.h"
#include "nsComponentManagerUtils.h"
#include "nsContentUtils.h"
#include "nsEscape.h"

#include "plstr.h" // PL_strcasestr(...)
#include "prtime.h" // for PR_Now
#include "nsNetUtil.h"
#include "nsIProtocolHandler.h"
#include "imgIRequest.h"
#include "nsProperties.h"
#include "nsIURL.h"

#include "mozilla/IntegerPrintfMacros.h"
#include "mozilla/SizeOfState.h"
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions image/imgRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions image/imgRequestProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 976c5b2

Please sign in to comment.