tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

gfxFontSrcURI.cpp (3014B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #include "gfxFontSrcURI.h"
      7 
      8 #include "mozilla/ServoStyleSet.h"
      9 #include "nsIProtocolHandler.h"
     10 #include "nsProxyRelease.h"
     11 #include "nsNetUtil.h"
     12 #include "nsSimpleURI.h"
     13 #include "nsURIHashKey.h"
     14 
     15 static bool HasFlag(nsIURI* aURI, uint32_t aFlag) {
     16  nsresult rv;
     17  bool value = false;
     18  rv = NS_URIChainHasFlags(aURI, aFlag, &value);
     19  return NS_SUCCEEDED(rv) && value;
     20 }
     21 
     22 gfxFontSrcURI::gfxFontSrcURI(nsIURI* aURI) : mURI(aURI) {
     23  MOZ_ASSERT(aURI);
     24 
     25  // If we have a data: URI, we know that it is backed by an nsSimpleURI,
     26  // and that we don't need to serialize it ahead of time.
     27  nsCString scheme;
     28  mURI->GetScheme(scheme);
     29 
     30  if (scheme.EqualsLiteral("data")) {
     31    // We know that nsSimpleURI::From returns us a pointer to the same object,
     32    // and we hold a strong reference to the object in mURI, so no need to
     33    // hold it strongly here as well.  (And we'd have to
     34    // NS_ReleaseOnMainThread it in our destructor anyway.)
     35    RefPtr<mozilla::net::nsSimpleURI> simpleURI =
     36        mozilla::net::nsSimpleURI::From(aURI);
     37    mSimpleURI = simpleURI;
     38 
     39    NS_ASSERTION(mSimpleURI,
     40                 "Why aren't our data: URLs backed by nsSimpleURI?");
     41  } else {
     42    mSimpleURI = nullptr;
     43  }
     44 
     45  if (!mSimpleURI) {
     46    mURI->GetSpec(mSpec);
     47  }
     48 
     49  mHash = nsURIHashKey::HashKey(mURI);
     50 }
     51 
     52 gfxFontSrcURI::~gfxFontSrcURI() = default;
     53 
     54 void gfxFontSrcURI::EnsureInitialized() {
     55  MOZ_ASSERT(NS_IsMainThread() || mozilla::ServoStyleSet::IsInServoTraversal());
     56 
     57  if (mInitialized) {
     58    return;
     59  }
     60 
     61  mInheritsSecurityContext =
     62      HasFlag(mURI, nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT);
     63  mSyncLoadIsOK = HasFlag(mURI, nsIProtocolHandler::URI_SYNC_LOAD_IS_OK);
     64  mInitialized = true;
     65 }
     66 
     67 bool gfxFontSrcURI::Equals(gfxFontSrcURI* aOther) {
     68  if (mSimpleURI) {
     69    if (aOther->mSimpleURI) {
     70      return mSimpleURI->Equals(aOther->mSimpleURI);
     71    }
     72 
     73    // The two URIs are probably different.  Do a quick check on the
     74    // schemes before deciding to serialize mSimpleURI (which might be
     75    // quite large).
     76    {
     77      nsCString thisScheme;
     78      mSimpleURI->GetScheme(thisScheme);
     79 
     80      nsCString otherScheme;
     81      if (!StringBeginsWith(aOther->mSpec, thisScheme)) {
     82        return false;
     83      }
     84    }
     85 
     86    nsCString thisSpec;
     87    mSimpleURI->GetSpec(thisSpec);
     88    return thisSpec == aOther->mSpec;
     89  }
     90 
     91  if (aOther->mSimpleURI) {
     92    return aOther->Equals(this);
     93  }
     94 
     95  return mSpec == aOther->mSpec;
     96 }
     97 
     98 nsresult gfxFontSrcURI::GetSpec(nsACString& aResult) {
     99  if (mSimpleURI) {
    100    return mSimpleURI->GetSpec(aResult);
    101  }
    102 
    103  aResult = mSpec;
    104  return NS_OK;
    105 }
    106 
    107 nsCString gfxFontSrcURI::GetSpecOrDefault() {
    108  if (mSimpleURI) {
    109    return mSimpleURI->GetSpecOrDefault();
    110  }
    111 
    112  return mSpec;
    113 }