1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=4 sw=2 sts=2 et cin: */
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/. */
9 A protocol handler for ``chrome:''
13 #include "nsChromeProtocolHandler.h"
14 #include "nsChromeRegistry.h"
16 #include "nsContentUtils.h"
17 #include "nsThreadUtils.h"
18 #include "nsIChannel.h"
19 #include "nsIChromeRegistry.h"
21 #include "nsIFileChannel.h"
22 #include "nsIStandardURL.h"
23 #include "nsNetUtil.h"
27 #include "nsStandardURL.h"
29 ////////////////////////////////////////////////////////////////////////////////
31 NS_IMPL_ISUPPORTS(nsChromeProtocolHandler
, nsIProtocolHandler
,
32 nsISupportsWeakReference
)
34 ////////////////////////////////////////////////////////////////////////////////
35 // nsIProtocolHandler methods:
38 nsChromeProtocolHandler::GetScheme(nsACString
& result
) {
39 result
.AssignLiteral("chrome");
44 nsChromeProtocolHandler::AllowPort(int32_t port
, const char* scheme
,
46 // don't override anything.
51 /* static */ nsresult
nsChromeProtocolHandler::CreateNewURI(
52 const nsACString
& aSpec
, const char* aCharset
, nsIURI
* aBaseURI
,
54 // Chrome: URLs (currently) have no additional structure beyond that provided
55 // by standard URLs, so there is no "outer" given to CreateInstance
57 nsCOMPtr
<nsIURI
> surl
;
59 NS_MutateURI(new mozilla::net::nsStandardURL::Mutator())
60 .Apply(&nsIStandardURLMutator::Init
, nsIStandardURL::URLTYPE_STANDARD
,
61 -1, aSpec
, aCharset
, aBaseURI
, nullptr)
68 // Canonify the "chrome:" URL; e.g., so that we collapse
69 // "chrome://navigator/content/" and "chrome://navigator/content"
70 // and "chrome://navigator/content/navigator.xul".
72 rv
= nsChromeRegistry::Canonify(surl
);
73 mozilla::Unused
<< NS_WARN_IF(NS_FAILED(rv
));
80 nsChromeProtocolHandler::NewChannel(nsIURI
* aURI
, nsILoadInfo
* aLoadInfo
,
81 nsIChannel
** aResult
) {
84 NS_ENSURE_ARG_POINTER(aURI
);
85 NS_ENSURE_ARG_POINTER(aLoadInfo
);
87 MOZ_ASSERT(aResult
, "Null out param");
89 nsCOMPtr
<nsIURI
> debugURL
= aURI
;
90 rv
= nsChromeRegistry::Canonify(debugURL
);
91 NS_ENSURE_SUCCESS(rv
, rv
);
93 nsCOMPtr
<nsIChannel
> result
;
95 if (!nsChromeRegistry::gChromeRegistry
) {
96 // We don't actually want this ref, we just want the service to
97 // initialize if it hasn't already.
98 nsCOMPtr
<nsIChromeRegistry
> reg
= mozilla::services::GetChromeRegistry();
99 NS_ENSURE_TRUE(nsChromeRegistry::gChromeRegistry
, NS_ERROR_FAILURE
);
102 nsCOMPtr
<nsIURI
> resolvedURI
;
103 rv
= nsChromeRegistry::gChromeRegistry
->ConvertChromeURL(
104 aURI
, getter_AddRefs(resolvedURI
));
107 printf("Couldn't convert chrome URL: %s\n", aURI
->GetSpecOrDefault().get());
112 // We don't want to allow the inner protocol handler modify the result
113 // principal URI since we want either |aURI| or anything pre-set by upper
114 // layers to prevail.
115 nsCOMPtr
<nsIURI
> savedResultPrincipalURI
;
117 aLoadInfo
->GetResultPrincipalURI(getter_AddRefs(savedResultPrincipalURI
));
118 NS_ENSURE_SUCCESS(rv
, rv
);
120 rv
= NS_NewChannelInternal(getter_AddRefs(result
), resolvedURI
, aLoadInfo
);
121 NS_ENSURE_SUCCESS(rv
, rv
);
124 nsCOMPtr
<nsIFileChannel
> fileChan(do_QueryInterface(result
));
126 nsCOMPtr
<nsIFile
> file
;
127 fileChan
->GetFile(getter_AddRefs(file
));
130 file
->Exists(&exists
);
132 printf("Chrome file doesn't exist: %s\n",
133 file
->HumanReadablePath().get());
138 // Make sure that the channel remembers where it was
139 // originally loaded from.
140 rv
= aLoadInfo
->SetResultPrincipalURI(savedResultPrincipalURI
);
141 NS_ENSURE_SUCCESS(rv
, rv
);
142 rv
= result
->SetOriginalURI(aURI
);
143 if (NS_FAILED(rv
)) return rv
;
145 // Use a system principal for /content and /skin files.
146 // See bug 1855225 for discussion about whether to extend it more generally
147 // to other chrome:// URIs.
149 aURI
->GetPathQueryRef(path
);
150 if (StringBeginsWith(path
, "/content/"_ns
) ||
151 StringBeginsWith(path
, "/skin/"_ns
)) {
152 result
->SetOwner(nsContentUtils::GetSystemPrincipal());
155 // XXX Removed dependency-tracking code from here, because we're not
156 // tracking them anyways (with fastload we checked only in DEBUG
157 // and with startupcache not at all), but this is where we would start
158 // if we need to re-add.
159 // See bug 531886, bug 533038.
160 result
->SetContentCharset("UTF-8"_ns
);
162 result
.forget(aResult
);
166 ////////////////////////////////////////////////////////////////////////////////