dom/file/ArchiveEvent.cpp

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:346937ed2d8b
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/. */
6
7 #include "ArchiveEvent.h"
8
9 #include "nsCExternalHandlerService.h"
10 #include "nsProxyRelease.h"
11
12 USING_FILE_NAMESPACE
13
14 NS_IMPL_ISUPPORTS0(ArchiveItem)
15
16 ArchiveItem::ArchiveItem()
17 {
18 MOZ_COUNT_CTOR(ArchiveItem);
19 }
20
21 ArchiveItem::~ArchiveItem()
22 {
23 MOZ_COUNT_DTOR(ArchiveItem);
24 }
25
26
27 nsCString
28 ArchiveItem::GetType()
29 {
30 if (mType.IsEmpty()) {
31 return NS_LITERAL_CSTRING("binary/octet-stream");
32 }
33
34 return mType;
35 }
36
37 void
38 ArchiveItem::SetType(const nsCString& aType)
39 {
40 mType = aType;
41 }
42
43 ArchiveReaderEvent::ArchiveReaderEvent(ArchiveReader* aArchiveReader)
44 : mArchiveReader(aArchiveReader)
45 {
46 MOZ_COUNT_CTOR(ArchiveReaderEvent);
47 }
48
49 ArchiveReaderEvent::~ArchiveReaderEvent()
50 {
51 if (!NS_IsMainThread()) {
52 nsIMIMEService* mimeService;
53 mMimeService.forget(&mimeService);
54
55 if (mimeService) {
56 nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
57 NS_WARN_IF_FALSE(mainThread, "Couldn't get the main thread! Leaking!");
58
59 if (mainThread) {
60 NS_ProxyRelease(mainThread, mimeService);
61 }
62 }
63 }
64
65 MOZ_COUNT_DTOR(ArchiveReaderEvent);
66 }
67
68 // From the filename to the mimetype:
69 nsresult
70 ArchiveReaderEvent::GetType(nsCString& aExt,
71 nsCString& aMimeType)
72 {
73 MOZ_ASSERT(NS_IsMainThread());
74
75 nsresult rv;
76
77 if (mMimeService.get() == nullptr) {
78 mMimeService = do_GetService(NS_MIMESERVICE_CONTRACTID, &rv);
79 NS_ENSURE_SUCCESS(rv, rv);
80 }
81
82 rv = mMimeService->GetTypeFromExtension(aExt, aMimeType);
83 NS_ENSURE_SUCCESS(rv, rv);
84
85 return NS_OK;
86 }
87
88 NS_IMETHODIMP
89 ArchiveReaderEvent::Run()
90 {
91 return Exec();
92 }
93
94 nsresult
95 ArchiveReaderEvent::RunShare(nsresult aStatus)
96 {
97 mStatus = aStatus;
98
99 nsCOMPtr<nsIRunnable> event = NS_NewRunnableMethod(this, &ArchiveReaderEvent::ShareMainThread);
100 NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
101
102 return NS_OK;
103 }
104
105 void
106 ArchiveReaderEvent::ShareMainThread()
107 {
108 nsTArray<nsCOMPtr<nsIDOMFile> > fileList;
109
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];
114
115 nsString tmp;
116 nsresult rv = item->GetFilename(tmp);
117 nsCString filename = NS_ConvertUTF16toUTF8(tmp);
118 if (NS_FAILED(rv)) {
119 continue;
120 }
121
122 int32_t offset = filename.RFindChar('.');
123 if (offset != kNotFound) {
124 filename.Cut(0, offset + 1);
125
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);
130 }
131 }
132
133 // This is a nsDOMFile:
134 nsRefPtr<nsIDOMFile> file = item->File(mArchiveReader);
135 if (file) {
136 fileList.AppendElement(file);
137 }
138 }
139 }
140
141 mArchiveReader->Ready(fileList, mStatus);
142 }

mercurial