Bug 664918. Part 0: Refactor nsMediaCache's handling of principals into a helper method in nsContentUtils. r=bzbarsky

This commit is contained in:
Robert O'Callahan 2012-04-30 15:11:00 +12:00
parent 8938ffc81e
commit 1fe7590d18
4 changed files with 46 additions and 35 deletions

View File

@ -1372,6 +1372,23 @@ public:
*/
static bool IsSystemPrincipal(nsIPrincipal* aPrincipal);
/**
* *aResourcePrincipal is a principal describing who may access the contents
* of a resource. The resource can only be consumed by a principal that
* subsumes *aResourcePrincipal. MAKE SURE THAT NOTHING EVER ACTS WITH THE
* AUTHORITY OF *aResourcePrincipal.
* It may be null to indicate that the resource has no data from any origin
* in it yet and anything may access the resource.
* Additional data is being mixed into the resource from aExtraPrincipal
* (which may be null; if null, no data is being mixed in and this function
* will do nothing). Update *aResourcePrincipal to reflect the new data.
* If *aResourcePrincipal subsumes aExtraPrincipal, nothing needs to change,
* otherwise *aResourcePrincipal is replaced with the system principal.
* Returns true if *aResourcePrincipal changed.
*/
static bool CombineResourcePrincipals(nsCOMPtr<nsIPrincipal>* aResourcePrincipal,
nsIPrincipal* aExtraPrincipal);
/**
* Trigger a link with uri aLinkURI. If aClick is false, this triggers a
* mouseover on the link, otherwise it triggers a load after doing a

View File

@ -147,6 +147,7 @@ static NS_DEFINE_CID(kXTFServiceCID, NS_XTFSERVICE_CID);
#include "nsIPermissionManager.h"
#include "nsIContentPrefService.h"
#include "nsIScriptObjectPrincipal.h"
#include "nsNullPrincipal.h"
#include "nsIRunnable.h"
#include "nsDOMJSUtils.h"
#include "nsGenericHTMLElement.h"
@ -4120,7 +4121,7 @@ nsContentUtils::ConvertToPlainText(const nsAString& aSourceBuffer,
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), "about:blank");
nsCOMPtr<nsIPrincipal> principal =
do_CreateInstance("@mozilla.org/nullprincipal;1");
do_CreateInstance(NS_NULLPRINCIPAL_CONTRACTID);
nsCOMPtr<nsIDOMDocument> domDocument;
nsresult rv = nsContentUtils::CreateDocument(EmptyString(),
EmptyString(),
@ -4466,6 +4467,29 @@ nsContentUtils::IsSystemPrincipal(nsIPrincipal* aPrincipal)
return NS_SUCCEEDED(rv) && isSystem;
}
bool
nsContentUtils::CombineResourcePrincipals(nsCOMPtr<nsIPrincipal>* aResourcePrincipal,
nsIPrincipal* aExtraPrincipal)
{
if (!aExtraPrincipal) {
return false;
}
if (!*aResourcePrincipal) {
*aResourcePrincipal = aExtraPrincipal;
return true;
}
if (*aResourcePrincipal == aExtraPrincipal) {
return false;
}
bool subsumes;
if (NS_SUCCEEDED((*aResourcePrincipal)->Subsumes(aExtraPrincipal, &subsumes)) &&
subsumes) {
return false;
}
sSecurityManager->GetSystemPrincipal(getter_AddRefs(*aResourcePrincipal));
return true;
}
/* static */
void
nsContentUtils::TriggerLink(nsIContent *aContent, nsPresContext *aPresContext,

View File

@ -45,6 +45,7 @@
#include "nsXULAppAPI.h"
#include "nsNetUtil.h"
#include "prio.h"
#include "nsContentUtils.h"
#include "nsThreadUtils.h"
#include "MediaResource.h"
#include "nsMathUtils.h"
@ -1705,35 +1706,7 @@ nsMediaCacheStream::NotifyDataStarted(PRInt64 aOffset)
void
nsMediaCacheStream::UpdatePrincipal(nsIPrincipal* aPrincipal)
{
if (!mPrincipal) {
NS_ASSERTION(!mUsingNullPrincipal, "Are we using a null principal or not?");
if (mUsingNullPrincipal) {
// Don't let mPrincipal be set to anything
return;
}
mPrincipal = aPrincipal;
return;
}
if (mPrincipal == aPrincipal) {
// Common case
NS_ASSERTION(!mUsingNullPrincipal, "We can't receive data from a null principal");
return;
}
if (mUsingNullPrincipal) {
// We've already fallen back to a null principal, so nothing more
// to do.
return;
}
bool equal;
nsresult rv = mPrincipal->Equals(aPrincipal, &equal);
if (NS_SUCCEEDED(rv) && equal)
return;
// Principals are not equal, so set mPrincipal to a null principal.
mPrincipal = do_CreateInstance("@mozilla.org/nullprincipal;1");
mUsingNullPrincipal = true;
nsContentUtils::CombineResourcePrincipals(&mPrincipal, aPrincipal);
}
void

View File

@ -236,7 +236,6 @@ public:
mDidNotifyDataEnded(false),
mIsSeekable(false), mCacheSuspended(false),
mChannelEnded(false),
mUsingNullPrincipal(false),
mChannelOffset(0), mStreamLength(-1),
mStreamOffset(0), mPlaybackBytesPerSecond(10000),
mPinCount(0), mCurrentMode(MODE_PLAYBACK),
@ -273,7 +272,8 @@ public:
return !mClosed &&
(!mDidNotifyDataEnded || NS_SUCCEEDED(mNotifyDataEndedStatus));
}
// Get the principal for this stream.
// Get the principal for this stream. Anything accessing the contents of
// this stream must have a principal that subsumes this principal.
nsIPrincipal* GetCurrentPrincipal() { return mPrincipal; }
// Ensure a global media cache update has run with this stream present.
// This ensures the cache has had a chance to suspend or unsuspend this stream.
@ -490,9 +490,6 @@ private:
bool mCacheSuspended;
// True if the channel ended and we haven't seeked it again.
bool mChannelEnded;
// True if mPrincipal is a null principal because we saw data from
// multiple origins
bool mUsingNullPrincipal;
// The offset where the next data from the channel will arrive
PRInt64 mChannelOffset;
// The reported or discovered length of the data, or -1 if nothing is