/src/mozilla-central/modules/libjar/nsJARProtocolHandler.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
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 "mozilla/ClearOnShutdown.h" |
7 | | #include "nsAutoPtr.h" |
8 | | #include "nsJARProtocolHandler.h" |
9 | | #include "nsIIOService.h" |
10 | | #include "nsCRT.h" |
11 | | #include "nsIComponentManager.h" |
12 | | #include "nsIServiceManager.h" |
13 | | #include "nsJARURI.h" |
14 | | #include "nsIURL.h" |
15 | | #include "nsJARChannel.h" |
16 | | #include "nsString.h" |
17 | | #include "nsNetCID.h" |
18 | | #include "nsIMIMEService.h" |
19 | | #include "nsMimeTypes.h" |
20 | | #include "nsThreadUtils.h" |
21 | | |
22 | | static NS_DEFINE_CID(kZipReaderCacheCID, NS_ZIPREADERCACHE_CID); |
23 | | |
24 | 3 | #define NS_JAR_CACHE_SIZE 32 |
25 | | |
26 | | //----------------------------------------------------------------------------- |
27 | | |
28 | | StaticRefPtr<nsJARProtocolHandler> gJarHandler; |
29 | | |
30 | | nsJARProtocolHandler::nsJARProtocolHandler() |
31 | 3 | { |
32 | 3 | MOZ_ASSERT(NS_IsMainThread()); |
33 | 3 | } |
34 | | |
35 | | nsJARProtocolHandler::~nsJARProtocolHandler() |
36 | 0 | {} |
37 | | |
38 | | nsresult |
39 | | nsJARProtocolHandler::Init() |
40 | 3 | { |
41 | 3 | nsresult rv; |
42 | 3 | |
43 | 3 | mJARCache = do_CreateInstance(kZipReaderCacheCID, &rv); |
44 | 3 | if (NS_FAILED(rv)) return rv; |
45 | 3 | |
46 | 3 | rv = mJARCache->Init(NS_JAR_CACHE_SIZE); |
47 | 3 | return rv; |
48 | 3 | } |
49 | | |
50 | | nsIMIMEService * |
51 | | nsJARProtocolHandler::MimeService() |
52 | 0 | { |
53 | 0 | if (!mMimeService) |
54 | 0 | mMimeService = do_GetService("@mozilla.org/mime;1"); |
55 | 0 |
|
56 | 0 | return mMimeService.get(); |
57 | 0 | } |
58 | | |
59 | | NS_IMPL_ISUPPORTS(nsJARProtocolHandler, |
60 | | nsIJARProtocolHandler, |
61 | | nsIProtocolHandler, |
62 | | nsISupportsWeakReference) |
63 | | |
64 | | already_AddRefed<nsJARProtocolHandler> |
65 | | nsJARProtocolHandler::GetSingleton() |
66 | 3 | { |
67 | 3 | if (!gJarHandler) { |
68 | 3 | gJarHandler = new nsJARProtocolHandler(); |
69 | 3 | if (NS_SUCCEEDED(gJarHandler->Init())) { |
70 | 3 | ClearOnShutdown(&gJarHandler); |
71 | 3 | } else { |
72 | 0 | gJarHandler = nullptr; |
73 | 0 | } |
74 | 3 | } |
75 | 3 | return do_AddRef(gJarHandler); |
76 | 3 | } |
77 | | |
78 | | NS_IMETHODIMP |
79 | | nsJARProtocolHandler::GetJARCache(nsIZipReaderCache* *result) |
80 | 0 | { |
81 | 0 | *result = mJARCache; |
82 | 0 | NS_ADDREF(*result); |
83 | 0 | return NS_OK; |
84 | 0 | } |
85 | | |
86 | | //////////////////////////////////////////////////////////////////////////////// |
87 | | // nsIProtocolHandler methods: |
88 | | |
89 | | NS_IMETHODIMP |
90 | | nsJARProtocolHandler::GetScheme(nsACString &result) |
91 | 0 | { |
92 | 0 | result.AssignLiteral("jar"); |
93 | 0 | return NS_OK; |
94 | 0 | } |
95 | | |
96 | | NS_IMETHODIMP |
97 | | nsJARProtocolHandler::GetDefaultPort(int32_t *result) |
98 | 0 | { |
99 | 0 | *result = -1; // no port for JAR: URLs |
100 | 0 | return NS_OK; |
101 | 0 | } |
102 | | |
103 | | NS_IMETHODIMP |
104 | | nsJARProtocolHandler::GetProtocolFlags(uint32_t *result) |
105 | 95 | { |
106 | 95 | // URI_LOADABLE_BY_ANYONE, since it's our inner URI that will matter |
107 | 95 | // anyway. |
108 | 95 | *result = URI_NORELATIVE | URI_NOAUTH | URI_LOADABLE_BY_ANYONE; |
109 | 95 | /* Although jar uris have their own concept of relative urls |
110 | 95 | it is very different from the standard behaviour, so we |
111 | 95 | have to say norelative here! */ |
112 | 95 | return NS_OK; |
113 | 95 | } |
114 | | |
115 | | NS_IMETHODIMP |
116 | | nsJARProtocolHandler::NewURI(const nsACString &aSpec, |
117 | | const char *aCharset, |
118 | | nsIURI *aBaseURI, |
119 | | nsIURI **result) |
120 | 5.35k | { |
121 | 5.35k | nsCOMPtr<nsIURI> base(aBaseURI); |
122 | 5.35k | return NS_MutateURI(new nsJARURI::Mutator()) |
123 | 5.35k | .Apply(NS_MutatorMethod(&nsIJARURIMutator::SetSpecBaseCharset, |
124 | 5.35k | nsCString(aSpec), base, aCharset)) |
125 | 5.35k | .Finalize(result); |
126 | 5.35k | } |
127 | | |
128 | | NS_IMETHODIMP |
129 | | nsJARProtocolHandler::NewChannel2(nsIURI* uri, |
130 | | nsILoadInfo* aLoadInfo, |
131 | | nsIChannel** result) |
132 | 5 | { |
133 | 5 | nsJARChannel *chan = new nsJARChannel(); |
134 | 5 | if (!chan) |
135 | 0 | return NS_ERROR_OUT_OF_MEMORY; |
136 | 5 | NS_ADDREF(chan); |
137 | 5 | |
138 | 5 | nsresult rv = chan->Init(uri); |
139 | 5 | if (NS_FAILED(rv)) { |
140 | 0 | NS_RELEASE(chan); |
141 | 0 | return rv; |
142 | 0 | } |
143 | 5 | |
144 | 5 | // set the loadInfo on the new channel |
145 | 5 | rv = chan->SetLoadInfo(aLoadInfo); |
146 | 5 | if (NS_FAILED(rv)) { |
147 | 0 | NS_RELEASE(chan); |
148 | 0 | return rv; |
149 | 0 | } |
150 | 5 | |
151 | 5 | *result = chan; |
152 | 5 | return NS_OK; |
153 | 5 | } |
154 | | |
155 | | NS_IMETHODIMP |
156 | | nsJARProtocolHandler::NewChannel(nsIURI *uri, nsIChannel **result) |
157 | 0 | { |
158 | 0 | return NewChannel2(uri, nullptr, result); |
159 | 0 | } |
160 | | |
161 | | |
162 | | NS_IMETHODIMP |
163 | | nsJARProtocolHandler::AllowPort(int32_t port, const char *scheme, bool *_retval) |
164 | 0 | { |
165 | 0 | // don't override anything. |
166 | 0 | *_retval = false; |
167 | 0 | return NS_OK; |
168 | 0 | } |
169 | | |
170 | | //////////////////////////////////////////////////////////////////////////////// |