Bug 1238042 - Extract a helper function to check if a JSObject is a global with a particular about: URI. r=ehsan

--HG--
extra : commitid : 9MHQnTiMoX9
This commit is contained in:
Kartikaya Gupta 2016-01-22 20:27:28 -05:00
parent 775c070ce4
commit be608f4519
3 changed files with 44 additions and 31 deletions

View File

@ -5,43 +5,14 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "js/TypeDecls.h"
#include "nsGlobalWindow.h"
#include "nsIPrincipal.h"
#include "nsIURI.h"
#include "nsString.h"
#include "xpcpublic.h"
#include "nsContentUtils.h"
namespace mozilla {
struct FeedWriterEnabled {
static bool IsEnabled(JSContext* cx, JSObject* aGlobal)
{
// Make sure the global is a window
nsGlobalWindow* win = xpc::WindowGlobalOrNull(aGlobal);
if (!win) {
return false;
}
// Make sure that the principal is about:feeds.
nsCOMPtr<nsIPrincipal> principal = win->GetPrincipal();
NS_ENSURE_TRUE(principal, false);
nsCOMPtr<nsIURI> uri;
principal->GetURI(getter_AddRefs(uri));
if (!uri) {
return false;
}
// First check the scheme to avoid getting long specs in the common case.
bool isAbout = false;
uri->SchemeIs("about", &isAbout);
if (!isAbout) {
return false;
}
// Now check the spec itself
nsAutoCString spec;
uri->GetSpec(spec);
return spec.EqualsLiteral("about:feeds");
return nsContentUtils::IsSpecificAboutPage(aGlobal, "about:feeds");
}
};

View File

@ -8894,3 +8894,37 @@ nsContentUtils::SerializeNodeToMarkup(nsINode* aRoot,
}
}
}
bool
nsContentUtils::IsSpecificAboutPage(JSObject* aGlobal, const char* aUri)
{
// aUri must start with about: or this isn't the right function to be using.
MOZ_ASSERT(strncmp(aUri, "about:", 6) == 0);
// Make sure the global is a window
nsGlobalWindow* win = xpc::WindowGlobalOrNull(aGlobal);
if (!win) {
return false;
}
// Make sure that the principal is about:feeds.
nsCOMPtr<nsIPrincipal> principal = win->GetPrincipal();
NS_ENSURE_TRUE(principal, false);
nsCOMPtr<nsIURI> uri;
principal->GetURI(getter_AddRefs(uri));
if (!uri) {
return false;
}
// First check the scheme to avoid getting long specs in the common case.
bool isAbout = false;
uri->SchemeIs("about", &isAbout);
if (!isAbout) {
return false;
}
// Now check the spec itself
nsAutoCString spec;
uri->GetSpec(spec);
return spec.EqualsASCII(aUri);
}

View File

@ -2565,6 +2565,14 @@ public:
bool aDescendentsOnly,
nsAString& aOut);
/*
* Returns true iff the provided JSObject is a global, and its URI matches
* the provided about: URI.
* @param aGlobal the JSObject whose URI to check, if it is a global.
* @param aUri the URI to match, e.g. "about:feeds"
*/
static bool IsSpecificAboutPage(JSObject* aGlobal, const char* aUri);
private:
static bool InitializeEventTable();