Bug 1856663 - Add more chunks for Android mochitest-plain. r=jmaher,taskgraph-reviewe...
[gecko.git] / gfx / thebes / PrintTargetPDF.cpp
blob1f8b73b950bad64e8210d93562a72e0f81ceaafb
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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/. */
6 #include "PrintTargetPDF.h"
8 #include "cairo.h"
9 #include "cairo-pdf.h"
10 #include "mozilla/AppShutdown.h"
11 #include "mozilla/StaticPrefs_layout.h"
12 #include "nsContentUtils.h"
13 #include "nsString.h"
15 namespace mozilla::gfx {
17 static cairo_status_t write_func(void* closure, const unsigned char* data,
18 unsigned int length) {
19 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
20 return CAIRO_STATUS_SUCCESS;
22 nsCOMPtr<nsIOutputStream> out = reinterpret_cast<nsIOutputStream*>(closure);
23 do {
24 uint32_t wrote = 0;
25 if (NS_FAILED(out->Write((const char*)data, length, &wrote))) {
26 break;
28 data += wrote;
29 length -= wrote;
30 } while (length > 0);
31 NS_ASSERTION(length == 0, "not everything was written to the file");
32 return CAIRO_STATUS_SUCCESS;
35 PrintTargetPDF::PrintTargetPDF(cairo_surface_t* aCairoSurface,
36 const IntSize& aSize, nsIOutputStream* aStream)
37 : PrintTarget(aCairoSurface, aSize), mStream(aStream) {}
39 PrintTargetPDF::~PrintTargetPDF() {
40 // We get called first, then PrintTarget's dtor. That means that mStream
41 // is destroyed before PrintTarget's dtor calls cairo_surface_destroy. This
42 // can be a problem if Finish() hasn't been called on us, since
43 // cairo_surface_destroy will then call cairo_surface_finish and that will
44 // end up invoking write_func above with the by now dangling pointer mStream
45 // that mCairoSurface stored. To prevent that from happening we must call
46 // Flush here before mStream is deleted.
47 Finish();
50 /* static */
51 already_AddRefed<PrintTargetPDF> PrintTargetPDF::CreateOrNull(
52 nsIOutputStream* aStream, const IntSize& aSizeInPoints) {
53 if (NS_WARN_IF(!aStream)) {
54 return nullptr;
57 cairo_surface_t* surface = cairo_pdf_surface_create_for_stream(
58 write_func, (void*)aStream, aSizeInPoints.width, aSizeInPoints.height);
59 if (cairo_surface_status(surface)) {
60 return nullptr;
63 nsAutoString creatorName;
64 if (NS_SUCCEEDED(nsContentUtils::GetLocalizedString(
65 nsContentUtils::eBRAND_PROPERTIES, "brandFullName", creatorName)) &&
66 !creatorName.IsEmpty()) {
67 creatorName.Append(u" " MOZILLA_VERSION);
68 cairo_pdf_surface_set_metadata(surface, CAIRO_PDF_METADATA_CREATOR,
69 NS_ConvertUTF16toUTF8(creatorName).get());
72 // The new object takes ownership of our surface reference.
73 RefPtr<PrintTargetPDF> target =
74 new PrintTargetPDF(surface, aSizeInPoints, aStream);
75 return target.forget();
78 nsresult PrintTargetPDF::BeginPage(const IntSize& aSizeInPoints) {
79 if (StaticPrefs::layout_css_page_orientation_enabled()) {
80 cairo_pdf_surface_set_size(mCairoSurface, aSizeInPoints.width,
81 aSizeInPoints.height);
82 if (cairo_surface_status(mCairoSurface)) {
83 return NS_ERROR_FAILURE;
86 return PrintTarget::BeginPage(aSizeInPoints);
89 nsresult PrintTargetPDF::EndPage() {
90 cairo_surface_show_page(mCairoSurface);
91 return PrintTarget::EndPage();
94 void PrintTargetPDF::Finish() {
95 if (mIsFinished ||
96 AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
97 // We don't want to call Close() on mStream more than once, and we don't
98 // want to block shutdown if for some reason the user shuts down the
99 // browser mid print.
100 return;
102 PrintTarget::Finish();
103 mStream->Close();
106 } // namespace mozilla::gfx