Issue #890 - Port cpp aboutRedirector to javascript

This commit is contained in:
NTD 2018-11-30 10:33:08 -05:00 committed by Roy Tam
commit ed5d1e2167
9 changed files with 139 additions and 263 deletions

View file

@ -1,3 +1,22 @@
# nsAboutRedirector.js
component {8cc51368-6aa0-43e8-b762-bde9b9fd828c} nsAboutRedirector.js
# Each entry here should be coupled with an entry in nsAboutRedirector.js
contract @mozilla.org/network/protocol/about;1?what=certerror {8cc51368-6aa0-43e8-b762-bde9b9fd828c}
contract @mozilla.org/network/protocol/about;1?what=downloads {8cc51368-6aa0-43e8-b762-bde9b9fd828c}
contract @mozilla.org/network/protocol/about;1?what=feeds {8cc51368-6aa0-43e8-b762-bde9b9fd828c}
contract @mozilla.org/network/protocol/about;1?what=home {8cc51368-6aa0-43e8-b762-bde9b9fd828c}
contract @mozilla.org/network/protocol/about;1?what=newtab {8cc51368-6aa0-43e8-b762-bde9b9fd828c}
contract @mozilla.org/network/protocol/about;1?what=palemoon {8cc51368-6aa0-43e8-b762-bde9b9fd828c}
contract @mozilla.org/network/protocol/about;1?what=permissions {8cc51368-6aa0-43e8-b762-bde9b9fd828c}
contract @mozilla.org/network/protocol/about;1?what=privatebrowsing {8cc51368-6aa0-43e8-b762-bde9b9fd828c}
contract @mozilla.org/network/protocol/about;1?what=rights {8cc51368-6aa0-43e8-b762-bde9b9fd828c}
contract @mozilla.org/network/protocol/about;1?what=robots {8cc51368-6aa0-43e8-b762-bde9b9fd828c}
contract @mozilla.org/network/protocol/about;1?what=sessionrestore {8cc51368-6aa0-43e8-b762-bde9b9fd828c}
#ifdef MOZ_SERVICES_SYNC
contract @mozilla.org/network/protocol/about;1?what=sync-progress {8cc51368-6aa0-43e8-b762-bde9b9fd828c}
contract @mozilla.org/network/protocol/about;1?what=sync-tabs {8cc51368-6aa0-43e8-b762-bde9b9fd828c}
#endif
# nsBrowserContentHandler.js
component {5d0ce354-df01-421a-83fb-7ead0990c24e} nsBrowserContentHandler.js application={8de7fcbb-c55c-4fbe-bfc5-fc555c87dbc4}
contract @mozilla.org/browser/clh;1 {5d0ce354-df01-421a-83fb-7ead0990c24e} application={8de7fcbb-c55c-4fbe-bfc5-fc555c87dbc4}

View file

