Back to index

lightning-sunbird  0.9+nobinonly
nsAboutBloat.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; 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.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsAboutBloat.h"
00039 #include "nsIIOService.h"
00040 #include "nsIServiceManager.h"
00041 #include "nsIStringStream.h"
00042 #include "nsXPIDLString.h"
00043 #include "nsIURI.h"
00044 #include "prtime.h"
00045 #include "nsCOMPtr.h"
00046 #include "nsIFileStreams.h"
00047 #include "nsNetUtil.h"
00048 #include "nsDirectoryServiceDefs.h"
00049 #include "nsILocalFile.h"
00050 #include "nsTraceRefcntImpl.h"
00051 
00052 #ifdef XP_MAC
00053 extern "C" void GC_gcollect(void);
00054 #else
00055 static void GC_gcollect() {}
00056 #endif
00057 
00058 NS_IMPL_ISUPPORTS1(nsAboutBloat, nsIAboutModule)
00059 
00060 NS_IMETHODIMP
00061 nsAboutBloat::NewChannel(nsIURI *aURI, nsIChannel **result)
00062 {
00063     NS_ENSURE_ARG_POINTER(aURI);
00064     nsresult rv;
00065     nsCAutoString path;
00066     rv = aURI->GetPath(path);
00067     if (NS_FAILED(rv)) return rv;
00068 
00069     nsTraceRefcntImpl::StatisticsType statType = nsTraceRefcntImpl::ALL_STATS;
00070     PRBool clear = PR_FALSE;
00071     PRBool leaks = PR_FALSE;
00072 
00073     PRInt32 pos = path.Find("?");
00074     if (pos > 0) {
00075         nsCAutoString param;
00076         (void)path.Right(param, path.Length() - (pos+1));
00077         if (param.EqualsLiteral("new"))
00078             statType = nsTraceRefcntImpl::NEW_STATS;
00079         else if (param.EqualsLiteral("clear"))
00080             clear = PR_TRUE;
00081         else if (param.EqualsLiteral("leaks"))
00082             leaks = PR_TRUE;
00083     }
00084 
00085     nsCOMPtr<nsIInputStream> inStr;
00086     if (clear) {
00087         nsTraceRefcntImpl::ResetStatistics();
00088 
00089         const char* msg = "Bloat statistics cleared.";
00090         rv = NS_NewCStringInputStream(getter_AddRefs(inStr), nsDependentCString(msg));
00091         if (NS_FAILED(rv)) return rv;
00092     }
00093     else if (leaks) {
00094         // dump the current set of leaks.
00095         GC_gcollect();
00096        
00097         const char* msg = "Memory leaks dumped.";
00098         rv = NS_NewCStringInputStream(getter_AddRefs(inStr), nsDependentCString(msg));
00099         if (NS_FAILED(rv)) return rv;
00100     }
00101     else {
00102         nsCOMPtr<nsIFile> file;
00103         rv = NS_GetSpecialDirectory(NS_OS_CURRENT_PROCESS_DIR, 
00104                                     getter_AddRefs(file));       
00105         if (NS_FAILED(rv)) return rv;
00106 
00107         rv = file->AppendNative(NS_LITERAL_CSTRING("bloatlogs"));
00108         if (NS_FAILED(rv)) return rv;
00109 
00110         PRBool exists;
00111         rv = file->Exists(&exists);
00112         if (NS_FAILED(rv)) return rv;
00113 
00114         if (!exists) {
00115             // On all the platforms that I know use permissions,
00116             // directories need to have the executable flag set
00117             // if you want to do anything inside the directory.
00118             rv = file->Create(nsIFile::DIRECTORY_TYPE, 0755);
00119             if (NS_FAILED(rv)) return rv;
00120         }
00121 
00122         nsCAutoString dumpFileName;
00123         if (statType == nsTraceRefcntImpl::ALL_STATS)
00124             dumpFileName.AssignLiteral("all-");
00125         else
00126             dumpFileName.AssignLiteral("new-");
00127         PRExplodedTime expTime;
00128         PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &expTime);
00129         char time[128];
00130         PR_FormatTimeUSEnglish(time, 128, "%Y-%m-%d-%H%M%S.txt", &expTime);
00131         dumpFileName += time;
00132         rv = file->AppendNative(dumpFileName);
00133         if (NS_FAILED(rv)) return rv;
00134 
00135         FILE* out;
00136         nsCOMPtr<nsILocalFile> lfile = do_QueryInterface(file);
00137         if (lfile == nsnull)
00138             return NS_ERROR_FAILURE;
00139         rv = lfile->OpenANSIFileDesc("w", &out);
00140         if (NS_FAILED(rv)) return rv;
00141 
00142         rv = nsTraceRefcntImpl::DumpStatistics(statType, out);
00143         ::fclose(out);
00144         if (NS_FAILED(rv)) return rv;
00145 
00146         rv = NS_NewLocalFileInputStream(getter_AddRefs(inStr), file);
00147         if (NS_FAILED(rv)) return rv;
00148     }
00149 
00150     nsIChannel* channel;
00151     rv = NS_NewInputStreamChannel(&channel, aURI, inStr,
00152                                   NS_LITERAL_CSTRING("text/plain"),
00153                                   NS_LITERAL_CSTRING("utf-8"));
00154     if (NS_FAILED(rv)) return rv;
00155 
00156     *result = channel;
00157     return rv;
00158 }
00159 
00160 NS_METHOD
00161 nsAboutBloat::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
00162 {
00163     nsAboutBloat* about = new nsAboutBloat();
00164     if (about == nsnull)
00165         return NS_ERROR_OUT_OF_MEMORY;
00166     NS_ADDREF(about);
00167     nsresult rv = about->QueryInterface(aIID, aResult);
00168     NS_RELEASE(about);
00169     return rv;
00170 }
00171