2012-07-27 17:21:34 -07: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-17 21:48:21 -07:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2013-03-17 01:51:36 -07:00
|
|
|
#include "mozilla/dom/ArchiveRequestBinding.h"
|
2012-07-27 17:21:34 -07:00
|
|
|
#include "nsContentUtils.h"
|
2013-05-22 09:05:26 -07:00
|
|
|
#include "nsCxPusher.h"
|
2012-07-27 17:21:34 -07:00
|
|
|
|
2014-03-17 21:48:19 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2012-07-27 17:21:34 -07:00
|
|
|
USING_FILE_NAMESPACE
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class used to make asynchronous the ArchiveRequest.
|
|
|
|
*/
|
|
|
|
class ArchiveRequestEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
|
|
|
ArchiveRequestEvent(ArchiveRequest* request)
|
|
|
|
: mRequest(request)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(ArchiveRequestEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
~ArchiveRequestEvent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(ArchiveRequestEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
private: //data
|
|
|
|
nsRefPtr<ArchiveRequest> mRequest;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ArchiveRequestEvent::Run()
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(mRequest, "the request is not longer valid");
|
|
|
|
mRequest->Run();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-06 15:23:13 -08:00
|
|
|
// ArchiveRequest
|
2012-07-27 17:21:34 -07:00
|
|
|
|
2014-01-06 18:53:23 -08:00
|
|
|
ArchiveRequest::ArchiveRequest(nsPIDOMWindow* aWindow,
|
2012-07-27 17:21:34 -07:00
|
|
|
ArchiveReader* aReader)
|
|
|
|
: DOMRequest(aWindow),
|
|
|
|
mArchiveReader(aReader)
|
|
|
|
{
|
2013-03-17 01:51:36 -07:00
|
|
|
MOZ_ASSERT(aReader);
|
|
|
|
|
2012-08-08 11:11:15 -07:00
|
|
|
MOZ_COUNT_CTOR(ArchiveRequest);
|
2012-07-27 17:21:34 -07:00
|
|
|
|
|
|
|
/* An event to make this request asynchronous: */
|
|
|
|
nsRefPtr<ArchiveRequestEvent> event = new ArchiveRequestEvent(this);
|
|
|
|
NS_DispatchToCurrentThread(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArchiveRequest::~ArchiveRequest()
|
|
|
|
{
|
2012-08-08 11:11:15 -07:00
|
|
|
MOZ_COUNT_DTOR(ArchiveRequest);
|
2012-07-27 17:21:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-03-17 21:48:19 -07:00
|
|
|
ArchiveRequest::PreHandleEvent(EventChainPreVisitor& aVisitor)
|
2012-07-27 17:21:34 -07:00
|
|
|
{
|
|
|
|
aVisitor.mCanHandle = true;
|
2012-07-30 07:20:58 -07:00
|
|
|
aVisitor.mParentTarget = nullptr;
|
2012-07-27 17:21:34 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-17 01:51:36 -07:00
|
|
|
/* virtual */ JSObject*
|
2013-04-25 09:29:54 -07:00
|
|
|
ArchiveRequest::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
2013-03-17 01:51:36 -07:00
|
|
|
{
|
|
|
|
return ArchiveRequestBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArchiveReader*
|
|
|
|
ArchiveRequest::Reader() const
|
2012-07-27 17:21:34 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2013-03-17 01:51:36 -07:00
|
|
|
return mArchiveReader;
|
2012-07-27 17:21:34 -07: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-06 15:23:13 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-08-08 14:07:39 -07:00
|
|
|
FireError(rv);
|
2012-11-06 15:23:13 -08:00
|
|
|
}
|
2012-07-27 17:21:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ArchiveRequest::OpGetFilenames()
|
|
|
|
{
|
|
|
|
mOperation = GetFilenames;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ArchiveRequest::OpGetFile(const nsAString& aFilename)
|
|
|
|
{
|
|
|
|
mOperation = GetFile;
|
|
|
|
mFilename = aFilename;
|
|
|
|
}
|
|
|
|
|
2012-11-06 15:23:13 -08:00
|
|
|
void
|
|
|
|
ArchiveRequest::OpGetFiles()
|
|
|
|
{
|
|
|
|
mOperation = GetFiles;
|
|
|
|
}
|
|
|
|
|
2012-07-27 17:21:34 -07:00
|
|
|
nsresult
|
|
|
|
ArchiveRequest::ReaderReady(nsTArray<nsCOMPtr<nsIDOMFile> >& aFileList,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
2012-08-17 18:04:03 -07:00
|
|
|
if (NS_FAILED(aStatus)) {
|
2012-08-08 14:07:39 -07:00
|
|
|
FireError(aStatus);
|
2012-07-27 17:21:34 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
|
|
|
|
NS_ENSURE_STATE(sc);
|
|
|
|
|
2013-02-26 11:04:13 -08:00
|
|
|
AutoPushJSContext cx(sc->GetNativeContext());
|
2012-07-27 17:21:34 -07:00
|
|
|
NS_ASSERTION(cx, "Failed to get a context!");
|
|
|
|
|
2013-09-04 14:06:57 -07:00
|
|
|
JS::Rooted<JSObject*> global(cx, sc->GetWindowProxy());
|
2012-07-27 17:21:34 -07:00
|
|
|
NS_ASSERTION(global, "Failed to get global object!");
|
|
|
|
|
2012-08-21 18:42:53 -07:00
|
|
|
JSAutoCompartment ac(cx, global);
|
|
|
|
|
2013-05-10 19:39:45 -07:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2012-08-21 18:42:53 -07:00
|
|
|
switch (mOperation) {
|
|
|
|
case GetFilenames:
|
2013-05-10 19:39:45 -07:00
|
|
|
rv = GetFilenamesResult(cx, result.address(), aFileList);
|
2012-08-21 18:42:53 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GetFile:
|
2013-10-11 22:02:39 -07:00
|
|
|
rv = GetFileResult(cx, &result, aFileList);
|
2012-08-21 18:42:53 -07:00
|
|
|
break;
|
2012-11-06 15:23:13 -08:00
|
|
|
|
|
|
|
case GetFiles:
|
2013-10-11 22:02:39 -07:00
|
|
|
rv = GetFilesResult(cx, &result, aFileList);
|
2012-11-06 15:23:13 -08:00
|
|
|
break;
|
2012-08-21 18:42:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Get*Result failed!");
|
2012-07-27 17:21:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-08-08 14:07:39 -07:00
|
|
|
FireSuccess(result);
|
2012-07-27 17:21:34 -07:00
|
|
|
}
|
|
|
|
else {
|
2012-08-08 14:07:39 -07:00
|
|
|
FireError(rv);
|
2012-07-27 17:21:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ArchiveRequest::GetFilenamesResult(JSContext* aCx,
|
2012-11-06 15:23:13 -08:00
|
|
|
JS::Value* aValue,
|
2012-07-27 17:21:34 -07:00
|
|
|
nsTArray<nsCOMPtr<nsIDOMFile> >& aFileList)
|
|
|
|
{
|
2014-02-12 02:50:46 -08:00
|
|
|
JS::Rooted<JSObject*> array(aCx, JS_NewArrayObject(aCx, aFileList.Length()));
|
2012-07-27 17:21:34 -07:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!array) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2014-01-25 01:31:17 -08:00
|
|
|
JS::Rooted<JSString*> str(aCx);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aFileList.Length(); ++i) {
|
2012-07-27 17:21:34 -07:00
|
|
|
nsCOMPtr<nsIDOMFile> file = aFileList[i];
|
|
|
|
|
|
|
|
nsString filename;
|
|
|
|
rv = file->GetName(filename);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-01-25 01:31:17 -08:00
|
|
|
str = JS_NewUCStringCopyZ(aCx, filename.get());
|
2012-07-27 17:21:34 -07:00
|
|
|
NS_ENSURE_TRUE(str, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2014-01-25 01:31:17 -08:00
|
|
|
if (NS_FAILED(rv) || !JS_SetElement(aCx, array, i, str)) {
|
2012-07-27 17:21:34 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_FreezeObject(aCx, array)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-11-06 15:23:13 -08:00
|
|
|
|
2012-07-27 17:21:34 -07:00
|
|
|
*aValue = OBJECT_TO_JSVAL(array);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ArchiveRequest::GetFileResult(JSContext* aCx,
|
2013-10-11 22:02:39 -07:00
|
|
|
JS::MutableHandle<JS::Value> aValue,
|
2012-07-27 17:21:34 -07:00
|
|
|
nsTArray<nsCOMPtr<nsIDOMFile> >& aFileList)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aFileList.Length(); ++i) {
|
2012-07-27 17:21:34 -07:00
|
|
|
nsCOMPtr<nsIDOMFile> file = aFileList[i];
|
|
|
|
|
|
|
|
nsString filename;
|
|
|
|
nsresult rv = file->GetName(filename);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (filename == mFilename) {
|
2013-07-29 16:45:27 -07:00
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
2013-05-02 02:12:46 -07:00
|
|
|
return nsContentUtils::WrapNative(aCx, global, file,
|
|
|
|
&NS_GET_IID(nsIDOMFile), aValue);
|
2012-07-27 17:21:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-11-06 15:23:13 -08:00
|
|
|
nsresult
|
|
|
|
ArchiveRequest::GetFilesResult(JSContext* aCx,
|
2013-10-11 22:02:39 -07:00
|
|
|
JS::MutableHandle<JS::Value> aValue,
|
2012-11-06 15:23:13 -08:00
|
|
|
nsTArray<nsCOMPtr<nsIDOMFile> >& aFileList)
|
|
|
|
{
|
2014-02-12 02:50:46 -08:00
|
|
|
JS::Rooted<JSObject*> array(aCx, JS_NewArrayObject(aCx, aFileList.Length()));
|
2012-11-06 15:23:13 -08:00
|
|
|
if (!array) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2012-11-07 17:34:50 -08:00
|
|
|
for (uint32_t i = 0; i < aFileList.Length(); ++i) {
|
2012-11-06 15:23:13 -08:00
|
|
|
nsCOMPtr<nsIDOMFile> file = aFileList[i];
|
|
|
|
|
2013-05-02 02:12:47 -07:00
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
2013-07-29 16:45:27 -07:00
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
2013-05-02 02:12:46 -07:00
|
|
|
nsresult rv = nsContentUtils::WrapNative(aCx, global, file,
|
2013-05-02 02:12:47 -07:00
|
|
|
&NS_GET_IID(nsIDOMFile),
|
2013-10-11 22:02:39 -07:00
|
|
|
&value);
|
2014-01-25 01:31:17 -08:00
|
|
|
if (NS_FAILED(rv) || !JS_SetElement(aCx, array, i, value)) {
|
2012-11-06 15:23:13 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-11 22:02:39 -07:00
|
|
|
aValue.setObject(*array);
|
2012-11-06 15:23:13 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-27 17:21:34 -07:00
|
|
|
// static
|
|
|
|
already_AddRefed<ArchiveRequest>
|
2014-01-06 18:53:23 -08:00
|
|
|
ArchiveRequest::Create(nsPIDOMWindow* aOwner,
|
2012-07-27 17:21:34 -07:00
|
|
|
ArchiveReader* aReader)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsRefPtr<ArchiveRequest> request = new ArchiveRequest(aOwner, aReader);
|
|
|
|
|
|
|
|
return request.forget();
|
|
|
|
}
|
|
|
|
|
2012-12-02 00:53:35 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_1(ArchiveRequest, DOMRequest,
|
|
|
|
mArchiveReader)
|
2012-07-27 17:21:34 -07: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)
|