Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
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=8 sts=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 |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "FileUtilsWin.h" |
michael@0 | 8 | |
michael@0 | 9 | #include <windows.h> |
michael@0 | 10 | #include <psapi.h> |
michael@0 | 11 | |
michael@0 | 12 | #include "nsWindowsHelpers.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace { |
michael@0 | 15 | |
michael@0 | 16 | // Scoped type used by HandleToFilename |
michael@0 | 17 | struct ScopedMappedViewTraits |
michael@0 | 18 | { |
michael@0 | 19 | typedef void* type; |
michael@0 | 20 | static void* empty() { return nullptr; } |
michael@0 | 21 | static void release(void* ptr) { UnmapViewOfFile(ptr); } |
michael@0 | 22 | }; |
michael@0 | 23 | typedef mozilla::Scoped<ScopedMappedViewTraits> ScopedMappedView; |
michael@0 | 24 | |
michael@0 | 25 | } // anonymous namespace |
michael@0 | 26 | |
michael@0 | 27 | namespace mozilla { |
michael@0 | 28 | |
michael@0 | 29 | bool |
michael@0 | 30 | HandleToFilename(HANDLE aHandle, const LARGE_INTEGER& aOffset, |
michael@0 | 31 | nsAString& aFilename) |
michael@0 | 32 | { |
michael@0 | 33 | aFilename.Truncate(); |
michael@0 | 34 | // This implementation is nice because it uses fully documented APIs that |
michael@0 | 35 | // are available on all Windows versions that we support. |
michael@0 | 36 | nsAutoHandle fileMapping(CreateFileMapping(aHandle, nullptr, PAGE_READONLY, |
michael@0 | 37 | 0, 1, nullptr)); |
michael@0 | 38 | if (!fileMapping) { |
michael@0 | 39 | return false; |
michael@0 | 40 | } |
michael@0 | 41 | ScopedMappedView view(MapViewOfFile(fileMapping, FILE_MAP_READ, |
michael@0 | 42 | aOffset.HighPart, aOffset.LowPart, 1)); |
michael@0 | 43 | if (!view) { |
michael@0 | 44 | return false; |
michael@0 | 45 | } |
michael@0 | 46 | nsAutoString mappedFilename; |
michael@0 | 47 | DWORD len = 0; |
michael@0 | 48 | SetLastError(ERROR_SUCCESS); |
michael@0 | 49 | do { |
michael@0 | 50 | mappedFilename.SetLength(mappedFilename.Length() + MAX_PATH); |
michael@0 | 51 | len = GetMappedFileNameW(GetCurrentProcess(), view, |
michael@0 | 52 | wwc(mappedFilename.BeginWriting()), |
michael@0 | 53 | mappedFilename.Length()); |
michael@0 | 54 | } while (!len && GetLastError() == ERROR_INSUFFICIENT_BUFFER); |
michael@0 | 55 | if (!len) { |
michael@0 | 56 | return false; |
michael@0 | 57 | } |
michael@0 | 58 | mappedFilename.Truncate(len); |
michael@0 | 59 | return NtPathToDosPath(mappedFilename, aFilename); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | } // namespace mozilla |
michael@0 | 63 |