2012-07-28 04:21:34 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "ArchiveReader.h"
|
|
|
|
#include "ArchiveRequest.h"
|
|
|
|
#include "ArchiveEvent.h"
|
|
|
|
#include "ArchiveZipEvent.h"
|
|
|
|
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
2013-03-10 11:57:14 +04:00
|
|
|
#include "mozilla/dom/ArchiveReaderBinding.h"
|
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2014-10-07 21:16:11 +04:00
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-11-08 19:57:17 +04:00
|
|
|
|
2013-03-10 11:57:14 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
2014-05-07 18:32:12 +04:00
|
|
|
USING_ARCHIVEREADER_NAMESPACE
|
2012-07-28 04:21:34 +04:00
|
|
|
|
2013-03-10 11:57:14 +04:00
|
|
|
/* static */ already_AddRefed<ArchiveReader>
|
2013-08-23 09:17:08 +04:00
|
|
|
ArchiveReader::Constructor(const GlobalObject& aGlobal,
|
2014-10-08 20:15:23 +04:00
|
|
|
File& aBlob,
|
2013-03-10 11:57:14 +04:00
|
|
|
const ArchiveReaderOptions& aOptions,
|
|
|
|
ErrorResult& aError)
|
2012-07-28 04:21:34 +04:00
|
|
|
{
|
2013-08-23 09:17:08 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
|
2013-03-10 11:57:14 +04:00
|
|
|
if (!window) {
|
|
|
|
aError.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-12-17 14:47:25 +04:00
|
|
|
nsAutoCString encoding;
|
2014-05-08 13:32:00 +04:00
|
|
|
if (!EncodingUtils::FindEncodingForLabelNoReplacement(aOptions.mEncoding,
|
|
|
|
encoding)) {
|
2013-12-17 14:47:25 +04:00
|
|
|
aError.ThrowTypeError(MSG_ENCODING_NOT_SUPPORTED, &aOptions.mEncoding);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-03-10 11:57:14 +04:00
|
|
|
nsRefPtr<ArchiveReader> reader =
|
2013-12-17 14:47:25 +04:00
|
|
|
new ArchiveReader(aBlob, window, encoding);
|
2013-03-10 11:57:14 +04:00
|
|
|
return reader.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:23 +04:00
|
|
|
ArchiveReader::ArchiveReader(File& aBlob, nsPIDOMWindow* aWindow,
|
2013-12-17 14:47:25 +04:00
|
|
|
const nsACString& aEncoding)
|
2014-10-08 20:15:22 +04:00
|
|
|
: mBlob(&aBlob)
|
2013-03-10 11:57:14 +04:00
|
|
|
, mWindow(aWindow)
|
|
|
|
, mStatus(NOT_STARTED)
|
|
|
|
, mEncoding(aEncoding)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWindow);
|
2012-07-28 04:21:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ArchiveReader::~ArchiveReader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-03-10 11:57:14 +04:00
|
|
|
/* virtual */ JSObject*
|
2014-04-09 02:27:18 +04:00
|
|
|
ArchiveReader::WrapObject(JSContext* aCx)
|
2012-11-08 19:57:17 +04:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-09 02:27:17 +04:00
|
|
|
return ArchiveReaderBinding::Wrap(aCx, this);
|
2012-11-08 19:57:17 +04:00
|
|
|
}
|
|
|
|
|
2012-07-28 04:21:34 +04:00
|
|
|
nsresult
|
|
|
|
ArchiveReader::RegisterRequest(ArchiveRequest* aRequest)
|
|
|
|
{
|
|
|
|
switch (mStatus) {
|
|
|
|
// Append to the list and let's start to work:
|
|
|
|
case NOT_STARTED:
|
|
|
|
mRequests.AppendElement(aRequest);
|
|
|
|
return OpenArchive();
|
|
|
|
|
|
|
|
// Just append to the list:
|
|
|
|
case WORKING:
|
|
|
|
mRequests.AppendElement(aRequest);
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Return data!
|
|
|
|
case READY:
|
|
|
|
RequestReady(aRequest);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(false, "unexpected mStatus value");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This returns the input stream
|
|
|
|
nsresult
|
|
|
|
ArchiveReader::GetInputStream(nsIInputStream** aInputStream)
|
|
|
|
{
|
|
|
|
// Getting the input stream
|
|
|
|
mBlob->GetInternalStream(aInputStream);
|
|
|
|
NS_ENSURE_TRUE(*aInputStream, NS_ERROR_UNEXPECTED);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
ArchiveReader::GetSize(uint64_t* aSize)
|
2012-07-28 04:21:34 +04:00
|
|
|
{
|
|
|
|
nsresult rv = mBlob->GetSize(aSize);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here we open the archive:
|
|
|
|
nsresult
|
|
|
|
ArchiveReader::OpenArchive()
|
|
|
|
{
|
|
|
|
mStatus = WORKING;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Target:
|
|
|
|
nsCOMPtr<nsIEventTarget> target = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
|
|
|
|
NS_ASSERTION(target, "Must have stream transport service");
|
|
|
|
|
|
|
|
// Here a Event to make everything async:
|
|
|
|
nsRefPtr<ArchiveReaderEvent> event;
|
|
|
|
|
|
|
|
/* FIXME: If we want to support more than 1 format we should check the content type here: */
|
2013-03-10 11:54:42 +04:00
|
|
|
event = new ArchiveReaderZipEvent(this, mEncoding);
|
2012-07-28 04:21:34 +04:00
|
|
|
rv = target->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// In order to be sure that this object exists when the event finishes its task,
|
|
|
|
// we increase the refcount here:
|
|
|
|
AddRef();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data received from the dispatched event:
|
|
|
|
void
|
|
|
|
ArchiveReader::Ready(nsTArray<nsCOMPtr<nsIDOMFile> >& aFileList,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
|
|
|
mStatus = READY;
|
|
|
|
|
|
|
|
// Let's store the values:
|
|
|
|
mData.fileList = aFileList;
|
|
|
|
mData.status = aStatus;
|
|
|
|
|
|
|
|
// Propagate the results:
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < mRequests.Length(); ++index) {
|
2012-07-28 04:21:34 +04:00
|
|
|
nsRefPtr<ArchiveRequest> request = mRequests[index];
|
|
|
|
RequestReady(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
mRequests.Clear();
|
|
|
|
|
|
|
|
// The async operation is concluded, we can decrease the reference:
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ArchiveReader::RequestReady(ArchiveRequest* aRequest)
|
|
|
|
{
|
|
|
|
// The request will do the rest:
|
|
|
|
aRequest->ReaderReady(mData.fileList, mData.status);
|
|
|
|
}
|
|
|
|
|
2013-03-17 12:51:36 +04:00
|
|
|
already_AddRefed<ArchiveRequest>
|
2013-03-10 11:57:14 +04:00
|
|
|
ArchiveReader::GetFilenames()
|
2012-07-28 04:21:34 +04:00
|
|
|
{
|
|
|
|
nsRefPtr<ArchiveRequest> request = GenerateArchiveRequest();
|
|
|
|
request->OpGetFilenames();
|
|
|
|
|
2013-03-10 11:57:14 +04:00
|
|
|
return request.forget();
|
2012-07-28 04:21:34 +04:00
|
|
|
}
|
|
|
|
|
2013-03-17 12:51:36 +04:00
|
|
|
already_AddRefed<ArchiveRequest>
|
2013-03-10 11:57:14 +04:00
|
|
|
ArchiveReader::GetFile(const nsAString& filename)
|
2012-07-28 04:21:34 +04:00
|
|
|
{
|
|
|
|
nsRefPtr<ArchiveRequest> request = GenerateArchiveRequest();
|
|
|
|
request->OpGetFile(filename);
|
|
|
|
|
2013-03-10 11:57:14 +04:00
|
|
|
return request.forget();
|
2012-07-28 04:21:34 +04:00
|
|
|
}
|
|
|
|
|
2013-03-17 12:51:36 +04:00
|
|
|
already_AddRefed<ArchiveRequest>
|
2013-03-10 11:57:14 +04:00
|
|
|
ArchiveReader::GetFiles()
|
2012-11-07 03:23:13 +04:00
|
|
|
{
|
|
|
|
nsRefPtr<ArchiveRequest> request = GenerateArchiveRequest();
|
|
|
|
request->OpGetFiles();
|
|
|
|
|
2013-03-10 11:57:14 +04:00
|
|
|
return request.forget();
|
2012-11-07 03:23:13 +04:00
|
|
|
}
|
|
|
|
|
2012-07-28 04:21:34 +04:00
|
|
|
already_AddRefed<ArchiveRequest>
|
|
|
|
ArchiveReader::GenerateArchiveRequest()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
return ArchiveRequest::Create(mWindow, this);
|
|
|
|
}
|
|
|
|
|
2014-04-29 12:57:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(ArchiveReader,
|
|
|
|
mBlob,
|
|
|
|
mWindow,
|
|
|
|
mData.fileList,
|
|
|
|
mRequests)
|
2012-07-28 04:21:34 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ArchiveReader)
|
2013-03-10 11:57:14 +04:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2012-07-28 04:21:34 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(ArchiveReader)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(ArchiveReader)
|