Bug 495822: Move OfflineAppAllowed from nsIOService to nsOfflineCacheUpdateService. r+sr=biesi

This commit is contained in:
Dave Camp 2008-10-19 21:12:25 -07:00
parent b208a7b6a3
commit a1df547c6e
11 changed files with 162 additions and 118 deletions

View File

@ -158,6 +158,8 @@ static NS_DEFINE_CID(kXTFServiceCID, NS_XTFSERVICE_CID);
#include "nsIDragService.h"
#include "nsIChannelEventSink.h"
#include "nsIInterfaceRequestor.h"
#include "nsIOfflineCacheUpdate.h"
#include "nsCPrefetchService.h"
#ifdef IBMBIDI
#include "nsIBidiKeyboard.h"
@ -824,17 +826,34 @@ nsContentUtils::GetOfflineAppManifest(nsIDOMWindow *aWindow, nsIURI **aURI)
PRBool
nsContentUtils::OfflineAppAllowed(nsIURI *aURI)
{
return NS_OfflineAppAllowed(aURI, sPrefBranch);
nsCOMPtr<nsIOfflineCacheUpdateService> updateService =
do_GetService(NS_OFFLINECACHEUPDATESERVICE_CONTRACTID);
if (!updateService) {
return PR_FALSE;
}
PRBool allowed;
nsresult rv = updateService->OfflineAppAllowedForURI(aURI,
sPrefBranch,
&allowed);
return NS_SUCCEEDED(rv) && allowed;
}
/* static */
PRBool
nsContentUtils::OfflineAppAllowed(nsIPrincipal *aPrincipal)
{
nsCOMPtr<nsIURI> codebaseURI;
aPrincipal->GetURI(getter_AddRefs(codebaseURI));
nsCOMPtr<nsIOfflineCacheUpdateService> updateService =
do_GetService(NS_OFFLINECACHEUPDATESERVICE_CONTRACTID);
if (!updateService) {
return PR_FALSE;
}
return OfflineAppAllowed(codebaseURI);
PRBool allowed;
nsresult rv = updateService->OfflineAppAllowed(aPrincipal,
sPrefBranch,
&allowed);
return NS_SUCCEEDED(rv) && allowed;
}
// static

View File

@ -85,6 +85,7 @@ REQUIRES = xpcom \
windowwatcher \
imglib2 \
jar \
prefetch \
$(NULL)
SDK_XPIDLSRCS = \

View File

@ -107,6 +107,8 @@
#include "nsIScrollableView.h"
#include "nsIScriptChannel.h"
#include "nsIURIClassifier.h"
#include "nsIOfflineCacheUpdate.h"
#include "nsCPrefetchService.h"
// we want to explore making the document own the load group
// so we can associate the document URI with the load group.
@ -7322,6 +7324,31 @@ nsDocShell::GetInheritedPrincipal(PRBool aConsiderCurrentDocument)
return nsnull;
}
PRBool
nsDocShell::ShouldCheckAppCache(nsIURI *aURI)
{
// Toplevel document loads in domains with the offline-app
// permission should check for an associated application
// cache.
nsCOMPtr<nsIDocShellTreeItem> root;
GetSameTypeRootTreeItem(getter_AddRefs(root));
if (root != this) {
return PR_FALSE;
}
nsCOMPtr<nsIOfflineCacheUpdateService> offlineService =
do_GetService(NS_OFFLINECACHEUPDATESERVICE_CONTRACTID);
if (!offlineService) {
return PR_FALSE;
}
PRBool allowed;
nsresult rv = offlineService->OfflineAppAllowedForURI(aURI,
nsnull,
&allowed);
return NS_SUCCEEDED(rv) && allowed;
}
nsresult
nsDocShell::DoURILoad(nsIURI * aURI,
nsIURI * aReferrerURI,
@ -7347,12 +7374,7 @@ nsDocShell::DoURILoad(nsIURI * aURI,
// tag first party URL loads
loadFlags |= nsIChannel::LOAD_INITIAL_DOCUMENT_URI;
// Toplevel document loads in domains with the offline-app
// permission should check for an associated application
// cache.
nsCOMPtr<nsIDocShellTreeItem> root;
GetSameTypeRootTreeItem(getter_AddRefs(root));
if (root == this && NS_OfflineAppAllowed(aURI)) {
if (ShouldCheckAppCache(aURI)) {
loadFlags |= nsICachingChannel::LOAD_CHECK_OFFLINE_CACHE;
}
}

View File

@ -272,6 +272,10 @@ protected:
// at the parent.
nsIPrincipal* GetInheritedPrincipal(PRBool aConsiderCurrentDocument);
// True if when loading aURI into this docshell, the channel should look
// for an appropriate application cache.
PRBool ShouldCheckAppCache(nsIURI * aURI);
// Actually open a channel and perform a URI load. Note: whatever owner is
// passed to this function will be set on the channel. Callers who wish to
// not have an owner on the channel should just pass null.

View File

@ -221,25 +221,3 @@ interface nsINetUtil : nsISupports
out long aCharsetStart,
out long aCharsetEnd);
};
/**
* nsINetUtil methods added in mozilla 1.9.1.
*
* XXX bug 451255: Merge this up in to nsINetUtil as soon as possible.
*/
[scriptable, uuid(da76ab60-2ec5-4649-84c4-4954a08f6d37)]
interface nsINetUtil_MOZILLA_1_9_1 : nsISupports
{
/**
* Checks whether a document at the given URI should have access
* to the offline cache.
* @param aURI
* The URI to check
* @param aPrefBranch
* The pref branch to use to check the
* offline-apps.allow_by_default pref. If not specified,
* the pref service will be used.
*/
boolean OfflineAppAllowed(in nsIURI aURI,
in nsIPrefBranch aPrefBranch);
};

