Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes
nsLocation Class Reference

#include <nsGlobalWindow.h>

Inheritance diagram for nsLocation:
Inheritance graph
[legend]
Collaboration diagram for nsLocation:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsLocation (nsIDocShell *aDocShell)
virtual ~nsLocation ()
NS_DECL_ISUPPORTS void SetDocShell (nsIDocShell *aDocShell)
nsIDocShellGetDocShell ()
void reload (in boolean forceget)
void replace (in DOMString url)
void assign (in DOMString url)
DOMString toString ()
void reload ()

Public Attributes

attribute DOMString hash
 These properties refer to the current location of the document.
attribute DOMString host
attribute DOMString hostname
attribute DOMString href
attribute DOMString pathname
attribute DOMString port
attribute DOMString protocol
attribute DOMString search

Protected Member Functions

nsresult GetURI (nsIURI **aURL, PRBool aGetInnermostURI=PR_FALSE)
nsresult GetWritableURI (nsIURI **aURL)
nsresult SetURI (nsIURI *aURL, PRBool aReplace=PR_FALSE)
nsresult SetHrefWithBase (const nsAString &aHref, nsIURI *aBase, PRBool aReplace)
nsresult SetHrefWithContext (JSContext *cx, const nsAString &aHref, PRBool aReplace)
nsresult GetSourceBaseURL (JSContext *cx, nsIURI **sourceURL)
nsresult GetSourceDocument (JSContext *cx, nsIDocument **aDocument)
nsresult CheckURL (nsIURI *url, nsIDocShellLoadInfo **aLoadInfo)
nsresult FindUsableBaseURI (nsIURI *aBaseURI, nsIDocShell *aParent, nsIURI **aUsableURI)

Protected Attributes

nsWeakPtr mDocShell

Detailed Description

Definition at line 729 of file nsGlobalWindow.h.


Constructor & Destructor Documentation

Definition at line 135 of file nsLocation.cpp.

{
  mDocShell = do_GetWeakReference(aDocShell);
}

Here is the call graph for this function:

nsLocation::~nsLocation ( ) [virtual]

Definition at line 140 of file nsLocation.cpp.

{
}

Member Function Documentation

void nsIDOMLocation::assign ( in DOMString  url) [inherited]
nsresult nsLocation::CheckURL ( nsIURI url,
nsIDocShellLoadInfo **  aLoadInfo 
) [protected]

Definition at line 171 of file nsLocation.cpp.

{
  *aLoadInfo = nsnull;

  nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
  if (!docShell) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  nsresult result;
  // Get JSContext from stack.
  nsCOMPtr<nsIJSContextStack>
    stack(do_GetService("@mozilla.org/js/xpc/ContextStack;1", &result));

  if (NS_FAILED(result))
    return NS_ERROR_FAILURE;

  JSContext *cx;

  if (NS_FAILED(GetContextFromStack(stack, &cx)))
    return NS_ERROR_FAILURE;

  nsCOMPtr<nsISupports> owner;
  nsCOMPtr<nsIURI> sourceURI;

  if (cx) {
    // No cx means that there's no JS running, or at least no JS that
    // was run through code that properly pushed a context onto the
    // context stack (as all code that runs JS off of web pages
    // does). We won't bother with security checks in this case, but
    // we need to create the loadinfo etc.

    // Get security manager.
    nsCOMPtr<nsIScriptSecurityManager>
      secMan(do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &result));

    if (NS_FAILED(result))
      return NS_ERROR_FAILURE;

    // Check to see if URI is allowed.
    result = secMan->CheckLoadURIFromScript(cx, aURI);

    if (NS_FAILED(result))
      return result;

    // Now get the principal to use when loading the URI
    nsCOMPtr<nsIPrincipal> principal;
    if (NS_FAILED(secMan->GetSubjectPrincipal(getter_AddRefs(principal))) ||
        !principal)
      return NS_ERROR_FAILURE;
    owner = do_QueryInterface(principal);

    principal->GetURI(getter_AddRefs(sourceURI));
  }

  // Create load info
  nsCOMPtr<nsIDocShellLoadInfo> loadInfo;
  docShell->CreateLoadInfo(getter_AddRefs(loadInfo));
  NS_ENSURE_TRUE(loadInfo, NS_ERROR_FAILURE);

  loadInfo->SetOwner(owner);

  // now set the referrer on the loadinfo
  if (sourceURI) {
    loadInfo->SetReferrer(sourceURI);
  }

  loadInfo.swap(*aLoadInfo);

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsLocation::FindUsableBaseURI ( nsIURI aBaseURI,
nsIDocShell aParent,
nsIURI **  aUsableURI 
) [protected]

