Bug 789224 - Remove CAPS_HACKER debugging instrumentation. r=mrbkap
authorBobby Holley <bobbyholley@gmail.com>
Mon, 22 Oct 2012 08:29:56 +0200
changeset 111139 a578debf08b952927b96d3109bf1cf4c66d3afc8
parent 111138 42b11a0fe323d57de8b4282d4ce27a08496b9c8c
child 111140 d82e14184e9faf313617a4cad51ecc50c615e87f
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersmrbkap
bugs789224
milestone19.0a1
Bug 789224 - Remove CAPS_HACKER debugging instrumentation. r=mrbkap This stuff is super ugly and confusing. I think we're better off without it.
caps/include/nsScriptSecurityManager.h
caps/src/nsScriptSecurityManager.cpp
--- a/caps/include/nsScriptSecurityManager.h
+++ b/caps/include/nsScriptSecurityManager.h
@@ -29,33 +29,16 @@ class nsIClassInfo;
 class nsIIOService;
 class nsIXPConnect;
 class nsIStringBundle;
 class nsSystemPrincipal;
 struct ClassPolicy;
 class ClassInfoData;
 class DomainPolicy;
 
-#if defined(DEBUG_mstoltz) || defined(DEBUG_caillon)
-#define DEBUG_CAPS_HACKER
-#endif
-
-#ifdef DEBUG_CAPS_HACKER
-#define DEBUG_CAPS_CheckPropertyAccessImpl
-#define DEBUG_CAPS_LookupPolicy
-#define DEBUG_CAPS_CheckComponentPermissions
-#endif
-
-#if 0
-#define DEBUG_CAPS_CanCreateWrapper
-#define DEBUG_CAPS_CanCreateInstance
-#define DEBUG_CAPS_CanGetService
-#define DEBUG_CAPS_DomainPolicyLifeCycle
-#endif
-
 /////////////////////
 // PrincipalKey //
 /////////////////////
 
 class PrincipalKey : public PLDHashEntryHdr
 {
 public:
     typedef const nsIPrincipal* KeyType;
@@ -231,22 +214,16 @@ InitClassPolicyEntry(PLDHashTable *table
 // Domain Policy
 class DomainPolicy : public PLDHashTable
 {
 public:
     DomainPolicy() : mWildcardPolicy(nullptr),
                      mRefCount(0)
     {
         mGeneration = sGeneration;
-
-#ifdef DEBUG_CAPS_DomainPolicyLifeCycle
-        ++sObjects;
-        _printPopulationInfo();
-#endif
-
     }
 
     bool Init()
     {
         static const PLDHashTableOps domainPolicyOps =
         {
             PL_DHashAllocTable,
             PL_DHashFreeTable,
@@ -261,22 +238,16 @@ public:
         return PL_DHashTableInit(this, &domainPolicyOps, nullptr,
                                  sizeof(ClassPolicy), 16);
     }
 
     ~DomainPolicy()
     {
         PL_DHashTableFinish(this);
         NS_ASSERTION(mRefCount == 0, "Wrong refcount in DomainPolicy dtor");
-#ifdef DEBUG_CAPS_DomainPolicyLifeCycle
-        printf("DomainPolicy deleted with mRefCount = %d\n", mRefCount);
-        --sObjects;
-        _printPopulationInfo();
-#endif
-
     }
 
     void Hold()
     {
         mRefCount++;
     }
 
     void Drop()
@@ -296,22 +267,16 @@ public:
     }
     
     ClassPolicy* mWildcardPolicy;
 
 private:
     uint32_t mRefCount;
     uint32_t mGeneration;
     static uint32_t sGeneration;
-    
-#ifdef DEBUG_CAPS_DomainPolicyLifeCycle
-    static uint32_t sObjects;
-    static void _printPopulationInfo();
-#endif
-
 };
 
 static void
 MoveClassPolicyEntry(PLDHashTable *table,
                      const PLDHashEntryHdr *from,
                      PLDHashEntryHdr *to)
 {
     memcpy(to, from, table->entrySize);
@@ -524,21 +489,16 @@ private:
 
     nsresult
     InitPolicies();
 
     nsresult
     InitDomainPolicy(JSContext* cx, const char* aPolicyName,
                      DomainPolicy* aDomainPolicy);
 
-#ifdef DEBUG_CAPS_HACKER
-    void
-    PrintPolicyDB();
-#endif
-
     // JS strings we need to clean up on shutdown
     static jsid sEnabledID;
 
     inline void
     ScriptSecurityPrefChanged();
 
     nsObjectHashtable* mOriginToPolicyMap;
     DomainPolicy* mDefaultPolicy;
--- a/caps/src/nsScriptSecurityManager.cpp
+++ b/caps/src/nsScriptSecurityManager.cpp
@@ -186,24 +186,16 @@ inline void SetPendingException(JSContex
 
 inline void SetPendingException(JSContext *cx, const PRUnichar *aMsg)
 {
     JSAutoRequest ar(cx);
     JS_ReportError(cx, "%hs", aMsg);
 }
 
 // DomainPolicy members
-#ifdef DEBUG_CAPS_DomainPolicyLifeCycle
-uint32_t DomainPolicy::sObjects=0;
-void DomainPolicy::_printPopulationInfo()
-{
-    printf("CAPS.DomainPolicy: Gen. %d, %d DomainPolicy objects.\n",
-        sGeneration, sObjects);
-}
-#endif
 uint32_t DomainPolicy::sGeneration = 0;
 
 // Helper class to get stuff from the ClassInfo and not waste extra time with
 // virtual method calls for things it has already gotten
 class ClassInfoData
 {
 public:
     ClassInfoData(nsIClassInfo *aClassInfo, const char *aName)
@@ -425,19 +417,16 @@ struct DomainEntry
             return true;
         char charBefore = anOrigin[len-thisLen-1];
         return (charBefore == '.' || charBefore == ':' || charBefore == '/');
     }
 
     nsCString         mOrigin;
     DomainPolicy*     mDomainPolicy;
     DomainEntry*      mNext;
-#if defined(DEBUG) || defined(DEBUG_CAPS_HACKER)
-    nsCString         mPolicyName_DEBUG;
-#endif
 };
 
 static bool
 DeleteDomainEntry(nsHashKey *aKey, void *aData, void* closure)
 {
     DomainEntry *entry = (DomainEntry*) aData;
     do
     {
@@ -662,22 +651,16 @@ nsScriptSecurityManager::CheckPropertyAc
         // We have native code or the system principal: just allow access
         return NS_OK;
 
     nsCOMPtr<nsIPrincipal> objectPrincipal;
 
     // Hold the class info data here so we don't have to go back to virtual
     // methods all the time
     ClassInfoData classInfoData(aClassInfo, aClassName);
-#ifdef DEBUG_CAPS_CheckPropertyAccessImpl
-    nsAutoCString propertyName;
-    propertyName.AssignWithConversion((PRUnichar*)IDToString(cx, aProperty));
-    printf("### CanAccess(%s.%s, %i) ", classInfoData.GetName(), 
-           propertyName.get(), aAction);
-#endif
 
     //-- Look up the security policy for this class and subject domain
     SecurityLevel securityLevel;
     rv = LookupPolicy(subjectPrincipal, classInfoData, aProperty, aAction, 
                       (ClassPolicy**)aCachedClassPolicy, &securityLevel);
     if (NS_FAILED(rv))
         return rv;
 
@@ -694,34 +677,25 @@ nsScriptSecurityManager::CheckPropertyAc
     }
 
     if (SECURITY_ACCESS_LEVEL_FLAG(securityLevel))
     // This flag means securityLevel is allAccess, noAccess, or sameOrigin
     {
         switch (securityLevel.level)
         {
         case SCRIPT_SECURITY_NO_ACCESS:
-#ifdef DEBUG_CAPS_CheckPropertyAccessImpl
-            printf("noAccess ");
-#endif
             rv = NS_ERROR_DOM_PROP_ACCESS_DENIED;
             break;
 
         case SCRIPT_SECURITY_ALL_ACCESS:
-#ifdef DEBUG_CAPS_CheckPropertyAccessImpl
-            printf("allAccess ");
-#endif
             rv = NS_OK;
             break;
 
         case SCRIPT_SECURITY_SAME_ORIGIN_ACCESS:
             {
-#ifdef DEBUG_CAPS_CheckPropertyAccessImpl
-                printf("sameOrigin ");
-#endif
                 nsCOMPtr<nsIPrincipal> principalHolder;
                 if(aJSObject)
                 {
                     objectPrincipal = doGetObjectPrincipal(aJSObject);
                     if (!objectPrincipal)
                         rv = NS_ERROR_DOM_SECURITY_ERR;
                 }
                 else
@@ -730,36 +704,27 @@ nsScriptSecurityManager::CheckPropertyAc
                     return NS_ERROR_FAILURE;
                 }
                 if(NS_SUCCEEDED(rv))
                     rv = CheckSameOriginDOMProp(subjectPrincipal, objectPrincipal,
                                                 aAction);
                 break;
             }
         default:
-#ifdef DEBUG_CAPS_CheckPropertyAccessImpl
-                printf("ERROR ");
-#endif
             NS_ERROR("Bad Security Level Value");
             return NS_ERROR_FAILURE;
         }
     }
     else // if SECURITY_ACCESS_LEVEL_FLAG is false, securityLevel is a capability
     {
-#ifdef DEBUG_CAPS_CheckPropertyAccessImpl
-        printf("Cap:%s ", securityLevel.capability);
-#endif
         rv = SubjectIsPrivileged() ? NS_OK : NS_ERROR_DOM_SECURITY_ERR;
     }
 
     if (NS_SUCCEEDED(rv))
     {
-#ifdef DEBUG_CAPS_CheckPropertyAccessImpl
-    printf(" GRANTED.\n");
-#endif
         return rv;
     }
 
     //--See if the object advertises a non-default level of access
     //  using nsISecurityCheckedComponent
     nsCOMPtr<nsISecurityCheckedComponent> checkedComponent =
         do_QueryInterface(aObj);
 
@@ -792,22 +757,16 @@ nsScriptSecurityManager::CheckPropertyAc
                 checkedComponent->CanCallMethod(objIID,
                                                 IDToString(cx, aProperty),
                                                 getter_Copies(objectSecurityLevel));
             }
         }
     }
     rv = CheckXPCPermissions(cx, aObj, aJSObject, subjectPrincipal,
                              objectSecurityLevel);
