content/base/src/FeedWriterEnabled.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/src/FeedWriterEnabled.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,47 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "js/TypeDecls.h"
    1.10 +#include "nsGlobalWindow.h"
    1.11 +#include "nsIPrincipal.h"
    1.12 +#include "nsIURI.h"
    1.13 +#include "nsString.h"
    1.14 +#include "xpcpublic.h"
    1.15 +
    1.16 +namespace mozilla {
    1.17 +
    1.18 +struct FeedWriterEnabled {
    1.19 +  static bool IsEnabled(JSContext* cx, JSObject* aGlobal)
    1.20 +  {
    1.21 +    // Make sure the global is a window
    1.22 +    nsGlobalWindow* win = xpc::WindowGlobalOrNull(aGlobal);
    1.23 +    if (!win) {
    1.24 +      return false;
    1.25 +    }
    1.26 +
    1.27 +    // Make sure that the principal is about:feeds.
    1.28 +    nsCOMPtr<nsIPrincipal> principal = win->GetPrincipal();
    1.29 +    NS_ENSURE_TRUE(principal, false);
    1.30 +    nsCOMPtr<nsIURI> uri;
    1.31 +    principal->GetURI(getter_AddRefs(uri));
    1.32 +    if (!uri) {
    1.33 +      return false;
    1.34 +    }
    1.35 +
    1.36 +    // First check the scheme to avoid getting long specs in the common case.
    1.37 +    bool isAbout = false;
    1.38 +    uri->SchemeIs("about", &isAbout);
    1.39 +    if (!isAbout) {
    1.40 +      return false;
    1.41 +    }
    1.42 +
    1.43 +    // Now check the spec itself
    1.44 +    nsAutoCString spec;
    1.45 +    uri->GetSpec(spec);
    1.46 +    return spec.Equals("about:feeds");
    1.47 +  }
    1.48 +};
    1.49 +
    1.50 +}

mercurial