2012-06-03 16:33:52 +00: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 "FileRequest.h"
|
|
|
|
|
2013-03-17 08:51:36 +00:00
|
|
|
#include "DOMFileRequest.h"
|
2013-05-22 16:05:26 +00:00
|
|
|
#include "nsCxPusher.h"
|
2012-06-03 16:33:52 +00:00
|
|
|
#include "nsEventDispatcher.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2012-09-06 07:14:49 +00:00
|
|
|
#include "nsIDOMProgressEvent.h"
|
2012-07-20 16:42:08 +00:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2012-06-03 16:33:52 +00:00
|
|
|
#include "FileHelper.h"
|
|
|
|
#include "LockedFile.h"
|
|
|
|
|
|
|
|
USING_FILE_NAMESPACE
|
|
|
|
|
|
|
|
FileRequest::FileRequest(nsIDOMWindow* aWindow)
|
2013-03-17 08:51:36 +00:00
|
|
|
: DOMRequest(aWindow)
|
2012-06-03 16:33:52 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
FileRequest::~FileRequest()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<FileRequest>
|
|
|
|
FileRequest::Create(nsIDOMWindow* aOwner,
|
2012-07-10 05:07:01 +00:00
|
|
|
LockedFile* aLockedFile,
|
|
|
|
bool aIsFileRequest)
|
2012-06-03 16:33:52 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2013-03-17 08:51:36 +00:00
|
|
|
nsRefPtr<FileRequest> request;
|
|
|
|
if (aIsFileRequest) {
|
|
|
|
request = new DOMFileRequest(aOwner);
|
|
|
|
} else {
|
|
|
|
request = new FileRequest(aOwner);
|
|
|
|
}
|
2012-06-03 16:33:52 +00:00
|
|
|
request->mLockedFile = aLockedFile;
|
|
|
|
|
|
|
|
return request.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
FileRequest::PreHandleEvent(nsEventChainPreVisitor& aVisitor)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
aVisitor.mCanHandle = true;
|
|
|
|
aVisitor.mParentTarget = mLockedFile;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
FileRequest::NotifyHelperCompleted(FileHelper* aFileHelper)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsresult rv = aFileHelper->mResultCode;
|
|
|
|
|
|
|
|
// If the request failed then fire error event and return.
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-08-08 21:07:39 +00:00
|
|
|
FireError(rv);
|
2012-06-03 16:33:52 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we need to get the result from the helper.
|
|
|
|
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
|
|
|
|
NS_ENSURE_STATE(sc);
|
|
|
|
|
2013-02-26 19:04:13 +00:00
|
|
|
AutoPushJSContext cx(sc->GetNativeContext());
|
2012-06-03 16:33:52 +00:00
|
|
|
NS_ASSERTION(cx, "Failed to get a context!");
|
|
|
|
|
2013-05-04 07:52:57 +00:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
|
|
|
|
2013-09-04 21:06:57 +00:00
|
|
|
JS::Rooted<JSObject*> global(cx, sc->GetWindowProxy());
|
2012-06-03 16:33:52 +00:00
|
|
|
NS_ASSERTION(global, "Failed to get global object!");
|
|
|
|
|
2012-08-22 01:42:53 +00:00
|
|
|
JSAutoCompartment ac(cx, global);
|
|
|
|
|
2013-05-04 07:52:57 +00:00
|
|
|
rv = aFileHelper->GetSuccessResult(cx, result.address());
|
2012-08-22 01:42:53 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("GetSuccessResult failed!");
|
2012-06-03 16:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-08-08 21:07:39 +00:00
|
|
|
FireSuccess(result);
|
2012-06-03 16:33:52 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-08-08 21:07:39 +00:00
|
|
|
FireError(rv);
|
2012-06-03 16:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-02 08:53:35 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_1(FileRequest, DOMRequest,
|
|
|
|
mLockedFile)
|
2012-06-03 16:33:52 +00:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileRequest)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMRequest)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(FileRequest, DOMRequest)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(FileRequest, DOMRequest)
|
|
|
|
|
|
|
|
void
|
2012-08-22 15:56:38 +00:00
|
|
|
FileRequest::FireProgressEvent(uint64_t aLoaded, uint64_t aTotal)
|
2012-06-03 16:33:52 +00:00
|
|
|
{
|
|
|
|
if (NS_FAILED(CheckInnerWindowCorrectness())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-06 07:14:49 +00:00
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
2013-03-09 11:34:29 +00:00
|
|
|
nsresult rv = NS_NewDOMProgressEvent(getter_AddRefs(event), this,
|
|
|
|
nullptr, nullptr);
|
2012-09-06 07:14:49 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMProgressEvent> progress = do_QueryInterface(event);
|
|
|
|
MOZ_ASSERT(progress);
|
|
|
|
rv = progress->InitProgressEvent(NS_LITERAL_STRING("progress"), false, false,
|
|
|
|
false, aLoaded, aTotal);
|
2012-08-07 07:06:29 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-06-03 16:33:52 +00:00
|
|
|
|
2012-09-27 20:11:31 +00:00
|
|
|
DispatchTrustedEvent(event);
|
2012-06-03 16:33:52 +00:00
|
|
|
}
|