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: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * |
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 "nsCOMPtr.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "nsPrintingPromptService.h" |
michael@0 | 10 | #include "nsIPrintingPromptService.h" |
michael@0 | 11 | #include "nsIFactory.h" |
michael@0 | 12 | #include "nsPIDOMWindow.h" |
michael@0 | 13 | #include "nsReadableUtils.h" |
michael@0 | 14 | #include "nsIEmbeddingSiteWindow.h" |
michael@0 | 15 | #include "nsIServiceManager.h" |
michael@0 | 16 | #include "nsIWebBrowserChrome.h" |
michael@0 | 17 | #include "nsIWindowWatcher.h" |
michael@0 | 18 | #include "nsPrintDialogUtil.h" |
michael@0 | 19 | |
michael@0 | 20 | // Printing Progress Includes |
michael@0 | 21 | #include "nsPrintProgress.h" |
michael@0 | 22 | #include "nsPrintProgressParams.h" |
michael@0 | 23 | #include "nsIWebProgressListener.h" |
michael@0 | 24 | |
michael@0 | 25 | // XP Dialog includes |
michael@0 | 26 | #include "nsIDialogParamBlock.h" |
michael@0 | 27 | #include "nsISupportsUtils.h" |
michael@0 | 28 | #include "nsISupportsArray.h" |
michael@0 | 29 | |
michael@0 | 30 | // Includes need to locate the native Window |
michael@0 | 31 | #include "nsIWidget.h" |
michael@0 | 32 | #include "nsIBaseWindow.h" |
michael@0 | 33 | #include "nsIWebBrowserChrome.h" |
michael@0 | 34 | #include "nsIDocShellTreeOwner.h" |
michael@0 | 35 | #include "nsIDocShellTreeItem.h" |
michael@0 | 36 | #include "nsIDocShell.h" |
michael@0 | 37 | #include "nsIInterfaceRequestorUtils.h" |
michael@0 | 38 | |
michael@0 | 39 | |
michael@0 | 40 | static const char *kPrintProgressDialogURL = "chrome://global/content/printProgress.xul"; |
michael@0 | 41 | static const char *kPrtPrvProgressDialogURL = "chrome://global/content/printPreviewProgress.xul"; |
michael@0 | 42 | static const char *kPageSetupDialogURL = "chrome://global/content/printPageSetup.xul"; |
michael@0 | 43 | |
michael@0 | 44 | /**************************************************************** |
michael@0 | 45 | ************************* ParamBlock *************************** |
michael@0 | 46 | ****************************************************************/ |
michael@0 | 47 | |
michael@0 | 48 | class ParamBlock { |
michael@0 | 49 | |
michael@0 | 50 | public: |
michael@0 | 51 | ParamBlock() |
michael@0 | 52 | { |
michael@0 | 53 | mBlock = 0; |
michael@0 | 54 | } |
michael@0 | 55 | ~ParamBlock() |
michael@0 | 56 | { |
michael@0 | 57 | NS_IF_RELEASE(mBlock); |
michael@0 | 58 | } |
michael@0 | 59 | nsresult Init() { |
michael@0 | 60 | return CallCreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &mBlock); |
michael@0 | 61 | } |
michael@0 | 62 | nsIDialogParamBlock * operator->() const { return mBlock; } |
michael@0 | 63 | operator nsIDialogParamBlock * const () { return mBlock; } |
michael@0 | 64 | |
michael@0 | 65 | private: |
michael@0 | 66 | nsIDialogParamBlock *mBlock; |
michael@0 | 67 | }; |
michael@0 | 68 | |
michael@0 | 69 | //***************************************************************************** |
michael@0 | 70 | |
michael@0 | 71 | NS_IMPL_ISUPPORTS(nsPrintingPromptService, nsIPrintingPromptService, nsIWebProgressListener) |
michael@0 | 72 | |
michael@0 | 73 | nsPrintingPromptService::nsPrintingPromptService() |
michael@0 | 74 | { |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | //----------------------------------------------------------- |
michael@0 | 78 | nsPrintingPromptService::~nsPrintingPromptService() |
michael@0 | 79 | { |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | //----------------------------------------------------------- |
michael@0 | 83 | nsresult |
michael@0 | 84 | nsPrintingPromptService::Init() |
michael@0 | 85 | { |
michael@0 | 86 | nsresult rv; |
michael@0 | 87 | mWatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv); |
michael@0 | 88 | return rv; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | //----------------------------------------------------------- |
michael@0 | 92 | HWND |
michael@0 | 93 | nsPrintingPromptService::GetHWNDForDOMWindow(nsIDOMWindow *aWindow) |
michael@0 | 94 | { |
michael@0 | 95 | nsCOMPtr<nsIWebBrowserChrome> chrome; |
michael@0 | 96 | |
michael@0 | 97 | // We might be embedded so check this path first |
michael@0 | 98 | if (mWatcher) { |
michael@0 | 99 | nsCOMPtr<nsIDOMWindow> fosterParent; |
michael@0 | 100 | if (!aWindow) |
michael@0 | 101 | { // it will be a dependent window. try to find a foster parent. |
michael@0 | 102 | mWatcher->GetActiveWindow(getter_AddRefs(fosterParent)); |
michael@0 | 103 | aWindow = fosterParent; |
michael@0 | 104 | } |
michael@0 | 105 | mWatcher->GetChromeForWindow(aWindow, getter_AddRefs(chrome)); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | if (chrome) { |
michael@0 | 109 | nsCOMPtr<nsIEmbeddingSiteWindow> site(do_QueryInterface(chrome)); |
michael@0 | 110 | if (site) |
michael@0 | 111 | { |
michael@0 | 112 | HWND w; |
michael@0 | 113 | site->GetSiteWindow(reinterpret_cast<void **>(&w)); |
michael@0 | 114 | return w; |
michael@0 | 115 | } |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | // Now we might be the Browser so check this path |
michael@0 | 119 | nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(aWindow)); |
michael@0 | 120 | |
michael@0 | 121 | nsCOMPtr<nsIDocShellTreeItem> treeItem = |
michael@0 | 122 | do_QueryInterface(window->GetDocShell()); |
michael@0 | 123 | if (!treeItem) return nullptr; |
michael@0 | 124 | |
michael@0 | 125 | nsCOMPtr<nsIDocShellTreeOwner> treeOwner; |
michael@0 | 126 | treeItem->GetTreeOwner(getter_AddRefs(treeOwner)); |
michael@0 | 127 | if (!treeOwner) return nullptr; |
michael@0 | 128 | |
michael@0 | 129 | nsCOMPtr<nsIWebBrowserChrome> webBrowserChrome(do_GetInterface(treeOwner)); |
michael@0 | 130 | if (!webBrowserChrome) return nullptr; |
michael@0 | 131 | |
michael@0 | 132 | nsCOMPtr<nsIBaseWindow> baseWin(do_QueryInterface(webBrowserChrome)); |
michael@0 | 133 | if (!baseWin) return nullptr; |
michael@0 | 134 | |
michael@0 | 135 | nsCOMPtr<nsIWidget> widget; |
michael@0 | 136 | baseWin->GetMainWidget(getter_AddRefs(widget)); |
michael@0 | 137 | if (!widget) return nullptr; |
michael@0 | 138 | |
michael@0 | 139 | return (HWND)widget->GetNativeData(NS_NATIVE_TMP_WINDOW); |
michael@0 | 140 | |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | |
michael@0 | 144 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 145 | // nsIPrintingPrompt |
michael@0 | 146 | |
michael@0 | 147 | //----------------------------------------------------------- |
michael@0 | 148 | NS_IMETHODIMP |
michael@0 | 149 | nsPrintingPromptService::ShowPrintDialog(nsIDOMWindow *parent, nsIWebBrowserPrint *webBrowserPrint, nsIPrintSettings *printSettings) |
michael@0 | 150 | { |
michael@0 | 151 | NS_ENSURE_ARG(parent); |
michael@0 | 152 | |
michael@0 | 153 | HWND hWnd = GetHWNDForDOMWindow(parent); |
michael@0 | 154 | NS_ASSERTION(hWnd, "Couldn't get native window for PRint Dialog!"); |
michael@0 | 155 | |
michael@0 | 156 | return NativeShowPrintDialog(hWnd, webBrowserPrint, printSettings); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | |
michael@0 | 160 | /* 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 | 161 | NS_IMETHODIMP |
michael@0 | 162 | nsPrintingPromptService::ShowProgress(nsIDOMWindow* parent, |
michael@0 | 163 | nsIWebBrowserPrint* webBrowserPrint, // ok to be null |
michael@0 | 164 | nsIPrintSettings* printSettings, // ok to be null |
michael@0 | 165 | nsIObserver* openDialogObserver, // ok to be null |
michael@0 | 166 | bool isForPrinting, |
michael@0 | 167 | nsIWebProgressListener** webProgressListener, |
michael@0 | 168 | nsIPrintProgressParams** printProgressParams, |
michael@0 | 169 | bool* notifyOnOpen) |
michael@0 | 170 | { |
michael@0 | 171 | NS_ENSURE_ARG(webProgressListener); |
michael@0 | 172 | NS_ENSURE_ARG(printProgressParams); |
michael@0 | 173 | NS_ENSURE_ARG(notifyOnOpen); |
michael@0 | 174 | |
michael@0 | 175 | *notifyOnOpen = false; |
michael@0 | 176 | if (mPrintProgress) { |
michael@0 | 177 | *webProgressListener = nullptr; |
michael@0 | 178 | *printProgressParams = nullptr; |
michael@0 | 179 | return NS_ERROR_FAILURE; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | nsPrintProgress* prtProgress = new nsPrintProgress(); |
michael@0 | 183 | mPrintProgress = prtProgress; |
michael@0 | 184 | mWebProgressListener = prtProgress; |
michael@0 | 185 | |
michael@0 | 186 | nsCOMPtr<nsIPrintProgressParams> prtProgressParams = new nsPrintProgressParams(); |
michael@0 | 187 | |
michael@0 | 188 | nsCOMPtr<nsIDOMWindow> parentWindow = parent; |
michael@0 | 189 | |
michael@0 | 190 | if (mWatcher && !parentWindow) { |
michael@0 | 191 | mWatcher->GetActiveWindow(getter_AddRefs(parentWindow)); |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | if (parentWindow) { |
michael@0 | 195 | mPrintProgress->OpenProgressDialog(parentWindow, |
michael@0 | 196 | isForPrinting ? kPrintProgressDialogURL : kPrtPrvProgressDialogURL, |
michael@0 | 197 | prtProgressParams, openDialogObserver, notifyOnOpen); |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | prtProgressParams.forget(printProgressParams); |
michael@0 | 201 | NS_ADDREF(*webProgressListener = this); |
michael@0 | 202 | |
michael@0 | 203 | return NS_OK; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | /* void showPageSetup (in nsIDOMWindow parent, in nsIPrintSettings printSettings); */ |
michael@0 | 207 | NS_IMETHODIMP |
michael@0 | 208 | nsPrintingPromptService::ShowPageSetup(nsIDOMWindow *parent, nsIPrintSettings *printSettings, nsIObserver *aObs) |
michael@0 | 209 | { |
michael@0 | 210 | NS_ENSURE_ARG(printSettings); |
michael@0 | 211 | |
michael@0 | 212 | ParamBlock block; |
michael@0 | 213 | nsresult rv = block.Init(); |
michael@0 | 214 | if (NS_FAILED(rv)) |
michael@0 | 215 | return rv; |
michael@0 | 216 | |
michael@0 | 217 | block->SetInt(0, 0); |
michael@0 | 218 | rv = DoDialog(parent, block, printSettings, kPageSetupDialogURL); |
michael@0 | 219 | |
michael@0 | 220 | // if aWebBrowserPrint is not null then we are printing |
michael@0 | 221 | // so we want to pass back NS_ERROR_ABORT on cancel |
michael@0 | 222 | if (NS_SUCCEEDED(rv)) |
michael@0 | 223 | { |
michael@0 | 224 | int32_t status; |
michael@0 | 225 | block->GetInt(0, &status); |
michael@0 | 226 | return status == 0?NS_ERROR_ABORT:NS_OK; |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | return rv; |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | /* void showPrinterProperties (in nsIDOMWindow parent, in wstring printerName, in nsIPrintSettings printSettings); */ |
michael@0 | 233 | NS_IMETHODIMP |
michael@0 | 234 | nsPrintingPromptService::ShowPrinterProperties(nsIDOMWindow *parent, const char16_t *printerName, nsIPrintSettings *printSettings) |
michael@0 | 235 | { |
michael@0 | 236 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | //----------------------------------------------------------- |
michael@0 | 240 | // Helper to Fly XP Dialog |
michael@0 | 241 | nsresult |
michael@0 | 242 | nsPrintingPromptService::DoDialog(nsIDOMWindow *aParent, |
michael@0 | 243 | nsIDialogParamBlock *aParamBlock, |
michael@0 | 244 | nsIPrintSettings* aPS, |
michael@0 | 245 | const char *aChromeURL) |
michael@0 | 246 | { |
michael@0 | 247 | NS_ENSURE_ARG(aParamBlock); |
michael@0 | 248 | NS_ENSURE_ARG(aPS); |
michael@0 | 249 | NS_ENSURE_ARG(aChromeURL); |
michael@0 | 250 | |
michael@0 | 251 | if (!mWatcher) |
michael@0 | 252 | return NS_ERROR_FAILURE; |
michael@0 | 253 | |
michael@0 | 254 | nsresult rv = NS_OK; |
michael@0 | 255 | |
michael@0 | 256 | // get a parent, if at all possible |
michael@0 | 257 | // (though we'd rather this didn't fail, it's OK if it does. so there's |
michael@0 | 258 | // no failure or null check.) |
michael@0 | 259 | nsCOMPtr<nsIDOMWindow> activeParent; // retain ownership for method lifetime |
michael@0 | 260 | if (!aParent) |
michael@0 | 261 | { |
michael@0 | 262 | mWatcher->GetActiveWindow(getter_AddRefs(activeParent)); |
michael@0 | 263 | aParent = activeParent; |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | // create a nsISupportsArray of the parameters |
michael@0 | 267 | // being passed to the window |
michael@0 | 268 | nsCOMPtr<nsISupportsArray> array; |
michael@0 | 269 | NS_NewISupportsArray(getter_AddRefs(array)); |
michael@0 | 270 | if (!array) return NS_ERROR_FAILURE; |
michael@0 | 271 | |
michael@0 | 272 | nsCOMPtr<nsISupports> psSupports(do_QueryInterface(aPS)); |
michael@0 | 273 | NS_ASSERTION(psSupports, "PrintSettings must be a supports"); |
michael@0 | 274 | array->AppendElement(psSupports); |
michael@0 | 275 | |
michael@0 | 276 | nsCOMPtr<nsISupports> blkSupps(do_QueryInterface(aParamBlock)); |
michael@0 | 277 | NS_ASSERTION(blkSupps, "IOBlk must be a supports"); |
michael@0 | 278 | array->AppendElement(blkSupps); |
michael@0 | 279 | |
michael@0 | 280 | nsCOMPtr<nsISupports> arguments(do_QueryInterface(array)); |
michael@0 | 281 | NS_ASSERTION(array, "array must be a supports"); |
michael@0 | 282 | |
michael@0 | 283 | |
michael@0 | 284 | nsCOMPtr<nsIDOMWindow> dialog; |
michael@0 | 285 | rv = mWatcher->OpenWindow(aParent, aChromeURL, "_blank", |
michael@0 | 286 | "centerscreen,chrome,modal,titlebar", arguments, |
michael@0 | 287 | getter_AddRefs(dialog)); |
michael@0 | 288 | |
michael@0 | 289 | return rv; |
michael@0 | 290 | } |
michael@0 | 291 | |
michael@0 | 292 | ////////////////////////////////////////////////////////////////////// |
michael@0 | 293 | // nsIWebProgressListener |
michael@0 | 294 | ////////////////////////////////////////////////////////////////////// |
michael@0 | 295 | |
michael@0 | 296 | /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aStateFlags, in nsresult aStatus); */ |
michael@0 | 297 | NS_IMETHODIMP |
michael@0 | 298 | nsPrintingPromptService::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, uint32_t aStateFlags, nsresult aStatus) |
michael@0 | 299 | { |
michael@0 | 300 | if ((aStateFlags & STATE_STOP) && mWebProgressListener) |
michael@0 | 301 | { |
michael@0 | 302 | mWebProgressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus); |
michael@0 | 303 | if (mPrintProgress) |
michael@0 | 304 | { |
michael@0 | 305 | mPrintProgress->CloseProgressDialog(true); |
michael@0 | 306 | } |
michael@0 | 307 | mPrintProgress = nullptr; |
michael@0 | 308 | mWebProgressListener = nullptr; |
michael@0 | 309 | } |
michael@0 | 310 | return NS_OK; |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */ |
michael@0 | 314 | NS_IMETHODIMP |
michael@0 | 315 | nsPrintingPromptService::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, int32_t aCurSelfProgress, int32_t aMaxSelfProgress, int32_t aCurTotalProgress, int32_t aMaxTotalProgress) |
michael@0 | 316 | { |
michael@0 | 317 | if (mWebProgressListener) |
michael@0 | 318 | { |
michael@0 | 319 | return mWebProgressListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress); |
michael@0 | 320 | } |
michael@0 | 321 | return NS_ERROR_FAILURE; |
michael@0 | 322 | } |
michael@0 | 323 | |
michael@0 | 324 | /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location, in unsigned long aFlags); */ |
michael@0 | 325 | NS_IMETHODIMP |
michael@0 | 326 | nsPrintingPromptService::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location, uint32_t aFlags) |
michael@0 | 327 | { |
michael@0 | 328 | if (mWebProgressListener) |
michael@0 | 329 | { |
michael@0 | 330 | return mWebProgressListener->OnLocationChange(aWebProgress, aRequest, location, aFlags); |
michael@0 | 331 | } |
michael@0 | 332 | return NS_ERROR_FAILURE; |
michael@0 | 333 | } |
michael@0 | 334 | |
michael@0 | 335 | /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */ |
michael@0 | 336 | NS_IMETHODIMP |
michael@0 | 337 | nsPrintingPromptService::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const char16_t *aMessage) |
michael@0 | 338 | { |
michael@0 | 339 | if (mWebProgressListener) |
michael@0 | 340 | { |
michael@0 | 341 | return mWebProgressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage); |
michael@0 | 342 | } |
michael@0 | 343 | return NS_ERROR_FAILURE; |
michael@0 | 344 | } |
michael@0 | 345 | |
michael@0 | 346 | /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */ |
michael@0 | 347 | NS_IMETHODIMP |
michael@0 | 348 | nsPrintingPromptService::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, uint32_t state) |
michael@0 | 349 | { |
michael@0 | 350 | if (mWebProgressListener) |
michael@0 | 351 | { |
michael@0 | 352 | return mWebProgressListener->OnSecurityChange(aWebProgress, aRequest, state); |
michael@0 | 353 | } |
michael@0 | 354 | return NS_ERROR_FAILURE; |
michael@0 | 355 | } |
michael@0 | 356 | |
michael@0 | 357 |