Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
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 | |
michael@0 | 6 | #include "nsPrintingPromptService.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nsIComponentManager.h" |
michael@0 | 9 | #include "nsIDialogParamBlock.h" |
michael@0 | 10 | #include "nsIDOMWindow.h" |
michael@0 | 11 | #include "nsIServiceManager.h" |
michael@0 | 12 | #include "nsISupportsUtils.h" |
michael@0 | 13 | #include "nsISupportsArray.h" |
michael@0 | 14 | #include "nsString.h" |
michael@0 | 15 | #include "nsIPrintDialogService.h" |
michael@0 | 16 | |
michael@0 | 17 | // Printing Progress Includes |
michael@0 | 18 | #include "nsPrintProgress.h" |
michael@0 | 19 | #include "nsPrintProgressParams.h" |
michael@0 | 20 | |
michael@0 | 21 | static const char *kPrintDialogURL = "chrome://global/content/printdialog.xul"; |
michael@0 | 22 | static const char *kPrintProgressDialogURL = "chrome://global/content/printProgress.xul"; |
michael@0 | 23 | static const char *kPrtPrvProgressDialogURL = "chrome://global/content/printPreviewProgress.xul"; |
michael@0 | 24 | static const char *kPageSetupDialogURL = "chrome://global/content/printPageSetup.xul"; |
michael@0 | 25 | static const char *kPrinterPropertiesURL = "chrome://global/content/printjoboptions.xul"; |
michael@0 | 26 | |
michael@0 | 27 | /**************************************************************** |
michael@0 | 28 | ************************* ParamBlock *************************** |
michael@0 | 29 | ****************************************************************/ |
michael@0 | 30 | |
michael@0 | 31 | class ParamBlock { |
michael@0 | 32 | |
michael@0 | 33 | public: |
michael@0 | 34 | ParamBlock() |
michael@0 | 35 | { |
michael@0 | 36 | mBlock = 0; |
michael@0 | 37 | } |
michael@0 | 38 | ~ParamBlock() |
michael@0 | 39 | { |
michael@0 | 40 | NS_IF_RELEASE(mBlock); |
michael@0 | 41 | } |
michael@0 | 42 | nsresult Init() { |
michael@0 | 43 | return CallCreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &mBlock); |
michael@0 | 44 | } |
michael@0 | 45 | nsIDialogParamBlock * operator->() const { return mBlock; } |
michael@0 | 46 | operator nsIDialogParamBlock * const () { return mBlock; } |
michael@0 | 47 | |
michael@0 | 48 | private: |
michael@0 | 49 | nsIDialogParamBlock *mBlock; |
michael@0 | 50 | }; |
michael@0 | 51 | |
michael@0 | 52 | /**************************************************************** |
michael@0 | 53 | ***************** nsPrintingPromptService ********************** |
michael@0 | 54 | ****************************************************************/ |
michael@0 | 55 | |
michael@0 | 56 | NS_IMPL_ISUPPORTS(nsPrintingPromptService, nsIPrintingPromptService, nsIWebProgressListener) |
michael@0 | 57 | |
michael@0 | 58 | nsPrintingPromptService::nsPrintingPromptService() |
michael@0 | 59 | { |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | nsPrintingPromptService::~nsPrintingPromptService() |
michael@0 | 63 | { |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | nsresult |
michael@0 | 67 | nsPrintingPromptService::Init() |
michael@0 | 68 | { |
michael@0 | 69 | nsresult rv; |
michael@0 | 70 | mWatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv); |
michael@0 | 71 | return rv; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | /* void showPrintDialog (in nsIDOMWindow parent, in nsIWebBrowserPrint webBrowserPrint, in nsIPrintSettings printSettings); */ |
michael@0 | 75 | NS_IMETHODIMP |
michael@0 | 76 | nsPrintingPromptService::ShowPrintDialog(nsIDOMWindow *parent, nsIWebBrowserPrint *webBrowserPrint, nsIPrintSettings *printSettings) |
michael@0 | 77 | { |
michael@0 | 78 | NS_ENSURE_ARG(webBrowserPrint); |
michael@0 | 79 | NS_ENSURE_ARG(printSettings); |
michael@0 | 80 | |
michael@0 | 81 | // Try to access a component dialog |
michael@0 | 82 | nsCOMPtr<nsIPrintDialogService> dlgPrint(do_GetService( |
michael@0 | 83 | NS_PRINTDIALOGSERVICE_CONTRACTID)); |
michael@0 | 84 | if (dlgPrint) |
michael@0 | 85 | return dlgPrint->Show(parent, printSettings, webBrowserPrint); |
michael@0 | 86 | |
michael@0 | 87 | // Show the built-in dialog instead |
michael@0 | 88 | ParamBlock block; |
michael@0 | 89 | nsresult rv = block.Init(); |
michael@0 | 90 | if (NS_FAILED(rv)) |
michael@0 | 91 | return rv; |
michael@0 | 92 | |
michael@0 | 93 | block->SetInt(0, 0); |
michael@0 | 94 | return DoDialog(parent, block, webBrowserPrint, printSettings, kPrintDialogURL); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | /* void showProgress (in nsIDOMWindow parent, in nsIWebBrowserPrint webBrowserPrint, in nsIPrintSettings printSettings, in nsIObserver openDialogObserver, in boolean isForPrinting, out nsIWebProgressListener webProgressListener, out nsIPrintProgressParams printProgressParams, out boolean notifyOnOpen); */ |
michael@0 | 98 | NS_IMETHODIMP |
michael@0 | 99 | nsPrintingPromptService::ShowProgress(nsIDOMWindow* parent, |
michael@0 | 100 | nsIWebBrowserPrint* webBrowserPrint, // ok to be null |
michael@0 | 101 | nsIPrintSettings* printSettings, // ok to be null |
michael@0 | 102 | nsIObserver* openDialogObserver, // ok to be null |
michael@0 | 103 | bool isForPrinting, |
michael@0 | 104 | nsIWebProgressListener** webProgressListener, |
michael@0 | 105 | nsIPrintProgressParams** printProgressParams, |
michael@0 | 106 | bool* notifyOnOpen) |
michael@0 | 107 | { |
michael@0 | 108 | NS_ENSURE_ARG(webProgressListener); |
michael@0 | 109 | NS_ENSURE_ARG(printProgressParams); |
michael@0 | 110 | NS_ENSURE_ARG(notifyOnOpen); |
michael@0 | 111 | |
michael@0 | 112 | *notifyOnOpen = false; |
michael@0 | 113 | |
michael@0 | 114 | nsPrintProgress* prtProgress = new nsPrintProgress(printSettings); |
michael@0 | 115 | mPrintProgress = prtProgress; |
michael@0 | 116 | mWebProgressListener = prtProgress; |
michael@0 | 117 | |
michael@0 | 118 | nsCOMPtr<nsIPrintProgressParams> prtProgressParams = new nsPrintProgressParams(); |
michael@0 | 119 | |
michael@0 | 120 | nsCOMPtr<nsIDOMWindow> parentWindow = parent; |
michael@0 | 121 | |
michael@0 | 122 | if (mWatcher && !parentWindow) { |
michael@0 | 123 | mWatcher->GetActiveWindow(getter_AddRefs(parentWindow)); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | if (parentWindow) { |
michael@0 | 127 | mPrintProgress->OpenProgressDialog(parentWindow, |
michael@0 | 128 | isForPrinting ? kPrintProgressDialogURL : kPrtPrvProgressDialogURL, |
michael@0 | 129 | prtProgressParams, openDialogObserver, notifyOnOpen); |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | prtProgressParams.forget(printProgressParams); |
michael@0 | 133 | NS_ADDREF(*webProgressListener = this); |
michael@0 | 134 | |
michael@0 | 135 | return NS_OK; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | /* void showPageSetup (in nsIDOMWindow parent, in nsIPrintSettings printSettings); */ |
michael@0 | 139 | NS_IMETHODIMP |
michael@0 | 140 | nsPrintingPromptService::ShowPageSetup(nsIDOMWindow *parent, nsIPrintSettings *printSettings, nsIObserver *aObs) |
michael@0 | 141 | { |
michael@0 | 142 | NS_ENSURE_ARG(printSettings); |
michael@0 | 143 | |
michael@0 | 144 | // Try to access a component dialog |
michael@0 | 145 | nsCOMPtr<nsIPrintDialogService> dlgPrint(do_GetService( |
michael@0 | 146 | NS_PRINTDIALOGSERVICE_CONTRACTID)); |
michael@0 | 147 | if (dlgPrint) |
michael@0 | 148 | return dlgPrint->ShowPageSetup(parent, printSettings); |
michael@0 | 149 | |
michael@0 | 150 | ParamBlock block; |
michael@0 | 151 | nsresult rv = block.Init(); |
michael@0 | 152 | if (NS_FAILED(rv)) |
michael@0 | 153 | return rv; |
michael@0 | 154 | |
michael@0 | 155 | block->SetInt(0, 0); |
michael@0 | 156 | return DoDialog(parent, block, nullptr, printSettings, kPageSetupDialogURL); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | /* void showPrinterProperties (in nsIDOMWindow parent, in wstring printerName, in nsIPrintSettings printSettings); */ |
michael@0 | 160 | NS_IMETHODIMP |
michael@0 | 161 | nsPrintingPromptService::ShowPrinterProperties(nsIDOMWindow *parent, const char16_t *printerName, nsIPrintSettings *printSettings) |
michael@0 | 162 | { |
michael@0 | 163 | /* fixme: We simply ignore the |aPrinter| argument here |
michael@0 | 164 | * We should get the supported printer attributes from the printer and |
michael@0 | 165 | * populate the print job options dialog with these data instead of using |
michael@0 | 166 | * the "default set" here. |
michael@0 | 167 | * However, this requires changes on all platforms and is another big chunk |
michael@0 | 168 | * of patches ... ;-( |
michael@0 | 169 | */ |
michael@0 | 170 | NS_ENSURE_ARG(printerName); |
michael@0 | 171 | NS_ENSURE_ARG(printSettings); |
michael@0 | 172 | |
michael@0 | 173 | ParamBlock block; |
michael@0 | 174 | nsresult rv = block.Init(); |
michael@0 | 175 | if (NS_FAILED(rv)) |
michael@0 | 176 | return rv; |
michael@0 | 177 | |
michael@0 | 178 | block->SetInt(0, 0); |
michael@0 | 179 | return DoDialog(parent, block, nullptr, printSettings, kPrinterPropertiesURL); |
michael@0 | 180 | |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | nsresult |
michael@0 | 184 | nsPrintingPromptService::DoDialog(nsIDOMWindow *aParent, |
michael@0 | 185 | nsIDialogParamBlock *aParamBlock, |
michael@0 | 186 | nsIWebBrowserPrint *aWebBrowserPrint, |
michael@0 | 187 | nsIPrintSettings* aPS, |
michael@0 | 188 | const char *aChromeURL) |
michael@0 | 189 | { |
michael@0 | 190 | NS_ENSURE_ARG(aParamBlock); |
michael@0 | 191 | NS_ENSURE_ARG(aPS); |
michael@0 | 192 | NS_ENSURE_ARG(aChromeURL); |
michael@0 | 193 | |
michael@0 | 194 | if (!mWatcher) |
michael@0 | 195 | return NS_ERROR_FAILURE; |
michael@0 | 196 | |
michael@0 | 197 | nsresult rv = NS_OK; |
michael@0 | 198 | |
michael@0 | 199 | // get a parent, if at all possible |
michael@0 | 200 | // (though we'd rather this didn't fail, it's OK if it does. so there's |
michael@0 | 201 | // no failure or null check.) |
michael@0 | 202 | nsCOMPtr<nsIDOMWindow> activeParent; // retain ownership for method lifetime |
michael@0 | 203 | if (!aParent) |
michael@0 | 204 | { |
michael@0 | 205 | mWatcher->GetActiveWindow(getter_AddRefs(activeParent)); |
michael@0 | 206 | aParent = activeParent; |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | // create a nsISupportsArray of the parameters |
michael@0 | 210 | // being passed to the window |
michael@0 | 211 | nsCOMPtr<nsISupportsArray> array; |
michael@0 | 212 | NS_NewISupportsArray(getter_AddRefs(array)); |
michael@0 | 213 | if (!array) return NS_ERROR_FAILURE; |
michael@0 | 214 | |
michael@0 | 215 | nsCOMPtr<nsISupports> psSupports(do_QueryInterface(aPS)); |
michael@0 | 216 | NS_ASSERTION(psSupports, "PrintSettings must be a supports"); |
michael@0 | 217 | array->AppendElement(psSupports); |
michael@0 | 218 | |
michael@0 | 219 | if (aWebBrowserPrint) { |
michael@0 | 220 | nsCOMPtr<nsISupports> wbpSupports(do_QueryInterface(aWebBrowserPrint)); |
michael@0 | 221 | NS_ASSERTION(wbpSupports, "nsIWebBrowserPrint must be a supports"); |
michael@0 | 222 | array->AppendElement(wbpSupports); |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | nsCOMPtr<nsISupports> blkSupps(do_QueryInterface(aParamBlock)); |
michael@0 | 226 | NS_ASSERTION(blkSupps, "IOBlk must be a supports"); |
michael@0 | 227 | array->AppendElement(blkSupps); |
michael@0 | 228 | |
michael@0 | 229 | nsCOMPtr<nsISupports> arguments(do_QueryInterface(array)); |
michael@0 | 230 | NS_ASSERTION(array, "array must be a supports"); |
michael@0 | 231 | |
michael@0 | 232 | |
michael@0 | 233 | nsCOMPtr<nsIDOMWindow> dialog; |
michael@0 | 234 | rv = mWatcher->OpenWindow(aParent, aChromeURL, "_blank", |
michael@0 | 235 | "centerscreen,chrome,modal,titlebar", arguments, |
michael@0 | 236 | getter_AddRefs(dialog)); |
michael@0 | 237 | |
michael@0 | 238 | // if aWebBrowserPrint is not null then we are printing |
michael@0 | 239 | // so we want to pass back NS_ERROR_ABORT on cancel |
michael@0 | 240 | if (NS_SUCCEEDED(rv) && aWebBrowserPrint) |
michael@0 | 241 | { |
michael@0 | 242 | int32_t status; |
michael@0 | 243 | aParamBlock->GetInt(0, &status); |
michael@0 | 244 | return status == 0?NS_ERROR_ABORT:NS_OK; |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | return rv; |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | ////////////////////////////////////////////////////////////////////// |
michael@0 | 251 | // nsIWebProgressListener |
michael@0 | 252 | ////////////////////////////////////////////////////////////////////// |
michael@0 | 253 | |
michael@0 | 254 | /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long aStateFlags, in nsresult aStatus); */ |
michael@0 | 255 | NS_IMETHODIMP |
michael@0 | 256 | nsPrintingPromptService::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, uint32_t aStateFlags, nsresult aStatus) |
michael@0 | 257 | { |
michael@0 | 258 | if ((aStateFlags & STATE_STOP) && mWebProgressListener) { |
michael@0 | 259 | mWebProgressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus); |
michael@0 | 260 | if (mPrintProgress) { |
michael@0 | 261 | mPrintProgress->CloseProgressDialog(true); |
michael@0 | 262 | } |
michael@0 | 263 | mPrintProgress = nullptr; |
michael@0 | 264 | mWebProgressListener = nullptr; |
michael@0 | 265 | } |
michael@0 | 266 | return NS_OK; |
michael@0 | 267 | } |
michael@0 | 268 | |
michael@0 | 269 | /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */ |
michael@0 | 270 | NS_IMETHODIMP |
michael@0 | 271 | nsPrintingPromptService::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, int32_t aCurSelfProgress, int32_t aMaxSelfProgress, int32_t aCurTotalProgress, int32_t aMaxTotalProgress) |
michael@0 | 272 | { |
michael@0 | 273 | if (mWebProgressListener) { |
michael@0 | 274 | return mWebProgressListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress); |
michael@0 | 275 | } |
michael@0 | 276 | return NS_OK; |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location, in unsigned long aFlags); */ |
michael@0 | 280 | NS_IMETHODIMP |
michael@0 | 281 | nsPrintingPromptService::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location, uint32_t aFlags) |
michael@0 | 282 | { |
michael@0 | 283 | if (mWebProgressListener) { |
michael@0 | 284 | return mWebProgressListener->OnLocationChange(aWebProgress, aRequest, location, aFlags); |
michael@0 | 285 | } |
michael@0 | 286 | return NS_OK; |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */ |
michael@0 | 290 | NS_IMETHODIMP |
michael@0 | 291 | nsPrintingPromptService::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const char16_t *aMessage) |
michael@0 | 292 | { |
michael@0 | 293 | if (mWebProgressListener) { |
michael@0 | 294 | return mWebProgressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage); |
michael@0 | 295 | } |
michael@0 | 296 | return NS_OK; |
michael@0 | 297 | } |
michael@0 | 298 | |
michael@0 | 299 | /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */ |
michael@0 | 300 | NS_IMETHODIMP |
michael@0 | 301 | nsPrintingPromptService::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, uint32_t state) |
michael@0 | 302 | { |
michael@0 | 303 | if (mWebProgressListener) { |
michael@0 | 304 | return mWebProgressListener->OnSecurityChange(aWebProgress, aRequest, state); |
michael@0 | 305 | } |
michael@0 | 306 | return NS_OK; |
michael@0 | 307 | } |