2020-07-13 22:59:26 +03:00
|
|
|
/* -*- 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"
|
2020-08-05 14:26:13 +03:00
|
|
|
#include "nsPaperMargin.h"
|
|
|
|
#include "nsPrinterBase.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
2020-07-13 22:59:26 +03:00
|
|
|
|
2020-08-05 14:26:13 +03:00
|
|
|
using mozilla::ErrorResult;
|
2020-08-20 09:11:08 +03:00
|
|
|
using mozilla::PaperInfo;
|
2020-08-05 14:26:13 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-08-26 14:10:32 +03:00
|
|
|
nsPaper::nsPaper(const mozilla::PaperInfo& aInfo)
|
|
|
|
: mPrinter(nullptr), mInfo(aInfo) {}
|
|
|
|
|
2020-08-05 14:26:13 +03:00
|
|
|
nsPaper::nsPaper(nsPrinterBase& aPrinter, const mozilla::PaperInfo& aInfo)
|
|
|
|
: mPrinter(&aPrinter), mInfo(aInfo) {}
|
|
|
|
|
|
|
|
nsPaper::~nsPaper() = default;
|
2020-07-13 22:59:26 +03:00
|
|
|
|
2020-09-17 23:05:08 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPaper::GetId(nsAString& aId) {
|
|
|
|
aId = mInfo.mId;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-07-13 22:59:26 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPaper::GetName(nsAString& aName) {
|
2020-09-17 23:05:08 +03:00
|
|
|
aName = mInfo.mName.IsEmpty() ? mInfo.mId : mInfo.mName;
|
2020-07-13 22:59:26 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPaper::GetWidth(double* aWidth) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aWidth);
|
2020-08-05 14:26:13 +03:00
|
|
|
*aWidth = mInfo.mSize.Width();
|
2020-07-13 22:59:26 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPaper::GetHeight(double* aHeight) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aHeight);
|
2020-08-05 14:26:13 +03:00
|
|
|
*aHeight = mInfo.mSize.Height();
|
2020-07-13 22:59:26 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2020-07-15 21:38:08 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2020-08-05 14:26:13 +03:00
|
|
|
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();
|
|
|
|
}
|
2020-07-15 21:38:08 +03:00
|
|
|
|
2020-08-05 14:26:13 +03:00
|
|
|
mMarginPromise = promise;
|
2020-07-15 21:38:08 +03:00
|
|
|
|
2020-08-05 14:26:13 +03:00
|
|
|
if (mInfo.mUnwriteableMargin) {
|
|
|
|
auto margin = mozilla::MakeRefPtr<nsPaperMargin>(*mInfo.mUnwriteableMargin);
|
|
|
|
mMarginPromise->MaybeResolve(margin);
|
|
|
|
} else {
|
2020-08-26 14:10:32 +03:00
|
|
|
if (mPrinter) {
|
2020-09-17 23:05:08 +03:00
|
|
|
mPrinter->QueryMarginsForPaper(*mMarginPromise, mInfo.mId);
|
2020-08-26 14:10:32 +03:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("common paper sizes should know their margins");
|
|
|
|
mMarginPromise->MaybeRejectWithNotSupportedError("Margins unavailable");
|
|
|
|
}
|
2020-08-05 14:26:13 +03:00
|
|
|
}
|
2020-07-15 21:38:08 +03:00
|
|
|
|
2020-08-05 14:26:13 +03:00
|
|
|
promise.forget(aPromise);
|
2020-07-15 21:38:08 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|