Definition at line 247 of file nsLocation.cpp.

{
  if (!aBaseURI || !aParent)
    return NS_ERROR_FAILURE;
  NS_ENSURE_ARG_POINTER(aUsableURI);

  *aUsableURI = nsnull;
  nsresult rv = NS_OK;    
  nsCOMPtr<nsIDocShell> parentDS = aParent;
  nsCOMPtr<nsIURI> baseURI = aBaseURI;
  nsCOMPtr<nsIIOService> ioService =
    do_GetService(NS_IOSERVICE_CONTRACTID, &rv);

  while(NS_SUCCEEDED(rv) && baseURI) {
    // Check if the current base uri supports relative uris.
    // We make this check by looking at the protocol flags of
    // the protocol handler. If the protocol flags has URI_NORELATIVE,
    // it means that the base uri does not support relative uris.
    nsCAutoString scheme;
    baseURI->GetScheme(scheme);
    nsCOMPtr<nsIProtocolHandler> protocolHandler;
    // Get the protocol handler for the base uri.
    ioService->GetProtocolHandler(scheme.get(), getter_AddRefs(protocolHandler));
    if (!protocolHandler)
      return NS_ERROR_FAILURE;
    PRUint32 pFlags; // Is there a default value for the protocol flags?
    protocolHandler->GetProtocolFlags(&pFlags);
    if (!(pFlags & nsIProtocolHandler::URI_NORELATIVE)) {
      *aUsableURI = baseURI;
      NS_ADDREF(*aUsableURI);
      return NS_OK;
    }

    // Get the same type parent docshell
    nsCOMPtr<nsIDocShellTreeItem> docShellAsTreeItem(do_QueryInterface(parentDS));
    if (!docShellAsTreeItem)
      return NS_ERROR_FAILURE;
    nsCOMPtr<nsIDocShellTreeItem> parentDSTreeItem;
    docShellAsTreeItem->GetSameTypeParent(getter_AddRefs(parentDSTreeItem));      
    nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(parentDSTreeItem));

    // Get the parent docshell's uri
    if (webNav) {
      rv = webNav->GetCurrentURI(getter_AddRefs(baseURI));
      parentDS = do_QueryInterface(parentDSTreeItem);
    }
    else
      return NS_ERROR_FAILURE;
  }  // while 

  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 164 of file nsLocation.cpp.

