Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / dom / archivereader / ArchiveEvent.cpp
blobd4536c864dde89c0f3c06a3ad5c2aa0669dd55ab
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);
27 nsCString
28 ArchiveItem::GetType()
30 if (mType.IsEmpty()) {
31 return NS_LITERAL_CSTRING("binary/octet-stream");
34 return mType;
37 void
38 ArchiveItem::SetType(const nsCString& aType)
40 mType = 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);
55 if (mimeService) {
56 nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
57 NS_WARN_IF_FALSE(mainThread, "Couldn't get the main thread! Leaking!");
59 if (mainThread) {
60 NS_ProxyRelease(mainThread, mimeService);
65 MOZ_COUNT_DTOR(ArchiveReaderEvent);
68 // From the filename to the mimetype:
69 nsresult
70 ArchiveReaderEvent::GetType(nsCString& aExt,
71 nsCString& aMimeType)
73 MOZ_ASSERT(NS_IsMainThread());
75 nsresult rv;
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);
85 return NS_OK;
88 NS_IMETHODIMP
89 ArchiveReaderEvent::Run()
91 return Exec();
94 nsresult
95 ArchiveReaderEvent::RunShare(nsresult aStatus)
97 mStatus = aStatus;
99 nsCOMPtr<nsIRunnable> event = NS_NewRunnableMethod(this, &ArchiveReaderEvent::ShareMainThread);
100 NS_DispatchToMainThread(event);
102 return NS_OK;
105 void
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];
115 nsString tmp;
116 nsresult rv = item->GetFilename(tmp);
117 nsCString filename = NS_ConvertUTF16toUTF8(tmp);
118 if (NS_FAILED(rv)) {
119 continue;
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:
127 nsCString type;
128 if (NS_SUCCEEDED(GetType(filename, type))) {
129 item->SetType(type);
133 // This is a nsDOMFile:
134 nsRefPtr<nsIDOMFile> file = item->File(mArchiveReader);
135 if (file) {
136 fileList.AppendElement(file);
141 mArchiveReader->Ready(fileList, mStatus);