2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Brian Ryner.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2000
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Scott MacGregor <mscott@netscape.com>
|
|
|
|
* Neil Rashbrook <neil@parkwaycc.co.uk>
|
|
|
|
* Ben Goodger <ben@mozilla.org>
|
2010-09-14 09:15:32 -07:00
|
|
|
* Siddharth Agarwal <sid.bugzilla@gmail.com>
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
|
|
|
|
#include "nsIconChannel.h"
|
|
|
|
#include "nsIIconURI.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsMimeTypes.h"
|
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsIStringStream.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsIFileURL.h"
|
|
|
|
#include "nsIMIMEService.h"
|
|
|
|
#include "nsCExternalHandlerService.h"
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
|
2009-04-12 07:55:29 -07:00
|
|
|
#if MOZ_WINSDK_TARGETVER >= MOZ_NTDDI_LONGHORN
|
2008-12-20 17:31:35 -08:00
|
|
|
#ifdef _WIN32_WINNT
|
|
|
|
#undef _WIN32_WINNT
|
|
|
|
#endif
|
|
|
|
#define _WIN32_WINNT 0x0600
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// we need windows.h to read out registry information...
|
|
|
|
#include <windows.h>
|
|
|
|
#include <shellapi.h>
|
|
|
|
#include <shlobj.h>
|
2011-04-29 14:39:01 -07:00
|
|
|
#include <objbase.h>
|
2008-08-27 14:44:55 -07:00
|
|
|
#include <wchar.h>
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
struct ICONFILEHEADER {
|
|
|
|
PRUint16 ifhReserved;
|
|
|
|
PRUint16 ifhType;
|
|
|
|
PRUint16 ifhCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ICONENTRY {
|
|
|
|
PRInt8 ieWidth;
|
|
|
|
PRInt8 ieHeight;
|
|
|
|
PRUint8 ieColors;
|
|
|
|
PRUint8 ieReserved;
|
|
|
|
PRUint16 iePlanes;
|
|
|
|
PRUint16 ieBitCount;
|
|
|
|
PRUint32 ieSizeImage;
|
|
|
|
PRUint32 ieFileOffset;
|
|
|
|
};
|
|
|
|
|
2009-04-12 07:55:29 -07:00
|
|
|
#if MOZ_WINSDK_TARGETVER >= MOZ_NTDDI_LONGHORN
|
2008-12-20 17:31:35 -08:00
|
|
|
typedef HRESULT (WINAPI*SHGetStockIconInfoPtr) (SHSTOCKICONID siid, UINT uFlags, SHSTOCKICONINFO *psii);
|
|
|
|
|
|
|
|
// Match stock icons with names
|
|
|
|
static SHSTOCKICONID GetStockIconIDForName(const nsACString &aStockName)
|
|
|
|
{
|
|
|
|
// UAC shield icon
|
|
|
|
if (aStockName == NS_LITERAL_CSTRING("uac-shield"))
|
|
|
|
return SIID_SHIELD;
|
|
|
|
|
|
|
|
return SIID_INVALID;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// nsIconChannel methods
|
|
|
|
nsIconChannel::nsIconChannel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIconChannel::~nsIconChannel()
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS4(nsIconChannel,
|
|
|
|
nsIChannel,
|
|
|
|
nsIRequest,
|
|
|
|
nsIRequestObserver,
|
|
|
|
nsIStreamListener)
|
|
|
|
|
|
|
|
nsresult nsIconChannel::Init(nsIURI* uri)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(uri, "no uri");
|
|
|
|
mUrl = uri;
|
2008-10-15 13:05:23 -07:00
|
|
|
mOriginalURI = uri;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv;
|
|
|
|
mPump = do_CreateInstance(NS_INPUTSTREAMPUMP_CONTRACTID, &rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIRequest methods:
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::GetName(nsACString &result)
|
|
|
|
{
|
|
|
|
return mUrl->GetSpec(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::IsPending(PRBool *result)
|
|
|
|
{
|
|
|
|
return mPump->IsPending(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::GetStatus(nsresult *status)
|
|
|
|
{
|
|
|
|
return mPump->GetStatus(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::Cancel(nsresult status)
|
|
|
|
{
|
|
|
|
return mPump->Cancel(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::Suspend(void)
|
|
|
|
{
|
|
|
|
return mPump->Suspend();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::Resume(void)
|
|
|
|
{
|
|
|
|
return mPump->Resume();
|
|
|
|
}
|
|
|
|
NS_IMETHODIMP nsIconChannel::GetLoadGroup(nsILoadGroup* *aLoadGroup)
|
|
|
|
{
|
|
|
|
*aLoadGroup = mLoadGroup;
|
|
|
|
NS_IF_ADDREF(*aLoadGroup);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::SetLoadGroup(nsILoadGroup* aLoadGroup)
|
|
|
|
{
|
|
|
|
mLoadGroup = aLoadGroup;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::GetLoadFlags(PRUint32 *aLoadAttributes)
|
|
|
|
{
|
|
|
|
return mPump->GetLoadFlags(aLoadAttributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::SetLoadFlags(PRUint32 aLoadAttributes)
|
|
|
|
{
|
|
|
|
return mPump->SetLoadFlags(aLoadAttributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIChannel methods:
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::GetOriginalURI(nsIURI* *aURI)
|
|
|
|
{
|
2008-10-15 13:05:23 -07:00
|
|
|
*aURI = mOriginalURI;
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ADDREF(*aURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::SetOriginalURI(nsIURI* aURI)
|
|
|
|
{
|
2008-10-15 13:05:23 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
2007-03-22 10:30:00 -07:00
|
|
|
mOriginalURI = aURI;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::GetURI(nsIURI* *aURI)
|
|
|
|
{
|
|
|
|
*aURI = mUrl;
|
|
|
|
NS_IF_ADDREF(*aURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIconChannel::Open(nsIInputStream **_retval)
|
|
|
|
{
|
|
|
|
return MakeInputStream(_retval, PR_FALSE);
|
|
|
|
}
|
|
|
|
|
2008-08-27 14:44:55 -07:00
|
|
|
nsresult nsIconChannel::ExtractIconInfoFromUrl(nsIFile ** aLocalFile, PRUint32 * aDesiredImageSize, nsCString &aContentType, nsCString &aFileExtension)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIMozIconURI> iconURI (do_QueryInterface(mUrl, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
iconURI->GetImageSize(aDesiredImageSize);
|
|
|
|
iconURI->GetContentType(aContentType);
|
|
|
|
iconURI->GetFileExtension(aFileExtension);
|
|
|
|
|
2010-05-13 13:20:30 -07:00
|
|
|
nsCOMPtr<nsIURL> url;
|
|
|
|
rv = iconURI->GetIconURL(getter_AddRefs(url));
|
|
|
|
if (NS_FAILED(rv) || !url) return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-13 13:20:30 -07:00
|
|
|
nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(url, &rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv) || !fileURL) return NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
rv = fileURL->GetFile(getter_AddRefs(file));
|
|
|
|
if (NS_FAILED(rv) || !file) return NS_OK;
|
2007-08-15 17:22:28 -07:00
|
|
|
|
|
|
|
return file->Clone(aLocalFile);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::AsyncOpen(nsIStreamListener *aListener, nsISupports *ctxt)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInputStream> inStream;
|
|
|
|
nsresult rv = MakeInputStream(getter_AddRefs(inStream), PR_TRUE);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
// Init our streampump
|
2011-03-31 17:15:27 -07:00
|
|
|
rv = mPump->Init(inStream, PRInt64(-1), PRInt64(-1), 0, 0, PR_FALSE);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
rv = mPump->AsyncRead(this, ctxt);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// Store our real listener
|
|
|
|
mListener = aListener;
|
|
|
|
// Add ourself to the load group, if available
|
|
|
|
if (mLoadGroup)
|
|
|
|
mLoadGroup->AddRequest(this, nsnull);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2008-08-27 14:44:55 -07:00
|
|
|
static DWORD GetSpecialFolderIcon(nsIFile* aFile, int aFolder, SHFILEINFOW* aSFI, UINT aInfoFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
DWORD shellResult = 0;
|
|
|
|
|
|
|
|
if (!aFile)
|
|
|
|
return shellResult;
|
|
|
|
|
2008-08-27 14:44:55 -07:00
|
|
|
PRUnichar fileNativePath[MAX_PATH];
|
|
|
|
nsAutoString fileNativePathStr;
|
|
|
|
aFile->GetPath(fileNativePathStr);
|
|
|
|
::GetShortPathNameW(fileNativePathStr.get(), fileNativePath, NS_ARRAY_LENGTH(fileNativePath));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
LPITEMIDLIST idList;
|
|
|
|
HRESULT hr = ::SHGetSpecialFolderLocation(NULL, aFolder, &idList);
|
|
|
|
if (SUCCEEDED(hr)) {
|
2008-08-27 14:44:55 -07:00
|
|
|
PRUnichar specialNativePath[MAX_PATH];
|
|
|
|
::SHGetPathFromIDListW(idList, specialNativePath);
|
|
|
|
::GetShortPathNameW(specialNativePath, specialNativePath, NS_ARRAY_LENGTH(specialNativePath));
|
2008-03-11 15:46:26 -07:00
|
|
|
|
2008-08-27 14:44:55 -07:00
|
|
|
if (!wcsicmp(fileNativePath, specialNativePath)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
aInfoFlags |= (SHGFI_PIDL | SHGFI_SYSICONINDEX);
|
2008-08-27 14:44:55 -07:00
|
|
|
shellResult = ::SHGetFileInfoW((LPCWSTR)(LPCITEMIDLIST)idList, 0, aSFI,
|
|
|
|
sizeof(*aSFI), aInfoFlags);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2011-04-29 14:39:01 -07:00
|
|
|
CoTaskMemFree(idList);
|
2007-03-22 10:30:00 -07:00
|
|
|
return shellResult;
|
|
|
|
}
|
|
|
|
|
2008-12-20 17:31:35 -08:00
|
|
|
static UINT GetSizeInfoFlag(PRUint32 aDesiredImageSize)
|
|
|
|
{
|
|
|
|
UINT infoFlag;
|
|
|
|
if (aDesiredImageSize > 16)
|
|
|
|
infoFlag = SHGFI_SHELLICONSIZE;
|
2009-07-28 23:19:34 -07:00
|
|
|
else
|
2008-12-20 17:31:35 -08:00
|
|
|
infoFlag = SHGFI_SMALLICON;
|
|
|
|
|
|
|
|
return infoFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsIconChannel::GetHIconFromFile(HICON *hIcon)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsXPIDLCString contentType;
|
2008-08-27 14:44:55 -07:00
|
|
|
nsCString fileExt;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIFile> localFile; // file we want an icon for
|
|
|
|
PRUint32 desiredImageSize;
|
2008-04-04 02:16:44 -07:00
|
|
|
nsresult rv = ExtractIconInfoFromUrl(getter_AddRefs(localFile), &desiredImageSize, contentType, fileExt);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// if the file exists, we are going to use it's real attributes...otherwise we only want to use it for it's extension...
|
2008-08-27 14:44:55 -07:00
|
|
|
SHFILEINFOW sfi;
|
2007-03-22 10:30:00 -07:00
|
|
|
UINT infoFlags = SHGFI_ICON;
|
|
|
|
|
|
|
|
PRBool fileExists = PR_FALSE;
|
|
|
|
|
2009-06-24 14:50:36 -07:00
|
|
|
nsAutoString filePath;
|
|
|
|
CopyASCIItoUTF16(fileExt, filePath);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (localFile)
|
|
|
|
{
|
2007-08-15 17:22:28 -07:00
|
|
|
rv = localFile->Normalize();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2009-06-24 14:50:36 -07:00
|
|
|
localFile->GetPath(filePath);
|
2007-08-15 17:22:28 -07:00
|
|
|
if (filePath.Length() < 2 || filePath[1] != ':')
|
|
|
|
return NS_ERROR_MALFORMED_URI; // UNC
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (filePath.Last() == ':')
|
|
|
|
filePath.Append('\\');
|
|
|
|
else {
|
|
|
|
localFile->Exists(&fileExists);
|
|
|
|
if (!fileExists)
|
2009-06-24 14:50:36 -07:00
|
|
|
localFile->GetLeafName(filePath);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fileExists)
|
|
|
|
infoFlags |= SHGFI_USEFILEATTRIBUTES;
|
|
|
|
|
2008-12-20 17:31:35 -08:00
|
|
|
infoFlags |= GetSizeInfoFlag(desiredImageSize);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// if we have a content type... then use it! but for existing files, we want
|
|
|
|
// to show their real icon.
|
|
|
|
if (!fileExists && !contentType.IsEmpty())
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIMIMEService> mimeService (do_GetService(NS_MIMESERVICE_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-04-04 02:16:44 -07:00
|
|
|
nsCAutoString defFileExt;
|
|
|
|
mimeService->GetPrimaryExtension(contentType, fileExt, defFileExt);
|
2007-03-22 10:30:00 -07:00
|
|
|
// If the mime service does not know about this mime type, we show
|
|
|
|
// the generic icon.
|
|
|
|
// In any case, we need to insert a '.' before the extension.
|
2009-06-24 14:50:36 -07:00
|
|
|
filePath = NS_LITERAL_STRING(".") + NS_ConvertUTF8toUTF16(defFileExt);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Is this the "Desktop" folder?
|
|
|
|
DWORD shellResult = GetSpecialFolderIcon(localFile, CSIDL_DESKTOP, &sfi, infoFlags);
|
|
|
|
if (!shellResult) {
|
|
|
|
// Is this the "My Documents" folder?
|
|
|
|
shellResult = GetSpecialFolderIcon(localFile, CSIDL_PERSONAL, &sfi, infoFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
// There are other "Special Folders" and Namespace entities that we are not
|
|
|
|
// fetching icons for, see:
|
|
|
|
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/enums/csidl.asp
|
|
|
|
// If we ever need to get them, code to do so would be inserted here.
|
|
|
|
|
|
|
|
// Not a special folder, or something else failed above.
|
|
|
|
if (!shellResult)
|
2009-06-24 14:50:36 -07:00
|
|
|
shellResult = ::SHGetFileInfoW(filePath.get(),
|
2008-08-27 14:44:55 -07:00
|
|
|
FILE_ATTRIBUTE_ARCHIVE, &sfi, sizeof(sfi), infoFlags);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (shellResult && sfi.hIcon)
|
2008-12-20 17:31:35 -08:00
|
|
|
*hIcon = sfi.hIcon;
|
|
|
|
else
|
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2009-04-12 07:55:29 -07:00
|
|
|
#if MOZ_WINSDK_TARGETVER >= MOZ_NTDDI_LONGHORN
|
2008-12-20 17:31:35 -08:00
|
|
|
nsresult nsIconChannel::GetStockHIcon(nsIMozIconURI *aIconURI, HICON *hIcon)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// We can only do this on Vista or above
|
|
|
|
HMODULE hShellDLL = ::LoadLibraryW(L"shell32.dll");
|
|
|
|
SHGetStockIconInfoPtr pSHGetStockIconInfo =
|
|
|
|
(SHGetStockIconInfoPtr) ::GetProcAddress(hShellDLL, "SHGetStockIconInfo");
|
|
|
|
|
|
|
|
if (pSHGetStockIconInfo)
|
|
|
|
{
|
|
|
|
PRUint32 desiredImageSize;
|
|
|
|
aIconURI->GetImageSize(&desiredImageSize);
|
|
|
|
nsCAutoString stockIcon;
|
|
|
|
aIconURI->GetStockIcon(stockIcon);
|
|
|
|
|
|
|
|
SHSTOCKICONID stockIconID = GetStockIconIDForName(stockIcon);
|
|
|
|
if (stockIconID == SIID_INVALID)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
UINT infoFlags = SHGSI_ICON;
|
|
|
|
infoFlags |= GetSizeInfoFlag(desiredImageSize);
|
|
|
|
|
|
|
|
SHSTOCKICONINFO sii = {0};
|
|
|
|
sii.cbSize = sizeof(sii);
|
|
|
|
HRESULT hr = pSHGetStockIconInfo(stockIconID, infoFlags, &sii);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
*hIcon = sii.hIcon;
|
|
|
|
else
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hShellDLL)
|
|
|
|
::FreeLibrary(hShellDLL);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-09-14 09:15:32 -07:00
|
|
|
// Given a BITMAPINFOHEADER, returns the size of the color table.
|
|
|
|
static int GetColorTableSize(BITMAPINFOHEADER* aHeader)
|
|
|
|
{
|
|
|
|
int colorTableSize = -1;
|
|
|
|
|
|
|
|
// http://msdn.microsoft.com/en-us/library/dd183376%28v=VS.85%29.aspx
|
|
|
|
switch (aHeader->biBitCount) {
|
|
|
|
case 0:
|
|
|
|
colorTableSize = 0;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
colorTableSize = 2 * sizeof(RGBQUAD);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
case 8:
|
|
|
|
{
|
|
|
|
// The maximum possible size for the color table is 2**bpp, so check for
|
|
|
|
// that and fail if we're not in those bounds
|
|
|
|
unsigned int maxEntries = 1 << (aHeader->biBitCount);
|
|
|
|
if (aHeader->biClrUsed > 0 && aHeader->biClrUsed <= maxEntries)
|
|
|
|
colorTableSize = aHeader->biClrUsed * sizeof(RGBQUAD);
|
|
|
|
else if (aHeader->biClrUsed == 0)
|
|
|
|
colorTableSize = maxEntries * sizeof(RGBQUAD);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 16:
|
|
|
|
case 32:
|
2011-08-29 22:44:04 -07:00
|
|
|
// If we have BI_BITFIELDS compression, we would normally need 3 DWORDS for
|
|
|
|
// the bitfields mask which would be stored in the color table; However,
|
|
|
|
// we instead force the bitmap to request data of type BI_RGB so the color
|
|
|
|
// table should be of size 0.
|
|
|
|
// Setting aHeader->biCompression = BI_RGB forces the later call to
|
|
|
|
// GetDIBits to return to us BI_RGB data.
|
|
|
|
if (aHeader->biCompression == BI_BITFIELDS)
|
|
|
|
aHeader->biCompression = BI_RGB;
|
2010-09-14 09:15:32 -07:00
|
|
|
colorTableSize = 0;
|
|
|
|
break;
|
|
|
|
case 24:
|
|
|
|
colorTableSize = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (colorTableSize < 0)
|
|
|
|
NS_WARNING("Unable to figure out the color table size for this bitmap");
|
|
|
|
|
|
|
|
return colorTableSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a header and a size, creates a freshly allocated BITMAPINFO structure.
|
|
|
|
// It is the caller's responsibility to null-check and delete the structure.
|
|
|
|
static BITMAPINFO* CreateBitmapInfo(BITMAPINFOHEADER* aHeader,
|
|
|
|
size_t aColorTableSize)
|
|
|
|
{
|
|
|
|
BITMAPINFO* bmi = (BITMAPINFO*) ::operator new(sizeof(BITMAPINFOHEADER) +
|
|
|
|
aColorTableSize,
|
|
|
|
mozilla::fallible_t());
|
|
|
|
if (bmi) {
|
|
|
|
memcpy(bmi, aHeader, sizeof(BITMAPINFOHEADER));
|
|
|
|
memset(bmi->bmiColors, 0, aColorTableSize);
|
|
|
|
}
|
|
|
|
return bmi;
|
|
|
|
}
|
|
|
|
|
2008-12-20 17:31:35 -08:00
|
|
|
nsresult nsIconChannel::MakeInputStream(nsIInputStream** _retval, PRBool nonBlocking)
|
|
|
|
{
|
|
|
|
// Check whether the icon requested's a file icon or a stock icon
|
2009-02-19 18:56:49 -08:00
|
|
|
nsresult rv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
// GetDIBits does not exist on windows mobile.
|
2008-12-20 17:31:35 -08:00
|
|
|
HICON hIcon = NULL;
|
|
|
|
|
2009-04-12 07:55:29 -07:00
|
|
|
#if MOZ_WINSDK_TARGETVER >= MOZ_NTDDI_LONGHORN
|
2008-12-20 17:31:35 -08:00
|
|
|
nsCOMPtr<nsIMozIconURI> iconURI(do_QueryInterface(mUrl, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCAutoString stockIcon;
|
|
|
|
iconURI->GetStockIcon(stockIcon);
|
|
|
|
if (!stockIcon.IsEmpty())
|
|
|
|
rv = GetStockHIcon(iconURI, &hIcon);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
rv = GetHIconFromFile(&hIcon);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (hIcon)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// we got a handle to an icon. Now we want to get a bitmap for the icon using GetIconInfo....
|
|
|
|
ICONINFO iconInfo;
|
2008-12-20 17:31:35 -08:00
|
|
|
if (GetIconInfo(hIcon, &iconInfo))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// we got the bitmaps, first find out their size
|
|
|
|
HDC hDC = CreateCompatibleDC(NULL); // get a device context for the screen.
|
2010-09-14 09:15:32 -07:00
|
|
|
BITMAPINFOHEADER maskHeader = {sizeof(BITMAPINFOHEADER)};
|
|
|
|
BITMAPINFOHEADER colorHeader = {sizeof(BITMAPINFOHEADER)};
|
|
|
|
int colorTableSize, maskTableSize;
|
|
|
|
if (GetDIBits(hDC, iconInfo.hbmMask, 0, 0, NULL, (BITMAPINFO*)&maskHeader, DIB_RGB_COLORS) &&
|
|
|
|
GetDIBits(hDC, iconInfo.hbmColor, 0, 0, NULL, (BITMAPINFO*)&colorHeader, DIB_RGB_COLORS) &&
|
|
|
|
maskHeader.biHeight == colorHeader.biHeight &&
|
|
|
|
maskHeader.biWidth == colorHeader.biWidth &&
|
|
|
|
colorHeader.biBitCount > 8 &&
|
|
|
|
colorHeader.biSizeImage > 0 &&
|
|
|
|
maskHeader.biSizeImage > 0 &&
|
|
|
|
(colorTableSize = GetColorTableSize(&colorHeader)) >= 0 &&
|
|
|
|
(maskTableSize = GetColorTableSize(&maskHeader)) >= 0) {
|
2009-07-28 23:19:31 -07:00
|
|
|
PRUint32 iconSize = sizeof(ICONFILEHEADER) +
|
|
|
|
sizeof(ICONENTRY) +
|
|
|
|
sizeof(BITMAPINFOHEADER) +
|
2010-09-14 09:15:32 -07:00
|
|
|
colorHeader.biSizeImage +
|
|
|
|
maskHeader.biSizeImage;
|
2009-07-28 23:19:31 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
char *buffer = new char[iconSize];
|
|
|
|
if (!buffer)
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
else {
|
2009-07-28 23:19:31 -07:00
|
|
|
char *whereTo = buffer;
|
|
|
|
int howMuch;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// the data starts with an icon file header
|
2009-07-28 23:19:31 -07:00
|
|
|
ICONFILEHEADER iconHeader;
|
|
|
|
iconHeader.ifhReserved = 0;
|
|
|
|
iconHeader.ifhType = 1;
|
|
|
|
iconHeader.ifhCount = 1;
|
|
|
|
howMuch = sizeof(ICONFILEHEADER);
|
|
|
|
memcpy(whereTo, &iconHeader, howMuch);
|
|
|
|
whereTo += howMuch;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-28 23:19:31 -07:00
|
|
|
// followed by the single icon entry
|
|
|
|
ICONENTRY iconEntry;
|
2010-09-14 09:15:32 -07:00
|
|
|
iconEntry.ieWidth = colorHeader.biWidth;
|
|
|
|
iconEntry.ieHeight = colorHeader.biHeight;
|
2009-07-28 23:19:31 -07:00
|
|
|
iconEntry.ieColors = 0;
|
|
|
|
iconEntry.ieReserved = 0;
|
|
|
|
iconEntry.iePlanes = 1;
|
2010-09-14 09:15:32 -07:00
|
|
|
iconEntry.ieBitCount = colorHeader.biBitCount;
|
2009-07-28 23:19:31 -07:00
|
|
|
iconEntry.ieSizeImage = sizeof(BITMAPINFOHEADER) +
|
2010-09-14 09:15:32 -07:00
|
|
|
colorHeader.biSizeImage +
|
|
|
|
maskHeader.biSizeImage;
|
2009-07-28 23:19:31 -07:00
|
|
|
iconEntry.ieFileOffset = sizeof(ICONFILEHEADER) + sizeof(ICONENTRY);
|
|
|
|
howMuch = sizeof(ICONENTRY);
|
|
|
|
memcpy(whereTo, &iconEntry, howMuch);
|
|
|
|
whereTo += howMuch;
|
|
|
|
|
|
|
|
// followed by the bitmap info header
|
|
|
|
// (doubling the height because icons have two bitmaps)
|
2010-09-14 09:15:32 -07:00
|
|
|
colorHeader.biHeight *= 2;
|
|
|
|
colorHeader.biSizeImage += maskHeader.biSizeImage;
|
2009-07-28 23:19:31 -07:00
|
|
|
howMuch = sizeof(BITMAPINFOHEADER);
|
2010-09-14 09:15:32 -07:00
|
|
|
memcpy(whereTo, &colorHeader, howMuch);
|
2009-07-28 23:19:31 -07:00
|
|
|
whereTo += howMuch;
|
2010-09-14 09:15:32 -07:00
|
|
|
colorHeader.biHeight /= 2;
|
|
|
|
colorHeader.biSizeImage -= maskHeader.biSizeImage;
|
|
|
|
|
|
|
|
// followed by the XOR bitmap data (colorHeader)
|
|
|
|
// (you'd expect the color table to come here, but it apparently doesn't)
|
|
|
|
BITMAPINFO* colorInfo = CreateBitmapInfo(&colorHeader, colorTableSize);
|
|
|
|
if (colorInfo && GetDIBits(hDC, iconInfo.hbmColor, 0,
|
|
|
|
colorHeader.biHeight, whereTo, colorInfo,
|
|
|
|
DIB_RGB_COLORS)) {
|
|
|
|
whereTo += colorHeader.biSizeImage;
|
|
|
|
|
|
|
|
// and finally the AND bitmap data (maskHeader)
|
|
|
|
BITMAPINFO* maskInfo = CreateBitmapInfo(&maskHeader, maskTableSize);
|
|
|
|
if (maskInfo && GetDIBits(hDC, iconInfo.hbmMask, 0,
|
|
|
|
maskHeader.biHeight, whereTo, maskInfo,
|
|
|
|
DIB_RGB_COLORS)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Now, create a pipe and stuff our data into it
|
|
|
|
nsCOMPtr<nsIInputStream> inStream;
|
|
|
|
nsCOMPtr<nsIOutputStream> outStream;
|
|
|
|
rv = NS_NewPipe(getter_AddRefs(inStream), getter_AddRefs(outStream),
|
|
|
|
iconSize, iconSize, nonBlocking);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
PRUint32 written;
|
|
|
|
rv = outStream->Write(buffer, iconSize, &written);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
NS_ADDREF(*_retval = inStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // if we got bitmap bits
|
2010-09-14 09:15:32 -07:00
|
|
|
delete maskInfo;
|
2007-03-22 10:30:00 -07:00
|
|
|
} // if we got mask bits
|
2010-09-14 09:15:32 -07:00
|
|
|
delete colorInfo;
|
2007-03-22 10:30:00 -07:00
|
|
|
delete [] buffer;
|
|
|
|
} // if we allocated the buffer
|
|
|
|
} // if we got mask size
|
|
|
|
|
|
|
|
DeleteDC(hDC);
|
|
|
|
DeleteObject(iconInfo.hbmColor);
|
|
|
|
DeleteObject(iconInfo.hbmMask);
|
|
|
|
} // if we got icon info
|
2008-12-20 17:31:35 -08:00
|
|
|
DestroyIcon(hIcon);
|
|
|
|
} // if we got an hIcon
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-28 23:19:31 -07:00
|
|
|
// If we didn't make a stream, then fail.
|
|
|
|
if (!*_retval && NS_SUCCEEDED(rv))
|
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::GetContentType(nsACString &aContentType)
|
|
|
|
{
|
|
|
|
aContentType.AssignLiteral("image/x-icon");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIconChannel::SetContentType(const nsACString &aContentType)
|
|
|
|
{
|
|
|
|
// It doesn't make sense to set the content-type on this type
|
|
|
|
// of channel...
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::GetContentCharset(nsACString &aContentCharset)
|
|
|
|
{
|
|
|
|
aContentCharset.Truncate();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIconChannel::SetContentCharset(const nsACString &aContentCharset)
|
|
|
|
{
|
|
|
|
// It doesn't make sense to set the content-charset on this type
|
|
|
|
// of channel...
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2010-08-30 13:20:39 -07:00
|
|
|
NS_IMETHODIMP nsIconChannel::GetContentLength(PRInt32 *aContentLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*aContentLength = mContentLength;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-08-30 13:20:39 -07:00
|
|
|
NS_IMETHODIMP nsIconChannel::SetContentLength(PRInt32 aContentLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("nsIconChannel::SetContentLength");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::GetOwner(nsISupports* *aOwner)
|
|
|
|
{
|
|
|
|
*aOwner = mOwner.get();
|
|
|
|
NS_IF_ADDREF(*aOwner);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::SetOwner(nsISupports* aOwner)
|
|
|
|
{
|
|
|
|
mOwner = aOwner;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::GetNotificationCallbacks(nsIInterfaceRequestor* *aNotificationCallbacks)
|
|
|
|
{
|
|
|
|
*aNotificationCallbacks = mCallbacks.get();
|
|
|
|
NS_IF_ADDREF(*aNotificationCallbacks);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::SetNotificationCallbacks(nsIInterfaceRequestor* aNotificationCallbacks)
|
|
|
|
{
|
|
|
|
mCallbacks = aNotificationCallbacks;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::GetSecurityInfo(nsISupports * *aSecurityInfo)
|
|
|
|
{
|
|
|
|
*aSecurityInfo = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIRequestObserver methods
|
|
|
|
NS_IMETHODIMP nsIconChannel::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
|
|
|
|
{
|
|
|
|
if (mListener)
|
|
|
|
return mListener->OnStartRequest(this, aContext);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIconChannel::OnStopRequest(nsIRequest* aRequest, nsISupports* aContext, nsresult aStatus)
|
|
|
|
{
|
|
|
|
if (mListener) {
|
|
|
|
mListener->OnStopRequest(this, aContext, aStatus);
|
|
|
|
mListener = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove from load group
|
|
|
|
if (mLoadGroup)
|
|
|
|
mLoadGroup->RemoveRequest(this, nsnull, aStatus);
|
|
|
|
|
|
|
|
// Drop notification callbacks to prevent cycles.
|
|
|
|
mCallbacks = nsnull;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIStreamListener methods
|
|
|
|
NS_IMETHODIMP nsIconChannel::OnDataAvailable(nsIRequest* aRequest,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsIInputStream* aStream,
|
|
|
|
PRUint32 aOffset,
|
|
|
|
PRUint32 aCount)
|
|
|
|
{
|
|
|
|
if (mListener)
|
|
|
|
return mListener->OnDataAvailable(this, aContext, aStream, aOffset, aCount);
|
|
|
|
return NS_OK;
|
|
|
|
}
|