summaryrefslogtreecommitdiff
path: root/widget/nsDeviceContextSpecProxy.cpp
blob: 7ff3c6e97480320a82e930abc6be833657bd848f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsDeviceContextSpecProxy.h"

#include "gfxASurface.h"
#include "gfxPlatform.h"
#include "mozilla/gfx/DrawEventRecorder.h"
#include "mozilla/gfx/PrintTargetThebes.h"
#include "mozilla/layout/RemotePrintJobChild.h"
#include "mozilla/RefPtr.h"
#include "mozilla/Unused.h"
#include "nsComponentManagerUtils.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsDirectoryServiceUtils.h"
#include "nsIPrintSession.h"
#include "nsIPrintSettings.h"
#include "nsIUUIDGenerator.h"

using mozilla::Unused;

using namespace mozilla;
using namespace mozilla::gfx;

NS_IMPL_ISUPPORTS(nsDeviceContextSpecProxy, nsIDeviceContextSpec)

NS_IMETHODIMP
nsDeviceContextSpecProxy::Init(nsIWidget* aWidget,
                               nsIPrintSettings* aPrintSettings,
                               bool aIsPrintPreview)
{
  nsresult rv;
  mRealDeviceContextSpec =
    do_CreateInstance("@mozilla.org/gfx/devicecontextspec;1", &rv);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  mRealDeviceContextSpec->Init(nullptr, aPrintSettings, aIsPrintPreview);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    mRealDeviceContextSpec = nullptr;
    return rv;
  }

  mPrintSettings = aPrintSettings;

  if (aIsPrintPreview) {
    return NS_OK;
  }

  // nsIPrintSettings only has a weak reference to nsIPrintSession, so we hold
  // it to make sure it's available for the lifetime of the print.
  rv = mPrintSettings->GetPrintSession(getter_AddRefs(mPrintSession));
  if (NS_FAILED(rv) || !mPrintSession) {
    NS_WARNING("We can't print via the parent without an nsIPrintSession.");
    return NS_ERROR_FAILURE;
  }

  rv = mPrintSession->GetRemotePrintJob(getter_AddRefs(mRemotePrintJob));
  if (NS_FAILED(rv) || !mRemotePrintJob) {
    NS_WARNING("We can't print via the parent without a RemotePrintJobChild.");
    return NS_ERROR_FAILURE;
  }

  rv = NS_GetSpecialDirectory(NS_APP_CONTENT_PROCESS_TEMP_DIR,
                              getter_AddRefs(mRecordingDir));
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  mUuidGenerator = do_GetService("@mozilla.org/uuid-generator;1", &rv);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  return NS_OK;
}

already_AddRefed<PrintTarget>
nsDeviceContextSpecProxy::MakePrintTarget()
{
  MOZ_ASSERT(mRealDeviceContextSpec);

  double width, height;
  nsresult rv = mPrintSettings->GetEffectivePageSize(&width, &height);
  if (NS_WARN_IF(NS_FAILED(rv)) || width <= 0 || height <= 0) {
    return nullptr;
  }

  // convert twips to points
  width /= TWIPS_PER_POINT_FLOAT;
  height /= TWIPS_PER_POINT_FLOAT;

  RefPtr<gfxASurface> surface = gfxPlatform::GetPlatform()->
    CreateOffscreenSurface(mozilla::gfx::IntSize::Truncate(width, height),
                           mozilla::gfx::SurfaceFormat::A8R8G8B8_UINT32);
  if (!surface) {
    return nullptr;
  }

  // The type of PrintTarget that we return here shouldn't really matter since
  // our implementation of GetDrawEventRecorder returns an object, which means
  // the DrawTarget returned by the PrintTarget will be a DrawTargetRecording.
  // The recording will be serialized and sent over to the parent process where
  // PrintTranslator::TranslateRecording will call MakePrintTarget (indirectly
  // via PrintTranslator::CreateDrawTarget) on whatever type of
  // nsIDeviceContextSpecProxy is created for the platform that we are running
  // on.  It is that DrawTarget that the recording will be replayed on to
  // print.
  // XXX(jwatt): The above isn't quite true.  We do want to use a
  // PrintTargetRecording here, but we can't until bug 1280324 is figured out
  // and fixed otherwise we will cause bug 1280181 to happen again.
  RefPtr<PrintTarget> target = PrintTargetThebes::CreateOrNull(surface);

  return target.forget();
}

