Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "ArchiveEvent.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "nsCExternalHandlerService.h" |
michael@0 | 10 | #include "nsProxyRelease.h" |
michael@0 | 11 | |
michael@0 | 12 | USING_FILE_NAMESPACE |
michael@0 | 13 | |
michael@0 | 14 | NS_IMPL_ISUPPORTS0(ArchiveItem) |
michael@0 | 15 | |
michael@0 | 16 | ArchiveItem::ArchiveItem() |
michael@0 | 17 | { |
michael@0 | 18 | MOZ_COUNT_CTOR(ArchiveItem); |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | ArchiveItem::~ArchiveItem() |
michael@0 | 22 | { |
michael@0 | 23 | MOZ_COUNT_DTOR(ArchiveItem); |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | |
michael@0 | 27 | nsCString |
michael@0 | 28 | ArchiveItem::GetType() |
michael@0 | 29 | { |
michael@0 | 30 | if (mType.IsEmpty()) { |
michael@0 | 31 | return NS_LITERAL_CSTRING("binary/octet-stream"); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | return mType; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | void |
michael@0 | 38 | ArchiveItem::SetType(const nsCString& aType) |
michael@0 | 39 | { |
michael@0 | 40 | mType = aType; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | ArchiveReaderEvent::ArchiveReaderEvent(ArchiveReader* aArchiveReader) |
michael@0 | 44 | : mArchiveReader(aArchiveReader) |
michael@0 | 45 | { |
michael@0 | 46 | MOZ_COUNT_CTOR(ArchiveReaderEvent); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | ArchiveReaderEvent::~ArchiveReaderEvent() |
michael@0 | 50 | { |
michael@0 | 51 | if (!NS_IsMainThread()) { |
michael@0 | 52 | nsIMIMEService* mimeService; |
michael@0 | 53 | mMimeService.forget(&mimeService); |
michael@0 | 54 | |
michael@0 | 55 | if (mimeService) { |
michael@0 | 56 | nsCOMPtr<nsIThread> mainThread = do_GetMainThread(); |
michael@0 | 57 | NS_WARN_IF_FALSE(mainThread, "Couldn't get the main thread! Leaking!"); |
michael@0 | 58 | |
michael@0 | 59 | if (mainThread) { |
michael@0 | 60 | NS_ProxyRelease(mainThread, mimeService); |
michael@0 | 61 | } |
michael@0 | 62 | } |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | MOZ_COUNT_DTOR(ArchiveReaderEvent); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | // From the filename to the mimetype: |
michael@0 | 69 | nsresult |
michael@0 | 70 | ArchiveReaderEvent::GetType(nsCString& aExt, |
michael@0 | 71 | nsCString& aMimeType) |
michael@0 | 72 | { |
michael@0 | 73 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 74 | |
michael@0 | 75 | nsresult rv; |
michael@0 | 76 | |
michael@0 | 77 | if (mMimeService.get() == nullptr) { |
michael@0 | 78 | mMimeService = do_GetService(NS_MIMESERVICE_CONTRACTID, &rv); |
michael@0 | 79 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | rv = mMimeService->GetTypeFromExtension(aExt, aMimeType); |
michael@0 | 83 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 84 | |
michael@0 | 85 | return NS_OK; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | NS_IMETHODIMP |
michael@0 | 89 | ArchiveReaderEvent::Run() |
michael@0 | 90 | { |
michael@0 | 91 | return Exec(); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | nsresult |
michael@0 | 95 | ArchiveReaderEvent::RunShare(nsresult aStatus) |
michael@0 | 96 | { |
michael@0 | 97 | mStatus = aStatus; |
michael@0 | 98 | |
michael@0 | 99 | nsCOMPtr<nsIRunnable> event = NS_NewRunnableMethod(this, &ArchiveReaderEvent::ShareMainThread); |
michael@0 | 100 | NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL); |
michael@0 | 101 | |
michael@0 | 102 | return NS_OK; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | void |
michael@0 | 106 | ArchiveReaderEvent::ShareMainThread() |
michael@0 | 107 | { |
michael@0 | 108 | nsTArray<nsCOMPtr<nsIDOMFile> > fileList; |
michael@0 | 109 | |
michael@0 | 110 | if (!NS_FAILED(mStatus)) { |
michael@0 | 111 | // This extra step must run in the main thread: |
michael@0 | 112 | for (uint32_t index = 0; index < mFileList.Length(); ++index) { |
michael@0 | 113 | nsRefPtr<ArchiveItem> item = mFileList[index]; |
michael@0 | 114 | |
michael@0 | 115 | nsString tmp; |
michael@0 | 116 | nsresult rv = item->GetFilename(tmp); |
michael@0 | 117 | nsCString filename = NS_ConvertUTF16toUTF8(tmp); |
michael@0 | 118 | if (NS_FAILED(rv)) { |
michael@0 | 119 | continue; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | int32_t offset = filename.RFindChar('.'); |
michael@0 | 123 | if (offset != kNotFound) { |
michael@0 | 124 | filename.Cut(0, offset + 1); |
michael@0 | 125 | |
michael@0 | 126 | // Just to be sure, if something goes wrong, the mimetype is an empty string: |
michael@0 | 127 | nsCString type; |
michael@0 | 128 | if (NS_SUCCEEDED(GetType(filename, type))) { |
michael@0 | 129 | item->SetType(type); |
michael@0 | 130 | } |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | // This is a nsDOMFile: |
michael@0 | 134 | nsRefPtr<nsIDOMFile> file = item->File(mArchiveReader); |
michael@0 | 135 | if (file) { |
michael@0 | 136 | fileList.AppendElement(file); |
michael@0 | 137 | } |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | mArchiveReader->Ready(fileList, mStatus); |
michael@0 | 142 | } |