1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Gagan Saksena (original author)
24 * Ryan Flint <rflint@mozilla.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 // See also: docshell/base/nsAboutRedirector.cpp
42 #include "AboutRedirector.h"
43 #include "nsNetUtil.h"
44 #include "nsIScriptSecurityManager.h"
49 NS_IMPL_ISUPPORTS1(AboutRedirector
, nsIAboutModule
)
54 PRUint32 flags
; // See nsIAboutModule. The URI_SAFE_FOR_UNTRUSTED_CONTENT
55 // flag does double duty here -- if it's not set, we don't
56 // drop chrome privileges.
60 Entries which do not have URI_SAFE_FOR_UNTRUSTED_CONTENT will run with chrome
61 privileges. This is potentially dangerous. Please use
62 URI_SAFE_FOR_UNTRUSTED_CONTENT in the third argument to each map item below
63 unless your about: page really needs chrome privileges. Security review is
64 required before adding new map entries without
65 URI_SAFE_FOR_UNTRUSTED_CONTENT. Also note, however, that adding
66 URI_SAFE_FOR_UNTRUSTED_CONTENT will allow random web sites to link to that
67 URI. Perhaps we should separate the two concepts out...
69 static RedirEntry kRedirMap
[] = {
70 #ifdef MOZ_SAFE_BROWSING
71 { "blocked", "chrome://browser/content/safebrowsing/blockedSite.xhtml",
72 nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT
|
73 nsIAboutModule::ALLOW_SCRIPT
|
74 nsIAboutModule::HIDE_FROM_ABOUTABOUT
},
76 { "certerror", "chrome://browser/content/certerror/aboutCertError.xhtml",
77 nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT
|
78 nsIAboutModule::ALLOW_SCRIPT
|
79 nsIAboutModule::HIDE_FROM_ABOUTABOUT
},
80 { "feeds", "chrome://browser/content/feeds/subscribe.xhtml",
81 nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT
|
82 nsIAboutModule::ALLOW_SCRIPT
|
83 nsIAboutModule::HIDE_FROM_ABOUTABOUT
},
84 { "privatebrowsing", "chrome://browser/content/aboutPrivateBrowsing.xhtml",
85 nsIAboutModule::ALLOW_SCRIPT
},
87 #ifdef MOZ_OFFICIAL_BRANDING
88 "chrome://global/content/aboutRights.xhtml",
90 "chrome://global/content/aboutRights-unbranded.xhtml",
92 nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT
|
93 nsIAboutModule::ALLOW_SCRIPT
},
94 { "robots", "chrome://browser/content/aboutRobots.xhtml",
95 nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT
|
96 nsIAboutModule::ALLOW_SCRIPT
},
97 { "sessionrestore", "chrome://browser/content/aboutSessionRestore.xhtml",
98 nsIAboutModule::ALLOW_SCRIPT
},
99 #ifdef MOZ_SERVICES_SYNC
100 { "sync-tabs", "chrome://browser/content/aboutSyncTabs.xul",
101 nsIAboutModule::ALLOW_SCRIPT
},
103 { "home", "chrome://browser/content/aboutHome.xhtml",
104 nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT
|
105 nsIAboutModule::ALLOW_SCRIPT
},
107 static const int kRedirTotal
= NS_ARRAY_LENGTH(kRedirMap
);
110 GetAboutModuleName(nsIURI
*aURI
)
115 PRInt32 f
= path
.FindChar('#');
119 f
= path
.FindChar('?');
128 AboutRedirector::NewChannel(nsIURI
*aURI
, nsIChannel
**result
)
130 NS_ENSURE_ARG_POINTER(aURI
);
131 NS_ASSERTION(result
, "must not be null");
133 nsCAutoString path
= GetAboutModuleName(aURI
);
136 nsCOMPtr
<nsIIOService
> ioService
= do_GetIOService(&rv
);
137 NS_ENSURE_SUCCESS(rv
, rv
);
139 for (int i
= 0; i
< kRedirTotal
; i
++) {
140 if (!strcmp(path
.get(), kRedirMap
[i
].id
)) {
141 nsCOMPtr
<nsIChannel
> tempChannel
;
142 rv
= ioService
->NewChannel(nsDependentCString(kRedirMap
[i
].url
),
143 nsnull
, nsnull
, getter_AddRefs(tempChannel
));
144 NS_ENSURE_SUCCESS(rv
, rv
);
146 tempChannel
->SetOriginalURI(aURI
);
148 // Keep the page from getting unnecessary privileges unless it needs them
149 if (kRedirMap
[i
].flags
& nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT
) {
150 nsCOMPtr
<nsIScriptSecurityManager
> securityManager
=
151 do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID
, &rv
);
152 NS_ENSURE_SUCCESS(rv
, rv
);
154 nsCOMPtr
<nsIPrincipal
> principal
;
155 rv
= securityManager
->GetCodebasePrincipal(aURI
, getter_AddRefs(principal
));
156 NS_ENSURE_SUCCESS(rv
, rv
);
158 rv
= tempChannel
->SetOwner(principal
);
159 NS_ENSURE_SUCCESS(rv
, rv
);
162 NS_ADDREF(*result
= tempChannel
);
167 return NS_ERROR_ILLEGAL_VALUE
;
171 AboutRedirector::GetURIFlags(nsIURI
*aURI
, PRUint32
*result
)
173 NS_ENSURE_ARG_POINTER(aURI
);
175 nsCAutoString name
= GetAboutModuleName(aURI
);
177 for (int i
= 0; i
< kRedirTotal
; i
++) {
178 if (name
.Equals(kRedirMap
[i
].id
)) {
179 *result
= kRedirMap
[i
].flags
;
184 return NS_ERROR_ILLEGAL_VALUE
;
188 AboutRedirector::Create(nsISupports
*aOuter
, REFNSIID aIID
, void **result
)
190 AboutRedirector
* about
= new AboutRedirector();
192 return NS_ERROR_OUT_OF_MEMORY
;
194 nsresult rv
= about
->QueryInterface(aIID
, result
);
199 } // namespace browser
200 } // namespace mozilla