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 "FileHandle.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "nsContentUtils.h" |
michael@0 | 10 | #include "nsDOMClassInfoID.h" |
michael@0 | 11 | #include "nsNetUtil.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "nsIDOMFile.h" |
michael@0 | 14 | #include "nsIFileStorage.h" |
michael@0 | 15 | |
michael@0 | 16 | #include "File.h" |
michael@0 | 17 | #include "FileRequest.h" |
michael@0 | 18 | #include "FileService.h" |
michael@0 | 19 | #include "LockedFile.h" |
michael@0 | 20 | #include "MetadataHelper.h" |
michael@0 | 21 | #include "mozilla/dom/FileHandleBinding.h" |
michael@0 | 22 | |
michael@0 | 23 | using namespace mozilla; |
michael@0 | 24 | using namespace mozilla::dom; |
michael@0 | 25 | USING_FILE_NAMESPACE |
michael@0 | 26 | |
michael@0 | 27 | namespace { |
michael@0 | 28 | |
michael@0 | 29 | class GetFileHelper : public MetadataHelper |
michael@0 | 30 | { |
michael@0 | 31 | public: |
michael@0 | 32 | GetFileHelper(LockedFile* aLockedFile, |
michael@0 | 33 | FileRequest* aFileRequest, |
michael@0 | 34 | MetadataParameters* aParams, |
michael@0 | 35 | FileHandle* aFileHandle) |
michael@0 | 36 | : MetadataHelper(aLockedFile, aFileRequest, aParams), |
michael@0 | 37 | mFileHandle(aFileHandle) |
michael@0 | 38 | { } |
michael@0 | 39 | |
michael@0 | 40 | virtual nsresult |
michael@0 | 41 | GetSuccessResult(JSContext* aCx, |
michael@0 | 42 | JS::MutableHandle<JS::Value> aVal) MOZ_OVERRIDE; |
michael@0 | 43 | |
michael@0 | 44 | virtual void |
michael@0 | 45 | ReleaseObjects() MOZ_OVERRIDE |
michael@0 | 46 | { |
michael@0 | 47 | mFileHandle = nullptr; |
michael@0 | 48 | |
michael@0 | 49 | MetadataHelper::ReleaseObjects(); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | private: |
michael@0 | 53 | nsRefPtr<FileHandle> mFileHandle; |
michael@0 | 54 | }; |
michael@0 | 55 | |
michael@0 | 56 | } // anonymous namespace |
michael@0 | 57 | |
michael@0 | 58 | NS_IMPL_CYCLE_COLLECTION_INHERITED(FileHandle, DOMEventTargetHelper, |
michael@0 | 59 | mFileStorage) |
michael@0 | 60 | |
michael@0 | 61 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileHandle) |
michael@0 | 62 | NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper) |
michael@0 | 63 | |
michael@0 | 64 | NS_IMPL_ADDREF_INHERITED(FileHandle, DOMEventTargetHelper) |
michael@0 | 65 | NS_IMPL_RELEASE_INHERITED(FileHandle, DOMEventTargetHelper) |
michael@0 | 66 | |
michael@0 | 67 | // static |
michael@0 | 68 | already_AddRefed<FileHandle> |
michael@0 | 69 | FileHandle::Create(nsPIDOMWindow* aWindow, |
michael@0 | 70 | nsIFileStorage* aFileStorage, |
michael@0 | 71 | nsIFile* aFile) |
michael@0 | 72 | { |
michael@0 | 73 | MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 74 | |
michael@0 | 75 | nsRefPtr<FileHandle> newFileHandle = new FileHandle(aWindow); |
michael@0 | 76 | |
michael@0 | 77 | newFileHandle->mFileStorage = aFileStorage; |
michael@0 | 78 | nsresult rv = aFile->GetLeafName(newFileHandle->mName); |
michael@0 | 79 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 80 | return nullptr; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | newFileHandle->mFile = aFile; |
michael@0 | 84 | newFileHandle->mFileName = newFileHandle->mName; |
michael@0 | 85 | |
michael@0 | 86 | return newFileHandle.forget(); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | // virtual |
michael@0 | 90 | already_AddRefed<nsISupports> |
michael@0 | 91 | FileHandle::CreateStream(nsIFile* aFile, bool aReadOnly) |
michael@0 | 92 | { |
michael@0 | 93 | nsresult rv; |
michael@0 | 94 | |
michael@0 | 95 | if (aReadOnly) { |
michael@0 | 96 | nsCOMPtr<nsIInputStream> stream; |
michael@0 | 97 | rv = NS_NewLocalFileInputStream(getter_AddRefs(stream), aFile, -1, -1, |
michael@0 | 98 | nsIFileInputStream::DEFER_OPEN); |
michael@0 | 99 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 100 | return nullptr; |
michael@0 | 101 | } |
michael@0 | 102 | return stream.forget(); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | nsCOMPtr<nsIFileStream> stream; |
michael@0 | 106 | rv = NS_NewLocalFileStream(getter_AddRefs(stream), aFile, -1, -1, |
michael@0 | 107 | nsIFileStream::DEFER_OPEN); |
michael@0 | 108 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 109 | return nullptr; |
michael@0 | 110 | } |
michael@0 | 111 | return stream.forget(); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | // virtual |
michael@0 | 115 | already_AddRefed<nsIDOMFile> |
michael@0 | 116 | FileHandle::CreateFileObject(LockedFile* aLockedFile, uint32_t aFileSize) |
michael@0 | 117 | { |
michael@0 | 118 | nsCOMPtr<nsIDOMFile> file = |
michael@0 | 119 | new File(mName, mType, aFileSize, mFile, aLockedFile); |
michael@0 | 120 | |
michael@0 | 121 | return file.forget(); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | // virtual |
michael@0 | 125 | JSObject* |
michael@0 | 126 | FileHandle::WrapObject(JSContext* aCx) |
michael@0 | 127 | { |
michael@0 | 128 | return FileHandleBinding::Wrap(aCx, this); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | already_AddRefed<LockedFile> |
michael@0 | 132 | FileHandle::Open(FileMode aMode, ErrorResult& aError) |
michael@0 | 133 | { |
michael@0 | 134 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 135 | |
michael@0 | 136 | if (FileService::IsShuttingDown() || mFileStorage->IsShuttingDown()) { |
michael@0 | 137 | aError.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); |
michael@0 | 138 | return nullptr; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | nsRefPtr<LockedFile> lockedFile = LockedFile::Create(this, aMode); |
michael@0 | 142 | if (!lockedFile) { |
michael@0 | 143 | aError.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); |
michael@0 | 144 | return nullptr; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | return lockedFile.forget(); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | already_AddRefed<DOMRequest> |
michael@0 | 151 | FileHandle::GetFile(ErrorResult& aError) |
michael@0 | 152 | { |
michael@0 | 153 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 154 | |
michael@0 | 155 | // Do nothing if the window is closed |
michael@0 | 156 | if (!GetOwner()) { |
michael@0 | 157 | return nullptr; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | nsRefPtr<LockedFile> lockedFile = |
michael@0 | 161 | LockedFile::Create(this, FileMode::Readonly, LockedFile::PARALLEL); |
michael@0 | 162 | if (!lockedFile) { |
michael@0 | 163 | aError.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); |
michael@0 | 164 | return nullptr; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | nsRefPtr<FileRequest> request = |
michael@0 | 168 | FileRequest::Create(GetOwner(), lockedFile, /* aWrapAsDOMRequest */ true); |
michael@0 | 169 | |
michael@0 | 170 | nsRefPtr<MetadataParameters> params = new MetadataParameters(true, false); |
michael@0 | 171 | |
michael@0 | 172 | nsRefPtr<GetFileHelper> helper = |
michael@0 | 173 | new GetFileHelper(lockedFile, request, params, this); |
michael@0 | 174 | |
michael@0 | 175 | nsresult rv = helper->Enqueue(); |
michael@0 | 176 | if (NS_FAILED(rv)) { |
michael@0 | 177 | aError.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); |
michael@0 | 178 | return nullptr; |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | return request.forget(); |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | nsresult |
michael@0 | 185 | GetFileHelper::GetSuccessResult(JSContext* aCx, |
michael@0 | 186 | JS::MutableHandle<JS::Value> aVal) |
michael@0 | 187 | { |
michael@0 | 188 | nsCOMPtr<nsIDOMFile> domFile = |
michael@0 | 189 | mFileHandle->CreateFileObject(mLockedFile, mParams->Size()); |
michael@0 | 190 | |
michael@0 | 191 | nsresult rv = |
michael@0 | 192 | nsContentUtils::WrapNative(aCx, domFile, &NS_GET_IID(nsIDOMFile), aVal); |
michael@0 | 193 | NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); |
michael@0 | 194 | return NS_OK; |
michael@0 | 195 | } |