summaryrefslogtreecommitdiffstats
path: root/gfx/thebes/gfxFontSrcURI.cpp
blob: 3153d2abce18c4ade8eb43571ef4358cfa674d81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* -*- 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 "mozilla/ServoStyleSet.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) : 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 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);
}

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;
}