Bug 1600283 - Remove uses of already_AddRefed. r=#dom-workers-and-storage draft
authorSimon Giesecke <sgiesecke@mozilla.com>
Wed, 04 Dec 2019 16:48:01 +0000
changeset 2515978 f7fff033e36e7637d7525cdb0e8a2bffef267703
parent 2515977 6dd3fd46bb224645f041f3a2ee4363984afe172f
child 2515979 27a8b5ded3a921b8de13db22fd3b1f8def422898
push id460090
push userreviewbot
push dateWed, 04 Dec 2019 16:51:29 +0000
treeherdertry@35d310cdd9cf [default view] [failures only]
bugs1600283
milestone73.0a1
Bug 1600283 - Remove uses of already_AddRefed. r=#dom-workers-and-storage Differential Revision: https://phabricator.services.mozilla.com/D55480 Differential Diff: PHID-DIFF-oh6euu5l4twrdsjuvyjk
dom/indexedDB/ActorsParent.cpp
dom/indexedDB/FileManager.h
--- a/dom/indexedDB/ActorsParent.cpp
+++ b/dom/indexedDB/ActorsParent.cpp
@@ -4610,26 +4610,26 @@ nsresult CreateStorageConnection(nsIFile
       return rv;
     }
   }
 
   connection.forget(aConnection);
   return NS_OK;
 }
 
-already_AddRefed<nsIFile> GetFileForPath(const nsAString& aPath) {
+nsCOMPtr<nsIFile> GetFileForPath(const nsAString& aPath) {
   MOZ_ASSERT(!aPath.IsEmpty());
 
   nsCOMPtr<nsIFile> file;
   if (NS_WARN_IF(
           NS_FAILED(NS_NewLocalFile(aPath, false, getter_AddRefs(file))))) {
     return nullptr;
   }
 
-  return file.forget();
+  return file;
 }
 
 nsresult GetStorageConnection(nsIFile* aDatabaseFile, int64_t aDirectoryLockId,
                               uint32_t aTelemetryId,
                               mozIStorageConnection** aConnection) {
   MOZ_ASSERT(!NS_IsMainThread());
   MOZ_ASSERT(!IsOnBackgroundThread());
   MOZ_ASSERT(aDatabaseFile);
@@ -8532,19 +8532,19 @@ class MOZ_STACK_CLASS FileHelper final {
   class ReadCallback;
   RefPtr<ReadCallback> mReadCallback;
 
  public:
   explicit FileHelper(FileManager* aFileManager) : mFileManager(aFileManager) {}
 
   nsresult Init();
 
-  already_AddRefed<nsIFile> GetFile(FileInfo* aFileInfo);
-
-  already_AddRefed<nsIFile> GetJournalFile(FileInfo* aFileInfo);
+  MOZ_MUST_USE nsCOMPtr<nsIFile> GetFile(FileInfo* aFileInfo);
+
+  MOZ_MUST_USE nsCOMPtr<nsIFile> GetJournalFile(FileInfo* aFileInfo);
 
   nsresult CreateFileFromStream(nsIFile* aFile, nsIFile* aJournalFile,
                                 nsIInputStream* aInputStream, bool aCompress);
 
   nsresult RemoveFile(nsIFile* aFile, nsIFile* aJournalFile);
 
  private:
   nsresult SyncCopy(nsIInputStream* aInputStream,
@@ -15771,46 +15771,46 @@ nsresult FileManager::Invalidate() {
     if (!info->LockedClearDBRefs()) {
       iter.Remove();
     }
   }
 
   return NS_OK;
 }
 
-already_AddRefed<nsIFile> FileManager::GetDirectory() {
+nsCOMPtr<nsIFile> FileManager::GetDirectory() {
   return GetFileForPath(mDirectoryPath);
 }
 
-already_AddRefed<nsIFile> FileManager::GetCheckedDirectory() {
-  nsCOMPtr<nsIFile> directory = GetDirectory();
+nsCOMPtr<nsIFile> FileManager::GetCheckedDirectory() {
+  auto directory = GetDirectory();
   if (NS_WARN_IF(!directory)) {
     return nullptr;
   }
 
   DebugOnly<bool> exists;
   MOZ_ASSERT(NS_SUCCEEDED(directory->Exists(&exists)));
   MOZ_ASSERT(exists);
 
   DebugOnly<bool> isDirectory;
   MOZ_ASSERT(NS_SUCCEEDED(directory->IsDirectory(&isDirectory)));
   MOZ_ASSERT(isDirectory);
 
-  return directory.forget();
-}
-
-already_AddRefed<nsIFile> FileManager::GetJournalDirectory() {
+  return directory;
+}
+
+nsCOMPtr<nsIFile> FileManager::GetJournalDirectory() {
   return GetFileForPath(mJournalDirectoryPath);
 }
 
-already_AddRefed<nsIFile> FileManager::EnsureJournalDirectory() {
+nsCOMPtr<nsIFile> FileManager::EnsureJournalDirectory() {
   // This can happen on the IO or on a transaction thread.
   MOZ_ASSERT(!NS_IsMainThread());
 
-  nsCOMPtr<nsIFile> journalDirectory = GetFileForPath(mJournalDirectoryPath);
+  auto journalDirectory = GetFileForPath(mJournalDirectoryPath);
   if (NS_WARN_IF(!journalDirectory)) {
     return nullptr;
   }
 
   bool exists;
   nsresult rv = journalDirectory->Exists(&exists);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return nullptr;
@@ -15828,58 +15828,61 @@ already_AddRefed<nsIFile> FileManager::E
     }
   } else {
     rv = journalDirectory->Create(nsIFile::DIRECTORY_TYPE, 0755);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return nullptr;
     }
   }
 
-  return journalDirectory.forget();
-}
-
-already_AddRefed<FileInfo> FileManager::GetFileInfo(int64_t aId) const {
+  return journalDirectory;
+}
+
+RefPtr<FileInfo> FileManager::GetFileInfo(int64_t aId) const {
   if (IndexedDatabaseManager::IsClosed()) {
     MOZ_ASSERT(false, "Shouldn't be called after shutdown!");
     return nullptr;
   }
 
+  // TODO: We cannot simply change this to RefPtr<FileInfo>, because
+  // FileInfo::AddRef also acquires the IndexedDatabaseManager::FileMutex. This
+  // looks quirky at least.
   FileInfo* fileInfo;
   {
     MutexAutoLock lock(IndexedDatabaseManager::FileMutex());
     fileInfo = mFileInfos.Get(aId);
   }
 
-  RefPtr<FileInfo> result = fileInfo;
-  return result.forget();
-}
-
-already_AddRefed<FileInfo> FileManager::GetNewFileInfo() {
+  return fileInfo;
+}
+
+RefPtr<FileInfo> FileManager::GetNewFileInfo() {
   MOZ_ASSERT(!IndexedDatabaseManager::IsClosed());
 
+  // TODO: We cannot simply change this to RefPtr<FileInfo>, because
+  // FileInfo::AddRef also acquires the IndexedDatabaseManager::FileMutex. This
+  // looks quirky at least.
   FileInfo* fileInfo;
   {
     MutexAutoLock lock(IndexedDatabaseManager::FileMutex());
 
     int64_t id = mLastFileId + 1;
 
     fileInfo = FileInfo::Create(this, id);
 
     mFileInfos.Put(id, fileInfo);
 
     mLastFileId = id;
   }
 
-  RefPtr<FileInfo> result = fileInfo;
-  return result.forget();
+  return fileInfo;
 }
 
 // static
-already_AddRefed<nsIFile> FileManager::GetFileForId(nsIFile* aDirectory,
-                                                    int64_t aId) {
+nsCOMPtr<nsIFile> FileManager::GetFileForId(nsIFile* aDirectory, int64_t aId) {
   MOZ_ASSERT(aDirectory);
   MOZ_ASSERT(aId > 0);
 
   nsAutoString id;
   id.AppendInt(aId);
 
   nsCOMPtr<nsIFile> file;
   nsresult rv = aDirectory->Clone(getter_AddRefs(file));
@@ -15887,36 +15890,36 @@ already_AddRefed<nsIFile> FileManager::G
     return nullptr;
   }
 
   rv = file->Append(id);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return nullptr;
   }
 
-  return file.forget();
+  return file;
 }
 
 // static
-already_AddRefed<nsIFile> FileManager::GetCheckedFileForId(nsIFile* aDirectory,
-                                                           int64_t aId) {
-  nsCOMPtr<nsIFile> file = GetFileForId(aDirectory, aId);
+nsCOMPtr<nsIFile> FileManager::GetCheckedFileForId(nsIFile* aDirectory,
+                                                   int64_t aId) {
+  auto file = GetFileForId(aDirectory, aId);
   if (NS_WARN_IF(!file)) {
     return nullptr;
   }
 
   DebugOnly<bool> exists;
   MOZ_ASSERT(NS_SUCCEEDED(file->Exists(&exists)));
   MOZ_ASSERT(exists);
 
   DebugOnly<bool> isFile;
   MOZ_ASSERT(NS_SUCCEEDED(file->IsFile(&isFile)));
   MOZ_ASSERT(isFile);
 
-  return file.forget();
+  return file;
 }
 
 // static
 nsresult FileManager::InitDirectory(nsIFile* aDirectory, nsIFile* aDatabaseFile,
                                     const nsACString& aOrigin,
                                     uint32_t aTelemetryId) {
   AssertIsOnIOThread();
   MOZ_ASSERT(aDirectory);
@@ -22791,41 +22794,40 @@ nsresult CreateFileOp::DoDatabaseWork() 
   mFileInfo = fileManager->GetNewFileInfo();
   if (NS_WARN_IF(!mFileInfo)) {
     IDB_REPORT_INTERNAL_ERR();
     return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
   }
 
   const int64_t fileId = mFileInfo->Id();
 
-  nsCOMPtr<nsIFile> journalDirectory = fileManager->EnsureJournalDirectory();
+  const auto journalDirectory = fileManager->EnsureJournalDirectory();
   if (NS_WARN_IF(!journalDirectory)) {
     IDB_REPORT_INTERNAL_ERR();
     return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
   }
 
-  nsCOMPtr<nsIFile> journalFile =
-      fileManager->GetFileForId(journalDirectory, fileId);
+  const auto journalFile = fileManager->GetFileForId(journalDirectory, fileId);
   if (NS_WARN_IF(!journalFile)) {
     IDB_REPORT_INTERNAL_ERR();
     return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
   }
 
   nsresult rv = journalFile->Create(nsIFile::NORMAL_FILE_TYPE, 0644);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
-  nsCOMPtr<nsIFile> fileDirectory = fileManager->GetDirectory();
+  const auto fileDirectory = fileManager->GetDirectory();
   if (NS_WARN_IF(!fileDirectory)) {
     IDB_REPORT_INTERNAL_ERR();
     return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
   }
 
-  nsCOMPtr<nsIFile> file = fileManager->GetFileForId(fileDirectory, fileId);
+  const auto file = fileManager->GetFileForId(fileDirectory, fileId);
   if (NS_WARN_IF(!file)) {
     IDB_REPORT_INTERNAL_ERR();
     return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
   }
 
   rv = file->Create(nsIFile::NORMAL_FILE_TYPE, 0644);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
@@ -24746,23 +24748,23 @@ nsresult ObjectStoreAddOrPutRequestOp::D
           if (NS_WARN_IF(NS_FAILED(rv))) {
             IDB_REPORT_INTERNAL_ERR();
             return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
           }
         }
 
         const RefPtr<FileInfo>& fileInfo = storedFileInfo.mFileInfo;
 
-        nsCOMPtr<nsIFile> file = fileHelper->GetFile(fileInfo);
+        const auto file = fileHelper->GetFile(fileInfo);
         if (NS_WARN_IF(!file)) {
           IDB_REPORT_INTERNAL_ERR();
           return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
         }
 
-        nsCOMPtr<nsIFile> journalFile = fileHelper->GetJournalFile(fileInfo);
+        const auto journalFile = fileHelper->GetJournalFile(fileInfo);
         if (NS_WARN_IF(!journalFile)) {
           IDB_REPORT_INTERNAL_ERR();
           return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
         }
 
         bool compress =
             storedFileInfo.mType == StructuredCloneFile::eStructuredClone;
 
@@ -26994,22 +26996,22 @@ DEBUGThreadSlower::AfterProcessNextEvent
 }
 
 #endif  // DEBUG
 
 nsresult FileHelper::Init() {
   MOZ_ASSERT(!IsOnBackgroundThread());
   MOZ_ASSERT(mFileManager);
 
-  nsCOMPtr<nsIFile> fileDirectory = mFileManager->GetCheckedDirectory();
+  auto fileDirectory = mFileManager->GetCheckedDirectory();
   if (NS_WARN_IF(!fileDirectory)) {
     return NS_ERROR_FAILURE;
   }
 
-  nsCOMPtr<nsIFile> journalDirectory = mFileManager->EnsureJournalDirectory();
+  auto journalDirectory = mFileManager->EnsureJournalDirectory();
   if (NS_WARN_IF(!journalDirectory)) {
     return NS_ERROR_FAILURE;
   }
 
   DebugOnly<bool> exists;
   MOZ_ASSERT(NS_SUCCEEDED(journalDirectory->Exists(&exists)));
   MOZ_ASSERT(exists);
 
@@ -27018,29 +27020,29 @@ nsresult FileHelper::Init() {
   MOZ_ASSERT(isDirectory);
 
   mFileDirectory = std::move(fileDirectory);
   mJournalDirectory = std::move(journalDirectory);
 
   return NS_OK;
 }
 
-already_AddRefed<nsIFile> FileHelper::GetFile(FileInfo* aFileInfo) {
+nsCOMPtr<nsIFile> FileHelper::GetFile(FileInfo* aFileInfo) {
   MOZ_ASSERT(!IsOnBackgroundThread());
   MOZ_ASSERT(aFileInfo);
   MOZ_ASSERT(mFileManager);
   MOZ_ASSERT(mFileDirectory);
 
   const int64_t fileId = aFileInfo->Id();
   MOZ_ASSERT(fileId > 0);
 
   return mFileManager->GetFileForId(mFileDirectory, fileId);
 }
 
-already_AddRefed<nsIFile> FileHelper::GetJournalFile(FileInfo* aFileInfo) {
+nsCOMPtr<nsIFile> FileHelper::GetJournalFile(FileInfo* aFileInfo) {
   MOZ_ASSERT(!IsOnBackgroundThread());
   MOZ_ASSERT(aFileInfo);
   MOZ_ASSERT(mFileManager);
   MOZ_ASSERT(mJournalDirectory);
 
   const int64_t fileId = aFileInfo->Id();
   MOZ_ASSERT(fileId > 0);
 
--- a/dom/indexedDB/FileManager.h
+++ b/dom/indexedDB/FileManager.h
@@ -40,21 +40,21 @@ class FileManager final {
 
   // Protected by IndexedDatabaseManager::FileMutex()
   nsDataHashtable<nsUint64HashKey, FileInfo*> mFileInfos;
 
   const bool mEnforcingQuota;
   bool mInvalidated;
 
  public:
-  static already_AddRefed<nsIFile> GetFileForId(nsIFile* aDirectory,
-                                                int64_t aId);
+  static MOZ_MUST_USE nsCOMPtr<nsIFile> GetFileForId(nsIFile* aDirectory,
+                                                     int64_t aId);
 
-  static already_AddRefed<nsIFile> GetCheckedFileForId(nsIFile* aDirectory,
-                                                       int64_t aId);
+  static MOZ_MUST_USE nsCOMPtr<nsIFile> GetCheckedFileForId(nsIFile* aDirectory,
+                                                            int64_t aId);
 
   static nsresult InitDirectory(nsIFile* aDirectory, nsIFile* aDatabaseFile,
                                 const nsACString& aOrigin,
                                 uint32_t aTelemetryId);
 
   static nsresult GetUsage(nsIFile* aDirectory, Maybe<uint64_t>& aUsage);
 
   static nsresult GetUsage(nsIFile* aDirectory, uint64_t& aUsage);
@@ -74,27 +74,27 @@ class FileManager final {
   bool EnforcingQuota() const { return mEnforcingQuota; }
 
   bool Invalidated() const { return mInvalidated; }
 
   nsresult Init(nsIFile* aDirectory, mozIStorageConnection* aConnection);
 
   nsresult Invalidate();
 
-  already_AddRefed<nsIFile> GetDirectory();
+  MOZ_MUST_USE nsCOMPtr<nsIFile> GetDirectory();
 
-  already_AddRefed<nsIFile> GetCheckedDirectory();
+  MOZ_MUST_USE nsCOMPtr<nsIFile> GetCheckedDirectory();
 
-  already_AddRefed<nsIFile> GetJournalDirectory();
+  MOZ_MUST_USE nsCOMPtr<nsIFile> GetJournalDirectory();
 
-  already_AddRefed<nsIFile> EnsureJournalDirectory();
+  MOZ_MUST_USE nsCOMPtr<nsIFile> EnsureJournalDirectory();
 
-  already_AddRefed<FileInfo> GetFileInfo(int64_t aId) const;
+  MOZ_MUST_USE RefPtr<FileInfo> GetFileInfo(int64_t aId) const;
 
-  already_AddRefed<FileInfo> GetNewFileInfo();
+  MOZ_MUST_USE RefPtr<FileInfo> GetNewFileInfo();
 
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FileManager)
 
  private:
   ~FileManager() = default;
 };
 
 }  // namespace indexedDB