1 /* -*- Mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  *
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef nsJARURI_h__
8 #define nsJARURI_h__
9 
10 #include "nsIJARURI.h"
11 #include "nsISerializable.h"
12 #include "nsCOMPtr.h"
13 #include "nsString.h"
14 #include "nsINestedURI.h"
15 #include "nsIURIMutator.h"
16 
17 #define NS_THIS_JARURI_IMPL_CID                      \
18   { /* 9a55f629-730b-4d08-b75b-fa7d9570a691 */       \
19     0x9a55f629, 0x730b, 0x4d08, {                    \
20       0xb7, 0x5b, 0xfa, 0x7d, 0x95, 0x70, 0xa6, 0x91 \
21     }                                                \
22   }
23 
24 #define NS_JARURI_CID                                \
25   { /* 245abae2-b947-4ded-a46d-9829d3cca462 */       \
26     0x245abae2, 0xb947, 0x4ded, {                    \
27       0xa4, 0x6d, 0x98, 0x29, 0xd3, 0xcc, 0xa4, 0x62 \
28     }                                                \
29   }
30 
31 #define NS_JARURIMUTATOR_CID                         \
32   { /* 19d9161b-a2a9-4518-b2c9-fcb8296d6dcd */       \
33     0x19d9161b, 0xa2a9, 0x4518, {                    \
34       0xb2, 0xc9, 0xfc, 0xb8, 0x29, 0x6d, 0x6d, 0xcd \
35     }                                                \
36   }
37 
38 class nsJARURI final : public nsIJARURI,
39                        public nsISerializable,
40                        public nsINestedURI {
41  public:
42   NS_DECL_THREADSAFE_ISUPPORTS
43   NS_DECL_NSIURI
44   NS_DECL_NSIURL
45   NS_DECL_NSIJARURI
46   NS_DECL_NSISERIALIZABLE
47   NS_DECL_NSINESTEDURI
48 
49   NS_DECLARE_STATIC_IID_ACCESSOR(NS_THIS_JARURI_IMPL_CID)
50 
51   // nsJARURI
52   nsresult FormatSpec(const nsACString& entryPath, nsACString& result,
53                       bool aIncludeScheme = true);
54   nsresult CreateEntryURL(const nsACString& entryFilename, const char* charset,
55                           nsIURL** url);
56 
57  protected:
58   nsJARURI();
59   virtual ~nsJARURI();
60   nsresult SetJAREntry(const nsACString& entryPath);
61   nsresult Init(const char* charsetHint);
62   nsresult SetSpecWithBase(const nsACString& aSpec, nsIURI* aBaseURL);
63 
64   // enum used in a few places to specify how .ref attribute should be handled
65   enum RefHandlingEnum { eIgnoreRef, eHonorRef, eReplaceRef };
66 
67   // Helper to share code between Equals methods.
68   virtual nsresult EqualsInternal(nsIURI* other,
69                                   RefHandlingEnum refHandlingMode,
70                                   bool* result);
71 
72   nsCOMPtr<nsIURI> mJARFile;
73   // mJarEntry stored as a URL so that we can easily access things
74   // like extensions, refs, etc.
75   nsCOMPtr<nsIURL> mJAREntry;
76   nsCString mCharsetHint;
77 
78  private:
79   nsresult Clone(nsIURI** aURI);
80   nsresult SetSpecInternal(const nsACString& input);
81   nsresult SetScheme(const nsACString& input);
82   nsresult SetUserPass(const nsACString& input);
83   nsresult SetUsername(const nsACString& input);
84   nsresult SetPassword(const nsACString& input);
85   nsresult SetHostPort(const nsACString& aValue);
86   nsresult SetHost(const nsACString& input);
87   nsresult SetPort(int32_t port);
88   nsresult SetPathQueryRef(const nsACString& input);
89   nsresult SetRef(const nsACString& input);
90   nsresult SetFilePath(const nsACString& input);
91   nsresult SetQuery(const nsACString& input);
92   nsresult SetQueryWithEncoding(const nsACString& input,
93                                 const mozilla::Encoding* encoding);
94   bool Deserialize(const mozilla::ipc::URIParams&);
95   nsresult ReadPrivate(nsIObjectInputStream* aStream);
96 
97   nsresult SetFileNameInternal(const nsACString& fileName);
98   nsresult SetFileBaseNameInternal(const nsACString& fileBaseName);
99   nsresult SetFileExtensionInternal(const nsACString& fileExtension);
100 
101  public:
102   class Mutator final : public nsIURIMutator,
103                         public BaseURIMutator<nsJARURI>,
104                         public nsIURLMutator,
105                         public nsISerializable,
106                         public nsIJARURIMutator {
107     NS_DECL_ISUPPORTS
NS_FORWARD_SAFE_NSIURISETTERS_RET(mURI)108     NS_FORWARD_SAFE_NSIURISETTERS_RET(mURI)
109     NS_DEFINE_NSIMUTATOR_COMMON
110     NS_DECL_NSIURLMUTATOR
111 
112     NS_IMETHOD
113     Write(nsIObjectOutputStream* aOutputStream) override {
114       return NS_ERROR_NOT_IMPLEMENTED;
115     }
116 
Read(nsIObjectInputStream * aStream)117     [[nodiscard]] NS_IMETHOD Read(nsIObjectInputStream* aStream) override {
118       return InitFromInputStream(aStream);
119     }
120 
121     NS_IMETHOD
SetSpecBaseCharset(const nsACString & aSpec,nsIURI * aBaseURI,const char * aCharset)122     SetSpecBaseCharset(const nsACString& aSpec, nsIURI* aBaseURI,
123                        const char* aCharset) override {
124       RefPtr<nsJARURI> uri;
125       if (mURI) {
126         mURI.swap(uri);
127       } else {
128         uri = new nsJARURI();
129       }
130 
131       nsresult rv = uri->Init(aCharset);
132       NS_ENSURE_SUCCESS(rv, rv);
133 
134       rv = uri->SetSpecWithBase(aSpec, aBaseURI);
135       if (NS_FAILED(rv)) {
136         return rv;
137       }
138 
139       mURI.swap(uri);
140       return NS_OK;
141     }
142 
Mutator()143     explicit Mutator() {}
144 
145    private:
~Mutator()146     virtual ~Mutator() {}
147 
148     friend class nsJARURI;
149   };
150 
151   friend BaseURIMutator<nsJARURI>;
152 };
153 
154 NS_DEFINE_STATIC_IID_ACCESSOR(nsJARURI, NS_THIS_JARURI_IMPL_CID)
155 
156 #endif  // nsJARURI_h__
157