Bug 581726 - Disable tab previews for windows that have panorama displayed. r=dao...
[mozilla-central.git] / browser / components / about / AboutRedirector.cpp
blob0172b341b0cbafea3a37713f8087d25c1898d483
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
13 * License.
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.
22 * Contributor(s):
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"
46 namespace mozilla {
47 namespace browser {
49 NS_IMPL_ISUPPORTS1(AboutRedirector, nsIAboutModule)
51 struct RedirEntry {
52 const char* id;
53 const char* url;
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 },
75 #endif
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 },
86 { "rights",
87 #ifdef MOZ_OFFICIAL_BRANDING
88 "chrome://global/content/aboutRights.xhtml",
89 #else
90 "chrome://global/content/aboutRights-unbranded.xhtml",
91 #endif
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 },
102 #endif
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);
109 static nsCAutoString
110 GetAboutModuleName(nsIURI *aURI)
112 nsCAutoString path;
113 aURI->GetPath(path);
115 PRInt32 f = path.FindChar('#');
116 if (f >= 0)
117 path.SetLength(f);
119 f = path.FindChar('?');
120 if (f >= 0)
121 path.SetLength(f);
123 ToLowerCase(path);
124 return path;
127 NS_IMETHODIMP
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);
135 nsresult rv;
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);
163 return rv;
167 return NS_ERROR_ILLEGAL_VALUE;
170 NS_IMETHODIMP
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;
180 return NS_OK;
184 return NS_ERROR_ILLEGAL_VALUE;
187 nsresult
188 AboutRedirector::Create(nsISupports *aOuter, REFNSIID aIID, void **result)
190 AboutRedirector* about = new AboutRedirector();
191 if (about == nsnull)
192 return NS_ERROR_OUT_OF_MEMORY;
193 NS_ADDREF(about);
194 nsresult rv = about->QueryInterface(aIID, result);
195 NS_RELEASE(about);
196 return rv;
199 } // namespace browser
200 } // namespace mozilla