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/. */
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
)
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;
34 nsPaper::GetId(nsAString
& aId
) {
40 nsPaper::GetName(nsAString
& aName
) {
41 aName
= mInfo
.mName
.IsEmpty() ? mInfo
.mId
: mInfo
.mName
;
46 nsPaper::GetWidth(double* aWidth
) {
47 NS_ENSURE_ARG_POINTER(aWidth
);
48 *aWidth
= mInfo
.mSize
.Width();
53 nsPaper::GetHeight(double* aHeight
) {
54 NS_ENSURE_ARG_POINTER(aHeight
);
55 *aHeight
= mInfo
.mSize
.Height();
60 nsPaper::GetUnwriteableMargin(JSContext
* aCx
, Promise
** aPromise
) {
61 if (RefPtr
<Promise
> existing
= mMarginPromise
) {
62 existing
.forget(aPromise
);
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
);
78 mPrinter
->QueryMarginsForPaper(*mMarginPromise
, mInfo
.mId
);
80 MOZ_ASSERT_UNREACHABLE("common paper sizes should know their margins");
81 mMarginPromise
->MaybeRejectWithNotSupportedError("Margins unavailable");
85 promise
.forget(aPromise
);