From 4e800df64357a2957ef077b3c9450c03449992fc Mon Sep 17 00:00:00 2001 From: "mozilla.mano%sent.com" Date: Wed, 13 Jul 2005 19:31:11 +0000 Subject: [PATCH] Bug 300292 - rewrite nsMacShellService::SetDesktopBackground(). r=jhpedemonte/mconnor, a=asa. git-svn-id: svn://10.0.0.236/trunk@176040 18797224-902f-48f8-a5cc-f745e15eee43 --- .../shell/src/nsMacShellService.cpp | 79 ++++++++++--------- 1 file changed, 43 insertions(+), 36 deletions(-) diff --git a/mozilla/browser/components/shell/src/nsMacShellService.cpp b/mozilla/browser/components/shell/src/nsMacShellService.cpp index f6681506d49..5803992d638 100644 --- a/mozilla/browser/components/shell/src/nsMacShellService.cpp +++ b/mozilla/browser/components/shell/src/nsMacShellService.cpp @@ -37,9 +37,11 @@ * ***** END LICENSE BLOCK ***** */ #include "nsDirectoryServiceDefs.h" -#include "nsIDOM3Node.h" #include "nsIDOMElement.h" #include "nsIDOMHTMLImageElement.h" +#include "nsIImageLoadingContent.h" +#include "nsIDocument.h" +#include "nsIContent.h" #include "nsIObserverService.h" #include "nsIPrefService.h" #include "nsIServiceManager.h" @@ -207,49 +209,44 @@ nsMacShellService::SetDesktopBackground(nsIDOMElement* aElement, { // Note: We don't support aPosition on OS X. - nsCOMPtr image(do_QueryInterface(aElement)); - if (!image) - return NS_ERROR_INVALID_ARG; + // Get the image URI: + nsresult rv; + nsCOMPtr imageContent = do_QueryInterface(aElement, + &rv); + NS_ENSURE_SUCCESS(rv, rv); + nsCOMPtr imageURI; + rv = imageContent->GetCurrentURI(getter_AddRefs(imageURI)); + NS_ENSURE_SUCCESS(rv, rv); - // Get the image URL: - nsAutoString src; - image->GetSrc(src); + // We need the referer URI for nsIWebBrowserPersist::saveURI + nsCOMPtr content = do_QueryInterface(aElement, &rv); + NS_ENSURE_SUCCESS(rv, rv); + nsCOMPtr doc; + doc = content->GetOwnerDoc(); + if (!doc) + return NS_ERROR_FAILURE; - PRUint32 flags = nsIWebBrowserPersist::PERSIST_FLAGS_NO_CONVERSION | - nsIWebBrowserPersist::PERSIST_FLAGS_REPLACE_EXISTING_FILES | - nsIWebBrowserPersist::PERSIST_FLAGS_FROM_CACHE; - nsCOMPtr wbp - (do_CreateInstance("@mozilla.org/embedding/browser/nsWebBrowserPersist;1")); - if (!wbp) - return NS_ERROR_OUT_OF_MEMORY; - - wbp->SetPersistFlags(flags); - - nsAutoString baseURI; - nsCOMPtr node(do_QueryInterface(aElement)); - node->GetBaseURI(baseURI); - - nsCOMPtr imageURI, docURI; - NS_NewURI(getter_AddRefs(imageURI), src); - NS_NewURI(getter_AddRefs(docURI), baseURI); - - if (!imageURI || !docURI) - return NS_ERROR_OUT_OF_MEMORY; + nsIURI *docURI = doc->GetDocumentURI(); + if (!docURI) + return NS_ERROR_FAILURE; + // Get the desired image file name nsCOMPtr imageURL(do_QueryInterface(imageURI)); + if (!imageURL) { + // XXXmano (bug 300293): Non-URL images (e.g. the data: protocol) are not + // yet supported. What filename should we take here? + return NS_ERROR_NOT_IMPLEMENTED; + } + nsCAutoString fileName; imageURL->GetFileName(fileName); - - nsresult rv; - nsCOMPtr fileLocator(do_GetService("@mozilla.org/file/directory_service;1", - &rv)); - if (NS_FAILED(rv)) - return rv; + nsCOMPtr fileLocator + (do_GetService("@mozilla.org/file/directory_service;1", &rv)); + NS_ENSURE_SUCCESS(rv, rv); // Get the current user's "Pictures" folder (That's ~/Pictures): fileLocator->Get(NS_OSX_PICTURE_DOCUMENTS_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(mBackgroundFile)); - if (!mBackgroundFile) return NS_ERROR_OUT_OF_MEMORY; @@ -259,10 +256,20 @@ nsMacShellService::SetDesktopBackground(nsIDOMElement* aElement, // and add the imgage file name itself: mBackgroundFile->Append(fileNameUnicode); + // Download the image; the desktop background will be set in OnStateChange() + nsCOMPtr wbp + (do_CreateInstance("@mozilla.org/embedding/browser/nsWebBrowserPersist;1", &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + PRUint32 flags = nsIWebBrowserPersist::PERSIST_FLAGS_NO_CONVERSION | + nsIWebBrowserPersist::PERSIST_FLAGS_REPLACE_EXISTING_FILES | + nsIWebBrowserPersist::PERSIST_FLAGS_FROM_CACHE; + + wbp->SetPersistFlags(flags); wbp->SetProgressListener(this); - // Download the image; the desktop background will be set in OnStateChange() - return wbp->SaveURI(imageURI, nsnull, docURI, nsnull, nsnull, mBackgroundFile); + return wbp->SaveURI(imageURI, nsnull, docURI, nsnull, nsnull, + mBackgroundFile); } NS_IMETHODIMP