dom/file/MetadataHelper.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 "MetadataHelper.h"
michael@0 8
michael@0 9 #include "LockedFile.h"
michael@0 10
michael@0 11 USING_FILE_NAMESPACE
michael@0 12
michael@0 13 nsresult
michael@0 14 MetadataHelper::DoAsyncRun(nsISupports* aStream)
michael@0 15 {
michael@0 16 bool readWrite = mLockedFile->mMode == FileMode::Readwrite;
michael@0 17
michael@0 18 nsRefPtr<AsyncMetadataGetter> getter =
michael@0 19 new AsyncMetadataGetter(aStream, mParams, readWrite);
michael@0 20
michael@0 21 nsresult rv = getter->AsyncWork(this, nullptr);
michael@0 22 NS_ENSURE_SUCCESS(rv, rv);
michael@0 23
michael@0 24 return NS_OK;
michael@0 25 }
michael@0 26
michael@0 27 nsresult
michael@0 28 MetadataHelper::GetSuccessResult(JSContext* aCx,
michael@0 29 JS::MutableHandle<JS::Value> aVal)
michael@0 30 {
michael@0 31 JS::Rooted<JSObject*> obj(aCx, JS_NewObject(aCx, nullptr, JS::NullPtr(),
michael@0 32 JS::NullPtr()));
michael@0 33 NS_ENSURE_TRUE(obj, NS_ERROR_OUT_OF_MEMORY);
michael@0 34
michael@0 35 if (mParams->SizeRequested()) {
michael@0 36 JS::Rooted<JS::Value> val(aCx, JS_NumberValue(mParams->Size()));
michael@0 37
michael@0 38 if (!JS_DefineProperty(aCx, obj, "size", val, JSPROP_ENUMERATE)) {
michael@0 39 return NS_ERROR_FAILURE;
michael@0 40 }
michael@0 41 }
michael@0 42
michael@0 43 if (mParams->LastModifiedRequested()) {
michael@0 44 double msec = mParams->LastModified();
michael@0 45 JSObject *date = JS_NewDateObjectMsec(aCx, msec);
michael@0 46 NS_ENSURE_TRUE(date, NS_ERROR_OUT_OF_MEMORY);
michael@0 47
michael@0 48 JS::Rooted<JS::Value> dateRoot(aCx, JS::ObjectValue(*date));
michael@0 49 if (!JS_DefineProperty(aCx, obj, "lastModified", dateRoot,
michael@0 50 JSPROP_ENUMERATE)) {
michael@0 51 return NS_ERROR_FAILURE;
michael@0 52 }
michael@0 53 }
michael@0 54
michael@0 55 aVal.setObject(*obj);
michael@0 56 return NS_OK;
michael@0 57 }
michael@0 58
michael@0 59 nsresult
michael@0 60 MetadataHelper::AsyncMetadataGetter::DoStreamWork(nsISupports* aStream)
michael@0 61 {
michael@0 62 nsresult rv;
michael@0 63
michael@0 64 if (mReadWrite) {
michael@0 65 // Force a flush (so all pending writes are flushed to the disk and file
michael@0 66 // metadata is updated too).
michael@0 67
michael@0 68 nsCOMPtr<nsIOutputStream> ostream = do_QueryInterface(aStream);
michael@0 69 NS_ASSERTION(ostream, "This should always succeed!");
michael@0 70
michael@0 71 rv = ostream->Flush();
michael@0 72 NS_ENSURE_SUCCESS(rv, rv);
michael@0 73 }
michael@0 74
michael@0 75 nsCOMPtr<nsIFileMetadata> metadata = do_QueryInterface(aStream);
michael@0 76
michael@0 77 if (mParams->SizeRequested()) {
michael@0 78 int64_t size;
michael@0 79 rv = metadata->GetSize(&size);
michael@0 80 NS_ENSURE_SUCCESS(rv, rv);
michael@0 81
michael@0 82 NS_ENSURE_TRUE(size >= 0, NS_ERROR_FAILURE);
michael@0 83
michael@0 84 mParams->mSize = uint64_t(size);
michael@0 85 }
michael@0 86
michael@0 87 if (mParams->LastModifiedRequested()) {
michael@0 88 int64_t lastModified;
michael@0 89 rv = metadata->GetLastModified(&lastModified);
michael@0 90 NS_ENSURE_SUCCESS(rv, rv);
michael@0 91
michael@0 92 mParams->mLastModified = lastModified;
michael@0 93 }
michael@0 94
michael@0 95 return NS_OK;
michael@0 96 }

mercurial