Bug 1880216 - Migrate Fenix docs into Sphinx. r=owlish,geckoview-reviewers,android...
[gecko.git] / widget / nsPaper.cpp
bloba940d3c1cb75cc6c5b8f038a0ef8804430acebcd
1 /* -*- Mode: C++; tab-width: 4; 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 "nsPaper.h"
7 #include "nsPaperMargin.h"
8 #include "nsPrinterBase.h"
9 #include "mozilla/ErrorResult.h"
10 #include "mozilla/dom/Promise.h"
12 using mozilla::ErrorResult;
13 using mozilla::PaperInfo;
15 NS_IMPL_CYCLE_COLLECTION(nsPaper, mMarginPromise, mPrinter)
17 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsPaper)
18 NS_INTERFACE_MAP_ENTRY(nsIPaper)
19 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPaper)
20 NS_INTERFACE_MAP_END
22 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsPaper)
23 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsPaper)
25 nsPaper::nsPaper(const mozilla::PaperInfo& aInfo)
26 : mPrinter(nullptr), mInfo(aInfo) {}
28 nsPaper::nsPaper(nsPrinterBase& aPrinter, const mozilla::PaperInfo& aInfo)
29 : mPrinter(&aPrinter), mInfo(aInfo) {}
31 nsPaper::~nsPaper() = default;
33 NS_IMETHODIMP
34 nsPaper::GetId(nsAString& aId) {
35 aId = mInfo.mId;
36 return NS_OK;
39 NS_IMETHODIMP
40 nsPaper::GetName(nsAString& aName) {
41 aName = mInfo.mName.IsEmpty() ? mInfo.mId : mInfo.mName;
42 return NS_OK;
45 NS_IMETHODIMP
46 nsPaper::GetWidth(double* aWidth) {
47 NS_ENSURE_ARG_POINTER(aWidth);
48 *aWidth = mInfo.mSize.Width();
49 return NS_OK;
52 NS_IMETHODIMP
53 nsPaper::GetHeight(double* aHeight) {
54 NS_ENSURE_ARG_POINTER(aHeight);
55 *aHeight = mInfo.mSize.Height();
56 return NS_OK;
59 NS_IMETHODIMP
60 nsPaper::GetUnwriteableMargin(JSContext* aCx, Promise** aPromise) {
61 if (RefPtr<Promise> existing = mMarginPromise) {
62 existing.forget(aPromise);
63 return NS_OK;
65 ErrorResult rv;
66 RefPtr<Promise> promise = Promise::Create(xpc::CurrentNativeGlobal(aCx), rv);
67 if (MOZ_UNLIKELY(rv.Failed())) {
68 return rv.StealNSResult();
71 mMarginPromise = promise;
73 if (mInfo.mUnwriteableMargin) {
74 auto margin = mozilla::MakeRefPtr<nsPaperMargin>(*mInfo.mUnwriteableMargin);
75 mMarginPromise->MaybeResolve(margin);
76 } else {
77 if (mPrinter) {
78 mPrinter->QueryMarginsForPaper(*mMarginPromise, mInfo.mId);
79 } else {
80 MOZ_ASSERT_UNREACHABLE("common paper sizes should know their margins");
81 mMarginPromise->MaybeRejectWithNotSupportedError("Margins unavailable");
85 promise.forget(aPromise);
86 return NS_OK;