@ -1,184 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// See also: docshell/base/nsAboutRedirector.cpp
#include "AboutRedirector.h"
#include "nsNetUtil.h"
#include "nsIScriptSecurityManager.h"
#include "mozilla/ArrayUtils.h"
namespace mozilla {
namespace browser {
NS_IMPL_ISUPPORTS(AboutRedirector, nsIAboutModule)
struct RedirEntry {
const char* id;
const char* url;
uint32_t flags;
};
/*
Entries which do not have URI_SAFE_FOR_UNTRUSTED_CONTENT will run with chrome
privileges. This is potentially dangerous. Please use
URI_SAFE_FOR_UNTRUSTED_CONTENT in the third argument to each map item below
unless your about: page really needs chrome privileges. Security review is
required before adding new map entries without
URI_SAFE_FOR_UNTRUSTED_CONTENT.
*/
static RedirEntry kRedirMap[] = {
{
"certerror", "chrome://browser/content/certerror/aboutCertError.xhtml",
nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
nsIAboutModule::ALLOW_SCRIPT |
nsIAboutModule::HIDE_FROM_ABOUTABOUT
},
{
"downloads", "chrome://browser/content/downloads/contentAreaDownloadsView.xul",
nsIAboutModule::ALLOW_SCRIPT
},
{
"feeds", "chrome://browser/content/feeds/subscribe.xhtml",
nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
nsIAboutModule::ALLOW_SCRIPT |
nsIAboutModule::HIDE_FROM_ABOUTABOUT
},
{
"home", "chrome://browser/content/abouthome/aboutHome.xhtml",
nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
nsIAboutModule::MAKE_LINKABLE |
nsIAboutModule::ALLOW_SCRIPT
},
{
"newtab", "chrome://browser/content/newtab/newTab.xhtml",
nsIAboutModule::ALLOW_SCRIPT
},
{
"palemoon", "chrome://browser/content/palemoon.xhtml",
nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
nsIAboutModule::HIDE_FROM_ABOUTABOUT
},
{
"permissions", "chrome://browser/content/permissions/aboutPermissions.xul",
nsIAboutModule::ALLOW_SCRIPT
},
{
"privatebrowsing", "chrome://browser/content/aboutPrivateBrowsing.xhtml",
nsIAboutModule::ALLOW_SCRIPT
},
{
"rights", "chrome://global/content/aboutRights.xhtml",
nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
nsIAboutModule::MAKE_LINKABLE |
nsIAboutModule::ALLOW_SCRIPT
},
{
"robots", "chrome://browser/content/aboutRobots.xhtml",
nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
nsIAboutModule::ALLOW_SCRIPT
},
{
"sessionrestore", "chrome://browser/content/aboutSessionRestore.xhtml",
nsIAboutModule::ALLOW_SCRIPT
},
#ifdef MOZ_SERVICES_SYNC
{
"sync-progress", "chrome://browser/content/sync/progress.xhtml",
nsIAboutModule::ALLOW_SCRIPT
},
{
"sync-tabs", "chrome://browser/content/sync/aboutSyncTabs.xul",
nsIAboutModule::ALLOW_SCRIPT
},
#endif
};
static const int kRedirTotal = ArrayLength(kRedirMap);
static nsAutoCString
GetAboutModuleName(nsIURI *aURI)
{
nsAutoCString path;
aURI->GetPath(path);
int32_t f = path.FindChar('#');
if (f >= 0)
path.SetLength(f);
f = path.FindChar('?');
if (f >= 0)
path.SetLength(f);
ToLowerCase(path);
return path;
}
NS_IMETHODIMP
AboutRedirector::NewChannel(nsIURI* aURI,
nsILoadInfo* aLoadInfo,
nsIChannel** result)
{
NS_ENSURE_ARG_POINTER(aURI);
NS_ASSERTION(result, "must not be null");
nsAutoCString path = GetAboutModuleName(aURI);
nsresult rv;
nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
NS_ENSURE_SUCCESS(rv, rv);
for (int i = 0; i < kRedirTotal; i++) {
if (!strcmp(path.get(), kRedirMap[i].id)) {
nsCOMPtr<nsIChannel> tempChannel;
nsCOMPtr<nsIURI> tempURI;
rv = NS_NewURI(getter_AddRefs(tempURI),
nsDependentCString(kRedirMap[i].url));
NS_ENSURE_SUCCESS(rv, rv);
rv = NS_NewChannelInternal(getter_AddRefs(tempChannel),
tempURI,
aLoadInfo);
NS_ENSURE_SUCCESS(rv, rv);
tempChannel->SetOriginalURI(aURI);
NS_ADDREF(*result = tempChannel);
return rv;
}
}
return NS_ERROR_ILLEGAL_VALUE;
}
NS_IMETHODIMP
AboutRedirector::GetURIFlags(nsIURI *aURI, uint32_t *result)
{
NS_ENSURE_ARG_POINTER(aURI);
nsAutoCString name = GetAboutModuleName(aURI);
for (int i = 0; i < kRedirTotal; i++) {
if (name.Equals(kRedirMap[i].id)) {
*result = kRedirMap[i].flags;
return NS_OK;
}
}
return NS_ERROR_ILLEGAL_VALUE;
}
nsresult
AboutRedirector::Create(nsISupports *aOuter, REFNSIID aIID, void **result)
{
AboutRedirector* about = new AboutRedirector();
if (about == nullptr)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(about);
nsresult rv = about->QueryInterface(aIID, result);
NS_RELEASE(about);
return rv;
}
} // namespace browser
} // namespace mozilla

