Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
nsContentBlocker Class Reference

#include <nsContentBlocker.h>

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

List of all members.

Public Member Functions

NS_DECL_ISUPPORTS
NS_DECL_NSICONTENTPOLICY
NS_DECL_NSIOBSERVER 
nsContentBlocker ()
nsresult Init ()
short shouldLoad (in unsigned long aContentType, in nsIURI aContentLocation, in nsIURI aRequestOrigin, in nsISupports aContext, in ACString aMimeTypeGuess, in nsISupports aExtra)
 Should the resource at this location be loaded? ShouldLoad will be called before loading the resource at aContentLocation to determine whether to start the load at all.
short shouldProcess (in unsigned long aContentType, in nsIURI aContentLocation, in nsIURI aRequestOrigin, in nsISupports aContext, in ACString aMimeType, in nsISupports aExtra)
 Should the resource be processed? ShouldProcess will be called once all the information passed to it has been determined about the resource, typically after part of the resource has been loaded.
void observe (in nsISupports aSubject, in string aTopic, in wstring aData)
 Observe will be called when there is a notification for the topic |aTopic|.

Public Attributes

const unsigned long TYPE_OTHER = 1
const unsigned long TYPE_SCRIPT = 2
 Indicates an executable script (such as JavaScript).
const unsigned long TYPE_IMAGE = 3
 Indicates an image (e.g., IMG elements).
const unsigned long TYPE_STYLESHEET = 4
 Indicates a stylesheet (e.g., STYLE elements).
const unsigned long TYPE_OBJECT = 5
 Indicates a generic object (plugin-handled content typically falls under this category).
const unsigned long TYPE_DOCUMENT = 6
 Indicates a document at the top-level (i.e., in a browser).
const unsigned long TYPE_SUBDOCUMENT = 7
 Indicates a document contained within another document (e.g., IFRAMEs, FRAMES, and OBJECTs).
const unsigned long TYPE_REFRESH = 8
 Indicates a timed refresh.
const short REJECT_REQUEST = -1
 Returned from shouldLoad or shouldProcess if the load or process request is rejected based on details of the request.
const short REJECT_TYPE = -2
 Returned from shouldLoad or shouldProcess if the load/process is rejected based solely on its type (of the above flags).
