diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /widget/nsPaper.cpp | |
parent | Initial commit. (diff) | |
download | firefox-e51783d008170d9ab27d25da98ca3a38b0a41b67.tar.xz firefox-e51783d008170d9ab27d25da98ca3a38b0a41b67.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'widget/nsPaper.cpp')
-rw-r--r-- | widget/nsPaper.cpp | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/widget/nsPaper.cpp b/widget/nsPaper.cpp new file mode 100644 index 0000000000..a940d3c1cb --- /dev/null +++ b/widget/nsPaper.cpp @@ -0,0 +1,87 @@ +/* -*- Mode: C++; tab-width: 4; 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 "nsPaper.h" +#include "nsPaperMargin.h" +#include "nsPrinterBase.h" +#include "mozilla/ErrorResult.h" +#include "mozilla/dom/Promise.h" + +using mozilla::ErrorResult; +using mozilla::PaperInfo; + +NS_IMPL_CYCLE_COLLECTION(nsPaper, mMarginPromise, mPrinter) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsPaper) + NS_INTERFACE_MAP_ENTRY(nsIPaper) + NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPaper) +NS_INTERFACE_MAP_END + +NS_IMPL_CYCLE_COLLECTING_ADDREF(nsPaper) +NS_IMPL_CYCLE_COLLECTING_RELEASE(nsPaper) + +nsPaper::nsPaper(const mozilla::PaperInfo& aInfo) + : mPrinter(nullptr), mInfo(aInfo) {} + +nsPaper::nsPaper(nsPrinterBase& aPrinter, const mozilla::PaperInfo& aInfo) + : mPrinter(&aPrinter), mInfo(aInfo) {} + +nsPaper::~nsPaper() = default; + +NS_IMETHODIMP +nsPaper::GetId(nsAString& aId) { + aId = mInfo.mId; + return NS_OK; +} + +NS_IMETHODIMP +nsPaper::GetName(nsAString& aName) { + aName = mInfo.mName.IsEmpty() ? mInfo.mId : mInfo.mName; + return NS_OK; +} + +NS_IMETHODIMP +nsPaper::GetWidth(double* aWidth) { + NS_ENSURE_ARG_POINTER(aWidth); + *aWidth = mInfo.mSize.Width(); + return NS_OK; +} + +NS_IMETHODIMP +nsPaper::GetHeight(double* aHeight) { + NS_ENSURE_ARG_POINTER(aHeight); + *aHeight = mInfo.mSize.Height(); + return NS_OK; +} + +NS_IMETHODIMP +nsPaper::GetUnwriteableMargin(JSContext* aCx, Promise** aPromise) { + if (RefPtr<Promise> existing = mMarginPromise) { + existing.forget(aPromise); + return NS_OK; + } + ErrorResult rv; + RefPtr<Promise> promise = Promise::Create(xpc::CurrentNativeGlobal(aCx), rv); + if (MOZ_UNLIKELY(rv.Failed())) { + return rv.StealNSResult(); + } + + mMarginPromise = promise; + + if (mInfo.mUnwriteableMargin) { + auto margin = mozilla::MakeRefPtr<nsPaperMargin>(*mInfo.mUnwriteableMargin); + mMarginPromise->MaybeResolve(margin); + } else { + if (mPrinter) { + mPrinter->QueryMarginsForPaper(*mMarginPromise, mInfo.mId); + } else { + MOZ_ASSERT_UNREACHABLE("common paper sizes should know their margins"); + mMarginPromise->MaybeRejectWithNotSupportedError("Margins unavailable"); + } + } + + promise.forget(aPromise); + return NS_OK; +} |