Back to index

lightning-sunbird  0.9+nobinonly
Static Public Member Functions
URIUtils Class Reference

A utility class for URI handling Not yet finished, only handles file URI at this point. More...

#include <txURIUtils.h>

List of all members.

Static Public Member Functions

static PRBool CanCallerAccess (nsIDOMNode *aNode)
static void ResetWithSource (nsIDocument *aNewDoc, nsIDOMNode *aSourceNode)
 Reset the given document with the document of the source node.
static void resolveHref (const nsAString &href, const nsAString &base, nsAString &dest)
 Resolves the given href argument, using the given documentBase if necessary.

Detailed Description

A utility class for URI handling Not yet finished, only handles file URI at this point.

Definition at line 73 of file txURIUtils.h.


Member Function Documentation

Definition at line 208 of file txURIUtils.cpp.

{
    if (!gTxSecurityManager) {
        // No security manager available, let any calls go through...

        return PR_TRUE;
    }

    nsCOMPtr<nsIPrincipal> subjectPrincipal;
    gTxSecurityManager->GetSubjectPrincipal(getter_AddRefs(subjectPrincipal));

    if (!subjectPrincipal) {
        // we're running as system, grant access to the node.

        return PR_TRUE;
    }

    // Check whether the subject principal is the system principal.
    // For performance, we will avoid calling SubjectPrincipalIsChrome()
    // since it calls GetSubjectPrincipal() which causes us to walk
    // the JS frame stack.  We already did that above, so just get the
    // system principal from the security manager, and do a raw comparison.
    nsCOMPtr<nsIPrincipal> systemPrincipal;
    gTxSecurityManager->GetSystemPrincipal(getter_AddRefs(systemPrincipal));

    if (subjectPrincipal == systemPrincipal) {
        // we're running as system, grant access to the node.

        return PR_TRUE;
    }

    // Make sure that this is a real node. We do this by first QI'ing to
    // nsIContent (which is important performance wise) and if that QI
    // fails we QI to nsIDocument. If both those QI's fail we won't let
    // the caller access this unknown node.
    nsIPrincipal *principal = nsnull;
    nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
    nsCOMPtr<nsIAttribute> attr;
    nsCOMPtr<nsIDocument> doc;

    if (!content) {
        doc = do_QueryInterface(aNode);

        if (!doc) {
            attr = do_QueryInterface(aNode);
            if (!attr) {
                // aNode is not a nsIContent, a nsIAttribute or a nsIDocument,
                // something weird is going on...

                NS_ERROR("aNode is not a nsIContent, a nsIAttribute or a nsIDocument!");

                return PR_FALSE;
            }
        }
    }

    if (!doc) {
        nsCOMPtr<nsIDOMDocument> domDoc;
        aNode->GetOwnerDocument(getter_AddRefs(domDoc));
        if (!domDoc) {
            nsINodeInfo *ni;
            if (content) {
                ni = content->GetNodeInfo();
            }
            else {
                ni = attr->NodeInfo();
            }

            if (!ni) {
                // aNode is not part of a document, let any caller access it.

                return PR_TRUE;
            }

            principal = ni->GetDocumentPrincipal();
            if (!principal) {
              // we can't get to the principal so we'll give up and give the
              // caller access

              return PR_TRUE;
            }
        }
        else {
            doc = do_QueryInterface(domDoc);
            NS_ASSERTION(doc, "QI to nsIDocument failed");
        }
    }

    if (!principal) {
        principal = doc->GetPrincipal();
    }

    if (!principal) {
        // We can't get hold of the principal for this node. This should happen
        // very rarely, like for textnodes out of the tree and <option>s created
        // using 'new Option'.

        return PR_TRUE;
    }

    if (principal == systemPrincipal) {
        // We already know the subject is NOT systemPrincipal, no point calling
        // CheckSameOriginPrincipal since we know they don't match.

        return PR_FALSE;
    }

    // Ask the securitymanager if we have "UniversalBrowserRead"
    PRBool caps = PR_FALSE;
    nsresult rv =
        gTxSecurityManager->IsCapabilityEnabled("UniversalBrowserRead",
                                                &caps);
    NS_ENSURE_SUCCESS(rv, PR_FALSE);
    if (caps) {
        return PR_TRUE;
    }

    rv = gTxSecurityManager->CheckSameOriginPrincipal(subjectPrincipal,
                                                      principal);

    return NS_SUCCEEDED(rv);
}

