2012-06-03 20:33:52 +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 "FileRequest.h"
|
|
|
|
|
|
|
|
#include "nsIJSContextStack.h"
|
|
|
|
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsEventDispatcher.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2012-09-06 11:14:49 +04:00
|
|
|
#include "nsIDOMProgressEvent.h"
|
2012-07-20 20:42:08 +04:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2012-06-03 20:33:52 +04:00
|
|
|
#include "FileHelper.h"
|
|
|
|
#include "LockedFile.h"
|
|
|
|
|
|
|
|
USING_FILE_NAMESPACE
|
|
|
|
|
|
|
|
FileRequest::FileRequest(nsIDOMWindow* aWindow)
|
2012-07-10 09:07:01 +04:00
|
|
|
: DOMRequest(aWindow), mIsFileRequest(true)
|
2012-06-03 20:33:52 +04: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 09:07:01 +04:00
|
|
|
LockedFile* aLockedFile,
|
|
|
|
bool aIsFileRequest)
|
2012-06-03 20:33:52 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsRefPtr<FileRequest> request = new FileRequest(aOwner);
|
|
|
|
request->mLockedFile = aLockedFile;
|
2012-07-10 09:07:01 +04:00
|
|
|
request->mIsFileRequest = aIsFileRequest;
|
2012-06-03 20:33:52 +04:00
|
|
|
|
|
|
|
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-09 01:07:39 +04:00
|
|
|
FireError(rv);
|
2012-06-03 20:33:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we need to get the result from the helper.
|
|
|
|
jsval result;
|
|
|
|
|
|
|
|
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
|
|
|
|
NS_ENSURE_STATE(sc);
|
|
|
|
|
|
|
|
JSContext* cx = sc->GetNativeContext();
|
|
|
|
NS_ASSERTION(cx, "Failed to get a context!");
|
|
|
|
|
|
|
|
JSObject* global = sc->GetNativeGlobal();
|
|
|
|
NS_ASSERTION(global, "Failed to get global object!");
|
|
|
|
|
|
|
|
JSAutoRequest ar(cx);
|
2012-08-22 05:42:53 +04:00
|
|
|
JSAutoCompartment ac(cx, global);
|
|
|
|
|
|
|
|
rv = aFileHelper->GetSuccessResult(cx, &result);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("GetSuccessResult failed!");
|
2012-06-03 20:33:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-08-09 01:07:39 +04:00
|
|
|
FireSuccess(result);
|
2012-06-03 20:33:52 +04:00
|
|
|
}
|
|
|
|
else {
|
2012-08-09 01:07:39 +04:00
|
|
|
FireError(rv);
|
2012-06-03 20:33:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FileRequest::GetLockedFile(nsIDOMLockedFile** aLockedFile)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMLockedFile> lockedFile(mLockedFile);
|
|
|
|
lockedFile.forget(aLockedFile);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-02 12:53:35 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_1(FileRequest, DOMRequest,
|
|
|
|
mLockedFile)
|
2012-06-03 20:33:52 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileRequest)
|
2012-07-10 09:07:01 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIDOMFileRequest, mIsFileRequest)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO_CONDITIONAL(FileRequest, mIsFileRequest)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO_CONDITIONAL(DOMRequest, !mIsFileRequest)
|
2012-06-03 20:33:52 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMRequest)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(FileRequest, DOMRequest)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(FileRequest, DOMRequest)
|
|
|
|
|
|
|
|
DOMCI_DATA(FileRequest, FileRequest)
|
|
|
|
|
2012-06-04 18:57:25 +04:00
|
|
|
NS_IMPL_EVENT_HANDLER(FileRequest, progress)
|
2012-06-03 20:33:52 +04:00
|
|
|
|
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
FileRequest::FireProgressEvent(uint64_t aLoaded, uint64_t aTotal)
|
2012-06-03 20:33:52 +04:00
|
|
|
{
|
|
|
|
if (NS_FAILED(CheckInnerWindowCorrectness())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-06 11:14:49 +04:00
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
nsresult rv = NS_NewDOMProgressEvent(getter_AddRefs(event), nullptr, nullptr);
|
|
|
|
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 11:06:29 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-06-03 20:33:52 +04:00
|
|
|
|
2012-09-28 00:11:31 +04:00
|
|
|
DispatchTrustedEvent(event);
|
2012-06-03 20:33:52 +04:00
|
|
|
}
|