-#ifdef DEBUG_CAPS_CheckPropertyAccessImpl
-    if(NS_SUCCEEDED(rv))
-        printf("CheckXPCPerms GRANTED.\n");
-    else
-        printf("CheckXPCPerms DENIED.\n");
-#endif
 
     if (NS_FAILED(rv)) //-- Security tests failed, access is denied, report error
     {
         nsAutoString stringName;
         switch(aAction)
         {
         case nsIXPCSecurityManager::ACCESS_GET_PROPERTY:
             stringName.AssignLiteral("GetPropertyDeniedOrigins");
@@ -1044,19 +1003,16 @@ nsScriptSecurityManager::LookupPolicy(ns
     else
     {
         aPrincipal->GetSecurityPolicy((void**)&dpolicy);
     }
 
     if (!dpolicy && mOriginToPolicyMap)
     {
         //-- Look up the relevant domain policy, if any
-#ifdef DEBUG_CAPS_LookupPolicy
-        printf("DomainLookup ");
-#endif
         if (nsCOMPtr<nsIExpandedPrincipal> exp = do_QueryInterface(aPrincipal)) 
         {
             // For expanded principals domain origin is not defined so let's just
             // use the default policy
             dpolicy = mDefaultPolicy;
         }
         else
         {
@@ -1119,20 +1075,16 @@ nsScriptSecurityManager::LookupPolicy(ns
     {
         // No per-domain policy for this principal (the more common case)
         // so look for a cached class policy from the object wrapper
         cpolicy = *aCachedClassPolicy;
     }
 
     if (!cpolicy)
     { //-- No cached policy for this class, need to look it up
-#ifdef DEBUG_CAPS_LookupPolicy
-        printf("ClassLookup ");
-#endif
-
         cpolicy = static_cast<ClassPolicy*>
                              (PL_DHashTableOperate(dpolicy,
                                                       aClassData.GetName(),
                                                       PL_DHASH_LOOKUP));
 
         if (PL_DHASH_ENTRY_IS_FREE(cpolicy))
             cpolicy = NO_POLICY_FOR_CLASS;
 
@@ -1532,19 +1484,16 @@ nsScriptSecurityManager::CheckLoadURIWit
                                  ArrayLength(formatStrings),
                                  getter_Copies(message));
         if (NS_SUCCEEDED(rv)) {
             nsCOMPtr<nsIConsoleService> console(
               do_GetService("@mozilla.org/consoleservice;1"));
             NS_ENSURE_TRUE(console, NS_ERROR_FAILURE);
 
             console->LogStringMessage(message.get());
-#ifdef DEBUG
-            fprintf(stderr, "%s\n", NS_ConvertUTF16toUTF8(message).get());
-#endif
         }
     }
     
     return NS_OK;
 }
 
 nsresult
 nsScriptSecurityManager::ReportError(JSContext* cx, const nsAString& messageTag,
@@ -1583,19 +1532,16 @@ nsScriptSecurityManager::ReportError(JSC
     }
     else // Print directly to the console
     {
         nsCOMPtr<nsIConsoleService> console(
             do_GetService("@mozilla.org/consoleservice;1"));
         NS_ENSURE_TRUE(console, NS_ERROR_FAILURE);
 
         console->LogStringMessage(message.get());
-#ifdef DEBUG
-        fprintf(stderr, "%s\n", NS_LossyConvertUTF16toASCII(message).get());
-#endif
     }
     return NS_OK;
 }
 
 NS_IMETHODIMP
 nsScriptSecurityManager::CheckLoadURIStrWithPrincipal(nsIPrincipal* aPrincipal,
                                                       const nsACString& aTargetURIStr,
                                                       uint32_t aFlags)
@@ -2297,28 +2243,20 @@ nsScriptSecurityManager::old_doGetObject
 
 NS_IMETHODIMP
 nsScriptSecurityManager::CanCreateWrapper(JSContext *cx,
                                           const nsIID &aIID,
                                           nsISupports *aObj,
                                           nsIClassInfo *aClassInfo,
                                           void **aPolicy)
 {
-#ifdef DEBUG_CAPS_CanCreateWrapper
-    char* iidStr = aIID.ToString();
-    printf("### CanCreateWrapper(%s) ", iidStr);
-    NS_Free(iidStr);
-#endif
 // XXX Special case for nsIXPCException ?
     ClassInfoData objClassInfo = ClassInfoData(aClassInfo, nullptr);
     if (objClassInfo.IsDOMClass())
     {
-#ifdef DEBUG_CAPS_CanCreateWrapper
-        printf("DOM class - GRANTED.\n");
-#endif
         return NS_OK;
     }
 
     //--See if the object advertises a non-default level of access
     //  using nsISecurityCheckedComponent
     nsCOMPtr<nsISecurityCheckedComponent> checkedComponent =
         do_QueryInterface(aObj);
 
@@ -2355,87 +2293,51 @@ nsScriptSecurityManager::CanCreateWrappe
         // call in order to throw the correct security exception later.
         rv2 = sStrBundle->FormatStringFromName(strName.get(),
                                                formatStrings,
                                                length,
                                                getter_Copies(errorMsg));
         NS_ENSURE_SUCCESS(rv2, rv2);
 
         SetPendingException(cx, errorMsg.get());
-
-#ifdef DEBUG_CAPS_CanCreateWrapper
-        printf("DENIED.\n");
-    }
-    else
-    {
-        printf("GRANTED.\n");
-#endif
     }
 
     return rv;
 }
 
 NS_IMETHODIMP
 nsScriptSecurityManager::CanCreateInstance(JSContext *cx,
                                            const nsCID &aCID)
 {
-#ifdef DEBUG_CAPS_CanCreateInstance
-    char* cidStr = aCID.ToString();
-    printf("### CanCreateInstance(%s) ", cidStr);
-    NS_Free(cidStr);
-#endif
-
     nsresult rv = CheckXPCPermissions(nullptr, nullptr, nullptr, nullptr, nullptr);
     if (NS_FAILED(rv))
     {
         //-- Access denied, report an error
         nsAutoCString errorMsg("Permission denied to create instance of class. CID=");
         char cidStr[NSID_LENGTH];
         aCID.ToProvidedString(cidStr);
         errorMsg.Append(cidStr);
         SetPendingException(cx, errorMsg.get());
-
-#ifdef DEBUG_CAPS_CanCreateInstance
-        printf("DENIED\n");
-    }
-    else
-    {
-        printf("GRANTED\n");
-#endif
     }
     return rv;
 }
 
 NS_IMETHODIMP
 nsScriptSecurityManager::CanGetService(JSContext *cx,
                                        const nsCID &aCID)
 {
-#ifdef DEBUG_CAPS_CanGetService
-    char* cidStr = aCID.ToString();
-    printf("### CanGetService(%s) ", cidStr);
-    NS_Free(cidStr);
-#endif
-
     nsresult rv = CheckXPCPermissions(nullptr, nullptr, nullptr, nullptr, nullptr);
     if (NS_FAILED(rv))
     {
         //-- Access denied, report an error
         nsAutoCString errorMsg("Permission denied to get service. CID=");
         char cidStr[NSID_LENGTH];
         aCID.ToProvidedString(cidStr);
         errorMsg.Append(cidStr);
         SetPendingException(cx, errorMsg.get());
-
-#ifdef DEBUG_CAPS_CanGetService
-        printf("DENIED\n");
-    }
-    else
-    {
-        printf("GRANTED\n");
-#endif
     }
 
     return rv;
 }
 
 
 NS_IMETHODIMP
 nsScriptSecurityManager::CanAccess(uint32_t aAction,
@@ -2845,19 +2747,16 @@ nsScriptSecurityManager::InitPolicies()
                 *domainCurrent = '\0';
                 nsCStringKey key(nextToLastDot ? nextToLastDot+1 : domainStart);
                 DomainEntry *newEntry = new DomainEntry(domainStart, domainPolicy);
                 if (!newEntry)
                 {
                     domainPolicy->Drop();
                     return NS_ERROR_OUT_OF_MEMORY;
                 }
-#ifdef DEBUG
-                newEntry->mPolicyName_DEBUG = nameBegin;
-#endif
                 DomainEntry *existingEntry = (DomainEntry *)
                     mOriginToPolicyMap->Get(&key);
                 if (!existingEntry)
                     mOriginToPolicyMap->Put(&key, newEntry);
                 else
                 {
                     if (existingEntry->Matches(domainStart))
                     {
@@ -2895,19 +2794,16 @@ nsScriptSecurityManager::InitPolicies()
         domainPolicy->Drop();
         if (NS_FAILED(rv))
             return rv;
     }
 
     // Reset the "dirty" flag
     mPolicyPrefsChanged = false;
 
-#ifdef DEBUG_CAPS_HACKER
-    PrintPolicyDB();
-#endif
     return NS_OK;
 }
 
 
 nsresult
 nsScriptSecurityManager::InitDomainPolicy(JSContext* cx,
                                           const char* aPolicyName,
                                           DomainPolicy* aDomainPolicy)
@@ -3113,94 +3009,8 @@ nsScriptSecurityManager::GetExtendedOrig
                                            bool aInMozBrowser,
                                            nsACString& aExtendedOrigin)
 {
   MOZ_ASSERT(aAppId != nsIScriptSecurityManager::UNKNOWN_APP_ID);
 
   mozilla::GetExtendedOrigin(aURI, aAppId, aInMozBrowser, aExtendedOrigin);
   return NS_OK;
 }
-
-///////////////////////////////////////////////////////////////////////////////
-// The following code prints the contents of the policy DB to the console.
-#ifdef DEBUG_CAPS_HACKER
-
-//typedef PLDHashOperator
-//(* PLDHashEnumerator)(PLDHashTable *table, PLDHashEntryHdr *hdr,
-//                      uint32_t number, void *arg);
-static PLDHashOperator
-PrintPropertyPolicy(PLDHashTable *table, PLDHashEntryHdr *entry,
-                    uint32_t number, void *arg)
-{
-    PropertyPolicy* pp = (PropertyPolicy*)entry;
-    nsAutoCString prop("        ");
-    JSContext* cx = (JSContext*)arg;
-    prop.AppendInt((uint32_t)pp->key);
-    prop += ' ';
-    LossyAppendUTF16toASCII((PRUnichar*)JS_GetStringChars(pp->key), prop);
-    prop += ": Get=";
-    if (SECURITY_ACCESS_LEVEL_FLAG(pp->mGet))
-        prop.AppendInt(pp->mGet.level);
-    else
-        prop += pp->mGet.capability;
-
-    prop += " Set=";
-    if (SECURITY_ACCESS_LEVEL_FLAG(pp->mSet))
-        prop.AppendInt(pp->mSet.level);
-    else
-        prop += pp->mSet.capability;
-        
-    printf("%s.\n", prop.get());
-    return PL_DHASH_NEXT;
-}
-
-static PLDHashOperator
-PrintClassPolicy(PLDHashTable *table, PLDHashEntryHdr *entry,
-                 uint32_t number, void *arg)
-{
-    ClassPolicy* cp = (ClassPolicy*)entry;
-    printf("    %s\n", cp->key);
-
-    PL_DHashTableEnumerate(cp->mPolicy, PrintPropertyPolicy, arg);
-    return PL_DHASH_NEXT;
-}
-
-// typedef bool
-// (* nsHashtableEnumFunc)(nsHashKey *aKey, void *aData, void* aClosure);
-static bool
-PrintDomainPolicy(nsHashKey *aKey, void *aData, void* aClosure)
-{
-    DomainEntry* de = (DomainEntry*)aData;
-    printf("----------------------------\n");
-    printf("Domain: %s Policy Name: %s.\n", de->mOrigin.get(),
-           de->mPolicyName_DEBUG.get());
-    PL_DHashTableEnumerate(de->mDomainPolicy, PrintClassPolicy, aClosure);
-    return true;
-}
-
-static bool
-PrintCapability(nsHashKey *aKey, void *aData, void* aClosure)
-{
-    char* cap = (char*)aData;
-    printf("    %s.\n", cap);
-    return true;
-}
-
-void
-nsScriptSecurityManager::PrintPolicyDB()
-{
-    printf("############## Security Policies ###############\n");
-    if(mOriginToPolicyMap)
-    {
-        JSContext* cx = GetCurrentJSContext();
-        if (!cx)
-            cx = GetSafeJSContext();
-        printf("----------------------------\n");
-        printf("Domain: Default.\n");
-        PL_DHashTableEnumerate(mDefaultPolicy, PrintClassPolicy, (void*)cx);
-        mOriginToPolicyMap->Enumerate(PrintDomainPolicy, (void*)cx);
-    }
-    printf("############ End Security Policies #############\n\n");
-    printf("############## Capabilities ###############\n");
-    mCapabilities->Enumerate(PrintCapability);
-    printf("############## End Capabilities ###############\n");
-}
-#endif