Thu, 15 Jan 2015 15:59:08 +0100
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 "ArchiveRequest.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "mozilla/EventDispatcher.h" |
michael@0 | 10 | #include "mozilla/dom/ArchiveRequestBinding.h" |
michael@0 | 11 | #include "nsContentUtils.h" |
michael@0 | 12 | #include "nsCxPusher.h" |
michael@0 | 13 | |
michael@0 | 14 | using namespace mozilla; |
michael@0 | 15 | |
michael@0 | 16 | USING_FILE_NAMESPACE |
michael@0 | 17 | |
michael@0 | 18 | /** |
michael@0 | 19 | * Class used to make asynchronous the ArchiveRequest. |
michael@0 | 20 | */ |
michael@0 | 21 | class ArchiveRequestEvent : public nsRunnable |
michael@0 | 22 | { |
michael@0 | 23 | public: |
michael@0 | 24 | NS_DECL_NSIRUNNABLE |
michael@0 | 25 | |
michael@0 | 26 | ArchiveRequestEvent(ArchiveRequest* request) |
michael@0 | 27 | : mRequest(request) |
michael@0 | 28 | { |
michael@0 | 29 | MOZ_COUNT_CTOR(ArchiveRequestEvent); |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | ~ArchiveRequestEvent() |
michael@0 | 33 | { |
michael@0 | 34 | MOZ_COUNT_DTOR(ArchiveRequestEvent); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | private: //data |
michael@0 | 38 | nsRefPtr<ArchiveRequest> mRequest; |
michael@0 | 39 | }; |
michael@0 | 40 | |
michael@0 | 41 | NS_IMETHODIMP |
michael@0 | 42 | ArchiveRequestEvent::Run() |
michael@0 | 43 | { |
michael@0 | 44 | NS_ABORT_IF_FALSE(mRequest, "the request is not longer valid"); |
michael@0 | 45 | mRequest->Run(); |
michael@0 | 46 | return NS_OK; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | // ArchiveRequest |
michael@0 | 50 | |
michael@0 | 51 | ArchiveRequest::ArchiveRequest(nsPIDOMWindow* aWindow, |
michael@0 | 52 | ArchiveReader* aReader) |
michael@0 | 53 | : DOMRequest(aWindow), |
michael@0 | 54 | mArchiveReader(aReader) |
michael@0 | 55 | { |
michael@0 | 56 | MOZ_ASSERT(aReader); |
michael@0 | 57 | |
michael@0 | 58 | MOZ_COUNT_CTOR(ArchiveRequest); |
michael@0 | 59 | |
michael@0 | 60 | /* An event to make this request asynchronous: */ |
michael@0 | 61 | nsRefPtr<ArchiveRequestEvent> event = new ArchiveRequestEvent(this); |
michael@0 | 62 | NS_DispatchToCurrentThread(event); |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | ArchiveRequest::~ArchiveRequest() |
michael@0 | 66 | { |
michael@0 | 67 | MOZ_COUNT_DTOR(ArchiveRequest); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | nsresult |
michael@0 | 71 | ArchiveRequest::PreHandleEvent(EventChainPreVisitor& aVisitor) |
michael@0 | 72 | { |
michael@0 | 73 | aVisitor.mCanHandle = true; |
michael@0 | 74 | aVisitor.mParentTarget = nullptr; |
michael@0 | 75 | return NS_OK; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | /* virtual */ JSObject* |
michael@0 | 79 | ArchiveRequest::WrapObject(JSContext* aCx) |
michael@0 | 80 | { |
michael@0 | 81 | return ArchiveRequestBinding::Wrap(aCx, this); |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | ArchiveReader* |
michael@0 | 85 | ArchiveRequest::Reader() const |
michael@0 | 86 | { |
michael@0 | 87 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 88 | |
michael@0 | 89 | return mArchiveReader; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | // Here the request is processed: |
michael@0 | 93 | void |
michael@0 | 94 | ArchiveRequest::Run() |
michael@0 | 95 | { |
michael@0 | 96 | // Register this request to the reader. |
michael@0 | 97 | // When the reader is ready to return data, a 'Ready()' will be called |
michael@0 | 98 | nsresult rv = mArchiveReader->RegisterRequest(this); |
michael@0 | 99 | if (NS_FAILED(rv)) { |
michael@0 | 100 | FireError(rv); |
michael@0 | 101 | } |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | void |
michael@0 | 105 | ArchiveRequest::OpGetFilenames() |
michael@0 | 106 | { |
michael@0 | 107 | mOperation = GetFilenames; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | void |
michael@0 | 111 | ArchiveRequest::OpGetFile(const nsAString& aFilename) |
michael@0 | 112 | { |
michael@0 | 113 | mOperation = GetFile; |
michael@0 | 114 | mFilename = aFilename; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | void |
michael@0 | 118 | ArchiveRequest::OpGetFiles() |
michael@0 | 119 | { |
michael@0 | 120 | mOperation = GetFiles; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | nsresult |
michael@0 | 124 | ArchiveRequest::ReaderReady(nsTArray<nsCOMPtr<nsIDOMFile> >& aFileList, |
michael@0 | 125 | nsresult aStatus) |
michael@0 | 126 | { |
michael@0 | 127 | if (NS_FAILED(aStatus)) { |
michael@0 | 128 | FireError(aStatus); |
michael@0 | 129 | return NS_OK; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | nsresult rv; |
michael@0 | 133 | |
michael@0 | 134 | nsIScriptContext* sc = GetContextForEventHandlers(&rv); |
michael@0 | 135 | NS_ENSURE_STATE(sc); |
michael@0 | 136 | |
michael@0 | 137 | AutoPushJSContext cx(sc->GetNativeContext()); |
michael@0 | 138 | NS_ASSERTION(cx, "Failed to get a context!"); |
michael@0 | 139 | |
michael@0 | 140 | JS::Rooted<JSObject*> global(cx, sc->GetWindowProxy()); |
michael@0 | 141 | NS_ASSERTION(global, "Failed to get global object!"); |
michael@0 | 142 | |
michael@0 | 143 | JSAutoCompartment ac(cx, global); |
michael@0 | 144 | |
michael@0 | 145 | JS::Rooted<JS::Value> result(cx); |
michael@0 | 146 | switch (mOperation) { |
michael@0 | 147 | case GetFilenames: |
michael@0 | 148 | rv = GetFilenamesResult(cx, result.address(), aFileList); |
michael@0 | 149 | break; |
michael@0 | 150 | |
michael@0 | 151 | case GetFile: |
michael@0 | 152 | rv = GetFileResult(cx, &result, aFileList); |
michael@0 | 153 | break; |
michael@0 | 154 | |
michael@0 | 155 | case GetFiles: |
michael@0 | 156 | rv = GetFilesResult(cx, &result, aFileList); |
michael@0 | 157 | break; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | if (NS_FAILED(rv)) { |
michael@0 | 161 | NS_WARNING("Get*Result failed!"); |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 165 | FireSuccess(result); |
michael@0 | 166 | } |
michael@0 | 167 | else { |
michael@0 | 168 | FireError(rv); |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | return NS_OK; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | nsresult |
michael@0 | 175 | ArchiveRequest::GetFilenamesResult(JSContext* aCx, |
michael@0 | 176 | JS::Value* aValue, |
michael@0 | 177 | nsTArray<nsCOMPtr<nsIDOMFile> >& aFileList) |
michael@0 | 178 | { |
michael@0 | 179 | JS::Rooted<JSObject*> array(aCx, JS_NewArrayObject(aCx, aFileList.Length())); |
michael@0 | 180 | nsresult rv; |
michael@0 | 181 | |
michael@0 | 182 | if (!array) { |
michael@0 | 183 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | JS::Rooted<JSString*> str(aCx); |
michael@0 | 187 | for (uint32_t i = 0; i < aFileList.Length(); ++i) { |
michael@0 | 188 | nsCOMPtr<nsIDOMFile> file = aFileList[i]; |
michael@0 | 189 | |
michael@0 | 190 | nsString filename; |
michael@0 | 191 | rv = file->GetName(filename); |
michael@0 | 192 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 193 | |
michael@0 | 194 | str = JS_NewUCStringCopyZ(aCx, filename.get()); |
michael@0 | 195 | NS_ENSURE_TRUE(str, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 196 | |
michael@0 | 197 | if (NS_FAILED(rv) || |
michael@0 | 198 | !JS_DefineElement(aCx, array, i, JS::StringValue(str), nullptr, nullptr, |
michael@0 | 199 | JSPROP_ENUMERATE)) { |
michael@0 | 200 | return NS_ERROR_FAILURE; |
michael@0 | 201 | } |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | if (!JS_FreezeObject(aCx, array)) { |
michael@0 | 205 | return NS_ERROR_FAILURE; |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | *aValue = OBJECT_TO_JSVAL(array); |
michael@0 | 209 | return NS_OK; |
michael@0 | 210 | } |
michael@0 | 211 | |
michael@0 | 212 | nsresult |
michael@0 | 213 | ArchiveRequest::GetFileResult(JSContext* aCx, |
michael@0 | 214 | JS::MutableHandle<JS::Value> aValue, |
michael@0 | 215 | nsTArray<nsCOMPtr<nsIDOMFile> >& aFileList) |
michael@0 | 216 | { |
michael@0 | 217 | for (uint32_t i = 0; i < aFileList.Length(); ++i) { |
michael@0 | 218 | nsCOMPtr<nsIDOMFile> file = aFileList[i]; |
michael@0 | 219 | |
michael@0 | 220 | nsString filename; |
michael@0 | 221 | nsresult rv = file->GetName(filename); |
michael@0 | 222 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 223 | |
michael@0 | 224 | if (filename == mFilename) { |
michael@0 | 225 | return nsContentUtils::WrapNative(aCx, file, &NS_GET_IID(nsIDOMFile), |
michael@0 | 226 | aValue); |
michael@0 | 227 | } |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | return NS_ERROR_FAILURE; |
michael@0 | 231 | } |
michael@0 | 232 | |
michael@0 | 233 | nsresult |
michael@0 | 234 | ArchiveRequest::GetFilesResult(JSContext* aCx, |
michael@0 | 235 | JS::MutableHandle<JS::Value> aValue, |
michael@0 | 236 | nsTArray<nsCOMPtr<nsIDOMFile> >& aFileList) |
michael@0 | 237 | { |
michael@0 | 238 | JS::Rooted<JSObject*> array(aCx, JS_NewArrayObject(aCx, aFileList.Length())); |
michael@0 | 239 | if (!array) { |
michael@0 | 240 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | for (uint32_t i = 0; i < aFileList.Length(); ++i) { |
michael@0 | 244 | nsCOMPtr<nsIDOMFile> file = aFileList[i]; |
michael@0 | 245 | |
michael@0 | 246 | JS::Rooted<JS::Value> value(aCx); |
michael@0 | 247 | nsresult rv = nsContentUtils::WrapNative(aCx, file, &NS_GET_IID(nsIDOMFile), |
michael@0 | 248 | &value); |
michael@0 | 249 | if (NS_FAILED(rv) || |
michael@0 | 250 | !JS_DefineElement(aCx, array, i, value, nullptr, nullptr, |
michael@0 | 251 | JSPROP_ENUMERATE)) { |
michael@0 | 252 | return NS_ERROR_FAILURE; |
michael@0 | 253 | } |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | aValue.setObject(*array); |
michael@0 | 257 | return NS_OK; |
michael@0 | 258 | } |
michael@0 | 259 | |
michael@0 | 260 | // static |
michael@0 | 261 | already_AddRefed<ArchiveRequest> |
michael@0 | 262 | ArchiveRequest::Create(nsPIDOMWindow* aOwner, |
michael@0 | 263 | ArchiveReader* aReader) |
michael@0 | 264 | { |
michael@0 | 265 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 266 | |
michael@0 | 267 | nsRefPtr<ArchiveRequest> request = new ArchiveRequest(aOwner, aReader); |
michael@0 | 268 | |
michael@0 | 269 | return request.forget(); |
michael@0 | 270 | } |
michael@0 | 271 | |
michael@0 | 272 | NS_IMPL_CYCLE_COLLECTION_INHERITED(ArchiveRequest, DOMRequest, |
michael@0 | 273 | mArchiveReader) |
michael@0 | 274 | |
michael@0 | 275 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ArchiveRequest) |
michael@0 | 276 | NS_INTERFACE_MAP_END_INHERITING(DOMRequest) |
michael@0 | 277 | |
michael@0 | 278 | NS_IMPL_ADDREF_INHERITED(ArchiveRequest, DOMRequest) |
michael@0 | 279 | NS_IMPL_RELEASE_INHERITED(ArchiveRequest, DOMRequest) |