const short REJECT_SERVER = -3
 Returned from shouldLoad or shouldProcess if the load/process is rejected based on the server it is hosted on or requested from (aContentLocation or aRequestOrigin), e.g., if you block an IMAGE because it is served from goatse.cx (even if you don't necessarily block other types from that server/domain).
const short REJECT_OTHER = -4
 Returned from shouldLoad or shouldProcess if the load/process is rejected based on some other criteria.
const short ACCEPT = 1
 Returned from shouldLoad or shouldProcess if the load or process request is not rejected.

Private Member Functions

 ~nsContentBlocker ()
void PrefChanged (nsIPrefBranch *, const char *)
nsresult TestPermission (nsIURI *aCurrentURI, nsIURI *aFirstURI, PRInt32 aContentType, PRBool *aPermission, PRBool *aFromPrefs)

Private Attributes

nsCOMPtr< nsIPermissionManagermPermissionManager
nsCOMPtr< nsIPrefBranch2mPrefBranchInternal
PRUint8 mBehaviorPref [NUMBER_OF_TYPES]

Detailed Description

Definition at line 52 of file nsContentBlocker.h.


Constructor & Destructor Documentation

NS_DECL_ISUPPORTS NS_DECL_NSICONTENTPOLICY NS_DECL_NSIOBSERVER nsContentBlocker::nsContentBlocker ( )
nsContentBlocker::~nsContentBlocker ( ) [inline, private]

Definition at line 67 of file nsContentBlocker.h.

{}

Member Function Documentation

Definition at line 80 of file nsContentBlocker.cpp.

{
  nsresult rv;
  mPermissionManager = do_GetService(NS_PERMISSIONMANAGER_CONTRACTID, &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIPrefService> prefService = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIPrefBranch> prefBranch;
  rv = prefService->GetBranch("permissions.default.", getter_AddRefs(prefBranch));
  NS_ENSURE_SUCCESS(rv, rv);

  // Migrate old image blocker pref
  nsCOMPtr<nsIPrefBranch> oldPrefBranch;
  oldPrefBranch = do_QueryInterface(prefService);
  PRInt32 oldPref;
  rv = oldPrefBranch->GetIntPref("network.image.imageBehavior", &oldPref);
  if (NS_SUCCEEDED(rv) && oldPref) {
    PRInt32 newPref;
    switch (oldPref) {
      default:
        newPref = BEHAVIOR_ACCEPT;
        break;
      case 1:
        newPref = BEHAVIOR_NOFOREIGN;
        break;
      case 2:
        newPref = BEHAVIOR_REJECT;
        break;
    }
    prefBranch->SetIntPref("image", newPref);
    oldPrefBranch->ClearUserPref("network.image.imageBehavior");
  }


  // The branch is not a copy of the prefservice, but a new object, because
  // it is a non-default branch. Adding obeservers to it will only work if
  // we make sure that the object doesn't die. So, keep a reference to it.
  mPrefBranchInternal = do_QueryInterface(prefBranch, &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = mPrefBranchInternal->AddObserver("", this, PR_TRUE);
  PrefChanged(prefBranch, nsnull);

  return rv;
}

Here is the call graph for this function:

void nsIObserver::observe ( in nsISupports  aSubject,
in string  aTopic,
in wstring  aData 
) [inherited]

Observe will be called when there is a notification for the topic |aTopic|.

This assumes that the object implementing this interface has been registered with an observer service such as the nsIObserverService.

If you expect multiple topics/subjects, the impl is responsible for filtering.

You should not modify, add, remove, or enumerate notifications in the implemention of observe.

Parameters:
aSubject: Notification specific interface pointer.
aTopic: The notification topic or subject.
aData: Notification specific wide string. subject event.
void nsContentBlocker::PrefChanged ( nsIPrefBranch aPrefBranch,
const char *  aPref 
) [private]

Definition at line 132 of file nsContentBlocker.cpp.

{
  PRInt32 val;

#define PREF_CHANGED(_P) (!aPref || !strcmp(aPref, _P))

  for(PRUint32 i = 0; i < NUMBER_OF_TYPES; ++i) {
    if (PREF_CHANGED(kTypeString[i]) &&
        NS_SUCCEEDED(aPrefBranch->GetIntPref(kTypeString[i], &val)))
      mBehaviorPref[i] = LIMIT(val, 1, 3, 1);
  }

}

Here is the caller graph for this function:

short nsIContentPolicy::shouldLoad ( in unsigned long  aContentType,
in nsIURI  aContentLocation,
in nsIURI  aRequestOrigin,
in nsISupports  aContext,
in ACString  aMimeTypeGuess,
in nsISupports  aExtra 
) [inherited]

Should the resource at this location be loaded? ShouldLoad will be called before loading the resource at aContentLocation to determine whether to start the load at all.

Parameters:
aContentTypethe type of content being tested. This will be one one of the TYPE_* constants.
aContentLocationthe location of the content being checked; must not be null
aRequestOriginOPTIONAL. the location of the resource that initiated this load request; can be null if inapplicable
aContextOPTIONAL. the nsIDOMNode or nsIDOMWindow that initiated the request, or something that can QI to one of those; can be null if inapplicable.
aMimeTypeGuessOPTIONAL. a guess for the requested content's MIME type, based on information available to the request initiator (e.g., an OBJECT's type attribute); does not reliably reflect the actual MIME type of the requested content
aExtraan OPTIONAL argument, pass-through for non-Gecko callers to pass extra data to callees.
Returns:
ACCEPT or REJECT_*
short nsIContentPolicy::shouldProcess ( in unsigned long  aContentType,
in nsIURI  aContentLocation,
in nsIURI  aRequestOrigin,
in nsISupports  aContext,
in ACString  aMimeType,
in nsISupports  aExtra 
) [inherited]

Should the resource be processed? ShouldProcess will be called once all the information passed to it has been determined about the resource, typically after part of the resource has been loaded.

Parameters:
aContentTypethe type of content being tested. This will be one one of the TYPE_* constants.
aContentLocationOPTIONAL; the location of the resource being requested: MAY be, e.g., a post-redirection URI for the resource.
aRequestOriginOPTIONAL. the location of the resource that initiated this load request; can be null if inapplicable
aContextOPTIONAL. the nsIDOMNode or nsIDOMWindow that initiated the request, or something that can QI to one of those; can be null if inapplicable.
aMimeTypethe MIME type of the requested resource (e.g., image/png), as reported by the networking library, if available (may be empty if inappropriate for the type, e.g., TYPE_REFRESH).
aExtraan OPTIONAL argument, pass-through for non-Gecko callers to pass extra data to callees.
Returns:
ACCEPT or REJECT_*
nsresult nsContentBlocker::TestPermission ( nsIURI aCurrentURI,
nsIURI aFirstURI,
PRInt32  aContentType,
PRBool aPermission,
PRBool aFromPrefs 
) [private]

Definition at line 222 of file nsContentBlocker.cpp.

{
  *aFromPrefs = PR_FALSE;
  // This default will also get used if there is an unknown value in the
  // permission list, or if the permission manager returns unknown values.
  *aPermission = PR_TRUE;

  // check the permission list first; if we find an entry, it overrides
  // default prefs.
  // Don't forget the aContentType ranges from 1..8, while the
  // array is indexed 0..7
  PRUint32 permission;
  nsresult rv = mPermissionManager->TestPermission(aCurrentURI, 
                                                   kTypeString[aContentType - 1],
                                                   &permission);
  NS_ENSURE_SUCCESS(rv, rv);

  // If there is nothing on the list, use the default.
  if (!permission) {
    permission = mBehaviorPref[aContentType - 1];
    *aFromPrefs = PR_TRUE;
  }

  // Use the fact that the nsIPermissionManager values map to 
  // the BEHAVIOR_* values above.
  switch (permission) {
  case BEHAVIOR_ACCEPT:
    *aPermission = PR_TRUE;
    break;
  case BEHAVIOR_REJECT:
    *aPermission = PR_FALSE;
    break;

  case BEHAVIOR_NOFOREIGN:
    // Third party checking

    // Need a requesting uri for third party checks to work.
    if (!aFirstURI)
      return NS_OK;

    PRBool trustedSource = PR_FALSE;
    rv = aFirstURI->SchemeIs("chrome", &trustedSource);
    NS_ENSURE_SUCCESS(rv,rv);
    if (!trustedSource) {
      rv = aFirstURI->SchemeIs("resource", &trustedSource);
      NS_ENSURE_SUCCESS(rv,rv);
    }
    if (trustedSource)
      return NS_OK;

    // compare tails of names checking to see if they have a common domain
    // we do this by comparing the tails of both names where each tail 
    // includes at least one dot
    
    // A more generic method somewhere would be nice

    nsCAutoString currentHost;
    rv = aCurrentURI->GetAsciiHost(currentHost);
    NS_ENSURE_SUCCESS(rv, rv);

    // Search for two dots, starting at the end.
    // If there are no two dots found, ++dot will turn to zero,
    // that will return the entire string.
    PRInt32 dot = currentHost.RFindChar('.');
    dot = currentHost.RFindChar('.', dot-1);
    ++dot;

    // Get the domain, ie the last part of the host (www.domain.com -> domain.com)
    // This will break on co.uk
    const nsCSubstring &tail =
      Substring(currentHost, dot, currentHost.Length() - dot);

    nsCAutoString firstHost;
    rv = aFirstURI->GetAsciiHost(firstHost);
    NS_ENSURE_SUCCESS(rv, rv);

    // If the tail is longer then the whole firstHost, it will never match
    if (firstHost.Length() < tail.Length()) {
      *aPermission = PR_FALSE;
      return NS_OK;
    }
    
    // Get the last part of the firstUri with the same length as |tail|
    const nsCSubstring &firstTail = 
      Substring(firstHost, firstHost.Length() - tail.Length(), tail.Length());

    // Check that both tails are the same, and that just before the tail in
    // |firstUri| there is a dot. That means both url are in the same domain
    if ((firstHost.Length() > tail.Length() && 
         firstHost.CharAt(firstHost.Length() - tail.Length() - 1) != '.') || 
        !tail.Equals(firstTail)) {
      *aPermission = PR_FALSE;
    }
    break;
  }
  
  return NS_OK;
}

Here is the call graph for this function:


Member Data Documentation

const short nsIContentPolicy::ACCEPT = 1 [inherited]

Returned from shouldLoad or shouldProcess if the load or process request is not rejected.

Definition at line 146 of file nsIContentPolicy.idl.

Definition at line 78 of file nsContentBlocker.h.

Definition at line 76 of file nsContentBlocker.h.

Definition at line 77 of file nsContentBlocker.h.

const short nsIContentPolicy::REJECT_OTHER = -4 [inherited]

Returned from shouldLoad or shouldProcess if the load/process is rejected based on some other criteria.

Mozilla callers will handle this like REJECT_REQUEST; third-party implementors may, for example, use this to direct their own callers to consult the extra parameter for additional details.

Definition at line 140 of file nsIContentPolicy.idl.

const short nsIContentPolicy::REJECT_REQUEST = -1 [inherited]

Returned from shouldLoad or shouldProcess if the load or process request is rejected based on details of the request.

Definition at line 110 of file nsIContentPolicy.idl.

const short nsIContentPolicy::REJECT_SERVER = -3 [inherited]

Returned from shouldLoad or shouldProcess if the load/process is rejected based on the server it is hosted on or requested from (aContentLocation or aRequestOrigin), e.g., if you block an IMAGE because it is served from goatse.cx (even if you don't necessarily block other types from that server/domain).

NOTE that it is not meant to stop future requests for this server--only the current request.

Definition at line 131 of file nsIContentPolicy.idl.

const short nsIContentPolicy::REJECT_TYPE = -2 [inherited]

Returned from shouldLoad or shouldProcess if the load/process is rejected based solely on its type (of the above flags).

NOTE that it is not meant to stop future requests for this type--only the current request.

Definition at line 119 of file nsIContentPolicy.idl.

const unsigned long nsIContentPolicy::TYPE_DOCUMENT = 6 [inherited]

Indicates a document at the top-level (i.e., in a browser).

Definition at line 83 of file nsIContentPolicy.idl.

const unsigned long nsIContentPolicy::TYPE_IMAGE = 3 [inherited]

Indicates an image (e.g., IMG elements).

Definition at line 67 of file nsIContentPolicy.idl.

const unsigned long nsIContentPolicy::TYPE_OBJECT = 5 [inherited]

Indicates a generic object (plugin-handled content typically falls under this category).

Definition at line 78 of file nsIContentPolicy.idl.

const unsigned long nsIContentPolicy::TYPE_OTHER = 1 [inherited]

Definition at line 57 of file nsIContentPolicy.idl.

const unsigned long nsIContentPolicy::TYPE_REFRESH = 8 [inherited]

Indicates a timed refresh.

shouldLoad will never get this, because it does not represent content to be loaded (the actual load triggered by the refresh will go through shouldLoad as expected).

shouldProcess will get this for, e.g., META Refresh elements and HTTP Refresh headers.

Definition at line 101 of file nsIContentPolicy.idl.

const unsigned long nsIContentPolicy::TYPE_SCRIPT = 2 [inherited]

Indicates an executable script (such as JavaScript).

Definition at line 62 of file nsIContentPolicy.idl.

const unsigned long nsIContentPolicy::TYPE_STYLESHEET = 4 [inherited]

Indicates a stylesheet (e.g., STYLE elements).

Definition at line 72 of file nsIContentPolicy.idl.

const unsigned long nsIContentPolicy::TYPE_SUBDOCUMENT = 7 [inherited]

Indicates a document contained within another document (e.g., IFRAMEs, FRAMES, and OBJECTs).

Definition at line 89 of file nsIContentPolicy.idl.


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