widget/windows/nsPrintSettingsWin.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: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5 #include "nsPrintSettingsWin.h"
michael@0 6 #include "nsCRT.h"
michael@0 7
michael@0 8 NS_IMPL_ISUPPORTS_INHERITED(nsPrintSettingsWin,
michael@0 9 nsPrintSettings,
michael@0 10 nsIPrintSettingsWin)
michael@0 11
michael@0 12 /** ---------------------------------------------------
michael@0 13 * See documentation in nsPrintSettingsWin.h
michael@0 14 * @update
michael@0 15 */
michael@0 16 nsPrintSettingsWin::nsPrintSettingsWin() :
michael@0 17 nsPrintSettings(),
michael@0 18 mDeviceName(nullptr),
michael@0 19 mDriverName(nullptr),
michael@0 20 mDevMode(nullptr)
michael@0 21 {
michael@0 22
michael@0 23 }
michael@0 24
michael@0 25 /** ---------------------------------------------------
michael@0 26 * See documentation in nsPrintSettingsWin.h
michael@0 27 * @update
michael@0 28 */
michael@0 29 nsPrintSettingsWin::nsPrintSettingsWin(const nsPrintSettingsWin& aPS) :
michael@0 30 mDeviceName(nullptr),
michael@0 31 mDriverName(nullptr),
michael@0 32 mDevMode(nullptr)
michael@0 33 {
michael@0 34 *this = aPS;
michael@0 35 }
michael@0 36
michael@0 37 /** ---------------------------------------------------
michael@0 38 * See documentation in nsPrintSettingsWin.h
michael@0 39 * @update
michael@0 40 */
michael@0 41 nsPrintSettingsWin::~nsPrintSettingsWin()
michael@0 42 {
michael@0 43 if (mDeviceName) nsMemory::Free(mDeviceName);
michael@0 44 if (mDriverName) nsMemory::Free(mDriverName);
michael@0 45 if (mDevMode) ::HeapFree(::GetProcessHeap(), 0, mDevMode);
michael@0 46 }
michael@0 47
michael@0 48 /* [noscript] attribute charPtr deviceName; */
michael@0 49 NS_IMETHODIMP nsPrintSettingsWin::SetDeviceName(const char16_t * aDeviceName)
michael@0 50 {
michael@0 51 if (mDeviceName) {
michael@0 52 nsMemory::Free(mDeviceName);
michael@0 53 }
michael@0 54 mDeviceName = aDeviceName?wcsdup(char16ptr_t(aDeviceName)):nullptr;
michael@0 55 return NS_OK;
michael@0 56 }
michael@0 57 NS_IMETHODIMP nsPrintSettingsWin::GetDeviceName(char16_t **aDeviceName)
michael@0 58 {
michael@0 59 NS_ENSURE_ARG_POINTER(aDeviceName);
michael@0 60 *aDeviceName = mDeviceName?reinterpret_cast<char16_t*>(wcsdup(mDeviceName)):nullptr;
michael@0 61 return NS_OK;
michael@0 62 }
michael@0 63
michael@0 64 /* [noscript] attribute charPtr driverName; */
michael@0 65 NS_IMETHODIMP nsPrintSettingsWin::SetDriverName(const char16_t * aDriverName)
michael@0 66 {
michael@0 67 if (mDriverName) {
michael@0 68 nsMemory::Free(mDriverName);
michael@0 69 }
michael@0 70 mDriverName = aDriverName?wcsdup(char16ptr_t(aDriverName)):nullptr;
michael@0 71 return NS_OK;
michael@0 72 }
michael@0 73 NS_IMETHODIMP nsPrintSettingsWin::GetDriverName(char16_t **aDriverName)
michael@0 74 {
michael@0 75 NS_ENSURE_ARG_POINTER(aDriverName);
michael@0 76 *aDriverName = mDriverName?reinterpret_cast<char16_t*>(wcsdup(mDriverName)):nullptr;
michael@0 77 return NS_OK;
michael@0 78 }
michael@0 79
michael@0 80 void nsPrintSettingsWin::CopyDevMode(DEVMODEW* aInDevMode, DEVMODEW *& aOutDevMode)
michael@0 81 {
michael@0 82 aOutDevMode = nullptr;
michael@0 83 size_t size = aInDevMode->dmSize + aInDevMode->dmDriverExtra;
michael@0 84 aOutDevMode = (LPDEVMODEW)::HeapAlloc (::GetProcessHeap(), HEAP_ZERO_MEMORY, size);
michael@0 85 if (aOutDevMode) {
michael@0 86 memcpy(aOutDevMode, aInDevMode, size);
michael@0 87 }
michael@0 88
michael@0 89 }
michael@0 90
michael@0 91 /* [noscript] attribute nsDevMode devMode; */
michael@0 92 NS_IMETHODIMP nsPrintSettingsWin::GetDevMode(DEVMODEW * *aDevMode)
michael@0 93 {
michael@0 94 NS_ENSURE_ARG_POINTER(aDevMode);
michael@0 95
michael@0 96 if (mDevMode) {
michael@0 97 CopyDevMode(mDevMode, *aDevMode);
michael@0 98 } else {
michael@0 99 *aDevMode = nullptr;
michael@0 100 }
michael@0 101 return NS_OK;
michael@0 102 }
michael@0 103
michael@0 104 NS_IMETHODIMP nsPrintSettingsWin::SetDevMode(DEVMODEW * aDevMode)
michael@0 105 {
michael@0 106 if (mDevMode) {
michael@0 107 ::HeapFree(::GetProcessHeap(), 0, mDevMode);
michael@0 108 mDevMode = nullptr;
michael@0 109 }
michael@0 110
michael@0 111 if (aDevMode) {
michael@0 112 CopyDevMode(aDevMode, mDevMode);
michael@0 113 }
michael@0 114 return NS_OK;
michael@0 115 }
michael@0 116
michael@0 117 //-------------------------------------------
michael@0 118 nsresult
michael@0 119 nsPrintSettingsWin::_Clone(nsIPrintSettings **_retval)
michael@0 120 {
michael@0 121 nsPrintSettingsWin* printSettings = new nsPrintSettingsWin(*this);
michael@0 122 return printSettings->QueryInterface(NS_GET_IID(nsIPrintSettings), (void**)_retval); // ref counts
michael@0 123 }
michael@0 124
michael@0 125 //-------------------------------------------
michael@0 126 nsPrintSettingsWin& nsPrintSettingsWin::operator=(const nsPrintSettingsWin& rhs)
michael@0 127 {
michael@0 128 if (this == &rhs) {
michael@0 129 return *this;
michael@0 130 }
michael@0 131
michael@0 132 ((nsPrintSettings&) *this) = rhs;
michael@0 133
michael@0 134 if (mDeviceName) {
michael@0 135 free(mDeviceName);
michael@0 136 }
michael@0 137
michael@0 138 if (mDriverName) {
michael@0 139 free(mDriverName);
michael@0 140 }
michael@0 141
michael@0 142 // Use free because we used the native malloc to create the memory
michael@0 143 if (mDevMode) {
michael@0 144 ::HeapFree(::GetProcessHeap(), 0, mDevMode);
michael@0 145 }
michael@0 146
michael@0 147 mDeviceName = rhs.mDeviceName?wcsdup(rhs.mDeviceName):nullptr;
michael@0 148 mDriverName = rhs.mDriverName?wcsdup(rhs.mDriverName):nullptr;
michael@0 149
michael@0 150 if (rhs.mDevMode) {
michael@0 151 CopyDevMode(rhs.mDevMode, mDevMode);
michael@0 152 } else {
michael@0 153 mDevMode = nullptr;
michael@0 154 }
michael@0 155
michael@0 156 return *this;
michael@0 157 }
michael@0 158
michael@0 159 //-------------------------------------------
michael@0 160 /* void assign (in nsIPrintSettings aPS); */
michael@0 161 nsresult
michael@0 162 nsPrintSettingsWin::_Assign(nsIPrintSettings *aPS)
michael@0 163 {
michael@0 164 nsPrintSettingsWin *psWin = static_cast<nsPrintSettingsWin*>(aPS);
michael@0 165 *this = *psWin;
michael@0 166 return NS_OK;
michael@0 167 }
michael@0 168
michael@0 169 //----------------------------------------------------------------------
michael@0 170 // Testing of assign and clone
michael@0 171 // This define turns on the testing module below
michael@0 172 // so at start up it writes and reads the prefs.
michael@0 173 #ifdef DEBUG_rodsX
michael@0 174 #include "nsIPrintOptions.h"
michael@0 175 #include "nsIServiceManager.h"
michael@0 176 class Tester {
michael@0 177 public:
michael@0 178 Tester();
michael@0 179 };
michael@0 180 Tester::Tester()
michael@0 181 {
michael@0 182 nsCOMPtr<nsIPrintSettings> ps;
michael@0 183 nsresult rv;
michael@0 184 nsCOMPtr<nsIPrintOptions> printService = do_GetService("@mozilla.org/gfx/printsettings-service;1", &rv);
michael@0 185 if (NS_SUCCEEDED(rv)) {
michael@0 186 rv = printService->CreatePrintSettings(getter_AddRefs(ps));
michael@0 187 }
michael@0 188
michael@0 189 if (ps) {
michael@0 190 ps->SetPrintOptions(nsIPrintSettings::kPrintOddPages, true);
michael@0 191 ps->SetPrintOptions(nsIPrintSettings::kPrintEvenPages, false);
michael@0 192 ps->SetMarginTop(1.0);
michael@0 193 ps->SetMarginLeft(1.0);
michael@0 194 ps->SetMarginBottom(1.0);
michael@0 195 ps->SetMarginRight(1.0);
michael@0 196 ps->SetScaling(0.5);
michael@0 197 ps->SetPrintBGColors(true);
michael@0 198 ps->SetPrintBGImages(true);
michael@0 199 ps->SetPrintRange(15);
michael@0 200 ps->SetHeaderStrLeft(NS_ConvertUTF8toUTF16("Left").get());
michael@0 201 ps->SetHeaderStrCenter(NS_ConvertUTF8toUTF16("Center").get());
michael@0 202 ps->SetHeaderStrRight(NS_ConvertUTF8toUTF16("Right").get());
michael@0 203 ps->SetFooterStrLeft(NS_ConvertUTF8toUTF16("Left").get());
michael@0 204 ps->SetFooterStrCenter(NS_ConvertUTF8toUTF16("Center").get());
michael@0 205 ps->SetFooterStrRight(NS_ConvertUTF8toUTF16("Right").get());
michael@0 206 ps->SetPaperName(NS_ConvertUTF8toUTF16("Paper Name").get());
michael@0 207 ps->SetPaperSizeType(10);
michael@0 208 ps->SetPaperData(1);
michael@0 209 ps->SetPaperWidth(100.0);
michael@0 210 ps->SetPaperHeight(50.0);
michael@0 211 ps->SetPaperSizeUnit(nsIPrintSettings::kPaperSizeMillimeters);
michael@0 212 ps->SetPrintReversed(true);
michael@0 213 ps->SetPrintInColor(true);
michael@0 214 ps->SetOrientation(nsIPrintSettings::kLandscapeOrientation);
michael@0 215 ps->SetPrintCommand(NS_ConvertUTF8toUTF16("Command").get());
michael@0 216 ps->SetNumCopies(2);
michael@0 217 ps->SetPrinterName(NS_ConvertUTF8toUTF16("Printer Name").get());
michael@0 218 ps->SetPrintToFile(true);
michael@0 219 ps->SetToFileName(NS_ConvertUTF8toUTF16("File Name").get());
michael@0 220 ps->SetPrintPageDelay(1000);
michael@0 221
michael@0 222 nsCOMPtr<nsIPrintSettings> ps2;
michael@0 223 if (NS_SUCCEEDED(rv)) {
michael@0 224 rv = printService->CreatePrintSettings(getter_AddRefs(ps2));
michael@0 225 }
michael@0 226
michael@0 227 ps2->Assign(ps);
michael@0 228
michael@0 229 nsCOMPtr<nsIPrintSettings> psClone;
michael@0 230 ps2->Clone(getter_AddRefs(psClone));
michael@0 231
michael@0 232 }
michael@0 233
michael@0 234 }
michael@0 235 Tester gTester;
michael@0 236 #endif
michael@0 237

mercurial