NS_IMETHODIMP
nsDeviceContextSpecProxy::GetDrawEventRecorder(mozilla::gfx::DrawEventRecorder** aDrawEventRecorder)
{
  MOZ_ASSERT(aDrawEventRecorder);
  RefPtr<mozilla::gfx::DrawEventRecorder> result = mRecorder;
  result.forget(aDrawEventRecorder);
  return NS_OK;
}

float
nsDeviceContextSpecProxy::GetDPI()
{
  MOZ_ASSERT(mRealDeviceContextSpec);

  return mRealDeviceContextSpec->GetDPI();
}

float
nsDeviceContextSpecProxy::GetPrintingScale()
{
  MOZ_ASSERT(mRealDeviceContextSpec);

  return mRealDeviceContextSpec->GetPrintingScale();
}

nsresult
nsDeviceContextSpecProxy::CreateUniqueTempPath(nsACString& aFilePath)
{
  MOZ_ASSERT(mRecordingDir);
  MOZ_ASSERT(mUuidGenerator);

  nsID uuid;
  nsresult rv = mUuidGenerator->GenerateUUIDInPlace(&uuid);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  char uuidChars[NSID_LENGTH];
  uuid.ToProvidedString(uuidChars);
  mRecordingFileName.AssignASCII(uuidChars);

  nsCOMPtr<nsIFile> recordingFile;
  rv = mRecordingDir->Clone(getter_AddRefs(recordingFile));
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  rv = recordingFile->AppendNative(mRecordingFileName);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

#ifdef XP_WIN
  return recordingFile->GetPersistentDescriptor(aFilePath);
#else
  return recordingFile->GetNativePath(aFilePath);
#endif
}

NS_IMETHODIMP
nsDeviceContextSpecProxy::BeginDocument(const nsAString& aTitle,
                                        const nsAString& aPrintToFileName,
                                        int32_t aStartPage, int32_t aEndPage)
{
  nsAutoCString recordingPath;
  nsresult rv = CreateUniqueTempPath(recordingPath);
  if (NS_FAILED(rv)) {
    return rv;
  }

  mRecorder = new mozilla::gfx::DrawEventRecorderFile(recordingPath.get());
  return mRemotePrintJob->InitializePrint(nsString(aTitle),
                                          nsString(aPrintToFileName),
                                          aStartPage, aEndPage);
}

NS_IMETHODIMP
nsDeviceContextSpecProxy::EndDocument()
{
  Unused << mRemotePrintJob->SendFinalizePrint();
  return NS_OK;
}

NS_IMETHODIMP
nsDeviceContextSpecProxy::AbortDocument()
{
  Unused << mRemotePrintJob->SendAbortPrint(NS_OK);
  return NS_OK;
}

NS_IMETHODIMP
nsDeviceContextSpecProxy::BeginPage()
{
  // Reopen the file, if necessary, ready for the next page.
  if (!mRecorder->IsOpen()) {
    nsAutoCString recordingPath;
    nsresult rv = CreateUniqueTempPath(recordingPath);
    if (NS_FAILED(rv)) {
      return rv;
    }

    mRecorder->OpenNew(recordingPath.get());
  }

  return NS_OK;
}

NS_IMETHODIMP
nsDeviceContextSpecProxy::EndPage()
{
  // Send the page recording to the parent.
  mRecorder->Close();
  mRemotePrintJob->ProcessPage(mRecordingFileName);

  return NS_OK;
}