Back to index

lightning-sunbird  0.9+nobinonly
nsContentPolicyUtils.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Zero-Knowledge Systems, Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Timothy Watt <riceman+moz@mail.rit.edu>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 /*
00040  * Utility routines for checking content load/process policy settings,
00041  * and routines helpful for content policy implementors.
00042  */
00043 
00044 #ifndef __nsContentPolicyUtils_h__
00045 #define __nsContentPolicyUtils_h__
00046 
00047 // for PR_LOGGING
00048 #include "prlog.h"
00049 
00050 #include "nsString.h"
00051 #include "nsIContentPolicy.h"
00052 #include "nsIMemory.h"
00053 #include "nsIServiceManager.h"
00054 #include "nsIContent.h"
00055 
00056 //XXXtw sadly, this makes consumers of nsContentPolicyUtils depend on widget
00057 #include "nsIDocument.h"
00058 #include "nsIScriptGlobalObject.h"
00059 
00060 #define NS_CONTENTPOLICY_CONTRACTID   "@mozilla.org/layout/content-policy;1"
00061 #define NS_CONTENTPOLICY_CATEGORY "content-policy"
00062 #define NS_CONTENTPOLICY_CID                              \
00063   {0x0e3afd3d, 0xeb60, 0x4c2b,                            \
00064      { 0x96, 0x3b, 0x56, 0xd7, 0xc4, 0x39, 0xf1, 0x24 }}
00065 
00071 #define NS_CP_ACCEPTED(val) ((val) == nsIContentPolicy::ACCEPT)
00072 
00078 #define NS_CP_REJECTED(val) ((val) != nsIContentPolicy::ACCEPT)
00079 
00080 // Offer convenient translations of constants -> const char*
00081 
00082 // convenience macro to reduce some repetative typing...
00083 // name is the name of a constant from this interface
00084 #define CASE_RETURN(name)          \
00085   case nsIContentPolicy:: name :   \
00086     return #name
00087 
00088 #ifdef PR_LOGGING
00089 
00098 inline const char *
00099 NS_CP_ResponseName(PRInt16 response)
00100 {
00101   switch (response) {
00102     CASE_RETURN( REJECT_REQUEST );
00103     CASE_RETURN( REJECT_TYPE    );
00104     CASE_RETURN( REJECT_SERVER  );
00105     CASE_RETURN( REJECT_OTHER   );
00106     CASE_RETURN( ACCEPT         );
00107   default:
00108     return "<Unknown Response>";
00109   }
00110 }
00111 
00121 inline const char *
00122 NS_CP_ContentTypeName(PRUint32 contentType)
00123 {
00124   switch (contentType) {
00125     CASE_RETURN( TYPE_OTHER      );
00126     CASE_RETURN( TYPE_SCRIPT     );
00127     CASE_RETURN( TYPE_IMAGE      );
00128     CASE_RETURN( TYPE_STYLESHEET );
00129     CASE_RETURN( TYPE_OBJECT     );
00130     CASE_RETURN( TYPE_DOCUMENT   );
00131     CASE_RETURN( TYPE_SUBDOCUMENT);
00132     CASE_RETURN( TYPE_REFRESH    );
00133   default:
00134     return "<Unknown Type>";
00135   }
00136 }
00137 
00138 #endif // defined(PR_LOGGING)
00139 
00140 #undef CASE_RETURN
00141 
00142 /* Passes on parameters from its "caller"'s context. */
00143 #define CHECK_CONTENT_POLICY(action)                                          \
00144     nsCOMPtr<nsIContentPolicy> policy =                                       \
00145          do_GetService(NS_CONTENTPOLICY_CONTRACTID);                          \
00146     if (!policy)                                                              \
00147         return NS_ERROR_FAILURE;                                              \
00148                                                                               \
00149     return policy-> action (contentType, contentLocation, requestOrigin,      \
00150                             context, mimeType, extra, decision);
00151 
00156 inline nsresult
00157 NS_CheckContentLoadPolicy(PRUint32          contentType,
00158                           nsIURI           *contentLocation,
00159                           nsIURI           *requestOrigin,
00160                           nsISupports      *context,
00161                           const nsACString &mimeType,
00162                           nsISupports      *extra,
00163                           PRInt16          *decision)
00164 {
00165     CHECK_CONTENT_POLICY(ShouldLoad);
00166 }
00167 
00172 inline nsresult
00173 NS_CheckContentProcessPolicy(PRUint32          contentType,
00174                              nsIURI           *contentLocation,
00175                              nsIURI           *requestOrigin,
00176                              nsISupports      *context,
00177                              const nsACString &mimeType,
00178                              nsISupports      *extra,
00179                              PRInt16          *decision)
00180 {
00181     CHECK_CONTENT_POLICY(ShouldProcess);
00182 }
00183 
00184 #undef CHECK_CONTENT_POLICY
00185 
00197 static nsIDocShell*
00198 NS_CP_GetDocShellFromContext(nsISupports *aContext)
00199 {
00200     if (!aContext) {
00201         return nsnull;
00202     }
00203 
00204     nsCOMPtr<nsIScriptGlobalObject> scriptGlobal = do_QueryInterface(aContext);
00205 
00206     if (!scriptGlobal) {
00207         // our context might be a document (which also QIs to nsIDOMNode), so
00208         // try that first
00209         nsCOMPtr<nsIDocument> doc = do_QueryInterface(aContext);
00210         if (!doc) {
00211             // we were not a document after all, get our ownerDocument,
00212             // hopefully
00213             nsCOMPtr<nsIContent> content = do_QueryInterface(aContext);
00214             if (content) {
00215                 doc = content->GetOwnerDoc();
00216             }
00217         }
00218 
00219         if (doc) {
00220             scriptGlobal = doc->GetScriptGlobalObject();
00221         }
00222     }
00223 
00224     if (!scriptGlobal) {
00225         return nsnull;
00226     }
00227 
00228     return scriptGlobal->GetDocShell();
00229 }
00230 
00231 #endif /* __nsContentPolicyUtils_h__ */