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"
|
2014-05-27 09:10:01 -07:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2012-07-27 17:21:34 -07:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
2014-03-17 21:48:19 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2014-05-07 07:32:12 -07:00
|
|
|
USING_ARCHIVEREADER_NAMESPACE
|
2012-07-27 17:21:34 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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*
|
2014-04-08 15:27:18 -07:00
|
|
|
ArchiveRequest::WrapObject(JSContext* aCx)
|
2013-03-17 01:51:36 -07: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-08 15:27:17 -07:00
|
|
|
return ArchiveRequestBinding::Wrap(aCx, this);
|
2013-03-17 01:51:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2014-05-27 09:10:01 -07:00
|
|
|
nsCOMPtr<nsIGlobalObject> globalObject = do_QueryInterface(GetOwner());
|
|
|
|
if (NS_WARN_IF(!globalObject)) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2012-07-27 17:21:34 -07:00
|
|
|
|
2014-05-27 09:10:01 -07:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
JSContext* cx = jsapi.cx();
|
2012-07-27 17:21:34 -07:00
|
|
|
|
2014-05-27 09:10:01 -07:00
|
|
|
JS::Rooted<JSObject*> global(cx, globalObject->GetGlobalJSObject());
|
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
|
|
|
|
2014-06-02 12:05:38 -07:00
|
|
|
case GetFiles:
|
|
|
|
rv = GetFilesResult(cx, &result, aFileList);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
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-04-09 21:58:41 -07:00
|
|
|
if (NS_FAILED(rv) ||
|
2014-04-30 02:10:33 -07:00
|
|
|
!JS_DefineElement(aCx, array, i, str, JSPROP_ENUMERATE)) {
|
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) {
|
2014-04-09 21:58:41 -07:00
|
|
|
return nsContentUtils::WrapNative(aCx, 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);
|
2014-04-09 21:58:41 -07:00
|
|
|
nsresult rv = nsContentUtils::WrapNative(aCx, file, &NS_GET_IID(nsIDOMFile),
|
2013-10-11 22:02:39 -07:00
|
|
|
&value);
|
2014-04-09 21:58:41 -07:00
|
|
|
if (NS_FAILED(rv) ||
|
2014-04-30 02:10:33 -07:00
|
|
|
!JS_DefineElement(aCx, array, i, value, JSPROP_ENUMERATE)) {
|
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();
|
|
|
|
}
|
|
|
|
|
2014-04-25 09:49:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(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)
|