Line data Source code
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 "nsIProtocolHandler.h"
9 : #include "nsProxyRelease.h"
10 : #include "nsNetUtil.h"
11 : #include "nsSimpleURI.h"
12 : #include "nsURIHashKey.h"
13 :
14 : static bool
15 0 : HasFlag(nsIURI* aURI, uint32_t aFlag)
16 : {
17 : nsresult rv;
18 0 : bool value = false;
19 0 : rv = NS_URIChainHasFlags(aURI, aFlag, &value);
20 0 : return NS_SUCCEEDED(rv) && value;
21 : }
22 :
23 0 : gfxFontSrcURI::gfxFontSrcURI(nsIURI* aURI)
24 : {
25 0 : MOZ_ASSERT(NS_IsMainThread());
26 0 : MOZ_ASSERT(aURI);
27 :
28 0 : mURI = aURI;
29 :
30 : // If we have a data: URI, we know that it is backed by an nsSimpleURI,
31 : // and that we don't need to serialize it ahead of time.
32 0 : nsCString scheme;
33 0 : mURI->GetScheme(scheme);
34 :
35 0 : if (scheme.EqualsLiteral("data")) {
36 : // We know that nsSimpleURI::From returns us a pointer to the same object,
37 : // and we hold a strong reference to the object in mURI, so no need to
38 : // hold it strongly here as well. (And we'd have to NS_ReleaseOnMainThread
39 : // it in our destructor anyway.)
40 : RefPtr<mozilla::net::nsSimpleURI> simpleURI =
41 0 : mozilla::net::nsSimpleURI::From(aURI);
42 0 : mSimpleURI = simpleURI;
43 :
44 0 : NS_ASSERTION(mSimpleURI, "Why aren't our data: URLs backed by nsSimpleURI?");
45 : } else {
46 0 : mSimpleURI = nullptr;
47 : }
48 :
49 0 : if (!mSimpleURI) {
50 0 : mURI->GetSpec(mSpec);
51 : }
52 :
53 0 : mHash = nsURIHashKey::HashKey(mURI);
54 :
55 0 : mInheritsSecurityContext =
56 0 : HasFlag(aURI, nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT);
57 0 : mSyncLoadIsOK =
58 0 : HasFlag(aURI, nsIProtocolHandler::URI_SYNC_LOAD_IS_OK);
59 0 : }
60 :
61 0 : gfxFontSrcURI::~gfxFontSrcURI()
62 : {
63 0 : NS_ReleaseOnMainThread("gfxFontSrcURI::mURI", mURI.forget());
64 0 : }
65 :
66 : bool
67 0 : gfxFontSrcURI::Equals(gfxFontSrcURI* aOther)
68 : {
69 0 : if (mSimpleURI) {
70 0 : if (aOther->mSimpleURI) {
71 0 : return mSimpleURI->Equals(aOther->mSimpleURI);
72 : }
73 :
74 : // The two URIs are probably different. Do a quick check on the
75 : // schemes before deciding to serialize mSimpleURI (which might be
76 : // quite large).
77 : {
78 0 : nsCString thisScheme;
79 0 : mSimpleURI->GetScheme(thisScheme);
80 :
81 0 : nsCString otherScheme;
82 0 : if (!StringBeginsWith(aOther->mSpec, thisScheme,
83 0 : nsDefaultCStringComparator())) {
84 0 : return false;
85 : }
86 : }
87 :
88 0 : nsCString thisSpec;
89 0 : mSimpleURI->GetSpec(thisSpec);
90 0 : return thisSpec == aOther->mSpec;
91 : }
92 :
93 0 : if (aOther->mSimpleURI) {
94 0 : return aOther->Equals(this);
95 : }
96 :
97 0 : return mSpec == aOther->mSpec;
98 : }
99 :
100 : nsresult
101 0 : gfxFontSrcURI::GetSpec(nsACString& aResult)
102 : {
103 0 : if (mSimpleURI) {
104 0 : return mSimpleURI->GetSpec(aResult);
105 : }
106 :
107 0 : aResult = mSpec;
108 0 : return NS_OK;
109 : }
110 :
111 : nsCString
112 0 : gfxFontSrcURI::GetSpecOrDefault()
113 : {
114 0 : if (mSimpleURI) {
115 0 : return mSimpleURI->GetSpecOrDefault();
116 : }
117 :
118 0 : return mSpec;
119 : }
|