View file

@ -1,32 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef AboutRedirector_h__
#define AboutRedirector_h__
#include "nsIAboutModule.h"
namespace mozilla {
namespace browser {
class AboutRedirector : public nsIAboutModule
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIABOUTMODULE
AboutRedirector() {}
static nsresult
Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
protected:
virtual ~AboutRedirector() {}
};
} // namespace browser
} // namespace mozilla
#endif // AboutRedirector_h__

View file

@ -1,19 +0,0 @@
# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
EXPORTS.mozilla.browser += [
'AboutRedirector.h',
]
SOURCES += [
'AboutRedirector.cpp',
]
FINAL_LIBRARY = 'browsercomps'
LOCAL_INCLUDES += [
'../build',
]

View file

@ -15,7 +15,6 @@ SOURCES += [
XPCOMBinaryComponent('browsercomps')
LOCAL_INCLUDES += [
'../about',
'../dirprovider',
'../feeds',
'../shell',

View file

@ -26,10 +26,6 @@
#define NS_PRIVATE_BROWSING_SERVICE_WRAPPER_CID \
{ 0x136e2c4d, 0xc5a4, 0x477c, { 0xb1, 0x31, 0xd9, 0x3d, 0x7d, 0x70, 0x4f, 0x64 } }
// 7e4bb6ad-2fc4-4dc6-89ef-23e8e5ccf980
#define NS_BROWSER_ABOUT_REDIRECTOR_CID \
{ 0x7e4bb6ad, 0x2fc4, 0x4dc6, { 0x89, 0xef, 0x23, 0xe8, 0xe5, 0xcc, 0xf9, 0x80 } }
// {6DEB193C-F87D-4078-BC78-5E64655B4D62}
#define NS_BROWSERDIRECTORYPROVIDER_CID \
{ 0x6deb193c, 0xf87d, 0x4078, { 0xbc, 0x78, 0x5e, 0x64, 0x65, 0x5b, 0x4d, 0x62 } }

View file

@ -18,8 +18,6 @@
#include "rdf.h"
#include "nsFeedSniffer.h"
#include "AboutRedirector.h"
#include "nsIAboutModule.h"
#include "nsNetCID.h"
@ -45,7 +43,6 @@ NS_DEFINE_NAMED_CID(NS_SHELLSERVICE_CID);
NS_DEFINE_NAMED_CID(NS_SHELLSERVICE_CID);
#endif
NS_DEFINE_NAMED_CID(NS_FEEDSNIFFER_CID);
NS_DEFINE_NAMED_CID(NS_BROWSER_ABOUT_REDIRECTOR_CID);
#ifdef XP_MACOSX
NS_DEFINE_NAMED_CID(NS_SHELLSERVICE_CID);
#endif
@ -58,7 +55,6 @@ static const mozilla::Module::CIDEntry kBrowserCIDs[] = {
{ &kNS_SHELLSERVICE_CID, false, nullptr, nsGNOMEShellServiceConstructor },
#endif
{ &kNS_FEEDSNIFFER_CID, false, nullptr, nsFeedSnifferConstructor },
{ &kNS_BROWSER_ABOUT_REDIRECTOR_CID, false, nullptr, AboutRedirector::Create },
#ifdef XP_MACOSX
{ &kNS_SHELLSERVICE_CID, false, nullptr, nsMacShellServiceConstructor },
#endif
@ -73,22 +69,6 @@ static const mozilla::Module::ContractIDEntry kBrowserContracts[] = {
{ NS_SHELLSERVICE_CONTRACTID, &kNS_SHELLSERVICE_CID },
#endif
{ NS_FEEDSNIFFER_CONTRACTID, &kNS_FEEDSNIFFER_CID },
{ NS_ABOUT_MODULE_CONTRACTID_PREFIX "certerror", &kNS_BROWSER_ABOUT_REDIRECTOR_CID },
{ NS_ABOUT_MODULE_CONTRACTID_PREFIX "socialerror", &kNS_BROWSER_ABOUT_REDIRECTOR_CID },
{ NS_ABOUT_MODULE_CONTRACTID_PREFIX "feeds", &kNS_BROWSER_ABOUT_REDIRECTOR_CID },
{ NS_ABOUT_MODULE_CONTRACTID_PREFIX "privatebrowsing", &kNS_BROWSER_ABOUT_REDIRECTOR_CID },
{ NS_ABOUT_MODULE_CONTRACTID_PREFIX "rights", &kNS_BROWSER_ABOUT_REDIRECTOR_CID },
{ NS_ABOUT_MODULE_CONTRACTID_PREFIX "palemoon", &kNS_BROWSER_ABOUT_REDIRECTOR_CID },
{ NS_ABOUT_MODULE_CONTRACTID_PREFIX "robots", &kNS_BROWSER_ABOUT_REDIRECTOR_CID },
{ NS_ABOUT_MODULE_CONTRACTID_PREFIX "sessionrestore", &kNS_BROWSER_ABOUT_REDIRECTOR_CID },
#ifdef MOZ_SERVICES_SYNC
{ NS_ABOUT_MODULE_CONTRACTID_PREFIX "sync-tabs", &kNS_BROWSER_ABOUT_REDIRECTOR_CID },
{ NS_ABOUT_MODULE_CONTRACTID_PREFIX "sync-progress", &kNS_BROWSER_ABOUT_REDIRECTOR_CID },
#endif
{ NS_ABOUT_MODULE_CONTRACTID_PREFIX "home", &kNS_BROWSER_ABOUT_REDIRECTOR_CID },
{ NS_ABOUT_MODULE_CONTRACTID_PREFIX "newtab", &kNS_BROWSER_ABOUT_REDIRECTOR_CID },
{ NS_ABOUT_MODULE_CONTRACTID_PREFIX "permissions", &kNS_BROWSER_ABOUT_REDIRECTOR_CID },
{ NS_ABOUT_MODULE_CONTRACTID_PREFIX "downloads", &kNS_BROWSER_ABOUT_REDIRECTOR_CID },
#ifdef XP_MACOSX
{ NS_SHELLSERVICE_CONTRACTID, &kNS_SHELLSERVICE_CID },
#endif

View file

@ -5,7 +5,6 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
DIRS += [
'about',
'certerror',
'dirprovider',
'downloads',
@ -32,9 +31,9 @@ XPIDL_SOURCES += [
XPIDL_MODULE = 'browsercompsbase'
EXTRA_COMPONENTS += [ 'BrowserComponents.manifest' ]
EXTRA_PP_COMPONENTS += [
'BrowserComponents.manifest',
'nsAboutRedirector.js',
'nsBrowserContentHandler.js',
'nsBrowserGlue.js',
]

View file

@ -0,0 +1,118 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var Ci = Components.interfaces;
var Cr = Components.results;
var Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
// See: netwerk/protocol/about/nsIAboutModule.idl
const URI_SAFE_FOR_UNTRUSTED_CONTENT = Ci.nsIAboutModule.URI_SAFE_FOR_UNTRUSTED_CONTENT;
const ALLOW_SCRIPT = Ci.nsIAboutModule.ALLOW_SCRIPT;
const HIDE_FROM_ABOUTABOUT = Ci.nsIAboutModule.HIDE_FROM_ABOUTABOUT;
const MAKE_LINKABLE = Ci.nsIAboutModule.MAKE_LINKABLE;
function AboutRedirector() {}
AboutRedirector.prototype = {
classDescription: "Browser about: Redirector",
classID: Components.ID("{8cc51368-6aa0-43e8-b762-bde9b9fd828c}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIAboutModule]),
// Each entry in the map has the key as the part after the "about:" and the
// value as a record with url and flags entries. Note that each addition here
// should be coupled with a corresponding addition in BrowserComponents.manifest.
_redirMap: {
"certerror": {
url: "chrome://browser/content/certerror/aboutCertError.xhtml",
flags: (URI_SAFE_FOR_UNTRUSTED_CONTENT | ALLOW_SCRIPT | HIDE_FROM_ABOUTABOUT)
},
"downloads": {
url: "chrome://browser/content/downloads/contentAreaDownloadsView.xul",
flags: ALLOW_SCRIPT
},
"feeds": {
url: "chrome://browser/content/feeds/subscribe.xhtml",
flags: (URI_SAFE_FOR_UNTRUSTED_CONTENT | ALLOW_SCRIPT | HIDE_FROM_ABOUTABOUT)
},
"home": {
url: "chrome://browser/content/abouthome/aboutHome.xhtml",
flags: (URI_SAFE_FOR_UNTRUSTED_CONTENT | MAKE_LINKABLE | ALLOW_SCRIPT)
},
"newtab": {
url: "chrome://browser/content/newtab/newTab.xhtml",
flags: ALLOW_SCRIPT
},
"palemoon": {
url: "chrome://browser/content/palemoon.xhtml",
flags: (URI_SAFE_FOR_UNTRUSTED_CONTENT | HIDE_FROM_ABOUTABOUT)
},
"permissions": {
url: "chrome://browser/content/permissions/aboutPermissions.xul",
flags: ALLOW_SCRIPT
},
"privatebrowsing": {
url: "chrome://browser/content/aboutPrivateBrowsing.xhtml",
flags: ALLOW_SCRIPT
},
"rights": {
url: "chrome://global/content/aboutRights.xhtml",
flags: (URI_SAFE_FOR_UNTRUSTED_CONTENT | MAKE_LINKABLE | ALLOW_SCRIPT)
},
"robots": {
url: "chrome://browser/content/aboutRobots.xhtml",
flags: (URI_SAFE_FOR_UNTRUSTED_CONTENT | ALLOW_SCRIPT | HIDE_FROM_ABOUTABOUT)
},
"sessionrestore": {
url: "chrome://browser/content/aboutSessionRestore.xhtml",
flags: ALLOW_SCRIPT
},
#ifdef MOZ_SERVICES_SYNC
"sync-progress": {
url: "chrome://browser/content/sync/progress.xhtml",
flags: ALLOW_SCRIPT
},
"sync-tabs": {
url: "chrome://browser/content/sync/aboutSyncTabs.xul",
flags: ALLOW_SCRIPT
},
#endif
},
/**
* Gets the module name from the given URI.
*/
_getModuleName: function AboutRedirector__getModuleName(aURI) {
// Strip out the first ? or #, and anything following it
let name = (/[^?#]+/.exec(aURI.path))[0];
return name.toLowerCase();
},
getURIFlags: function(aURI) {
let name = this._getModuleName(aURI);
if (!(name in this._redirMap))
throw Cr.NS_ERROR_ILLEGAL_VALUE;
return this._redirMap[name].flags;
},
newChannel: function(aURI, aLoadInfo) {
let name = this._getModuleName(aURI);
if (!(name in this._redirMap))
throw Cr.NS_ERROR_ILLEGAL_VALUE;
let newURI = Services.io.newURI(this._redirMap[name].url, null, null);
let channel = Services.io.newChannelFromURIWithLoadInfo(newURI, aLoadInfo);
channel.originalURI = aURI;
if (this._redirMap[name].flags & Ci.nsIAboutModule.URI_SAFE_FOR_UNTRUSTED_CONTENT) {
let principal = Services.scriptSecurityManager.getNoAppCodebasePrincipal(aURI);
channel.owner = principal;
}
return channel;
}
};
var NSGetFactory = XPCOMUtils.generateNSGetFactory([AboutRedirector]);