dom/file/FileRequest.cpp

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "FileRequest.h"
michael@0 8
michael@0 9 #include "mozilla/EventDispatcher.h"
michael@0 10 #include "mozilla/dom/FileRequestBinding.h"
michael@0 11 #include "nsCxPusher.h"
michael@0 12 #include "nsError.h"
michael@0 13 #include "nsIDOMProgressEvent.h"
michael@0 14 #include "nsDOMClassInfoID.h"
michael@0 15 #include "FileHelper.h"
michael@0 16 #include "LockedFile.h"
michael@0 17
michael@0 18 using namespace mozilla;
michael@0 19
michael@0 20 USING_FILE_NAMESPACE
michael@0 21
michael@0 22 FileRequest::FileRequest(nsPIDOMWindow* aWindow)
michael@0 23 : DOMRequest(aWindow), mWrapAsDOMRequest(false)
michael@0 24 {
michael@0 25 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
michael@0 26 }
michael@0 27
michael@0 28 FileRequest::~FileRequest()
michael@0 29 {
michael@0 30 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
michael@0 31 }
michael@0 32
michael@0 33 // static
michael@0 34 already_AddRefed<FileRequest>
michael@0 35 FileRequest::Create(nsPIDOMWindow* aOwner, LockedFile* aLockedFile,
michael@0 36 bool aWrapAsDOMRequest)
michael@0 37 {
michael@0 38 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
michael@0 39
michael@0 40 nsRefPtr<FileRequest> request = new FileRequest(aOwner);
michael@0 41 request->mLockedFile = aLockedFile;
michael@0 42 request->mWrapAsDOMRequest = aWrapAsDOMRequest;
michael@0 43
michael@0 44 return request.forget();
michael@0 45 }
michael@0 46
michael@0 47 nsresult
michael@0 48 FileRequest::PreHandleEvent(EventChainPreVisitor& aVisitor)
michael@0 49 {
michael@0 50 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
michael@0 51
michael@0 52 aVisitor.mCanHandle = true;
michael@0 53 aVisitor.mParentTarget = mLockedFile;
michael@0 54 return NS_OK;
michael@0 55 }
michael@0 56
michael@0 57 nsresult
michael@0 58 FileRequest::NotifyHelperCompleted(FileHelper* aFileHelper)
michael@0 59 {
michael@0 60 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
michael@0 61
michael@0 62 nsresult rv = aFileHelper->mResultCode;
michael@0 63
michael@0 64 // If the request failed then fire error event and return.
michael@0 65 if (NS_FAILED(rv)) {
michael@0 66 FireError(rv);
michael@0 67 return NS_OK;
michael@0 68 }
michael@0 69
michael@0 70 // Otherwise we need to get the result from the helper.
michael@0 71 nsIScriptContext* sc = GetContextForEventHandlers(&rv);
michael@0 72 NS_ENSURE_STATE(sc);
michael@0 73
michael@0 74 AutoJSContext cx;
michael@0 75 NS_ASSERTION(cx, "Failed to get a context!");
michael@0 76
michael@0 77 JS::Rooted<JS::Value> result(cx);
michael@0 78
michael@0 79 JS::Rooted<JSObject*> global(cx, sc->GetWindowProxy());
michael@0 80 NS_ASSERTION(global, "Failed to get global object!");
michael@0 81
michael@0 82 JSAutoCompartment ac(cx, global);
michael@0 83
michael@0 84 rv = aFileHelper->GetSuccessResult(cx, &result);
michael@0 85 if (NS_FAILED(rv)) {
michael@0 86 NS_WARNING("GetSuccessResult failed!");
michael@0 87 }
michael@0 88
michael@0 89 if (NS_SUCCEEDED(rv)) {
michael@0 90 FireSuccess(result);
michael@0 91 }
michael@0 92 else {
michael@0 93 FireError(rv);
michael@0 94 }
michael@0 95
michael@0 96 return NS_OK;
michael@0 97 }
michael@0 98
michael@0 99 NS_IMPL_CYCLE_COLLECTION_INHERITED(FileRequest, DOMRequest,
michael@0 100 mLockedFile)
michael@0 101
michael@0 102 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileRequest)
michael@0 103 NS_INTERFACE_MAP_END_INHERITING(DOMRequest)
michael@0 104
michael@0 105 NS_IMPL_ADDREF_INHERITED(FileRequest, DOMRequest)
michael@0 106 NS_IMPL_RELEASE_INHERITED(FileRequest, DOMRequest)
michael@0 107
michael@0 108 // virtual
michael@0 109 JSObject*
michael@0 110 FileRequest::WrapObject(JSContext* aCx)
michael@0 111 {
michael@0 112 if (mWrapAsDOMRequest) {
michael@0 113 return DOMRequest::WrapObject(aCx);
michael@0 114 }
michael@0 115 return FileRequestBinding::Wrap(aCx, this);
michael@0 116 }
michael@0 117
michael@0 118 LockedFile*
michael@0 119 FileRequest::GetLockedFile() const
michael@0 120 {
michael@0 121 MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
michael@0 122 return mLockedFile;
michael@0 123 }
michael@0 124
michael@0 125 void
michael@0 126 FileRequest::FireProgressEvent(uint64_t aLoaded, uint64_t aTotal)
michael@0 127 {
michael@0 128 if (NS_FAILED(CheckInnerWindowCorrectness())) {
michael@0 129 return;
michael@0 130 }
michael@0 131
michael@0 132 nsCOMPtr<nsIDOMEvent> event;
michael@0 133 nsresult rv = NS_NewDOMProgressEvent(getter_AddRefs(event), this,
michael@0 134 nullptr, nullptr);
michael@0 135 if (NS_FAILED(rv)) {
michael@0 136 return;
michael@0 137 }
michael@0 138
michael@0 139 nsCOMPtr<nsIDOMProgressEvent> progress = do_QueryInterface(event);
michael@0 140 MOZ_ASSERT(progress);
michael@0 141 rv = progress->InitProgressEvent(NS_LITERAL_STRING("progress"), false, false,
michael@0 142 false, aLoaded, aTotal);
michael@0 143 if (NS_FAILED(rv)) {
michael@0 144 return;
michael@0 145 }
michael@0 146
michael@0 147 DispatchTrustedEvent(event);
michael@0 148 }

mercurial