Bug 891986 - Keep the source ArrayBuffer to a decodeAudioData call alive until the...
[gecko.git] / dom / file / ArchiveEvent.cpp
blob1088edcbdf89d01a167b0961fe29918e5eb71d5e
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "ArchiveEvent.h"
9 #include "nsContentUtils.h"
10 #include "nsCExternalHandlerService.h"
11 #include "nsProxyRelease.h"
13 USING_FILE_NAMESPACE
15 NS_IMPL_THREADSAFE_ISUPPORTS0(ArchiveItem)
17 ArchiveItem::ArchiveItem()
19 MOZ_COUNT_CTOR(ArchiveItem);
22 ArchiveItem::~ArchiveItem()
24 MOZ_COUNT_DTOR(ArchiveItem);
28 nsCString
29 ArchiveItem::GetType()
31 if (mType.IsEmpty()) {
32 return NS_LITERAL_CSTRING("binary/octet-stream");
35 return mType;
38 void
39 ArchiveItem::SetType(const nsCString& aType)
41 mType = aType;
44 ArchiveReaderEvent::ArchiveReaderEvent(ArchiveReader* aArchiveReader)
45 : mArchiveReader(aArchiveReader)
47 MOZ_COUNT_CTOR(ArchiveReaderEvent);
50 ArchiveReaderEvent::~ArchiveReaderEvent()
52 if (!NS_IsMainThread()) {
53 nsIMIMEService* mimeService;
54 mMimeService.forget(&mimeService);
56 if (mimeService) {
57 nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
58 NS_WARN_IF_FALSE(mainThread, "Couldn't get the main thread! Leaking!");
60 if (mainThread) {
61 NS_ProxyRelease(mainThread, mimeService);
66 MOZ_COUNT_DTOR(ArchiveReaderEvent);
69 // From the filename to the mimetype:
70 nsresult
71 ArchiveReaderEvent::GetType(nsCString& aExt,
72 nsCString& aMimeType)
74 MOZ_ASSERT(NS_IsMainThread());
76 nsresult rv;
78 if (mMimeService.get() == nullptr) {
79 mMimeService = do_GetService(NS_MIMESERVICE_CONTRACTID, &rv);
80 NS_ENSURE_SUCCESS(rv, rv);
83 rv = mMimeService->GetTypeFromExtension(aExt, aMimeType);
84 NS_ENSURE_SUCCESS(rv, rv);
86 return NS_OK;
89 NS_IMETHODIMP
90 ArchiveReaderEvent::Run()
92 return Exec();
95 nsresult
96 ArchiveReaderEvent::RunShare(nsresult aStatus)
98 mStatus = aStatus;
100 nsCOMPtr<nsIRunnable> event = NS_NewRunnableMethod(this, &ArchiveReaderEvent::ShareMainThread);
101 NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
103 return NS_OK;
106 void
107 ArchiveReaderEvent::ShareMainThread()
109 nsTArray<nsCOMPtr<nsIDOMFile> > fileList;
111 if (!NS_FAILED(mStatus)) {
112 // This extra step must run in the main thread:
113 for (uint32_t index = 0; index < mFileList.Length(); ++index) {
114 nsRefPtr<ArchiveItem> item = mFileList[index];
116 nsString tmp;
117 nsresult rv = item->GetFilename(tmp);
118 nsCString filename = NS_ConvertUTF16toUTF8(tmp);
119 if (NS_FAILED(rv)) {
120 continue;
123 int32_t offset = filename.RFindChar('.');
124 if (offset != kNotFound) {
125 filename.Cut(0, offset + 1);
127 // Just to be sure, if something goes wrong, the mimetype is an empty string:
128 nsCString type;
129 if (NS_SUCCEEDED(GetType(filename, type))) {
130 item->SetType(type);
134 // This is a nsDOMFile:
135 nsRefPtr<nsIDOMFile> file = item->File(mArchiveReader);
136 if (file) {
137 fileList.AppendElement(file);
142 mArchiveReader->Ready(fileList, mStatus);