Bug 1376964 - Part 5: Add OMT wrapper for nsIURIs useful for font stuff. r=jfkthame

MozReview-Commit-ID: FkBYOoqC6x6

--HG--
extra : rebase_source : c6e30a1b11d12c4d4e070f26a746162827b639a4
This commit is contained in:
Cameron McCormack 2017-07-07 17:29:44 +08:00
parent 5d2c03ca7b
commit f96118fca4
5 changed files with 208 additions and 0 deletions

View File

@ -0,0 +1,119 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* 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 "nsIProtocolHandler.h"
#include "nsProxyRelease.h"
#include "nsNetUtil.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)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aURI);
mURI = 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 know that nsSimpleURI::From returns us a pointer to the same object,
// and we hold a strong reference to the object in mURI, so no need to
// hold it strongly here as well. (And we'd have to NS_ReleaseOnMainThread
// it in our destructor anyway.)
RefPtr<mozilla::net::nsSimpleURI> simpleURI =
mozilla::net::nsSimpleURI::From(aURI);
mSimpleURI = simpleURI;
NS_ASSERTION(mSimpleURI, "Why aren't our data: URLs backed by nsSimpleURI?");
} else {
mSimpleURI = nullptr;
}
if (!mSimpleURI) {
mURI->GetSpec(mSpec);
}
mHash = nsURIHashKey::HashKey(mURI);
mInheritsSecurityContext =
HasFlag(aURI, nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT);
mSyncLoadIsOK =
HasFlag(aURI, nsIProtocolHandler::URI_SYNC_LOAD_IS_OK);
}
gfxFontSrcURI::~gfxFontSrcURI()
{
NS_ReleaseOnMainThread("gfxFontSrcURI::mURI", mURI.forget());
}
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,
nsDefaultCStringComparator())) {
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;
}

View File

@ -0,0 +1,67 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* 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/. */
#ifndef MOZILLA_GFX_FONTSRCURI_H
#define MOZILLA_GFX_FONTSRCURI_H
#include "nsCOMPtr.h"
#include "nsIURI.h"
#include "PLDHashTable.h"
namespace mozilla {
namespace net {
class nsSimpleURI;
} // namespace net
} // namespace mozilla
/**
* A wrapper for an nsIURI that can be used OMT, which has cached information
* useful for the gfxUserFontSet.
*/
class gfxFontSrcURI
{
public:
explicit gfxFontSrcURI(nsIURI* aURI);
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(gfxFontSrcURI)
nsIURI* get() { return mURI; }
bool Equals(gfxFontSrcURI* aOther);
nsresult GetSpec(nsACString& aResult);
nsCString GetSpecOrDefault();
PLDHashNumber Hash() const { return mHash; }
bool InheritsSecurityContext() const { return mInheritsSecurityContext; }
bool SyncLoadIsOK() const { return mSyncLoadIsOK; }
private:
~gfxFontSrcURI();
// The URI.
nsCOMPtr<nsIURI> mURI;
// If the nsIURI is an nsSimpleURI for a data: URL, this is a pointer to it.
// (Just a weak reference since mURI holds the strong reference.)
//
// We store this so that we don't duplicate the URL spec for data: URLs,
// which can be much larger than other URLs.
mozilla::net::nsSimpleURI* mSimpleURI;
// If the nsIURI is not an nsSimpleURI, this is its spec.
nsCString mSpec;
// Precomputed hash for mURI.
PLDHashNumber mHash;
// Whether the nsIURI's protocol handler has the URI_INHERITS_SECURITY_CONTEXT
// flag.
bool mInheritsSecurityContext;
// Whether the nsIURI's protocol handler has teh URI_SYNC_LOAD_IS_OK flag.
bool mSyncLoadIsOK;
};
#endif // MOZILLA_GFX_FONTSRCURI_H

View File

@ -30,6 +30,7 @@ EXPORTS += [
'gfxFontFeatures.h',
'gfxFontInfoLoader.h',
'gfxFontPrefLangList.h',
'gfxFontSrcURI.h',
'gfxFontUtils.h',
'gfxFontVariations.h',
'gfxGradientCache.h',
@ -192,6 +193,7 @@ UNIFIED_SOURCES += [
'gfxFontFeatures.cpp',
'gfxFontInfoLoader.cpp',
'gfxFontMissingGlyphs.cpp',
'gfxFontSrcURI.cpp',
'gfxGlyphExtents.cpp',
'gfxGradientCache.cpp',
'gfxGraphiteShaper.cpp',

View File

@ -31,6 +31,19 @@ static NS_DEFINE_CID(kThisSimpleURIImplementationCID,
NS_THIS_SIMPLEURI_IMPLEMENTATION_CID);
static NS_DEFINE_CID(kSimpleURICID, NS_SIMPLEURI_CID);
/* static */ already_AddRefed<nsSimpleURI>
nsSimpleURI::From(nsIURI* aURI)
{
RefPtr<nsSimpleURI> uri;
nsresult rv = aURI->QueryInterface(kThisSimpleURIImplementationCID,
getter_AddRefs(uri));
if (NS_FAILED(rv)) {
return nullptr;
}
return uri.forget();
}
////////////////////////////////////////////////////////////////////////////////
// nsSimpleURI methods:

View File

@ -45,10 +45,17 @@ public:
NS_DECL_NSIMUTABLE
NS_DECL_NSIIPCSERIALIZABLEURI
static already_AddRefed<nsSimpleURI> From(nsIURI* aURI);
// nsSimpleURI methods:
nsSimpleURI();
bool Equals(nsSimpleURI* aOther)
{
return EqualsInternal(aOther, eHonorRef);
}
// nsISizeOf
// Among the sub-classes that inherit (directly or indirectly) from
// nsSimpleURI, measurement of the following members may be added later if