already_AddRefed<nsMIMEInfoWin> nsOSHelperAppService::GetByExtension(const nsAFlatString& aFileExt, const char *aTypeHint)
{
if (aFileExt.IsEmpty())
return nsnull;
// windows registry assumes your file extension is going to include the '.'.
// so make sure it's there...
nsAutoString fileExtToUse;
if (aFileExt.First() != PRUnichar('.'))
fileExtToUse = PRUnichar('.');
fileExtToUse.Append(aFileExt);
// Try to get an entry from the windows registry.
nsCOMPtr<nsIWindowsRegKey> regKey =
do_CreateInstance("@mozilla.org/windows-registry-key;1");
if (!regKey)
return nsnull;
nsresult rv = regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT,
fileExtToUse,
nsIWindowsRegKey::ACCESS_QUERY_VALUE);
if (NS_FAILED(rv))
return nsnull;
nsCAutoString typeToUse;
if (aTypeHint && *aTypeHint) {
typeToUse.Assign(aTypeHint);
}
else {
nsAutoString temp;
if (NS_FAILED(regKey->ReadStringValue(NS_LITERAL_STRING("Content Type"),
temp)) || temp.IsEmpty()) {
return nsnull;
}
// Content-Type is always in ASCII
LossyAppendUTF16toASCII(temp, typeToUse);
}
nsMIMEInfoWin* mimeInfo = new nsMIMEInfoWin(typeToUse);
if (!mimeInfo)
return nsnull; // out of memory
NS_ADDREF(mimeInfo);
// don't append the '.'
mimeInfo->AppendExtension(NS_ConvertUTF16toUTF8(Substring(fileExtToUse, 1)));
mimeInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault);
nsAutoString appInfo;
bool found;
// Retrieve the default application for this extension
if (mAppAssoc) {
// Vista: use the new application association COM interfaces
// for resolving helpers.
nsString assocType(fileExtToUse);
PRUnichar * pResult = nsnull;
HRESULT hr = mAppAssoc->QueryCurrentDefault(assocType.get(),
AT_FILEEXTENSION, AL_EFFECTIVE,
&pResult);
if (SUCCEEDED(hr)) {
found = true;
appInfo.Assign(pResult);
CoTaskMemFree(pResult);
}
else {
found = false;
}
}
else
{
found = NS_SUCCEEDED(regKey->ReadStringValue(EmptyString(),
appInfo));
}
// Bug 358297 - ignore the default handler, force the user to choose app
if (appInfo.EqualsLiteral("XPSViewer.Document"))
found = false;
if (!found) {
NS_IF_RELEASE(mimeInfo); // we failed to really find an entry in the registry
return nsnull;
}
// Get other nsIMIMEInfo fields from registry, if possible.
nsAutoString defaultDescription;
nsCOMPtr<nsIFile> defaultApplication;
if (NS_FAILED(GetDefaultAppInfo(appInfo, defaultDescription,
getter_AddRefs(defaultApplication)))) {
NS_IF_RELEASE(mimeInfo);
return nsnull;
}
mimeInfo->SetDefaultDescription(defaultDescription);
mimeInfo->SetDefaultApplicationHandler(defaultApplication);
// Grab the general description
GetMIMEInfoFromRegistry(appInfo, mimeInfo);
//.........这里部分代码省略.........
//.........这里部分代码省略.........
}
nsAutoCString originSuffix;
BasePrincipal::Cast(principal)->OriginAttributesRef().CreateSuffix(originSuffix);
nsCOMPtr<nsIURI> origin;
principal->GetURI(getter_AddRefs(origin));
if (!origin) {
return NS_OK;
}
nsAutoCString host;
origin->GetHost(host);
if (host.IsEmpty()) {
return NS_OK;
}
nsAutoCString originScope;
rv = CreateReversedDomain(host, originScope);
NS_ENSURE_SUCCESS(rv, rv);
Notify("session-only-cleared", NS_ConvertUTF8toUTF16(originSuffix), originScope);
return NS_OK;
}
// Clear everything (including so and pb data) from caches and database
// for the gived domain and subdomains.
if (!strcmp(aTopic, "browser:purge-domain-data")) {
// Convert the domain name to the ACE format
nsAutoCString aceDomain;
nsCOMPtr<nsIIDNService> converter = do_GetService(NS_IDNSERVICE_CONTRACTID);
if (converter) {
rv = converter->ConvertUTF8toACE(NS_ConvertUTF16toUTF8(aData), aceDomain);
NS_ENSURE_SUCCESS(rv, rv);
} else {
// In case the IDN service is not available, this is the best we can come up with!
rv = NS_EscapeURL(NS_ConvertUTF16toUTF8(aData),
esc_OnlyNonASCII | esc_AlwaysCopy,
aceDomain,
fallible);
NS_ENSURE_SUCCESS(rv, rv);
}
nsAutoCString originScope;
rv = CreateReversedDomain(aceDomain, originScope);
NS_ENSURE_SUCCESS(rv, rv);
DOMStorageDBBridge* db = DOMStorageCache::StartDatabase();
NS_ENSURE_TRUE(db, NS_ERROR_FAILURE);
db->AsyncClearMatchingOrigin(originScope);
Notify("domain-data-cleared", EmptyString(), originScope);
return NS_OK;
}
// Clear all private-browsing caches
if (!strcmp(aTopic, "last-pb-context-exited")) {
Notify("private-browsing-data-cleared");
return NS_OK;
}
// Clear data of the origins whose prefixes will match the suffix.
nsresult sbDeviceXMLInfo::Read(nsIDOMDocument* aDeviceXMLInfoDocument)
{
// Validate arguments.
NS_ENSURE_ARG_POINTER(aDeviceXMLInfoDocument);
// Function variables.
nsresult rv;
// Get the list of all device info elements.
nsCOMPtr<nsIDOMNodeList> nodeList;
rv = aDeviceXMLInfoDocument->GetElementsByTagNameNS
(NS_LITERAL_STRING(SB_DEVICE_INFO_NS),
NS_LITERAL_STRING("deviceinfo"),
getter_AddRefs(nodeList));
NS_ENSURE_SUCCESS(rv, rv);
// Search all device info elements for one that matches target device.
PRUint32 nodeCount;
rv = nodeList->GetLength(&nodeCount);
NS_ENSURE_SUCCESS(rv, rv);
for (PRUint32 i = 0; i < nodeCount; i++) {
// Get the next device info element.
nsCOMPtr<nsIDOMNode> node;
rv = nodeList->Item(i, getter_AddRefs(node));
NS_ENSURE_SUCCESS(rv, rv);
// Use device info node if it matches target device and is
// newer than any previous match
nsString foundVersion;
nsCOMPtr<nsIDOMNode> deviceNode;
rv = DeviceMatchesDeviceInfoNode(node,
foundVersion,
getter_AddRefs(deviceNode));
NS_ENSURE_SUCCESS(rv, rv);
if (foundVersion.IsEmpty()) {
// Not a match
continue;
}
if (mDeviceInfoVersion.IsEmpty() ||
NS_CompareVersions(
NS_LossyConvertUTF16toASCII(foundVersion).get(),
NS_LossyConvertUTF16toASCII(mDeviceInfoVersion).get()) > 0)
{
// Found version is greater than current version, if any. Keep this
// node and replace any previously found node
// Log the found device info if logging enabled
if (mLogDeviceInfo) {
nsCOMPtr<nsIDOMSerializer> serializer =
do_CreateInstance("@mozilla.org/xmlextras/xmlserializer;1");
// Translate the found deviceinfo element to XML
nsString fullXml(NS_LITERAL_STRING("<ERROR PRINTING deviceinfo NODE>"));
if (serializer) {
serializer->SerializeToString(node, fullXml);
}
// Translate the device element matching this device to XML, if any
nsString deviceXml(NS_LITERAL_STRING("<ERROR PRINTING device NODE>"));
if (deviceNode && serializer) {
serializer->SerializeToString(deviceNode, deviceXml);
}
nsCAutoString curVersUtf8 = NS_ConvertUTF16toUTF8(mDeviceInfoVersion);
nsCAutoString foundVersUtf8 = NS_ConvertUTF16toUTF8(foundVersion);
// Log the device info and version. The first line has two
// alternate forms, depending on whether the existing device
// info is being replaced:
//
// FOUND deviceinfo version <found version>:
// - OR -
// REPLACED deviceinfo version <current version> with <found version>:
Log("%s deviceinfo version %s%s%s:\n%s%s%s",
mDeviceInfoElement ? "REPLACED" : "FOUND",
curVersUtf8.get(), // current version OR blank
mDeviceInfoElement ? " with " : "",
foundVersUtf8.get(), // found version OR found version
NS_ConvertUTF16toUTF8(fullXml).get(),
deviceNode ? "\n\nMATCHING device element:\n" : "",
deviceNode ? NS_ConvertUTF16toUTF8(deviceXml).get() : "");
}
mDeviceInfoVersion.Assign(foundVersion);
mDeviceInfoElement = do_QueryInterface(node, &rv);
NS_ENSURE_SUCCESS(rv, rv);
if (deviceNode) {
mDeviceElement = do_QueryInterface(deviceNode, &rv);
NS_ENSURE_SUCCESS(rv, rv);
}
else {
mDeviceElement = nsnull;
}
}
}
//.........这里部分代码省略.........
//.........这里部分代码省略.........
NS_ENSURE_SUCCESS(rv, rv);
#ifdef XP_WIN
// Setup the file to check.
WINTRUST_FILE_INFO fileToCheck = {0};
fileToCheck.cbStruct = sizeof(WINTRUST_FILE_INFO);
fileToCheck.pcwszFilePath = filePath.Data();
fileToCheck.hFile = nullptr;
fileToCheck.pgKnownSubject = nullptr;
// We want to check it is signed and trusted.
WINTRUST_DATA trustData = {0};
trustData.cbStruct = sizeof(trustData);
trustData.pPolicyCallbackData = nullptr;
trustData.pSIPClientData = nullptr;
trustData.dwUIChoice = WTD_UI_NONE;
trustData.fdwRevocationChecks = WTD_REVOKE_NONE;
trustData.dwUnionChoice = WTD_CHOICE_FILE;
trustData.dwStateAction = WTD_STATEACTION_VERIFY;
trustData.hWVTStateData = nullptr;
trustData.pwszURLReference = nullptr;
// Disallow revocation checks over the network
trustData.dwProvFlags = WTD_CACHE_ONLY_URL_RETRIEVAL;
// no UI
trustData.dwUIContext = 0;
trustData.pFile = &fileToCheck;
// The WINTRUST_ACTION_GENERIC_VERIFY_V2 policy verifies that the certificate
// chains up to a trusted root CA and has appropriate permissions to sign
// code.
GUID policyGUID = WINTRUST_ACTION_GENERIC_VERIFY_V2;
// Check if the file is signed by something that is trusted. If the file is
// not signed, this is a no-op.
LONG ret = WinVerifyTrust(nullptr, &policyGUID, &trustData);
CRYPT_PROVIDER_DATA* cryptoProviderData = nullptr;
// According to the Windows documentation, we should check against 0 instead
// of ERROR_SUCCESS, which is an HRESULT.
if (ret == 0) {
cryptoProviderData = WTHelperProvDataFromStateData(trustData.hWVTStateData);
}
if (cryptoProviderData) {
// Lock because signature information is read on the main thread.
MutexAutoLock lock(mLock);
LOG(("Downloaded trusted and signed file [this = %p].", this));
// A binary may have multiple signers. Each signer may have multiple certs
// in the chain.
for (DWORD i = 0; i < cryptoProviderData->csSigners; ++i) {
const CERT_CHAIN_CONTEXT* certChainContext =
cryptoProviderData->pasSigners[i].pChainContext;
if (!certChainContext) {
break;
}
for (DWORD j = 0; j < certChainContext->cChain; ++j) {
const CERT_SIMPLE_CHAIN* certSimpleChain =
certChainContext->rgpChain[j];
if (!certSimpleChain) {
break;
}
nsCOMPtr<nsIX509CertList> nssCertList =
do_CreateInstance(NS_X509CERTLIST_CONTRACTID);
if (!nssCertList) {
break;
}
bool extractionSuccess = true;
for (DWORD k = 0; k < certSimpleChain->cElement; ++k) {
CERT_CHAIN_ELEMENT* certChainElement = certSimpleChain->rgpElement[k];
if (certChainElement->pCertContext->dwCertEncodingType !=
X509_ASN_ENCODING) {
continue;
}
nsCOMPtr<nsIX509Cert> nssCert = nullptr;
rv = certDB->ConstructX509(
reinterpret_cast<char *>(
certChainElement->pCertContext->pbCertEncoded),
certChainElement->pCertContext->cbCertEncoded,
getter_AddRefs(nssCert));
if (!nssCert) {
extractionSuccess = false;
LOG(("Couldn't create NSS cert [this = %p]", this));
break;
}
nssCertList->AddCert(nssCert);
nsString subjectName;
nssCert->GetSubjectName(subjectName);
LOG(("Adding cert %s [this = %p]",
NS_ConvertUTF16toUTF8(subjectName).get(), this));
}
if (extractionSuccess) {
mSignatureInfo.AppendObject(nssCertList);
}
}
}
// Free the provider data if cryptoProviderData is not null.
trustData.dwStateAction = WTD_STATEACTION_CLOSE;
WinVerifyTrust(nullptr, &policyGUID, &trustData);
} else {
LOG(("Downloaded unsigned or untrusted file [this = %p].", this));
}
#endif
return NS_OK;
}
nsresult
nsXHTMLContentSerializer::EscapeURI(nsIContent* aContent, const nsAString& aURI, nsAString& aEscapedURI)
{
// URL escape %xx cannot be used in JS.
// No escaping if the scheme is 'javascript'.
if (IsJavaScript(aContent, nsGkAtoms::href, kNameSpaceID_None, aURI)) {
aEscapedURI = aURI;
return NS_OK;
}
// nsITextToSubURI does charset convert plus uri escape
// This is needed to convert to a document charset which is needed to support existing browsers.
// But we eventually want to use UTF-8 instead of a document charset, then the code would be much simpler.
// See HTML 4.01 spec, "Appendix B.2.1 Non-ASCII characters in URI attribute values"
nsCOMPtr<nsITextToSubURI> textToSubURI;
nsAutoString uri(aURI); // in order to use FindCharInSet()
nsresult rv = NS_OK;
if (!mCharset.IsEmpty() && !IsASCII(uri)) {
textToSubURI = do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
}
int32_t start = 0;
int32_t end;
nsAutoString part;
nsXPIDLCString escapedURI;
aEscapedURI.Truncate(0);
// Loop and escape parts by avoiding escaping reserved characters
// (and '%', '#', as well as '[' and ']' for IPv6 address literals).
while ((end = uri.FindCharInSet("%#;/?:@&=+$,[]", start)) != -1) {
part = Substring(aURI, start, (end-start));
if (textToSubURI && !IsASCII(part)) {
rv = textToSubURI->ConvertAndEscape(mCharset.get(), part.get(), getter_Copies(escapedURI));
NS_ENSURE_SUCCESS(rv, rv);
}
else {
escapedURI.Adopt(nsEscape(NS_ConvertUTF16toUTF8(part).get(), url_Path));
}
AppendASCIItoUTF16(escapedURI, aEscapedURI);
// Append a reserved character without escaping.
part = Substring(aURI, end, 1);
aEscapedURI.Append(part);
start = end + 1;
}
if (start < (int32_t) aURI.Length()) {
// Escape the remaining part.
part = Substring(aURI, start, aURI.Length()-start);
if (textToSubURI) {
rv = textToSubURI->ConvertAndEscape(mCharset.get(), part.get(), getter_Copies(escapedURI));
NS_ENSURE_SUCCESS(rv, rv);
}
else {
escapedURI.Adopt(nsEscape(NS_ConvertUTF16toUTF8(part).get(), url_Path));
}
AppendASCIItoUTF16(escapedURI, aEscapedURI);
}
return rv;
}
请发表评论