/src/mozilla-central/toolkit/components/printingui/nsPrintingPromptService.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
3 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
5 | | |
6 | | #include "nsPrintingPromptService.h" |
7 | | |
8 | | #include "mozilla/ClearOnShutdown.h" |
9 | | #include "mozilla/RefPtr.h" |
10 | | #include "mozilla/StaticPtr.h" |
11 | | #include "nsIDOMWindow.h" |
12 | | #include "nsIServiceManager.h" |
13 | | #include "nsISupportsUtils.h" |
14 | | #include "nsString.h" |
15 | | #include "nsIPrintDialogService.h" |
16 | | #include "nsPIDOMWindow.h" |
17 | | #include "nsXULAppAPI.h" |
18 | | |
19 | | // Printing Progress Includes |
20 | | #if !defined(XP_MACOSX) |
21 | | #include "nsPrintProgress.h" |
22 | | #include "nsPrintProgressParams.h" |
23 | | |
24 | | static const char* kPrintProgressDialogURL = |
25 | | "chrome://global/content/printProgress.xul"; |
26 | | static const char* kPrtPrvProgressDialogURL = |
27 | | "chrome://global/content/printPreviewProgress.xul"; |
28 | | #endif |
29 | | |
30 | | using namespace mozilla; |
31 | | |
32 | | NS_IMPL_ISUPPORTS(nsPrintingPromptService, |
33 | | nsIPrintingPromptService, |
34 | | nsIWebProgressListener) |
35 | | |
36 | | |
37 | | StaticRefPtr<nsPrintingPromptService> sSingleton; |
38 | | |
39 | | /* static */ already_AddRefed<nsPrintingPromptService> |
40 | | nsPrintingPromptService::GetSingleton() |
41 | 0 | { |
42 | 0 | MOZ_ASSERT(XRE_IsParentProcess(), |
43 | 0 | "The content process must use nsPrintingProxy"); |
44 | 0 |
|
45 | 0 | if (!sSingleton) { |
46 | 0 | sSingleton = new nsPrintingPromptService(); |
47 | 0 | sSingleton->Init(); |
48 | 0 | ClearOnShutdown(&sSingleton); |
49 | 0 | } |
50 | 0 |
|
51 | 0 | return do_AddRef(sSingleton); |
52 | 0 | } |
53 | | |
54 | | |
55 | 0 | nsPrintingPromptService::nsPrintingPromptService() = default; |
56 | | |
57 | 0 | nsPrintingPromptService::~nsPrintingPromptService() = default; |
58 | | |
59 | | nsresult |
60 | | nsPrintingPromptService::Init() |
61 | 0 | { |
62 | 0 | #if !defined(XP_MACOSX) |
63 | 0 | nsresult rv; |
64 | 0 | mWatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv); |
65 | 0 | return rv; |
66 | | #else |
67 | | return NS_OK; |
68 | | #endif |
69 | | } |
70 | | |
71 | | NS_IMETHODIMP |
72 | | nsPrintingPromptService::ShowPrintDialog(mozIDOMWindowProxy* parent, |
73 | | nsIWebBrowserPrint* webBrowserPrint, |
74 | | nsIPrintSettings* printSettings) |
75 | 0 | { |
76 | 0 | NS_ENSURE_ARG(webBrowserPrint); |
77 | 0 | NS_ENSURE_ARG(printSettings); |
78 | 0 |
|
79 | 0 | nsCOMPtr<nsIPrintDialogService> dlgPrint( |
80 | 0 | do_GetService(NS_PRINTDIALOGSERVICE_CONTRACTID)); |
81 | 0 | if (dlgPrint) |
82 | 0 | return dlgPrint->Show( |
83 | 0 | nsPIDOMWindowOuter::From(parent), printSettings, webBrowserPrint); |
84 | 0 | |
85 | 0 | return NS_ERROR_FAILURE; |
86 | 0 | } |
87 | | |
88 | | NS_IMETHODIMP |
89 | | nsPrintingPromptService::ShowProgress( |
90 | | mozIDOMWindowProxy* parent, |
91 | | nsIWebBrowserPrint* webBrowserPrint, // ok to be null |
92 | | nsIPrintSettings* printSettings, // ok to be null |
93 | | nsIObserver* openDialogObserver, // ok to be null |
94 | | bool isForPrinting, |
95 | | nsIWebProgressListener** webProgressListener, |
96 | | nsIPrintProgressParams** printProgressParams, |
97 | | bool* notifyOnOpen) |
98 | 0 | { |
99 | 0 | #if !defined(XP_MACOSX) |
100 | 0 | NS_ENSURE_ARG(webProgressListener); |
101 | 0 | NS_ENSURE_ARG(printProgressParams); |
102 | 0 | NS_ENSURE_ARG(notifyOnOpen); |
103 | 0 |
|
104 | 0 | *notifyOnOpen = false; |
105 | 0 | if (mPrintProgress) { |
106 | 0 | *webProgressListener = nullptr; |
107 | 0 | *printProgressParams = nullptr; |
108 | 0 | return NS_ERROR_FAILURE; |
109 | 0 | } |
110 | 0 | |
111 | 0 | nsPrintProgress* prtProgress = new nsPrintProgress(printSettings); |
112 | 0 | mPrintProgress = prtProgress; |
113 | 0 | mWebProgressListener = prtProgress; |
114 | 0 |
|
115 | 0 | nsCOMPtr<nsIPrintProgressParams> prtProgressParams = |
116 | 0 | new nsPrintProgressParams(); |
117 | 0 |
|
118 | 0 | nsCOMPtr<mozIDOMWindowProxy> parentWindow = parent; |
119 | 0 |
|
120 | 0 | if (mWatcher && !parentWindow) { |
121 | 0 | mWatcher->GetActiveWindow(getter_AddRefs(parentWindow)); |
122 | 0 | } |
123 | 0 |
|
124 | 0 | if (parentWindow) { |
125 | 0 | mPrintProgress->OpenProgressDialog(parentWindow, |
126 | 0 | isForPrinting ? kPrintProgressDialogURL |
127 | 0 | : kPrtPrvProgressDialogURL, |
128 | 0 | prtProgressParams, |
129 | 0 | openDialogObserver, |
130 | 0 | notifyOnOpen); |
131 | 0 | } |
132 | 0 |
|
133 | 0 | prtProgressParams.forget(printProgressParams); |
134 | 0 | NS_ADDREF(*webProgressListener = this); |
135 | 0 |
|
136 | 0 | return NS_OK; |
137 | | #else |
138 | | return NS_ERROR_NOT_IMPLEMENTED; |
139 | | #endif |
140 | | } |
141 | | |
142 | | NS_IMETHODIMP |
143 | | nsPrintingPromptService::ShowPageSetup(mozIDOMWindowProxy* parent, |
144 | | nsIPrintSettings* printSettings) |
145 | 0 | { |
146 | 0 | NS_ENSURE_ARG(printSettings); |
147 | 0 |
|
148 | 0 | nsCOMPtr<nsIPrintDialogService> dlgPrint( |
149 | 0 | do_GetService(NS_PRINTDIALOGSERVICE_CONTRACTID)); |
150 | 0 | if (dlgPrint) |
151 | 0 | return dlgPrint->ShowPageSetup(nsPIDOMWindowOuter::From(parent), |
152 | 0 | printSettings); |
153 | 0 | |
154 | 0 | return NS_ERROR_FAILURE; |
155 | 0 | } |
156 | | |
157 | | NS_IMETHODIMP |
158 | | nsPrintingPromptService::OnStateChange(nsIWebProgress* aWebProgress, |
159 | | nsIRequest* aRequest, |
160 | | uint32_t aStateFlags, |
161 | | nsresult aStatus) |
162 | 0 | { |
163 | 0 | #if !defined(XP_MACOSX) |
164 | 0 | if ((aStateFlags & STATE_STOP) && mWebProgressListener) { |
165 | 0 | mWebProgressListener->OnStateChange( |
166 | 0 | aWebProgress, aRequest, aStateFlags, aStatus); |
167 | 0 | if (mPrintProgress) { |
168 | 0 | mPrintProgress->CloseProgressDialog(true); |
169 | 0 | } |
170 | 0 | mPrintProgress = nullptr; |
171 | 0 | mWebProgressListener = nullptr; |
172 | 0 | } |
173 | 0 | #endif |
174 | 0 | return NS_OK; |
175 | 0 | } |
176 | | |
177 | | NS_IMETHODIMP |
178 | | nsPrintingPromptService::OnProgressChange(nsIWebProgress* aWebProgress, |
179 | | nsIRequest* aRequest, |
180 | | int32_t aCurSelfProgress, |
181 | | int32_t aMaxSelfProgress, |
182 | | int32_t aCurTotalProgress, |
183 | | int32_t aMaxTotalProgress) |
184 | 0 | { |
185 | 0 | #if !defined(XP_MACOSX) |
186 | 0 | if (mWebProgressListener) { |
187 | 0 | return mWebProgressListener->OnProgressChange(aWebProgress, |
188 | 0 | aRequest, |
189 | 0 | aCurSelfProgress, |
190 | 0 | aMaxSelfProgress, |
191 | 0 | aCurTotalProgress, |
192 | 0 | aMaxTotalProgress); |
193 | 0 | } |
194 | 0 | #endif |
195 | 0 | return NS_OK; |
196 | 0 | } |
197 | | |
198 | | NS_IMETHODIMP |
199 | | nsPrintingPromptService::OnLocationChange(nsIWebProgress* aWebProgress, |
200 | | nsIRequest* aRequest, |
201 | | nsIURI* location, |
202 | | uint32_t aFlags) |
203 | 0 | { |
204 | 0 | #if !defined(XP_MACOSX) |
205 | 0 | if (mWebProgressListener) { |
206 | 0 | return mWebProgressListener->OnLocationChange( |
207 | 0 | aWebProgress, aRequest, location, aFlags); |
208 | 0 | } |
209 | 0 | #endif |
210 | 0 | return NS_OK; |
211 | 0 | } |
212 | | |
213 | | NS_IMETHODIMP |
214 | | nsPrintingPromptService::OnStatusChange(nsIWebProgress* aWebProgress, |
215 | | nsIRequest* aRequest, |
216 | | nsresult aStatus, |
217 | | const char16_t* aMessage) |
218 | 0 | { |
219 | 0 | #if !defined(XP_MACOSX) |
220 | 0 | if (mWebProgressListener) { |
221 | 0 | return mWebProgressListener->OnStatusChange( |
222 | 0 | aWebProgress, aRequest, aStatus, aMessage); |
223 | 0 | } |
224 | 0 | #endif |
225 | 0 | return NS_OK; |
226 | 0 | } |
227 | | |
228 | | NS_IMETHODIMP |
229 | | nsPrintingPromptService::OnSecurityChange(nsIWebProgress* aWebProgress, |
230 | | nsIRequest* aRequest, |
231 | | uint32_t state) |
232 | 0 | { |
233 | 0 | #if !defined(XP_MACOSX) |
234 | 0 | if (mWebProgressListener) { |
235 | 0 | return mWebProgressListener->OnSecurityChange( |
236 | 0 | aWebProgress, aRequest, state); |
237 | 0 | } |
238 | 0 | #endif |
239 | 0 | return NS_OK; |
240 | 0 | } |