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"
15 NS_IMPL_THREADSAFE_ISUPPORTS0(ArchiveItem
)
17 ArchiveItem::ArchiveItem()
19 MOZ_COUNT_CTOR(ArchiveItem
);
22 ArchiveItem::~ArchiveItem()
24 MOZ_COUNT_DTOR(ArchiveItem
);
29 ArchiveItem::GetType()
31 if (mType
.IsEmpty()) {
32 return NS_LITERAL_CSTRING("binary/octet-stream");
39 ArchiveItem::SetType(const nsCString
& 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
);
57 nsCOMPtr
<nsIThread
> mainThread
= do_GetMainThread();
58 NS_WARN_IF_FALSE(mainThread
, "Couldn't get the main thread! Leaking!");
61 NS_ProxyRelease(mainThread
, mimeService
);
66 MOZ_COUNT_DTOR(ArchiveReaderEvent
);
69 // From the filename to the mimetype:
71 ArchiveReaderEvent::GetType(nsCString
& aExt
,
74 MOZ_ASSERT(NS_IsMainThread());
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
);
90 ArchiveReaderEvent::Run()
96 ArchiveReaderEvent::RunShare(nsresult aStatus
)
100 nsCOMPtr
<nsIRunnable
> event
= NS_NewRunnableMethod(this, &ArchiveReaderEvent::ShareMainThread
);
101 NS_DispatchToMainThread(event
, NS_DISPATCH_NORMAL
);
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
];
117 nsresult rv
= item
->GetFilename(tmp
);
118 nsCString filename
= NS_ConvertUTF16toUTF8(tmp
);
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:
129 if (NS_SUCCEEDED(GetType(filename
, type
))) {
134 // This is a nsDOMFile:
135 nsRefPtr
<nsIDOMFile
> file
= item
->File(mArchiveReader
);
137 fileList
.AppendElement(file
);
142 mArchiveReader
->Ready(fileList
, mStatus
);