{
  nsCOMPtr<nsIDocShell> docshell(do_QueryReferent(mDocShell));
  return docshell;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsLocation::GetSourceBaseURL ( JSContext cx,
nsIURI **  sourceURL 
) [protected]

Definition at line 1015 of file nsLocation.cpp.

{
  nsCOMPtr<nsIDocument> doc;
  nsresult rv = GetSourceDocument(cx, getter_AddRefs(doc));
  if (doc) {
    NS_IF_ADDREF(*sourceURL = doc->GetBaseURI());
  } else {
    *sourceURL = nsnull;
  }

  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsLocation::GetSourceDocument ( JSContext cx,
nsIDocument **  aDocument 
) [protected]

Definition at line 982 of file nsLocation.cpp.

{
  // XXX Code duplicated from nsHTMLDocument
  // XXX Tom said this reminded him of the "Six Degrees of
  // Kevin Bacon" game. We try to get from here to there using
  // whatever connections possible. The problem is that this
  // could break if any of the connections along the way change.
  // I wish there were a better way.

  nsresult rv = NS_ERROR_FAILURE;

  // We need to use the dynamically scoped global and assume that the
  // current JSContext is a DOM context with a nsIScriptGlobalObject so
  // that we can get the url of the caller.
  // XXX This will fail on non-DOM contexts :(

  nsCOMPtr<nsIDOMWindow> window =
    do_QueryInterface(nsJSUtils::GetDynamicScriptGlobal(cx), &rv);

  if (window) {
    nsCOMPtr<nsIDOMDocument> domDoc;
    rv = window->GetDocument(getter_AddRefs(domDoc));
    if (domDoc) {
      return CallQueryInterface(domDoc, aDocument);
    }
  } else {
    *aDocument = nsnull;
  }

  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsLocation::GetURI ( nsIURI **  aURL,
PRBool  aGetInnermostURI = PR_FALSE 
) [protected]

Definition at line 303 of file nsLocation.cpp.

{
  *aURI = nsnull;

  nsresult rv;
  nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
  nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(docShell, &rv));
  if (NS_FAILED(rv)) {
    return rv;
  }

  nsCOMPtr<nsIURI> uri;
  rv = webNav->GetCurrentURI(getter_AddRefs(uri));
  NS_ENSURE_SUCCESS(rv, rv);

  // It is valid for docshell to return a null URI. Don't try to fixup
  // if this happens.
  if (!uri) {
    return NS_OK;
  }

  if (aGetInnermostURI) {
    nsCOMPtr<nsIJARURI> jarURI(do_QueryInterface(uri));
    while (jarURI) {
      jarURI->GetJARFile(getter_AddRefs(uri));
      jarURI = do_QueryInterface(uri);
    }
  }

  NS_ASSERTION(uri, "nsJARURI screwed up?");

  nsCOMPtr<nsIURIFixup> urifixup(do_GetService(NS_URIFIXUP_CONTRACTID, &rv));
  NS_ENSURE_SUCCESS(rv, rv);

  return urifixup->CreateExposableURI(uri, aURI);
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsLocation::GetWritableURI ( nsIURI **  aURL) [protected]

Definition at line 341 of file nsLocation.cpp.

{
  *aURI = nsnull;

  nsCOMPtr<nsIURI> uri;

  nsresult rv = GetURI(getter_AddRefs(uri));
  if (NS_FAILED(rv) || !uri) {
    return rv;
  }

  return uri->Clone(aURI);
}

Here is the call graph for this function:

void nsIDOMNSLocation::reload ( ) [inherited]
void nsIDOMLocation::reload ( in boolean  forceget) [inherited]
void nsIDOMLocation::replace ( in DOMString  url) [inherited]

Definition at line 158 of file nsLocation.cpp.

{
   mDocShell = do_GetWeakReference(aDocShell);
}

Here is the call graph for this function:

nsresult nsLocation::SetHrefWithBase ( const nsAString &  aHref,
nsIURI aBase,
PRBool  aReplace 
) [protected]

Definition at line 595 of file nsLocation.cpp.

{
  nsresult result;
  nsCOMPtr<nsIURI> newUri, baseURI;

  nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));

  // Try to make sure the base url is something that will be useful. 
  result = FindUsableBaseURI(aBase,  docShell, getter_AddRefs(baseURI));
  if (!baseURI)  {
    // If nothing useful was found, just use what you have.
    baseURI = aBase;
  }

  nsCAutoString docCharset;
  if (NS_SUCCEEDED(GetDocumentCharacterSetForURI(aHref, docCharset)))
    result = NS_NewURI(getter_AddRefs(newUri), aHref, docCharset.get(), baseURI);
  else
    result = NS_NewURI(getter_AddRefs(newUri), aHref, nsnull, baseURI);

  if (newUri) {
    /* Check with the scriptContext if it is currently processing a script tag.
     * If so, this must be a <script> tag with a location.href in it.
     * we want to do a replace load, in such a situation. 
     * In other cases, for example if a event handler or a JS timer
     * had a location.href in it, we want to do a normal load,
     * so that the new url will be appended to Session History.
     * This solution is tricky. Hopefully it isn't going to bite
     * anywhere else. This is part of solution for bug # 39938, 72197
     * 
     */
    PRBool inScriptTag=PR_FALSE;
    // Get JSContext from stack.
    nsCOMPtr<nsIJSContextStack> stack(do_GetService("@mozilla.org/js/xpc/ContextStack;1", &result));

    if (stack) {
      JSContext *cx;

      result = GetContextFromStack(stack, &cx);
      if (cx) {
        nsIScriptContext *scriptContext =
          nsJSUtils::GetDynamicScriptContext(cx);

        if (scriptContext) {
          if (scriptContext->GetProcessingScriptTag()) {
            // Now check to make sure that the script is running in our window,
            // since we only want to replace if the location is set by a
            // <script> tag in the same window.  See bug 178729.
            nsCOMPtr<nsIScriptGlobalObject> ourGlobal(do_GetInterface(docShell));
            inScriptTag = (ourGlobal == scriptContext->GetGlobalObject());
          }
        }  
      } //cx
    }  // stack

    return SetURI(newUri, aReplace || inScriptTag);
  }

  return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsLocation::SetHrefWithContext ( JSContext cx,
const nsAString &  aHref,
PRBool  aReplace 
) [protected]

Definition at line 579 of file nsLocation.cpp.

{
  nsCOMPtr<nsIURI> base;

  // Get the source of the caller
  nsresult result = GetSourceBaseURL(cx, getter_AddRefs(base));

  if (NS_FAILED(result)) {
    return result;
  }

  return SetHrefWithBase(aHref, base, aReplace);
}

Here is the call graph for this function:

nsresult nsLocation::SetURI ( nsIURI aURL,
PRBool  aReplace = PR_FALSE 
) [protected]

Definition at line 356 of file nsLocation.cpp.

{
  nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
  if (docShell) {
    nsCOMPtr<nsIDocShellLoadInfo> loadInfo;
    nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(docShell));

    if(NS_FAILED(CheckURL(aURI, getter_AddRefs(loadInfo))))
      return NS_ERROR_FAILURE;

    if (aReplace) {
      loadInfo->SetLoadType(nsIDocShellLoadInfo::loadStopContentAndReplace);
    } else {
      loadInfo->SetLoadType(nsIDocShellLoadInfo::loadStopContent);
    }

    return docShell->LoadURI(aURI, loadInfo,
                             nsIWebNavigation::LOAD_FLAGS_NONE, PR_TRUE);
  }

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

DOMString nsIDOMLocation::toString ( ) [inherited]

Member Data Documentation

attribute DOMString nsIDOMLocation::hash [inherited]

These properties refer to the current location of the document.

This will correspond to the URI shown in the location bar, which can be different from the documentURI of the document.

Definition at line 50 of file nsIDOMLocation.idl.

attribute DOMString nsIDOMLocation::host [inherited]

Definition at line 51 of file nsIDOMLocation.idl.

attribute DOMString nsIDOMLocation::hostname [inherited]

Definition at line 52 of file nsIDOMLocation.idl.

attribute DOMString nsIDOMLocation::href [inherited]

Definition at line 53 of file nsIDOMLocation.idl.

Definition at line 765 of file nsGlobalWindow.h.

attribute DOMString nsIDOMLocation::pathname [inherited]

Definition at line 54 of file nsIDOMLocation.idl.

attribute DOMString nsIDOMLocation::port [inherited]

Definition at line 55 of file nsIDOMLocation.idl.

attribute DOMString nsIDOMLocation::protocol [inherited]

Definition at line 56 of file nsIDOMLocation.idl.

attribute DOMString nsIDOMLocation::search [inherited]

Definition at line 57 of file nsIDOMLocation.idl.


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