Here is the call graph for this function:

void URIUtils::ResetWithSource ( nsIDocument aNewDoc,
nsIDOMNode aSourceNode 
) [static]

Reset the given document with the document of the source node.

Definition at line 333 of file txURIUtils.cpp.

{
    if (!aSourceNode) {
        // XXXbz passing nsnull as the first arg to Reset is illegal
        aNewDoc->Reset(nsnull, nsnull);
        return;
    }

    nsCOMPtr<nsIDocument> sourceDoc = do_QueryInterface(aSourceNode);
    if (!sourceDoc) {
        nsCOMPtr<nsIDOMDocument> sourceDOMDocument;
        aSourceNode->GetOwnerDocument(getter_AddRefs(sourceDOMDocument));
        sourceDoc = do_QueryInterface(sourceDOMDocument);
    }
    if (!sourceDoc) {
        NS_ASSERTION(0, "no source document found");
        // XXXbz passing nsnull as the first arg to Reset is illegal
        aNewDoc->Reset(nsnull, nsnull);
        return;
    }

    nsIPrincipal* sourcePrincipal = sourceDoc->GetPrincipal();
    if (!sourcePrincipal) {
        return;
    }

    // Copy the channel and loadgroup from the source document.
    nsCOMPtr<nsILoadGroup> loadGroup = sourceDoc->GetDocumentLoadGroup();
    nsCOMPtr<nsIChannel> channel = sourceDoc->GetChannel();
    if (!channel) {
        // Need to synthesize one
        if (NS_FAILED(NS_NewChannel(getter_AddRefs(channel),
                                    sourceDoc->GetDocumentURI(),
                                    nsnull,
                                    loadGroup))) {
            return;
        }
        channel->SetOwner(sourcePrincipal);
    }
    aNewDoc->Reset(channel, loadGroup);
    aNewDoc->SetPrincipal(sourcePrincipal);
    aNewDoc->SetBaseURI(sourceDoc->GetBaseURI());

    // Copy charset
    aNewDoc->SetDocumentCharacterSet(sourceDoc->GetDocumentCharacterSet());
    aNewDoc->SetDocumentCharacterSetSource(
          sourceDoc->GetDocumentCharacterSetSource());
}

Here is the call graph for this function:

Here is the caller graph for this function:

void URIUtils::resolveHref ( const nsAString &  href,
const nsAString &  base,
nsAString &  dest 
) [static]

Resolves the given href argument, using the given documentBase if necessary.

URIUtils A set of utilities for handling URIs.

The new resolved href will be appended to the given dest String

Resolves the given href argument, using the given documentBase if necessary. The new resolved href will be appended to the given dest String

Definition at line 169 of file txURIUtils.cpp.

                                            {
    if (base.IsEmpty()) {
        dest.Append(href);
        return;
    }
    if (href.IsEmpty()) {
        dest.Append(base);
        return;
    }

#ifndef TX_EXE
    nsCOMPtr<nsIURI> pURL;
    nsAutoString resultHref;
    nsresult result = NS_NewURI(getter_AddRefs(pURL), base);
    if (NS_SUCCEEDED(result)) {
        NS_MakeAbsoluteURI(resultHref, href, pURL);
        dest.Append(resultHref);
    }
#else
    nsAutoString documentBase;
    getDocumentBase(PromiseFlatString(base), documentBase);

    //-- join document base + href
    if (!documentBase.IsEmpty()) {
        dest.Append(documentBase);
        if (documentBase.CharAt(documentBase.Length()-1) != HREF_PATH_SEP)
            dest.Append(PRUnichar(HREF_PATH_SEP));
    }
    dest.Append(href);

#endif
} //-- resolveHref

Here is the call graph for this function:

Here is the caller graph for this function:


The documentation for this class was generated from the following files: