michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "ArchiveEvent.h" michael@0: michael@0: #include "nsCExternalHandlerService.h" michael@0: #include "nsProxyRelease.h" michael@0: michael@0: USING_FILE_NAMESPACE michael@0: michael@0: NS_IMPL_ISUPPORTS0(ArchiveItem) michael@0: michael@0: ArchiveItem::ArchiveItem() michael@0: { michael@0: MOZ_COUNT_CTOR(ArchiveItem); michael@0: } michael@0: michael@0: ArchiveItem::~ArchiveItem() michael@0: { michael@0: MOZ_COUNT_DTOR(ArchiveItem); michael@0: } michael@0: michael@0: michael@0: nsCString michael@0: ArchiveItem::GetType() michael@0: { michael@0: if (mType.IsEmpty()) { michael@0: return NS_LITERAL_CSTRING("binary/octet-stream"); michael@0: } michael@0: michael@0: return mType; michael@0: } michael@0: michael@0: void michael@0: ArchiveItem::SetType(const nsCString& aType) michael@0: { michael@0: mType = aType; michael@0: } michael@0: michael@0: ArchiveReaderEvent::ArchiveReaderEvent(ArchiveReader* aArchiveReader) michael@0: : mArchiveReader(aArchiveReader) michael@0: { michael@0: MOZ_COUNT_CTOR(ArchiveReaderEvent); michael@0: } michael@0: michael@0: ArchiveReaderEvent::~ArchiveReaderEvent() michael@0: { michael@0: if (!NS_IsMainThread()) { michael@0: nsIMIMEService* mimeService; michael@0: mMimeService.forget(&mimeService); michael@0: michael@0: if (mimeService) { michael@0: nsCOMPtr mainThread = do_GetMainThread(); michael@0: NS_WARN_IF_FALSE(mainThread, "Couldn't get the main thread! Leaking!"); michael@0: michael@0: if (mainThread) { michael@0: NS_ProxyRelease(mainThread, mimeService); michael@0: } michael@0: } michael@0: } michael@0: michael@0: MOZ_COUNT_DTOR(ArchiveReaderEvent); michael@0: } michael@0: michael@0: // From the filename to the mimetype: michael@0: nsresult michael@0: ArchiveReaderEvent::GetType(nsCString& aExt, michael@0: nsCString& aMimeType) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: michael@0: nsresult rv; michael@0: michael@0: if (mMimeService.get() == nullptr) { michael@0: mMimeService = do_GetService(NS_MIMESERVICE_CONTRACTID, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: rv = mMimeService->GetTypeFromExtension(aExt, aMimeType); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: ArchiveReaderEvent::Run() michael@0: { michael@0: return Exec(); michael@0: } michael@0: michael@0: nsresult michael@0: ArchiveReaderEvent::RunShare(nsresult aStatus) michael@0: { michael@0: mStatus = aStatus; michael@0: michael@0: nsCOMPtr event = NS_NewRunnableMethod(this, &ArchiveReaderEvent::ShareMainThread); michael@0: NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: ArchiveReaderEvent::ShareMainThread() michael@0: { michael@0: nsTArray > fileList; michael@0: michael@0: if (!NS_FAILED(mStatus)) { michael@0: // This extra step must run in the main thread: michael@0: for (uint32_t index = 0; index < mFileList.Length(); ++index) { michael@0: nsRefPtr item = mFileList[index]; michael@0: michael@0: nsString tmp; michael@0: nsresult rv = item->GetFilename(tmp); michael@0: nsCString filename = NS_ConvertUTF16toUTF8(tmp); michael@0: if (NS_FAILED(rv)) { michael@0: continue; michael@0: } michael@0: michael@0: int32_t offset = filename.RFindChar('.'); michael@0: if (offset != kNotFound) { michael@0: filename.Cut(0, offset + 1); michael@0: michael@0: // Just to be sure, if something goes wrong, the mimetype is an empty string: michael@0: nsCString type; michael@0: if (NS_SUCCEEDED(GetType(filename, type))) { michael@0: item->SetType(type); michael@0: } michael@0: } michael@0: michael@0: // This is a nsDOMFile: michael@0: nsRefPtr file = item->File(mArchiveReader); michael@0: if (file) { michael@0: fileList.AppendElement(file); michael@0: } michael@0: } michael@0: } michael@0: michael@0: mArchiveReader->Ready(fileList, mStatus); michael@0: }