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 "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*
|
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
|
|
|
ArchiveRequest::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-03-17 12:51:36 +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 ArchiveRequestBinding::Wrap(aCx, this, aGivenProto);
|
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
|
2015-05-12 15:09:51 +03:00
|
|
|
ArchiveRequest::ReaderReady(nsTArray<nsRefPtr<File>>& aFileList,
|
2012-07-28 04:21:34 +04:00
|
|
|
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,
|
2015-05-12 15:09:51 +03:00
|
|
|
nsTArray<nsRefPtr<File>>& aFileList)
|
2012-07-28 04:21:34 +04:00
|
|
|
{
|
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) {
|
2015-05-12 15:09:51 +03:00
|
|
|
nsRefPtr<File> file = aFileList[i];
|
2012-07-28 04:21:34 +04:00
|
|
|
|
|
|
|
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,
|
2015-05-12 15:09:51 +03:00
|
|
|
nsTArray<nsRefPtr<File>>& aFileList)
|
2012-07-28 04:21:34 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < aFileList.Length(); ++i) {
|
2015-05-12 15:09:51 +03:00
|
|
|
nsRefPtr<File> file = aFileList[i];
|
2012-07-28 04:21:34 +04:00
|
|
|
|
|
|
|
nsString filename;
|
|
|
|
nsresult rv = file->GetName(filename);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (filename == mFilename) {
|
2015-05-12 15:09:51 +03:00
|
|
|
if (!ToJSValue(aCx, file, aValue)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
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,
|
2015-05-12 15:09:51 +03:00
|
|
|
nsTArray<nsRefPtr<File>>& aFileList)
|
2012-11-07 03:23:13 +04:00
|
|
|
{
|
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) {
|
2015-05-12 15:09:51 +03:00
|
|
|
nsRefPtr<File> file = aFileList[i];
|
2012-11-07 03:23:13 +04:00
|
|
|
|
2013-05-02 13:12:47 +04:00
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
2015-05-12 15:09:51 +03:00
|
|
|
if (!ToJSValue(aCx, file, &value)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!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)
|