diff --git a/extensions/cookie/nsPermissionManager.cpp b/extensions/cookie/nsPermissionManager.cpp index 3023a9b9deba..5a901c965ca3 100644 --- a/extensions/cookie/nsPermissionManager.cpp +++ b/extensions/cookie/nsPermissionManager.cpp @@ -340,92 +340,95 @@ UpgradeHostToOriginAndInsert(const nsACString& aHost, const nsAFlatCString& aTyp // to guess what ports and protocols we want to add permissions for. // We find every URI which they have visited with this host (or a subdomain of this host), // and try to add it as a principal. + bool foundHistory = false; + nsCOMPtr histSrv = do_GetService(NS_NAVHISTORYSERVICE_CONTRACTID); - nsCOMPtr histQuery; - rv = histSrv->GetNewQuery(getter_AddRefs(histQuery)); - NS_ENSURE_SUCCESS(rv, rv); + if (histSrv) { + nsCOMPtr histQuery; + rv = histSrv->GetNewQuery(getter_AddRefs(histQuery)); + NS_ENSURE_SUCCESS(rv, rv); - // We want to only find history items for this particular host, and subdomains - rv = histQuery->SetDomain(aHost); - NS_ENSURE_SUCCESS(rv, rv); + // We want to only find history items for this particular host, and subdomains + rv = histQuery->SetDomain(aHost); + NS_ENSURE_SUCCESS(rv, rv); - rv = histQuery->SetDomainIsHost(false); - NS_ENSURE_SUCCESS(rv, rv); + rv = histQuery->SetDomainIsHost(false); + NS_ENSURE_SUCCESS(rv, rv); - nsCOMPtr histQueryOpts; - rv = histSrv->GetNewQueryOptions(getter_AddRefs(histQueryOpts)); - NS_ENSURE_SUCCESS(rv, rv); + nsCOMPtr histQueryOpts; + rv = histSrv->GetNewQueryOptions(getter_AddRefs(histQueryOpts)); + NS_ENSURE_SUCCESS(rv, rv); - // We want to get the URIs for every item in the user's history with the given host - rv = histQueryOpts->SetResultType(nsINavHistoryQueryOptions::RESULTS_AS_URI); - NS_ENSURE_SUCCESS(rv, rv); + // We want to get the URIs for every item in the user's history with the given host + rv = histQueryOpts->SetResultType(nsINavHistoryQueryOptions::RESULTS_AS_URI); + NS_ENSURE_SUCCESS(rv, rv); - // We only search history, because searching both bookmarks and history - // is not supported, and history tends to be more comprehensive. - rv = histQueryOpts->SetQueryType(nsINavHistoryQueryOptions::QUERY_TYPE_HISTORY); - NS_ENSURE_SUCCESS(rv, rv); + // We only search history, because searching both bookmarks and history + // is not supported, and history tends to be more comprehensive. + rv = histQueryOpts->SetQueryType(nsINavHistoryQueryOptions::QUERY_TYPE_HISTORY); + NS_ENSURE_SUCCESS(rv, rv); - // We include hidden URIs (such as those visited via iFrames) as they may have permissions too - rv = histQueryOpts->SetIncludeHidden(true); - NS_ENSURE_SUCCESS(rv, rv); + // We include hidden URIs (such as those visited via iFrames) as they may have permissions too + rv = histQueryOpts->SetIncludeHidden(true); + NS_ENSURE_SUCCESS(rv, rv); - nsCOMPtr histResult; - rv = histSrv->ExecuteQuery(histQuery, histQueryOpts, getter_AddRefs(histResult)); - NS_ENSURE_SUCCESS(rv, rv); + nsCOMPtr histResult; + rv = histSrv->ExecuteQuery(histQuery, histQueryOpts, getter_AddRefs(histResult)); + NS_ENSURE_SUCCESS(rv, rv); - nsCOMPtr histResultContainer; - rv = histResult->GetRoot(getter_AddRefs(histResultContainer)); - NS_ENSURE_SUCCESS(rv, rv); + nsCOMPtr histResultContainer; + rv = histResult->GetRoot(getter_AddRefs(histResultContainer)); + NS_ENSURE_SUCCESS(rv, rv); - rv = histResultContainer->SetContainerOpen(true); - NS_ENSURE_SUCCESS(rv, rv); + rv = histResultContainer->SetContainerOpen(true); + NS_ENSURE_SUCCESS(rv, rv); - uint32_t childCount = 0; - rv = histResultContainer->GetChildCount(&childCount); - NS_ENSURE_SUCCESS(rv, rv); + uint32_t childCount = 0; + rv = histResultContainer->GetChildCount(&childCount); + NS_ENSURE_SUCCESS(rv, rv); - bool foundHistory = false; - for (uint32_t i = 0; i < childCount; i++) { - nsCOMPtr child; - histResultContainer->GetChild(i, getter_AddRefs(child)); - if (NS_FAILED(rv)) continue; + for (uint32_t i = 0; i < childCount; i++) { + nsCOMPtr child; + histResultContainer->GetChild(i, getter_AddRefs(child)); + if (NS_FAILED(rv)) continue; - uint32_t type; - rv = child->GetType(&type); - if (NS_FAILED(rv) || type != nsINavHistoryResultNode::RESULT_TYPE_URI) { - NS_WARNING("Unexpected non-RESULT_TYPE_URI node in " - "UpgradeHostToOriginAndInsert()"); - continue; + uint32_t type; + rv = child->GetType(&type); + if (NS_FAILED(rv) || type != nsINavHistoryResultNode::RESULT_TYPE_URI) { + NS_WARNING("Unexpected non-RESULT_TYPE_URI node in " + "UpgradeHostToOriginAndInsert()"); + continue; + } + + nsAutoCString uriSpec; + rv = child->GetUri(uriSpec); + if (NS_FAILED(rv)) continue; + + nsCOMPtr uri; + rv = NS_NewURI(getter_AddRefs(uri), uriSpec); + if (NS_FAILED(rv)) continue; + + // Use the provided host - this URI may be for a subdomain, rather than the host we care about. + rv = uri->SetHost(aHost); + if (NS_FAILED(rv)) continue; + + // We now have a URI which we can make a nsIPrincipal out of + nsCOMPtr principal; + rv = GetPrincipal(uri, aAppId, aIsInBrowserElement, getter_AddRefs(principal)); + if (NS_FAILED(rv)) continue; + + // Insert it! (The backend should be able to deal with us inserting the same origin repeatedly) + foundHistory = true; + rv = aHelper->Insert(principal, aType, aPermission, + aExpireType, aExpireTime, aModificationTime); + NS_WARN_IF(NS_FAILED(rv)); } - nsAutoCString uriSpec; - rv = child->GetUri(uriSpec); - if (NS_FAILED(rv)) continue; - - nsCOMPtr uri; - rv = NS_NewURI(getter_AddRefs(uri), uriSpec); - if (NS_FAILED(rv)) continue; - - // Use the provided host - this URI may be for a subdomain, rather than the host we care about. - rv = uri->SetHost(aHost); - if (NS_FAILED(rv)) continue; - - // We now have a URI which we can make a nsIPrincipal out of - nsCOMPtr principal; - rv = GetPrincipal(uri, aAppId, aIsInBrowserElement, getter_AddRefs(principal)); - if (NS_FAILED(rv)) continue; - - // Insert it! (The backend should be able to deal with us inserting the same origin repeatedly) - foundHistory = true; - rv = aHelper->Insert(principal, aType, aPermission, - aExpireType, aExpireTime, aModificationTime); - NS_WARN_IF(NS_FAILED(rv)); + rv = histResultContainer->SetContainerOpen(false); + NS_ENSURE_SUCCESS(rv, rv); } - rv = histResultContainer->SetContainerOpen(false); - NS_ENSURE_SUCCESS(rv, rv); - // If we didn't find any origins for this host in the poermissions database, // we can insert the default http:// and https:// permissions into the database. // This has a relatively high liklihood of applying the permission to the correct