2012-07-28 04:21:34 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-05-03 22:32:37 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-07-28 04:21:34 +04:00
|
|
|
/* 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"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsNetCID.h"
|
2012-07-28 04:21:34 +04:00
|
|
|
|
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,
|
2015-05-12 15:09:51 +03:00
|
|
|
Blob& aBlob,
|
2013-03-10 11:57:14 +04:00
|
|
|
const ArchiveReaderOptions& aOptions,
|
|
|
|
ErrorResult& aError)
|
2012-07-28 04:21:34 +04:00
|
|
|
{
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> 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)) {
|
2015-11-20 21:36:46 +03:00
|
|
|
aError.ThrowRangeError<MSG_ENCODING_NOT_SUPPORTED>(aOptions.mEncoding);
|
2013-12-17 14:47:25 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<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();
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
ArchiveReader::ArchiveReader(Blob& aBlob, nsPIDOMWindowInner* aWindow,
|
2013-12-17 14:47:25 +04:00
|
|
|
const nsACString& aEncoding)
|
2015-05-12 15:11:03 +03:00
|
|
|
: mBlobImpl(aBlob.Impl())
|
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*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
ArchiveReader::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2012-11-08 19:57:17 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return ArchiveReaderBinding::Wrap(aCx, this, aGivenProto);
|
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
|
2015-05-19 17:36:37 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
mBlobImpl->GetInternalStream(aInputStream, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
|
|
|
|
2012-07-28 04:21:34 +04:00
|
|
|
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
|
|
|
{
|
2015-01-12 00:34:57 +03:00
|
|
|
ErrorResult rv;
|
2015-05-12 15:11:03 +03:00
|
|
|
*aSize = mBlobImpl->GetSize(rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2012-07-28 04:21:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ArchiveReaderEvent> event;
|
2012-07-28 04:21:34 +04:00
|
|
|
|
|
|
|
/* 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
|
2015-10-18 08:24:48 +03:00
|
|
|
ArchiveReader::Ready(nsTArray<RefPtr<File>>& aFileList,
|
2012-07-28 04:21:34 +04:00
|
|
|
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) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ArchiveRequest> request = mRequests[index];
|
2012-07-28 04:21:34 +04:00
|
|
|
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
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ArchiveRequest> request = GenerateArchiveRequest();
|
2012-07-28 04:21:34 +04:00
|
|
|
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
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ArchiveRequest> request = GenerateArchiveRequest();
|
2012-07-28 04:21:34 +04:00
|
|
|
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
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ArchiveRequest> request = GenerateArchiveRequest();
|
2012-11-07 03:23:13 +04:00
|
|
|
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,
|
2015-05-12 15:11:03 +03:00
|
|
|
mBlobImpl,
|
2014-04-29 12:57:00 +04:00
|
|
|
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)
|