/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gfxFontSrcURI.h"
#include "mozilla/ServoStyleSet.h"
#include "nsIProtocolHandler.h"
#include "nsProxyRelease.h"
#include "nsNetUtil.h"
#include "nsQueryObject.h"
#include "nsSimpleURI.h"
#include "nsURIHashKey.h"
static bool HasFlag(nsIURI* aURI, uint32_t aFlag) {
nsresult rv;
bool value =
false;
rv = NS_URIChainHasFlags(aURI, aFlag, &value);
return NS_SUCCEEDED(rv) && value;
}
gfxFontSrcURI::gfxFontSrcURI(nsIURI* aURI) : mURI(aURI) {
MOZ_ASSERT(aURI);
// If we have a data: URI, we know that it is backed by an nsSimpleURI,
// and that we don't need to serialize it ahead of time.
nsCString scheme;
mURI->GetScheme(scheme);
if (scheme.EqualsLiteral(
"data")) {
// We hold a strong reference to the object in mURI, so no need to hold it
// strongly here as well.
RefPtr<mozilla::net::nsSimpleURI> simpleURI = do_QueryObject(aURI);
MOZ_ASSERT(simpleURI,
"The data: URL should be backed by nsSimpleURI");
MOZ_ASSERT(simpleURI == aURI,
"The QueryObject must have the same object, otherwise holding a "
"raw pointer here is unsafe");
mSimpleURI = simpleURI;
}
else {
mSimpleURI = nullptr;
}
if (!mSimpleURI) {
mURI->GetSpec(mSpec);
}
mHash = nsURIHashKey::HashKey(mURI);
}
gfxFontSrcURI::~gfxFontSrcURI() =
default;
void gfxFontSrcURI::EnsureInitialized() {
MOZ_ASSERT(NS_IsMainThread() || mozilla::ServoStyleSet::IsInServoTraversal());
if (mInitialized) {
return;
}
mInheritsSecurityContext =
HasFlag(mURI, nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT);
mSyncLoadIsOK = HasFlag(mURI, nsIProtocolHandler::URI_SYNC_LOAD_IS_OK);
mInitialized =
true;
}
bool gfxFontSrcURI::Equals(gfxFontSrcURI* aOther) {
if (mSimpleURI) {
if (aOther->mSimpleURI) {
return mSimpleURI->Equals(aOther->mSimpleURI);
}
// The two URIs are probably different. Do a quick check on the
// schemes before deciding to serialize mSimpleURI (which might be
// quite large).
{
nsCString thisScheme;
mSimpleURI->GetScheme(thisScheme);
nsCString otherScheme;
if (!StringBeginsWith(aOther->mSpec, thisScheme)) {
return false;
}
}
nsCString thisSpec;
mSimpleURI->GetSpec(thisSpec);
return thisSpec == aOther->mSpec;
}
if (aOther->mSimpleURI) {
return aOther->Equals(
this);
}
return mSpec == aOther->mSpec;
}
nsresult gfxFontSrcURI::GetSpec(nsACString& aResult) {
if (mSimpleURI) {
return mSimpleURI->GetSpec(aResult);
}
aResult = mSpec;
return NS_OK;
}
nsCString gfxFontSrcURI::GetSpecOrDefault() {
if (mSimpleURI) {
return mSimpleURI->GetSpecOrDefault();
}
return mSpec;
}