View File

@ -1443,30 +1443,6 @@ NS_GetFinalChannelURI(nsIChannel* channel, nsIURI** uri)
return channel->GetOriginalURI(uri);
}
/**
* Checks whether a document at the given URI should have access
* to the offline cache.
* @param uri
* The URI to check
* @param prefBranch
* The pref branch to use to check the
* offline-apps.allow_by_default pref. If not specified,
* the pref service will be used.
*/
inline PRBool
NS_OfflineAppAllowed(nsIURI *aURI, nsIPrefBranch *aPrefBranch = nsnull)
{
nsresult rv;
nsCOMPtr<nsINetUtil_MOZILLA_1_9_1> util = do_GetIOService(&rv);
NS_ENSURE_SUCCESS(rv, PR_FALSE);
PRBool allowed;
rv = util->OfflineAppAllowed(aURI, aPrefBranch, &allowed);
NS_ENSURE_SUCCESS(rv, PR_FALSE);
return allowed;
}
static inline PRInt32
GetEffectivePort(nsIURI* aURI)
{

View File

@ -279,13 +279,12 @@ nsIOService::GetInstance() {
return gIOService;
}
NS_IMPL_THREADSAFE_ISUPPORTS6(nsIOService,
NS_IMPL_THREADSAFE_ISUPPORTS5(nsIOService,
nsIIOService,
nsIIOService2,
nsINetUtil,
nsIObserver,
nsISupportsWeakReference,
nsINetUtil_MOZILLA_1_9_1)
nsISupportsWeakReference)
////////////////////////////////////////////////////////////////////////////////
@ -984,59 +983,3 @@ nsIOService::ExtractCharsetFromContentType(const nsACString &aTypeHeader,
}
return NS_OK;
}
NS_IMETHODIMP
nsIOService::OfflineAppAllowed(nsIURI *aURI,
nsIPrefBranch *aPrefBranch,
PRBool *aAllowed)
{
*aAllowed = PR_FALSE;
nsCOMPtr<nsIURI> innerURI = NS_GetInnermostURI(aURI);
if (!innerURI)
return NS_OK;
// only http and https applications can use offline APIs.
PRBool match;
nsresult rv = innerURI->SchemeIs("http", &match);
NS_ENSURE_SUCCESS(rv, rv);
if (!match) {
rv = innerURI->SchemeIs("https", &match);
NS_ENSURE_SUCCESS(rv, rv);
if (!match) {
return NS_OK;
}
}
nsCOMPtr<nsIPermissionManager> permissionManager =
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
if (!permissionManager) {
return NS_OK;
}
PRUint32 perm;
permissionManager->TestExactPermission(innerURI, "offline-app", &perm);
if (perm == nsIPermissionManager::UNKNOWN_ACTION) {
nsCOMPtr<nsIPrefBranch> branch = aPrefBranch;
if (!branch) {
branch = do_GetService(NS_PREFSERVICE_CONTRACTID);
}
if (branch) {
rv = branch->GetBoolPref("offline-apps.allow_by_default", aAllowed);
NS_ENSURE_SUCCESS(rv, rv);
}
return NS_OK;
}
if (perm == nsIPermissionManager::DENY_ACTION) {
return NS_OK;
}
*aAllowed = PR_TRUE;
return NS_OK;
}

View File

