Bug 1013211 - Use the final resource URI after redirects etc as the source URI when dragging an image. r=roc

This commit is contained in:
Mats Palmgren 2014-11-10 22:06:24 +00:00
parent 0379b8ef89
commit b4f43a55d9
5 changed files with 65 additions and 23 deletions

View File

@ -515,14 +515,6 @@ DragDataProducer::Produce(DataTransfer* aDataTransfer,
// grab the href as the url, use alt text as the title of the
// area if it's there. the drag data is the image tag and src
// attribute.
nsCOMPtr<nsIURI> imageURI;
image->GetCurrentURI(getter_AddRefs(imageURI));
if (imageURI) {
nsAutoCString spec;
imageURI->GetSpec(spec);
CopyUTF8toUTF16(spec, mUrlString);
}
nsCOMPtr<nsIDOMElement> imageElement(do_QueryInterface(image));
// XXXbz Shouldn't we use the "title" attr for title? Using
// "alt" seems very wrong....
@ -530,13 +522,10 @@ DragDataProducer::Produce(DataTransfer* aDataTransfer,
imageElement->GetAttribute(NS_LITERAL_STRING("alt"), mTitleString);
}
if (mTitleString.IsEmpty()) {
mTitleString = mUrlString;
}
nsCOMPtr<imgIRequest> imgRequest;
mUrlString.Truncate();
// grab the image data, and its request.
nsCOMPtr<imgIRequest> imgRequest;
nsCOMPtr<imgIContainer> img =
nsContentUtils::GetImageFromContent(image,
getter_AddRefs(imgRequest));
@ -547,7 +536,7 @@ DragDataProducer::Produce(DataTransfer* aDataTransfer,
// Fix the file extension in the URL if necessary
if (imgRequest && mimeService) {
nsCOMPtr<nsIURI> imgUri;
imgRequest->GetURI(getter_AddRefs(imgUri));
imgRequest->GetCurrentURI(getter_AddRefs(imgUri));
nsCOMPtr<nsIURL> imgUrl(do_QueryInterface(imgUri));
@ -568,6 +557,7 @@ DragDataProducer::Produce(DataTransfer* aDataTransfer,
// pass out the image source string
CopyUTF8toUTF16(spec, mImageSourceString);
mUrlString = mImageSourceString;
bool validExtension;
if (extension.IsEmpty() ||
@ -602,6 +592,18 @@ DragDataProducer::Produce(DataTransfer* aDataTransfer,
}
}
}
if (mUrlString.IsEmpty()) {
nsCOMPtr<nsIURI> imageURI;
image->GetCurrentURI(getter_AddRefs(imageURI));
if (imageURI) {
nsAutoCString spec;
imageURI->GetSpec(spec);
CopyUTF8toUTF16(spec, mUrlString);
}
}
if (mTitleString.IsEmpty()) {
mTitleString = mUrlString;
}
if (parentLink) {
// If we are dragging around an image in an anchor, then we

View File

@ -19,7 +19,7 @@ interface nsIPrincipal;
* @version 0.1
* @see imagelib2
*/
[scriptable, builtinclass, uuid(710f22f0-558b-11e4-8ed6-0800200c9a66)]
[scriptable, builtinclass, uuid(dc61f0ea-4139-4c2a-ae69-cec82d33e089)]
interface imgIRequest : nsIRequest
{
/**
@ -83,6 +83,11 @@ interface imgIRequest : nsIRequest
*/
readonly attribute nsIURI URI;
/**
* The URI of the resource we ended up loading after all redirects, etc.
*/
readonly attribute nsIURI currentURI;
readonly attribute imgINotificationObserver notificationObserver;
readonly attribute string mimeType;

View File

@ -40,14 +40,17 @@ using namespace mozilla;
using namespace mozilla::image;
#if defined(PR_LOGGING)
PRLogModuleInfo *
PRLogModuleInfo*
GetImgLog()
{
static PRLogModuleInfo *sImgLog;
static PRLogModuleInfo* sImgLog;
if (!sImgLog)
sImgLog = PR_NewLogModule("imgRequest");
return sImgLog;
}
#define LOG_TEST(level) (GetImgLog() && PR_LOG_TEST(GetImgLog(), (level)))
#else
#define LOG_TEST(level) false
#endif
NS_IMPL_ISUPPORTS(imgRequest,
@ -367,6 +370,21 @@ nsresult imgRequest::GetURI(ImageURL **aURI)
return NS_ERROR_FAILURE;
}
nsresult imgRequest::GetCurrentURI(nsIURI **aURI)
{
MOZ_ASSERT(aURI);
LOG_FUNC(GetImgLog(), "imgRequest::GetCurrentURI");
if (mCurrentURI) {
*aURI = mCurrentURI;
NS_ADDREF(*aURI);
return NS_OK;
}
return NS_ERROR_FAILURE;
}
nsresult imgRequest::GetImageErrorCode()
{
return mImageErrorCode;
@ -1064,16 +1082,24 @@ imgRequest::OnRedirectVerifyCallback(nsresult result)
mTimedChannel = do_QueryInterface(mChannel);
mNewRedirectChannel = nullptr;
#if defined(PR_LOGGING)
nsAutoCString oldspec;
if (mCurrentURI)
mCurrentURI->GetSpec(oldspec);
LOG_MSG_WITH_PARAM(GetImgLog(), "imgRequest::OnChannelRedirect", "old", oldspec.get());
#endif
if (LOG_TEST(PR_LOG_DEBUG)) {
nsAutoCString spec;
if (mCurrentURI)
mCurrentURI->GetSpec(spec);
LOG_MSG_WITH_PARAM(GetImgLog(), "imgRequest::OnChannelRedirect", "old", spec.get());
}
// make sure we have a protocol that returns data rather than opens
// an external application, e.g. mailto:
mChannel->GetURI(getter_AddRefs(mCurrentURI));
if (LOG_TEST(PR_LOG_DEBUG)) {
nsAutoCString spec;
if (mCurrentURI)
mCurrentURI->GetSpec(spec);
LOG_MSG_WITH_PARAM(GetImgLog(), "imgRequest::OnChannelRedirect", "new", spec.get());
}
bool doesNotReturnData = false;
nsresult rv =
NS_URIChainHasFlags(mCurrentURI, nsIProtocolHandler::URI_DOES_NOT_RETURN_DATA,

View File

@ -137,6 +137,7 @@ public:
// OK to use on any thread.
nsresult GetURI(ImageURL **aURI);
nsresult GetCurrentURI(nsIURI **aURI);
nsresult GetImageErrorCode(void);

View File

@ -537,6 +537,14 @@ NS_IMETHODIMP imgRequestProxy::GetURI(nsIURI **aURI)
return NS_OK;
}
nsresult imgRequestProxy::GetCurrentURI(nsIURI **aURI)
{
if (!GetOwner())
return NS_ERROR_FAILURE;
return GetOwner()->GetCurrentURI(aURI);
}
nsresult imgRequestProxy::GetURI(ImageURL **aURI)
{
if (!mURI)