dom/file/ArchiveReader.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "ArchiveReader.h"
michael@0 8 #include "ArchiveRequest.h"
michael@0 9 #include "ArchiveEvent.h"
michael@0 10 #include "ArchiveZipEvent.h"
michael@0 11
michael@0 12 #include "nsIURI.h"
michael@0 13 #include "nsNetUtil.h"
michael@0 14
michael@0 15 #include "mozilla/dom/ArchiveReaderBinding.h"
michael@0 16 #include "mozilla/dom/BindingDeclarations.h"
michael@0 17 #include "mozilla/Preferences.h"
michael@0 18 #include "mozilla/dom/EncodingUtils.h"
michael@0 19
michael@0 20 using namespace mozilla;
michael@0 21 using namespace mozilla::dom;
michael@0 22 USING_FILE_NAMESPACE
michael@0 23
michael@0 24 /* static */ already_AddRefed<ArchiveReader>
michael@0 25 ArchiveReader::Constructor(const GlobalObject& aGlobal,
michael@0 26 nsIDOMBlob* aBlob,
michael@0 27 const ArchiveReaderOptions& aOptions,
michael@0 28 ErrorResult& aError)
michael@0 29 {
michael@0 30 MOZ_ASSERT(aBlob);
michael@0 31
michael@0 32 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
michael@0 33 if (!window) {
michael@0 34 aError.Throw(NS_ERROR_UNEXPECTED);
michael@0 35 return nullptr;
michael@0 36 }
michael@0 37
michael@0 38 nsAutoCString encoding;
michael@0 39 if (!EncodingUtils::FindEncodingForLabel(aOptions.mEncoding, encoding) ||
michael@0 40 encoding.EqualsLiteral("replacement")) {
michael@0 41 aError.ThrowTypeError(MSG_ENCODING_NOT_SUPPORTED, &aOptions.mEncoding);
michael@0 42 return nullptr;
michael@0 43 }
michael@0 44
michael@0 45 nsRefPtr<ArchiveReader> reader =
michael@0 46 new ArchiveReader(aBlob, window, encoding);
michael@0 47 return reader.forget();
michael@0 48 }
michael@0 49
michael@0 50 ArchiveReader::ArchiveReader(nsIDOMBlob* aBlob, nsPIDOMWindow* aWindow,
michael@0 51 const nsACString& aEncoding)
michael@0 52 : mBlob(aBlob)
michael@0 53 , mWindow(aWindow)
michael@0 54 , mStatus(NOT_STARTED)
michael@0 55 , mEncoding(aEncoding)
michael@0 56 {
michael@0 57 MOZ_ASSERT(aBlob);
michael@0 58 MOZ_ASSERT(aWindow);
michael@0 59
michael@0 60 SetIsDOMBinding();
michael@0 61 }
michael@0 62
michael@0 63 ArchiveReader::~ArchiveReader()
michael@0 64 {
michael@0 65 }
michael@0 66
michael@0 67 /* virtual */ JSObject*
michael@0 68 ArchiveReader::WrapObject(JSContext* aCx)
michael@0 69 {
michael@0 70 return ArchiveReaderBinding::Wrap(aCx, this);
michael@0 71 }
michael@0 72
michael@0 73 nsresult
michael@0 74 ArchiveReader::RegisterRequest(ArchiveRequest* aRequest)
michael@0 75 {
michael@0 76 switch (mStatus) {
michael@0 77 // Append to the list and let's start to work:
michael@0 78 case NOT_STARTED:
michael@0 79 mRequests.AppendElement(aRequest);
michael@0 80 return OpenArchive();
michael@0 81
michael@0 82 // Just append to the list:
michael@0 83 case WORKING:
michael@0 84 mRequests.AppendElement(aRequest);
michael@0 85 return NS_OK;
michael@0 86
michael@0 87 // Return data!
michael@0 88 case READY:
michael@0 89 RequestReady(aRequest);
michael@0 90 return NS_OK;
michael@0 91 }
michael@0 92
michael@0 93 NS_ASSERTION(false, "unexpected mStatus value");
michael@0 94 return NS_OK;
michael@0 95 }
michael@0 96
michael@0 97 // This returns the input stream
michael@0 98 nsresult
michael@0 99 ArchiveReader::GetInputStream(nsIInputStream** aInputStream)
michael@0 100 {
michael@0 101 // Getting the input stream
michael@0 102 mBlob->GetInternalStream(aInputStream);
michael@0 103 NS_ENSURE_TRUE(*aInputStream, NS_ERROR_UNEXPECTED);
michael@0 104 return NS_OK;
michael@0 105 }
michael@0 106
michael@0 107 nsresult
michael@0 108 ArchiveReader::GetSize(uint64_t* aSize)
michael@0 109 {
michael@0 110 nsresult rv = mBlob->GetSize(aSize);
michael@0 111 NS_ENSURE_SUCCESS(rv, rv);
michael@0 112 return NS_OK;
michael@0 113 }
michael@0 114
michael@0 115 // Here we open the archive:
michael@0 116 nsresult
michael@0 117 ArchiveReader::OpenArchive()
michael@0 118 {
michael@0 119 mStatus = WORKING;
michael@0 120 nsresult rv;
michael@0 121
michael@0 122 // Target:
michael@0 123 nsCOMPtr<nsIEventTarget> target = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
michael@0 124 NS_ASSERTION(target, "Must have stream transport service");
michael@0 125
michael@0 126 // Here a Event to make everything async:
michael@0 127 nsRefPtr<ArchiveReaderEvent> event;
michael@0 128
michael@0 129 /* FIXME: If we want to support more than 1 format we should check the content type here: */
michael@0 130 event = new ArchiveReaderZipEvent(this, mEncoding);
michael@0 131 rv = target->Dispatch(event, NS_DISPATCH_NORMAL);
michael@0 132 NS_ENSURE_SUCCESS(rv, rv);
michael@0 133
michael@0 134 // In order to be sure that this object exists when the event finishes its task,
michael@0 135 // we increase the refcount here:
michael@0 136 AddRef();
michael@0 137
michael@0 138 return NS_OK;
michael@0 139 }
michael@0 140
michael@0 141 // Data received from the dispatched event:
michael@0 142 void
michael@0 143 ArchiveReader::Ready(nsTArray<nsCOMPtr<nsIDOMFile> >& aFileList,
michael@0 144 nsresult aStatus)
michael@0 145 {
michael@0 146 mStatus = READY;
michael@0 147
michael@0 148 // Let's store the values:
michael@0 149 mData.fileList = aFileList;
michael@0 150 mData.status = aStatus;
michael@0 151
michael@0 152 // Propagate the results:
michael@0 153 for (uint32_t index = 0; index < mRequests.Length(); ++index) {
michael@0 154 nsRefPtr<ArchiveRequest> request = mRequests[index];
michael@0 155 RequestReady(request);
michael@0 156 }
michael@0 157
michael@0 158 mRequests.Clear();
michael@0 159
michael@0 160 // The async operation is concluded, we can decrease the reference:
michael@0 161 Release();
michael@0 162 }
michael@0 163
michael@0 164 void
michael@0 165 ArchiveReader::RequestReady(ArchiveRequest* aRequest)
michael@0 166 {
michael@0 167 // The request will do the rest:
michael@0 168 aRequest->ReaderReady(mData.fileList, mData.status);
michael@0 169 }
michael@0 170
michael@0 171 already_AddRefed<ArchiveRequest>
michael@0 172 ArchiveReader::GetFilenames()
michael@0 173 {
michael@0 174 nsRefPtr<ArchiveRequest> request = GenerateArchiveRequest();
michael@0 175 request->OpGetFilenames();
michael@0 176
michael@0 177 return request.forget();
michael@0 178 }
michael@0 179
michael@0 180 already_AddRefed<ArchiveRequest>
michael@0 181 ArchiveReader::GetFile(const nsAString& filename)
michael@0 182 {
michael@0 183 nsRefPtr<ArchiveRequest> request = GenerateArchiveRequest();
michael@0 184 request->OpGetFile(filename);
michael@0 185
michael@0 186 return request.forget();
michael@0 187 }
michael@0 188
michael@0 189 already_AddRefed<ArchiveRequest>
michael@0 190 ArchiveReader::GetFiles()
michael@0 191 {
michael@0 192 nsRefPtr<ArchiveRequest> request = GenerateArchiveRequest();
michael@0 193 request->OpGetFiles();
michael@0 194
michael@0 195 return request.forget();
michael@0 196 }
michael@0 197
michael@0 198 already_AddRefed<ArchiveRequest>
michael@0 199 ArchiveReader::GenerateArchiveRequest()
michael@0 200 {
michael@0 201 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
michael@0 202 return ArchiveRequest::Create(mWindow, this);
michael@0 203 }
michael@0 204
michael@0 205 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_4(ArchiveReader,
michael@0 206 mBlob,
michael@0 207 mWindow,
michael@0 208 mData.fileList,
michael@0 209 mRequests)
michael@0 210
michael@0 211 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ArchiveReader)
michael@0 212 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
michael@0 213 NS_INTERFACE_MAP_ENTRY(nsISupports)
michael@0 214 NS_INTERFACE_MAP_END
michael@0 215
michael@0 216 NS_IMPL_CYCLE_COLLECTING_ADDREF(ArchiveReader)
michael@0 217 NS_IMPL_CYCLE_COLLECTING_RELEASE(ArchiveReader)

mercurial