@ -77,7 +77,6 @@ class nsIPrefBranch2;
class nsIOService : public nsIIOService2
, public nsIObserver
, public nsINetUtil
, public nsINetUtil_MOZILLA_1_9_1
, public nsSupportsWeakReference
{
public:
@ -86,7 +85,6 @@ public:
NS_DECL_NSIIOSERVICE2
NS_DECL_NSIOBSERVER
NS_DECL_NSINETUTIL
NS_DECL_NSINETUTIL_MOZILLA_1_9_1
// Gets the singleton instance of the IO Service, creating it as needed
// Returns nsnull on out of memory or failure to initialize.

View File

@ -53,6 +53,7 @@ REQUIRES = xpcom \
nkcache \
chardet \
pref \
caps \
$(NULL)
CPPSRCS = \

View File

@ -43,6 +43,8 @@ interface nsIDOMNode;
interface nsIDOMDocument;
interface nsIDOMLoadStatus;
interface nsIOfflineCacheUpdate;
interface nsIPrincipal;
interface nsIPrefBranch;
[scriptable, uuid(0aa38757-999c-44d6-bdb4-7dd32634fa83)]
interface nsIOfflineCacheUpdateObserver : nsISupports {
@ -217,7 +219,7 @@ interface nsIOfflineCacheUpdate : nsISupports {
void removeObserver(in nsIOfflineCacheUpdateObserver aObserver);
};
[scriptable, uuid(3abee04b-5bbb-4405-b659-35f780e38da0)]
[scriptable, uuid(6fd2030f-7b00-4102-a0e3-d73078821eb1)]
interface nsIOfflineCacheUpdateService : nsISupports {
/**
* Constants for the offline-app permission.
@ -253,4 +255,32 @@ interface nsIOfflineCacheUpdateService : nsISupports {
void scheduleOnDocumentStop(in nsIURI aManifestURI,
in nsIURI aDocumentURI,
in nsIDOMDocument aDocument);
/**
* Checks whether a principal should have access to the offline
* cache.
* @param aPrincipal
* The principal to check.
* @param aPrefBranch
* The pref branch to use to check the
* offline-apps.allow_by_default pref. If not specified,
* the pref service will be used.
*/
boolean offlineAppAllowed(in nsIPrincipal aPrincipal,
in nsIPrefBranch aPrefBranch);
/**
* Checks whether a document at the given URI should have access
* to the offline cache.
* @param aURI
* The URI to check
* @param aPrefBranch
* The pref branch to use to check the
* offline-apps.allow_by_default pref. If not specified,
* the pref service will be used.
*/
boolean offlineAppAllowedForURI(in nsIURI aURI,
in nsIPrefBranch aPrefBranch);
};

View File

@ -54,6 +54,8 @@
#include "nsIWebProgress.h"
#include "nsICryptoHash.h"
#include "nsICacheEntryDescriptor.h"
#include "nsIPermissionManager.h"
#include "nsIPrincipal.h"
#include "nsIPrefBranch.h"
#include "nsIPrefService.h"
#include "nsNetCID.h"
@ -2063,3 +2065,73 @@ nsOfflineCacheUpdateService::OnSecurityChange(nsIWebProgress *aWebProgress,
NS_NOTREACHED("notification excluded in AddProgressListener(...)");
return NS_OK;
}
NS_IMETHODIMP
nsOfflineCacheUpdateService::OfflineAppAllowed(nsIPrincipal *aPrincipal,
nsIPrefBranch *aPrefBranch,
PRBool *aAllowed)
{
nsCOMPtr<nsIURI> codebaseURI;
nsresult rv = aPrincipal->GetURI(getter_AddRefs(codebaseURI));
NS_ENSURE_SUCCESS(rv, rv);
return OfflineAppAllowedForURI(codebaseURI, aPrefBranch, aAllowed);
}
NS_IMETHODIMP
nsOfflineCacheUpdateService::OfflineAppAllowedForURI(nsIURI *aURI,
nsIPrefBranch *aPrefBranch,
PRBool *aAllowed)
{
*aAllowed = PR_FALSE;
nsCOMPtr<nsIURI> innerURI = NS_GetInnermostURI(aURI);
if (!innerURI)
return NS_OK;
// only http and https applications can use offline APIs.
PRBool match;
nsresult rv = innerURI->SchemeIs("http", &match);
NS_ENSURE_SUCCESS(rv, rv);
if (!match) {
rv = innerURI->SchemeIs("https", &match);
NS_ENSURE_SUCCESS(rv, rv);
if (!match) {
return NS_OK;
}
}
nsCOMPtr<nsIPermissionManager> permissionManager =
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
if (!permissionManager) {
return NS_OK;
}
PRUint32 perm;
permissionManager->TestExactPermission(innerURI, "offline-app", &perm);
if (perm == nsIPermissionManager::UNKNOWN_ACTION) {
nsCOMPtr<nsIPrefBranch> branch = aPrefBranch;
if (!branch) {
branch = do_GetService(NS_PREFSERVICE_CONTRACTID);
}
if (branch) {
rv = branch->GetBoolPref("offline-apps.allow_by_default", aAllowed);
if (NS_FAILED(rv)) {
*aAllowed = PR_FALSE;
}
}
return NS_OK;
}
if (perm == nsIPermissionManager::DENY_ACTION) {
return NS_OK;
}
*aAllowed = PR_TRUE;
return NS_OK;
}