diff --git a/content/base/src/nsContentUtils.cpp b/content/base/src/nsContentUtils.cpp index dd72abb80a1..8c6a42eb69d 100644 --- a/content/base/src/nsContentUtils.cpp +++ b/content/base/src/nsContentUtils.cpp @@ -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 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 codebaseURI; - aPrincipal->GetURI(getter_AddRefs(codebaseURI)); + nsCOMPtr 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 diff --git a/docshell/base/Makefile.in b/docshell/base/Makefile.in index 57778df051d..970ac316800 100644 --- a/docshell/base/Makefile.in +++ b/docshell/base/Makefile.in @@ -85,6 +85,7 @@ REQUIRES = xpcom \ windowwatcher \ imglib2 \ jar \ + prefetch \ $(NULL) SDK_XPIDLSRCS = \ diff --git a/docshell/base/nsDocShell.cpp b/docshell/base/nsDocShell.cpp index 0060c5e3573..cafb74d19c1 100644 --- a/docshell/base/nsDocShell.cpp +++ b/docshell/base/nsDocShell.cpp @@ -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 root; + GetSameTypeRootTreeItem(getter_AddRefs(root)); + if (root != this) { + return PR_FALSE; + } + + nsCOMPtr 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 root; - GetSameTypeRootTreeItem(getter_AddRefs(root)); - if (root == this && NS_OfflineAppAllowed(aURI)) { + if (ShouldCheckAppCache(aURI)) { loadFlags |= nsICachingChannel::LOAD_CHECK_OFFLINE_CACHE; } } diff --git a/docshell/base/nsDocShell.h b/docshell/base/nsDocShell.h index 747acf880a2..ce7ce897451 100644 --- a/docshell/base/nsDocShell.h +++ b/docshell/base/nsDocShell.h @@ -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. diff --git a/netwerk/base/public/nsINetUtil.idl b/netwerk/base/public/nsINetUtil.idl index 288f87dcc6a..529d2587008 100644 --- a/netwerk/base/public/nsINetUtil.idl +++ b/netwerk/base/public/nsINetUtil.idl @@ -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); -}; diff --git a/netwerk/base/public/nsNetUtil.h b/netwerk/base/public/nsNetUtil.h index bcb26df374b..39250feb0d6 100644 --- a/netwerk/base/public/nsNetUtil.h +++ b/netwerk/base/public/nsNetUtil.h @@ -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 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) { diff --git a/netwerk/base/src/nsIOService.cpp b/netwerk/base/src/nsIOService.cpp index 7d3afaf896b..3838ca673a4 100644 --- a/netwerk/base/src/nsIOService.cpp +++ b/netwerk/base/src/nsIOService.cpp @@ -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 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 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 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; -} - diff --git a/netwerk/base/src/nsIOService.h b/netwerk/base/src/nsIOService.h index 171381bfd0d..f2817aebc6e 100644 --- a/netwerk/base/src/nsIOService.h +++ b/netwerk/base/src/nsIOService.h @@ -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. diff --git a/uriloader/prefetch/Makefile.in b/uriloader/prefetch/Makefile.in index 175be859c6f..930dc202c6c 100644 --- a/uriloader/prefetch/Makefile.in +++ b/uriloader/prefetch/Makefile.in @@ -53,6 +53,7 @@ REQUIRES = xpcom \ nkcache \ chardet \ pref \ + caps \ $(NULL) CPPSRCS = \ diff --git a/uriloader/prefetch/nsIOfflineCacheUpdate.idl b/uriloader/prefetch/nsIOfflineCacheUpdate.idl index a32dd54fb93..7b64c896f92 100644 --- a/uriloader/prefetch/nsIOfflineCacheUpdate.idl +++ b/uriloader/prefetch/nsIOfflineCacheUpdate.idl @@ -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); }; + + diff --git a/uriloader/prefetch/nsOfflineCacheUpdate.cpp b/uriloader/prefetch/nsOfflineCacheUpdate.cpp index 85b136f92fc..f9f8aec22f3 100644 --- a/uriloader/prefetch/nsOfflineCacheUpdate.cpp +++ b/uriloader/prefetch/nsOfflineCacheUpdate.cpp @@ -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 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 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 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 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; +} +