Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/toolkit/components/printingui/ipc/PrintingParent.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "mozilla/dom/Element.h"
8
#include "mozilla/dom/TabParent.h"
9
#include "mozilla/Preferences.h"
10
#include "mozilla/Unused.h"
11
#include "nsIContent.h"
12
#include "nsIDocument.h"
13
#include "nsIDOMWindow.h"
14
#include "nsIPrintingPromptService.h"
15
#include "nsIPrintProgressParams.h"
16
#include "nsIPrintSettingsService.h"
17
#include "nsIServiceManager.h"
18
#include "nsServiceManagerUtils.h"
19
#include "nsIWebProgressListener.h"
20
#include "PrintingParent.h"
21
#include "PrintDataUtils.h"
22
#include "PrintProgressDialogParent.h"
23
#include "PrintSettingsDialogParent.h"
24
#include "mozilla/layout/RemotePrintJobParent.h"
25
26
using namespace mozilla;
27
using namespace mozilla::dom;
28
using namespace mozilla::layout;
29
30
namespace mozilla {
31
namespace embedding {
32
mozilla::ipc::IPCResult
33
PrintingParent::RecvShowProgress(PBrowserParent* parent,
34
                                 PPrintProgressDialogParent* printProgressDialog,
35
                                 PRemotePrintJobParent* remotePrintJob,
36
                                 const bool& isForPrinting)
37
0
{
38
0
  bool notifyOnOpen = false;
39
0
40
0
  nsCOMPtr<nsPIDOMWindowOuter> parentWin = DOMWindowFromBrowserParent(parent);
41
0
  nsCOMPtr<nsIPrintingPromptService> pps(do_GetService("@mozilla.org/embedcomp/printingprompt-service;1"));
42
0
43
0
  PrintProgressDialogParent* dialogParent =
44
0
    static_cast<PrintProgressDialogParent*>(printProgressDialog);
45
0
  nsCOMPtr<nsIObserver> observer = do_QueryInterface(dialogParent);
46
0
47
0
  nsCOMPtr<nsIWebProgressListener> printProgressListener;
48
0
  nsCOMPtr<nsIPrintProgressParams> printProgressParams;
49
0
50
0
  nsresult rv = NS_ERROR_INVALID_ARG;
51
0
  if (parentWin && pps) {
52
0
    rv = pps->ShowProgress(parentWin, nullptr, nullptr, observer,
53
0
                           isForPrinting,
54
0
                           getter_AddRefs(printProgressListener),
55
0
                           getter_AddRefs(printProgressParams),
56
0
                           &notifyOnOpen);
57
0
  }
58
0
59
0
  if (NS_SUCCEEDED(rv)) {
60
0
    if (remotePrintJob) {
61
0
      // If we have a RemotePrintJob use that as a more general forwarder for
62
0
      // print progress listeners.
63
0
      static_cast<RemotePrintJobParent*>(remotePrintJob)
64
0
          ->RegisterListener(printProgressListener);
65
0
    } else {
66
0
      dialogParent->SetWebProgressListener(printProgressListener);
67
0
    }
68
0
69
0
    dialogParent->SetPrintProgressParams(printProgressParams);
70
0
  }
71
0
72
0
  // NOTE: If we aren't going to observe an event on our observer, we need to
73
0
  // fake one. This takes the form of sending the SendDialogOpened message. This
74
0
  // is safe because the child process proxy will always return `true` for
75
0
  // notifyOnOpen, as the request will always be async when performed across
76
0
  // process boundaries.
77
0
  //
78
0
  // We can pass nullptr for all of the arguments, as all consumers of this
79
0
  // observer don't care about the subject, topic, or data.
80
0
  //
81
0
  // If notifyOnOpen is true, then the ShowProgress call will handle notifying
82
0
  // our observer for us.
83
0
  if (!notifyOnOpen) {
84
0
    observer->Observe(nullptr, nullptr, nullptr);
85
0
  }
86
0
  return IPC_OK();
87
0
}
88
89
nsresult
90
PrintingParent::ShowPrintDialog(PBrowserParent* aParent,
91
                                const PrintData& aData,
92
                                PrintData* aResult)
93
0
{
94
0
  // If aParent is null this call is just being used to get print settings from
95
0
  // the printer for print preview.
96
0
  bool isPrintPreview = !aParent;
97
0
  nsCOMPtr<nsPIDOMWindowOuter> parentWin;
98
0
  if (aParent) {
99
0
    parentWin = DOMWindowFromBrowserParent(aParent);
100
0
    if (!parentWin) {
101
0
      return NS_ERROR_FAILURE;
102
0
    }
103
0
  }
104
0
105
0
  nsCOMPtr<nsIPrintingPromptService> pps(do_GetService("@mozilla.org/embedcomp/printingprompt-service;1"));
106
0
  if (!pps) {
107
0
    return NS_ERROR_FAILURE;
108
0
  }
109
0
110
0
  // The initSettings we got can be wrapped using
111
0
  // PrintDataUtils' MockWebBrowserPrint, which implements enough of
112
0
  // nsIWebBrowserPrint to keep the dialogs happy.
113
0
  nsCOMPtr<nsIWebBrowserPrint> wbp = new MockWebBrowserPrint(aData);
114
0
115
0
  // Use the existing RemotePrintJob and its settings, if we have one, to make
116
0
  // sure they stay current.
117
0
  RemotePrintJobParent* remotePrintJob =
118
0
    static_cast<RemotePrintJobParent*>(aData.remotePrintJobParent());
119
0
  nsCOMPtr<nsIPrintSettings> settings;
120
0
  nsresult rv;
121
0
  if (remotePrintJob) {
122
0
    settings = remotePrintJob->GetPrintSettings();
123
0
  } else {
124
0
    rv = mPrintSettingsSvc->GetNewPrintSettings(getter_AddRefs(settings));
125
0
    NS_ENSURE_SUCCESS(rv, rv);
126
0
  }
127
0
128
0
  // We only want to use the print silently setting from the parent.
129
0
  bool printSilently;
130
0
  rv = settings->GetPrintSilent(&printSilently);
131
0
  NS_ENSURE_SUCCESS(rv, rv);
132
0
133
0
  rv = mPrintSettingsSvc->DeserializeToPrintSettings(aData, settings);
134
0
  NS_ENSURE_SUCCESS(rv, rv);
135
0
136
0
  rv = settings->SetPrintSilent(printSilently);
137
0
  NS_ENSURE_SUCCESS(rv, rv);
138
0
139
0
  nsString printerName;
140
0
  settings->GetPrinterName(printerName);
141
0
#ifdef MOZ_X11
142
0
  // Requesting the default printer name on Linux has been removed in the child,
143
0
  // because it was causing a sandbox violation (see Bug 1329216).
144
0
  // If no printer name is set at this point, use the print settings service
145
0
  // to get the default printer name, unless we're printing to file.
146
0
  bool printToFile = false;
147
0
  MOZ_ALWAYS_SUCCEEDS(settings->GetPrintToFile(&printToFile));
148
0
  if (!printToFile && printerName.IsEmpty()) {
149
0
    mPrintSettingsSvc->GetDefaultPrinterName(printerName);
150
0
    settings->SetPrinterName(printerName);
151
0
  }
152
0
  mPrintSettingsSvc->InitPrintSettingsFromPrinter(printerName, settings);
153
0
#endif
154
0
155
0
  // If this is for print preview or we are printing silently then we just need
156
0
  // to initialize the print settings with anything specific from the printer.
157
0
  if (isPrintPreview || printSilently ||
158
0
      Preferences::GetBool("print.always_print_silent", printSilently)) {
159
0
    settings->SetIsInitializedFromPrinter(false);
160
0
    mPrintSettingsSvc->InitPrintSettingsFromPrinter(printerName, settings);
161
0
  } else {
162
0
    rv = pps->ShowPrintDialog(parentWin, wbp, settings);
163
0
    NS_ENSURE_SUCCESS(rv, rv);
164
0
  }
165
0
166
0
  if (isPrintPreview) {
167
0
    // For print preview we don't want a RemotePrintJob just the settings.
168
0
    rv = mPrintSettingsSvc->SerializeToPrintData(settings, nullptr, aResult);
169
0
  } else {
170
0
    rv = SerializeAndEnsureRemotePrintJob(settings, nullptr, remotePrintJob,
171
0
                                          aResult);
172
0
  }
173
0
174
0
  return rv;
175
0
}
176
177
mozilla::ipc::IPCResult
178
PrintingParent::RecvShowPrintDialog(PPrintSettingsDialogParent* aDialog,
179
                                    PBrowserParent* aParent,
180
                                    const PrintData& aData)
181
0
{
182
0
  PrintData resultData;
183
0
  nsresult rv = ShowPrintDialog(aParent, aData, &resultData);
184
0
185
0
  // The child has been spinning an event loop while waiting
186
0
  // to hear about the print settings. We return the results
187
0
  // with an async message which frees the child process from
188
0
  // its nested event loop.
189
0
  if (NS_FAILED(rv)) {
190
0
    mozilla::Unused << PPrintingParent::PPrintSettingsDialogParent::Send__delete__(aDialog, rv);
191
0
  } else {
192
0
    mozilla::Unused << PPrintingParent::PPrintSettingsDialogParent::Send__delete__(aDialog, resultData);
193
0
  }
194
0
  return IPC_OK();
195
0
}
196
197
mozilla::ipc::IPCResult
198
PrintingParent::RecvSavePrintSettings(const PrintData& aData,
199
                                      const bool& aUsePrinterNamePrefix,
200
                                      const uint32_t& aFlags,
201
                                      nsresult* aResult)
202
0
{
203
0
  nsCOMPtr<nsIPrintSettings> settings;
204
0
  *aResult = mPrintSettingsSvc->GetNewPrintSettings(getter_AddRefs(settings));
205
0
  NS_ENSURE_SUCCESS(*aResult, IPC_OK());
206
0
207
0
  *aResult = mPrintSettingsSvc->DeserializeToPrintSettings(aData, settings);
208
0
  NS_ENSURE_SUCCESS(*aResult, IPC_OK());
209
0
210
0
  *aResult = mPrintSettingsSvc->SavePrintSettingsToPrefs(settings,
211
0
                                                        aUsePrinterNamePrefix,
212
0
                                                        aFlags);
213
0
214
0
  return IPC_OK();
215
0
}
216
217
PPrintProgressDialogParent*
218
PrintingParent::AllocPPrintProgressDialogParent()
219
0
{
220
0
  PrintProgressDialogParent* actor = new PrintProgressDialogParent();
221
0
  NS_ADDREF(actor); // De-ref'd in the __delete__ handler for
222
0
                    // PrintProgressDialogParent.
223
0
  return actor;
224
0
}
225
226
bool
227
PrintingParent::DeallocPPrintProgressDialogParent(PPrintProgressDialogParent* doomed)
228
0
{
229
0
  // We can't just delete the PrintProgressDialogParent since somebody might
230
0
  // still be holding a reference to it as nsIObserver, so just decrement the
231
0
  // refcount instead.
232
0
  PrintProgressDialogParent* actor = static_cast<PrintProgressDialogParent*>(doomed);
233
0
  NS_RELEASE(actor);
234
0
  return true;
235
0
}
236
237
PPrintSettingsDialogParent*
238
PrintingParent::AllocPPrintSettingsDialogParent()
239
0
{
240
0
  return new PrintSettingsDialogParent();
241
0
}
242
243
bool
244
PrintingParent::DeallocPPrintSettingsDialogParent(PPrintSettingsDialogParent* aDoomed)
245
0
{
246
0
  delete aDoomed;
247
0
  return true;
248
0
}
249
250
PRemotePrintJobParent*
251
PrintingParent::AllocPRemotePrintJobParent()
252
0
{
253
0
  MOZ_ASSERT_UNREACHABLE("No default constructors for implementations.");
254
0
  return nullptr;
255
0
}
256
257
bool
258
PrintingParent::DeallocPRemotePrintJobParent(PRemotePrintJobParent* aDoomed)
259
0
{
260
0
  delete aDoomed;
261
0
  return true;
262
0
}
263
264
void
265
PrintingParent::ActorDestroy(ActorDestroyReason aWhy)
266
0
{
267
0
}
268
269
nsPIDOMWindowOuter*
270
PrintingParent::DOMWindowFromBrowserParent(PBrowserParent* parent)
271
0
{
272
0
  if (!parent) {
273
0
    return nullptr;
274
0
  }
275
0
276
0
  TabParent* tabParent = TabParent::GetFrom(parent);
277
0
  if (!tabParent) {
278
0
    return nullptr;
279
0
  }
280
0
281
0
  nsCOMPtr<Element> frameElement = tabParent->GetOwnerElement();
282
0
  if (!frameElement) {
283
0
    return nullptr;
284
0
  }
285
0
286
0
  nsCOMPtr<nsIContent> frame(do_QueryInterface(frameElement));
287
0
  if (!frame) {
288
0
    return nullptr;
289
0
  }
290
0
291
0
  nsCOMPtr<nsPIDOMWindowOuter> parentWin = frame->OwnerDoc()->GetWindow();
292
0
  if (!parentWin) {
293
0
    return nullptr;
294
0
  }
295
0
296
0
  return parentWin;
297
0
}
298
299
nsresult
300
PrintingParent::SerializeAndEnsureRemotePrintJob(
301
  nsIPrintSettings* aPrintSettings, nsIWebProgressListener* aListener,
302
  layout::RemotePrintJobParent* aRemotePrintJob, PrintData* aPrintData)
303
0
{
304
0
  MOZ_ASSERT(aPrintData);
305
0
306
0
  nsresult rv;
307
0
  nsCOMPtr<nsIPrintSettings> printSettings;
308
0
  if (aPrintSettings) {
309
0
    printSettings = aPrintSettings;
310
0
  } else {
311
0
    rv = mPrintSettingsSvc->GetNewPrintSettings(getter_AddRefs(printSettings));
312
0
    if (NS_WARN_IF(NS_FAILED(rv))) {
313
0
      return rv;
314
0
    }
315
0
  }
316
0
317
0
  rv = mPrintSettingsSvc->SerializeToPrintData(printSettings, nullptr,
318
0
                                               aPrintData);
319
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
320
0
    return rv;
321
0
  }
322
0
323
0
  RemotePrintJobParent* remotePrintJob;
324
0
  if (aRemotePrintJob) {
325
0
    remotePrintJob = aRemotePrintJob;
326
0
    aPrintData->remotePrintJobParent() = remotePrintJob;
327
0
  } else {
328
0
    remotePrintJob = new RemotePrintJobParent(aPrintSettings);
329
0
    aPrintData->remotePrintJobParent() =
330
0
      SendPRemotePrintJobConstructor(remotePrintJob);
331
0
  }
332
0
  if (aListener) {
333
0
    remotePrintJob->RegisterListener(aListener);
334
0
  }
335
0
336
0
  return NS_OK;
337
0
}
338
339
PrintingParent::PrintingParent()
340
0
{
341
0
  mPrintSettingsSvc =
342
0
    do_GetService("@mozilla.org/gfx/printsettings-service;1");
343
0
  MOZ_ASSERT(mPrintSettingsSvc);
344
0
}
345
346
PrintingParent::~PrintingParent()
347
0
{
348
0
}
349
350
} // namespace embedding
351
} // namespace mozilla
352