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/. */
6 #ifndef nsSimpleURI_h__
7 #define nsSimpleURI_h__
9 #include "mozilla/MemoryReporting.h"
11 #include "nsISerializable.h"
13 #include "nsIClassInfo.h"
14 #include "nsISizeOf.h"
15 #include "nsIURIMutator.h"
16 #include "nsISimpleURIMutator.h"
21 #define NS_THIS_SIMPLEURI_IMPLEMENTATION_CID \
22 { /* 0b9bb0c2-fee6-470b-b9b9-9fd9462b5e19 */ \
23 0x0b9bb0c2, 0xfee6, 0x470b, { \
24 0xb9, 0xb9, 0x9f, 0xd9, 0x46, 0x2b, 0x5e, 0x19 \
28 class nsSimpleURI
: public nsIURI
, public nsISerializable
, public nsISizeOf
{
30 nsSimpleURI() = default;
31 virtual ~nsSimpleURI() = default;
34 NS_DECL_THREADSAFE_ISUPPORTS
36 NS_DECL_NSISERIALIZABLE
38 static already_AddRefed
<nsSimpleURI
> From(nsIURI
* aURI
);
40 // nsSimpleURI methods:
42 bool Equals(nsSimpleURI
* aOther
) { return EqualsInternal(aOther
, eHonorRef
); }
45 // Among the sub-classes that inherit (directly or indirectly) from
46 // nsSimpleURI, measurement of the following members may be added later if
47 // DMD finds it is worthwhile:
48 // - nsJSURI: mBaseURI
49 // - nsSimpleNestedURI: mInnerURI
50 // - nsBlobURI: mPrincipal
51 virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf
) const override
;
52 virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf
) const override
;
55 // enum used in a few places to specify how .ref attribute should be handled
56 enum RefHandlingEnum
{ eIgnoreRef
, eHonorRef
, eReplaceRef
};
58 virtual nsresult
Clone(nsIURI
** result
);
59 virtual nsresult
SetSpecInternal(const nsACString
& aSpec
,
60 bool aStripWhitespace
= false);
61 virtual nsresult
SetScheme(const nsACString
& input
);
62 virtual nsresult
SetUserPass(const nsACString
& input
);
63 nsresult
SetUsername(const nsACString
& input
);
64 virtual nsresult
SetPassword(const nsACString
& input
);
65 virtual nsresult
SetHostPort(const nsACString
& aValue
);
66 virtual nsresult
SetHost(const nsACString
& input
);
67 virtual nsresult
SetPort(int32_t port
);
68 virtual nsresult
SetPathQueryRef(const nsACString
& aPath
);
69 virtual nsresult
SetRef(const nsACString
& aRef
);
70 virtual nsresult
SetFilePath(const nsACString
& aFilePath
);
71 virtual nsresult
SetQuery(const nsACString
& aQuery
);
72 virtual nsresult
SetQueryWithEncoding(const nsACString
& aQuery
,
73 const Encoding
* encoding
);
74 nsresult
ReadPrivate(nsIObjectInputStream
* stream
);
76 // Helper to share code between Equals methods.
77 virtual nsresult
EqualsInternal(nsIURI
* other
,
78 RefHandlingEnum refHandlingMode
,
81 // Helper to be used by inherited classes who want to test
82 // equality given an assumed nsSimpleURI. This must NOT check
83 // the passed-in other for QI to our CID.
84 bool EqualsInternal(nsSimpleURI
* otherUri
, RefHandlingEnum refHandlingMode
);
86 // Used by StartClone (and versions of StartClone in subclasses) to
87 // handle the ref in the right way for clones.
88 void SetRefOnClone(nsSimpleURI
* url
, RefHandlingEnum refHandlingMode
,
89 const nsACString
& newRef
);
91 // NOTE: This takes the refHandlingMode as an argument because
92 // nsSimpleNestedURI's specialized version needs to know how to clone
94 virtual nsSimpleURI
* StartClone(RefHandlingEnum refHandlingMode
,
95 const nsACString
& newRef
);
97 // Helper to share code between Clone methods.
98 virtual nsresult
CloneInternal(RefHandlingEnum refHandlingMode
,
99 const nsACString
& newRef
, nsIURI
** result
);
101 void TrimTrailingCharactersFromPath();
102 nsresult
EscapeAndSetPathQueryRef(const nsACString
& aPath
);
103 nsresult
SetPathQueryRefInternal(const nsACString
& aPath
);
105 bool Deserialize(const mozilla::ipc::URIParams
&);
108 nsCString mPath
; // NOTE: mPath does not include ref, as an optimization
109 nsCString mRef
; // so that URIs with different refs can share string data.
111 mQuery
; // so that URLs with different querys can share string data.
112 bool mIsRefValid
{false}; // To distinguish between empty-ref and no-ref.
113 // To distinguish between empty-query and no-query.
114 bool mIsQueryValid
{false};
117 class Mutator final
: public nsIURIMutator
,
118 public BaseURIMutator
<nsSimpleURI
>,
119 public nsISimpleURIMutator
,
120 public nsISerializable
{
122 NS_FORWARD_SAFE_NSIURISETTERS_RET(mURI
)
123 NS_DEFINE_NSIMUTATOR_COMMON
126 Write(nsIObjectOutputStream
* aOutputStream
) override
{
127 return NS_ERROR_NOT_IMPLEMENTED
;
130 [[nodiscard
]] NS_IMETHOD
Read(nsIObjectInputStream
* aStream
) override
{
131 return InitFromInputStream(aStream
);
134 [[nodiscard
]] NS_IMETHOD
SetSpecAndFilterWhitespace(
135 const nsACString
& aSpec
, nsIURIMutator
** aMutator
) override
{
137 *aMutator
= do_AddRef(this).take();
141 RefPtr
<nsSimpleURI
> uri
= new nsSimpleURI();
142 rv
= uri
->SetSpecInternal(aSpec
, /* filterWhitespace */ true);
146 mURI
= std::move(uri
);
150 explicit Mutator() = default;
153 virtual ~Mutator() = default;
155 friend class nsSimpleURI
;
158 friend BaseURIMutator
<nsSimpleURI
>;
162 } // namespace mozilla
164 #endif // nsSimpleURI_h__