Bug 1154435 - [nsDumpUtil] Refine nsDumpUtils::OpenTempFile to make this function more flexible. r=dhylands
authorAlphan Chen <alchen@mozilla.com>
Thu, 04 Jun 2015 14:47:09 +0800
changeset 290334 a07e5bda361f1962f28c735ebd00e6744a21e58a
parent 290333 7907970ddde9e61a331371c19e51f4afbf1d87fb
child 290335 c60a6aab34a0c09e5e8cd6294037114a83628f73
push id5119
push usertmielczarek@mozilla.com
push dateFri, 04 Sep 2015 12:13:35 +0000
reviewersdhylands
bugs1154435
milestone43.0a1
Bug 1154435 - [nsDumpUtil] Refine nsDumpUtils::OpenTempFile to make this function more flexible. r=dhylands - Add one more mode in this function (CREATE/CREATE_UNIQUE) - export the header file for the usage of other components
xpcom/base/moz.build
xpcom/base/nsDumpUtils.cpp
xpcom/base/nsDumpUtils.h
--- a/xpcom/base/moz.build
+++ b/xpcom/base/moz.build
@@ -48,16 +48,17 @@ EXPORTS += [
     'nsAgg.h',
     'nsAlgorithm.h',
     'nsAutoPtr.h',
     'nsAutoRef.h',
     'nsCom.h',
     'nscore.h',
     'nsCycleCollector.h',
     'nsDebugImpl.h',
+    'nsDumpUtils.h',
     'nsError.h',
     'nsGZFileWriter.h',
     'nsIID.h',
     'nsInterfaceRequestorAgg.h',
     'nsISizeOf.h',
     'nsISupportsBase.h',
     'nsObjCExceptions.h',
     'nsQueryObject.h',
--- a/xpcom/base/nsDumpUtils.cpp
+++ b/xpcom/base/nsDumpUtils.cpp
@@ -430,17 +430,17 @@ FifoWatcher::OnFileCanReadWithoutBlockin
 
 #endif // XP_UNIX }
 
 // In Android case, this function will open a file named aFilename under
 // /data/local/tmp/"aFoldername".
 // Otherwise, it will open a file named aFilename under "NS_OS_TEMP_DIR".
 /* static */ nsresult
 nsDumpUtils::OpenTempFile(const nsACString& aFilename, nsIFile** aFile,
-                          const nsACString& aFoldername)
+                          const nsACString& aFoldername, Mode aMode)
 {
 #ifdef ANDROID
   // For Android, first try the downloads directory which is world-readable
   // rather than the temp directory which is not.
   if (!*aFile) {
     char* env = PR_GetEnv("DOWNLOADS_DIRECTORY");
     if (env) {
       NS_NewNativeLocalFile(nsCString(env), /* followLinks = */ true, aFile);
@@ -483,19 +483,23 @@ nsDumpUtils::OpenTempFile(const nsACStri
 
   nsCOMPtr<nsIFile> file(*aFile);
 
   rv = file->AppendNative(aFilename);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
-  rv = file->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0666);
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return rv;
+  if (aMode == CREATE_UNIQUE) {
+    rv = file->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0666);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
+  } else {
+    file->Create(nsIFile::NORMAL_FILE_TYPE, 0666);
   }
 
 #ifdef ANDROID
   // Make this file world-read/writable; the permissions passed to the
   // CreateUnique call above are not sufficient on Android, which runs with a
   // umask.
   nsAutoCString path;
   rv = file->GetNativePath(path);
--- a/xpcom/base/nsDumpUtils.h
+++ b/xpcom/base/nsDumpUtils.h
@@ -175,25 +175,31 @@ private:
   }
 
   mozilla::Mutex mSignalInfoLock; // protects mSignalInfo
   SignalInfoArray mSignalInfo;
 };
 
 #endif // XP_UNIX }
 
-
 class nsDumpUtils
 {
 public:
+
+  enum Mode {
+    CREATE,
+    CREATE_UNIQUE
+  };
+
   /**
    * This function creates a new unique file based on |aFilename| in a
    * world-readable temp directory. This is the system temp directory
    * or, in the case of Android, the downloads directory. If |aFile| is
    * non-null, it is assumed to point to a folder, and that folder is used
    * instead.
    */
   static nsresult OpenTempFile(const nsACString& aFilename,
                                nsIFile** aFile,
-                               const nsACString& aFoldername = EmptyCString());
+                               const nsACString& aFoldername = EmptyCString(),
+                               Mode aMode = CREATE_UNIQUE);
 };
 
 #endif