2012-06-03 09:33:52 -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 "FileRequest.h"
|
|
|
|
|
2014-05-07 07:32:12 -07:00
|
|
|
#include "FileHelper.h"
|
|
|
|
#include "js/RootingAPI.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "LockedFile.h"
|
|
|
|
#include "MainThreadUtils.h"
|
2014-03-16 09:53:44 -07:00
|
|
|
#include "mozilla/dom/FileRequestBinding.h"
|
2014-05-07 07:32:12 -07:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2013-05-22 09:05:26 -07:00
|
|
|
#include "nsCxPusher.h"
|
2014-05-07 07:32:12 -07:00
|
|
|
#include "nsDebug.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2014-05-07 07:32:12 -07:00
|
|
|
#include "nsIDOMEvent.h"
|
2012-09-06 00:14:49 -07:00
|
|
|
#include "nsIDOMProgressEvent.h"
|
2014-05-07 07:32:12 -07:00
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsLiteralString.h"
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-05-07 07:32:12 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-01-06 18:53:23 -08:00
|
|
|
FileRequest::FileRequest(nsPIDOMWindow* aWindow)
|
2014-03-16 09:53:44 -07:00
|
|
|
: DOMRequest(aWindow), mWrapAsDOMRequest(false)
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
FileRequest::~FileRequest()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<FileRequest>
|
2014-03-16 09:53:44 -07:00
|
|
|
FileRequest::Create(nsPIDOMWindow* aOwner, LockedFile* aLockedFile,
|
|
|
|
bool aWrapAsDOMRequest)
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2014-02-09 00:04:37 -08:00
|
|
|
nsRefPtr<FileRequest> request = new FileRequest(aOwner);
|
2012-06-03 09:33:52 -07:00
|
|
|
request->mLockedFile = aLockedFile;
|
2014-03-16 09:53:44 -07:00
|
|
|
request->mWrapAsDOMRequest = aWrapAsDOMRequest;
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
return request.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-03-17 21:48:19 -07:00
|
|
|
FileRequest::PreHandleEvent(EventChainPreVisitor& aVisitor)
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
|
|
|
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 14:07:39 -07:00
|
|
|
FireError(rv);
|
2012-06-03 09:33:52 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we need to get the result from the helper.
|
|
|
|
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
|
|
|
|
NS_ENSURE_STATE(sc);
|
|
|
|
|
2014-03-04 08:34:49 -08:00
|
|
|
AutoJSContext cx;
|
2012-06-03 09:33:52 -07:00
|
|
|
NS_ASSERTION(cx, "Failed to get a context!");
|
|
|
|
|
2013-05-04 00:52:57 -07:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
|
|
|
|
2013-09-04 14:06:57 -07:00
|
|
|
JS::Rooted<JSObject*> global(cx, sc->GetWindowProxy());
|
2012-06-03 09:33:52 -07:00
|
|
|
NS_ASSERTION(global, "Failed to get global object!");
|
|
|
|
|
2012-08-21 18:42:53 -07:00
|
|
|
JSAutoCompartment ac(cx, global);
|
|
|
|
|
2014-01-23 11:49:40 -08:00
|
|
|
rv = aFileHelper->GetSuccessResult(cx, &result);
|
2012-08-21 18:42:53 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("GetSuccessResult failed!");
|
2012-06-03 09:33:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-08-08 14:07:39 -07:00
|
|
|
FireSuccess(result);
|
2012-06-03 09:33:52 -07:00
|
|
|
}
|
|
|
|
else {
|
2012-08-08 14:07:39 -07:00
|
|
|
FireError(rv);
|
2012-06-03 09:33:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-25 09:49:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(FileRequest, DOMRequest,
|
|
|
|
mLockedFile)
|
2012-06-03 09:33:52 -07: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)
|
|
|
|
|
2014-03-16 09:53:44 -07:00
|
|
|
// virtual
|
|
|
|
JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
FileRequest::WrapObject(JSContext* aCx)
|
2014-03-16 09:53:44 -07:00
|
|
|
{
|
|
|
|
if (mWrapAsDOMRequest) {
|
2014-04-08 15:27:18 -07:00
|
|
|
return DOMRequest::WrapObject(aCx);
|
2014-03-16 09:53:44 -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 FileRequestBinding::Wrap(aCx, this);
|
2014-03-16 09:53:44 -07:00
|
|
|
}
|
|
|
|
|
2014-03-24 08:10:27 -07:00
|
|
|
LockedFile*
|
2014-03-16 09:53:44 -07:00
|
|
|
FileRequest::GetLockedFile() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
return mLockedFile;
|
|
|
|
}
|
|
|
|
|
2012-06-03 09:33:52 -07:00
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
FileRequest::FireProgressEvent(uint64_t aLoaded, uint64_t aTotal)
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
|
|
|
if (NS_FAILED(CheckInnerWindowCorrectness())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-06 00:14:49 -07:00
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
2013-03-09 03:34:29 -08:00
|
|
|
nsresult rv = NS_NewDOMProgressEvent(getter_AddRefs(event), this,
|
|
|
|
nullptr, nullptr);
|
2012-09-06 00:14:49 -07: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 00:06:29 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2012-09-27 13:11:31 -07:00
|
|
|
DispatchTrustedEvent(event);
|
2012-06-03 09:33:52 -07:00
|
|
|
}
|
2014-05-07 07:32:12 -07:00
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|