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 "nsCExternalHandlerService.h"
10 #include "nsProxyRelease.h"
12 USING_ARCHIVEREADER_NAMESPACE
14 NS_IMPL_ISUPPORTS0(ArchiveItem
)
16 ArchiveItem::ArchiveItem()
18 MOZ_COUNT_CTOR(ArchiveItem
);
21 ArchiveItem::~ArchiveItem()
23 MOZ_COUNT_DTOR(ArchiveItem
);
28 ArchiveItem::GetType()
30 if (mType
.IsEmpty()) {
31 return NS_LITERAL_CSTRING("binary/octet-stream");
38 ArchiveItem::SetType(const nsCString
& aType
)
43 ArchiveReaderEvent::ArchiveReaderEvent(ArchiveReader
* aArchiveReader
)
44 : mArchiveReader(aArchiveReader
)
46 MOZ_COUNT_CTOR(ArchiveReaderEvent
);
49 ArchiveReaderEvent::~ArchiveReaderEvent()
51 if (!NS_IsMainThread()) {
52 nsIMIMEService
* mimeService
;
53 mMimeService
.forget(&mimeService
);
56 nsCOMPtr
<nsIThread
> mainThread
= do_GetMainThread();
57 NS_WARN_IF_FALSE(mainThread
, "Couldn't get the main thread! Leaking!");
60 NS_ProxyRelease(mainThread
, mimeService
);
65 MOZ_COUNT_DTOR(ArchiveReaderEvent
);
68 // From the filename to the mimetype:
70 ArchiveReaderEvent::GetType(nsCString
& aExt
,
73 MOZ_ASSERT(NS_IsMainThread());
77 if (mMimeService
.get() == nullptr) {
78 mMimeService
= do_GetService(NS_MIMESERVICE_CONTRACTID
, &rv
);
79 NS_ENSURE_SUCCESS(rv
, rv
);
82 rv
= mMimeService
->GetTypeFromExtension(aExt
, aMimeType
);
83 NS_ENSURE_SUCCESS(rv
, rv
);
89 ArchiveReaderEvent::Run()
95 ArchiveReaderEvent::RunShare(nsresult aStatus
)
99 nsCOMPtr
<nsIRunnable
> event
= NS_NewRunnableMethod(this, &ArchiveReaderEvent::ShareMainThread
);
100 NS_DispatchToMainThread(event
);
106 ArchiveReaderEvent::ShareMainThread()
108 nsTArray
<nsCOMPtr
<nsIDOMFile
> > fileList
;
110 if (!NS_FAILED(mStatus
)) {
111 // This extra step must run in the main thread:
112 for (uint32_t index
= 0; index
< mFileList
.Length(); ++index
) {
113 nsRefPtr
<ArchiveItem
> item
= mFileList
[index
];
116 nsresult rv
= item
->GetFilename(tmp
);
117 nsCString filename
= NS_ConvertUTF16toUTF8(tmp
);
122 int32_t offset
= filename
.RFindChar('.');
123 if (offset
!= kNotFound
) {
124 filename
.Cut(0, offset
+ 1);
126 // Just to be sure, if something goes wrong, the mimetype is an empty string:
128 if (NS_SUCCEEDED(GetType(filename
, type
))) {
133 // This is a nsDOMFile:
134 nsRefPtr
<nsIDOMFile
> file
= item
->File(mArchiveReader
);
136 fileList
.AppendElement(file
);
141 mArchiveReader
->Ready(fileList
, mStatus
);