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 "ArchiveRequest.h"
|
|
|
|
|
2014-03-18 08:48:21 +04:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2013-03-17 12:51:36 +04:00
|
|
|
#include "mozilla/dom/ArchiveRequestBinding.h"
|
2014-05-27 20:10:01 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2012-07-28 04:21:34 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
2014-03-18 08:48:19 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2014-05-07 18:32:12 +04:00
|
|
|
USING_ARCHIVEREADER_NAMESPACE
|
2012-07-28 04:21:34 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class used to make asynchronous the ArchiveRequest.
|
|
|
|
*/
|
|
|
|
class ArchiveRequestEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit ArchiveRequestEvent(ArchiveRequest* aRequest)
|
|
|
|
: mRequest(aRequest)
|
2012-07-28 04:21:34 +04:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(ArchiveRequestEvent);
|
|
|
|
}
|
|
|
|
|
2014-07-15 19:37:45 +04:00
|
|
|
protected:
|
2012-07-28 04:21:34 +04:00
|
|
|
~ArchiveRequestEvent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(ArchiveRequestEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
private: //data
|
|
|
|
nsRefPtr<ArchiveRequest> mRequest;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ArchiveRequestEvent::Run()
|
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(mRequest, "the request is not longer valid");
|
2012-07-28 04:21:34 +04:00
|
|
|
mRequest->Run();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-07 03:23:13 +04:00
|
|
|
// ArchiveRequest
|
2012-07-28 04:21:34 +04:00
|
|
|
|
2014-01-07 06:53:23 +04:00
|
|
|
ArchiveRequest::ArchiveRequest(nsPIDOMWindow* aWindow,
|
2012-07-28 04:21:34 +04:00
|
|
|
ArchiveReader* aReader)
|
|
|
|
: DOMRequest(aWindow),
|
|
|
|
mArchiveReader(aReader)
|
|
|
|
{
|
2013-03-17 12:51:36 +04:00
|
|
|
MOZ_ASSERT(aReader);
|
|
|
|
|
2012-08-08 22:11:15 +04:00
|
|
|
MOZ_COUNT_CTOR(ArchiveRequest);
|
2012-07-28 04:21:34 +04:00
|
|
|
|
|
|
|
/* An event to make this request asynchronous: */
|
|
|
|
nsRefPtr<ArchiveRequestEvent> event = new ArchiveRequestEvent(this);
|
|
|
|
NS_DispatchToCurrentThread(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArchiveRequest::~ArchiveRequest()
|
|
|
|
{
|
2012-08-08 22:11:15 +04:00
|
|
|
MOZ_COUNT_DTOR(ArchiveRequest);
|
2012-07-28 04:21:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-03-18 08:48:19 +04:00
|
|
|
ArchiveRequest::PreHandleEvent(EventChainPreVisitor& aVisitor)
|
2012-07-28 04:21:34 +04:00
|
|
|
{
|
|
|
|
aVisitor.mCanHandle = true;
|
2012-07-30 18:20:58 +04:00
|
|
|
aVisitor.mParentTarget = nullptr;
|
2012-07-28 04:21:34 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-17 12:51:36 +04:00
|
|
|
/* virtual */ JSObject*
|
2014-04-09 02:27:18 +04:00
|
|
|
ArchiveRequest::WrapObject(JSContext* aCx)
|
2013-03-17 12:51:36 +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 ArchiveRequestBinding::Wrap(aCx, this);
|
2013-03-17 12:51:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ArchiveReader*
|
|
|
|
ArchiveRequest::Reader() const
|
2012-07-28 04:21:34 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2013-03-17 12:51:36 +04:00
|
|
|
return mArchiveReader;
|
2012-07-28 04:21:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Here the request is processed:
|
|
|
|
void
|
|
|
|
ArchiveRequest::Run()
|
|
|
|
{
|
|
|
|
// Register this request to the reader.
|
|
|
|
// When the reader is ready to return data, a 'Ready()' will be called
|
|
|
|
nsresult rv = mArchiveReader->RegisterRequest(this);
|
2012-11-07 03:23:13 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-08-09 01:07:39 +04:00
|
|
|
FireError(rv);
|
2012-11-07 03:23:13 +04:00
|
|
|
}
|
2012-07-28 04:21:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ArchiveRequest::OpGetFilenames()
|
|
|
|
{
|
|
|
|
mOperation = GetFilenames;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ArchiveRequest::OpGetFile(const nsAString& aFilename)
|
|
|
|
{
|
|
|
|
mOperation = GetFile;
|
|
|
|
mFilename = aFilename;
|
|
|
|
}
|
|
|
|
|
2012-11-07 03:23:13 +04:00
|
|
|
void
|
|
|
|
ArchiveRequest::OpGetFiles()
|
|
|
|
{
|
|
|
|
mOperation = GetFiles;
|
|
|
|
}
|
|
|
|
|
2012-07-28 04:21:34 +04:00
|
|
|
nsresult
|
|
|
|
ArchiveRequest::ReaderReady(nsTArray<nsCOMPtr<nsIDOMFile> >& aFileList,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
2012-08-18 05:04:03 +04:00
|
|
|
if (NS_FAILED(aStatus)) {
|
2012-08-09 01:07:39 +04:00
|
|
|
FireError(aStatus);
|
2012-07-28 04:21:34 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2014-06-19 11:21:14 +04:00
|
|
|
AutoJSAPI jsapi;
|
2014-06-25 14:17:17 +04:00
|
|
|
if (NS_WARN_IF(!jsapi.Init(GetOwner()))) {
|
2014-05-27 20:10:01 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
2012-07-28 04:21:34 +04:00
|
|
|
|
2013-05-11 06:39:45 +04:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2012-08-22 05:42:53 +04:00
|
|
|
switch (mOperation) {
|
|
|
|
case GetFilenames:
|
2013-05-11 06:39:45 +04:00
|
|
|
rv = GetFilenamesResult(cx, result.address(), aFileList);
|
2012-08-22 05:42:53 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GetFile:
|
2013-10-12 09:02:39 +04:00
|
|
|
rv = GetFileResult(cx, &result, aFileList);
|
2012-08-22 05:42:53 +04:00
|
|
|
break;
|
2012-11-07 03:23:13 +04:00
|
|
|
|
2014-06-02 23:05:38 +04:00
|
|
|
case GetFiles:
|
|
|
|
rv = GetFilesResult(cx, &result, aFileList);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
break;
|
2012-08-22 05:42:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Get*Result failed!");
|
2012-07-28 04:21:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-08-09 01:07:39 +04:00
|
|
|
FireSuccess(result);
|
2012-07-28 04:21:34 +04:00
|
|
|
}
|
|
|
|
else {
|
2012-08-09 01:07:39 +04:00
|
|
|
FireError(rv);
|
2012-07-28 04:21:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ArchiveRequest::GetFilenamesResult(JSContext* aCx,
|
2012-11-07 03:23:13 +04:00
|
|
|
JS::Value* aValue,
|
2012-07-28 04:21:34 +04:00
|
|
|
nsTArray<nsCOMPtr<nsIDOMFile> >& aFileList)
|
|
|
|
{
|
2014-02-12 14:50:46 +04:00
|
|
|
JS::Rooted<JSObject*> array(aCx, JS_NewArrayObject(aCx, aFileList.Length()));
|
2012-07-28 04:21:34 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!array) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2014-01-25 13:31:17 +04:00
|
|
|
JS::Rooted<JSString*> str(aCx);
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < aFileList.Length(); ++i) {
|
2012-07-28 04:21:34 +04:00
|
|
|
nsCOMPtr<nsIDOMFile> file = aFileList[i];
|
|
|
|
|
|
|
|
nsString filename;
|
|
|
|
rv = file->GetName(filename);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-01-25 13:31:17 +04:00
|
|
|
str = JS_NewUCStringCopyZ(aCx, filename.get());
|
2012-07-28 04:21:34 +04:00
|
|
|
NS_ENSURE_TRUE(str, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2014-04-10 08:58:41 +04:00
|
|
|
if (NS_FAILED(rv) ||
|
2014-04-30 13:10:33 +04:00
|
|
|
!JS_DefineElement(aCx, array, i, str, JSPROP_ENUMERATE)) {
|
2012-07-28 04:21:34 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_FreezeObject(aCx, array)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-11-07 03:23:13 +04:00
|
|
|
|
2012-07-28 04:21:34 +04:00
|
|
|
*aValue = OBJECT_TO_JSVAL(array);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ArchiveRequest::GetFileResult(JSContext* aCx,
|
2013-10-12 09:02:39 +04:00
|
|
|
JS::MutableHandle<JS::Value> aValue,
|
2012-07-28 04:21:34 +04:00
|
|
|
nsTArray<nsCOMPtr<nsIDOMFile> >& aFileList)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < aFileList.Length(); ++i) {
|
2012-07-28 04:21:34 +04:00
|
|
|
nsCOMPtr<nsIDOMFile> file = aFileList[i];
|
|
|
|
|
|
|
|
nsString filename;
|
|
|
|
nsresult rv = file->GetName(filename);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (filename == mFilename) {
|
2014-04-10 08:58:41 +04:00
|
|
|
return nsContentUtils::WrapNative(aCx, file, &NS_GET_IID(nsIDOMFile),
|
|
|
|
aValue);
|
2012-07-28 04:21:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-11-07 03:23:13 +04:00
|
|
|
nsresult
|
|
|
|
ArchiveRequest::GetFilesResult(JSContext* aCx,
|
2013-10-12 09:02:39 +04:00
|
|
|
JS::MutableHandle<JS::Value> aValue,
|
2012-11-07 03:23:13 +04:00
|
|
|
nsTArray<nsCOMPtr<nsIDOMFile> >& aFileList)
|
|
|
|
{
|
2014-02-12 14:50:46 +04:00
|
|
|
JS::Rooted<JSObject*> array(aCx, JS_NewArrayObject(aCx, aFileList.Length()));
|
2012-11-07 03:23:13 +04:00
|
|
|
if (!array) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2012-11-08 05:34:50 +04:00
|
|
|
for (uint32_t i = 0; i < aFileList.Length(); ++i) {
|
2012-11-07 03:23:13 +04:00
|
|
|
nsCOMPtr<nsIDOMFile> file = aFileList[i];
|
|
|
|
|
2013-05-02 13:12:47 +04:00
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
2014-04-10 08:58:41 +04:00
|
|
|
nsresult rv = nsContentUtils::WrapNative(aCx, file, &NS_GET_IID(nsIDOMFile),
|
2013-10-12 09:02:39 +04:00
|
|
|
&value);
|
2014-04-10 08:58:41 +04:00
|
|
|
if (NS_FAILED(rv) ||
|
2014-04-30 13:10:33 +04:00
|
|
|
!JS_DefineElement(aCx, array, i, value, JSPROP_ENUMERATE)) {
|
2012-11-07 03:23:13 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-12 09:02:39 +04:00
|
|
|
aValue.setObject(*array);
|
2012-11-07 03:23:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-28 04:21:34 +04:00
|
|
|
// static
|
|
|
|
already_AddRefed<ArchiveRequest>
|
2014-01-07 06:53:23 +04:00
|
|
|
ArchiveRequest::Create(nsPIDOMWindow* aOwner,
|
2012-07-28 04:21:34 +04:00
|
|
|
ArchiveReader* aReader)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsRefPtr<ArchiveRequest> request = new ArchiveRequest(aOwner, aReader);
|
|
|
|
|
|
|
|
return request.forget();
|
|
|
|
}
|
|
|
|
|
2014-04-25 20:49:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(ArchiveRequest, DOMRequest,
|
|
|
|
mArchiveReader)
|
2012-07-28 04:21:34 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ArchiveRequest)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMRequest)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(ArchiveRequest, DOMRequest)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ArchiveRequest, DOMRequest)
|