2020-08-04 15:06:37 +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 "nsPrinterBase.h"
|
2020-08-05 14:26:13 +03:00
|
|
|
#include "nsPaperMargin.h"
|
|
|
|
#include <utility>
|
2020-08-05 04:24:49 +03:00
|
|
|
#include "nsPaper.h"
|
2020-08-20 09:11:08 +03:00
|
|
|
#include "nsIPrintSettings.h"
|
2020-08-10 01:05:57 +03:00
|
|
|
#include "PrintBackgroundTask.h"
|
2020-08-04 15:06:37 +03:00
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
2020-08-05 14:26:13 +03:00
|
|
|
using mozilla::dom::Promise;
|
|
|
|
using mozilla::gfx::MarginDouble;
|
2020-08-04 15:06:37 +03:00
|
|
|
|
2020-10-14 00:52:04 +03:00
|
|
|
// The maximum error when considering a paper size equal, in points.
|
|
|
|
// There is some variance in the actual sizes returned by printer drivers and
|
|
|
|
// print servers for paper sizes. This is a best-guess based on initial
|
|
|
|
// telemetry which should catch most near-miss dimensions. This should let us
|
|
|
|
// get consistent paper size names even when the size isn't quite exactly the
|
|
|
|
// correct size.
|
|
|
|
static constexpr double kPaperSizePointsEpsilon = 4.0;
|
|
|
|
|
2020-11-03 23:35:38 +03:00
|
|
|
// Basic implementation of nsIPrinterInfo
|
|
|
|
class nsPrinterInfo : public nsIPrinterInfo {
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(nsPrinterInfo)
|
|
|
|
NS_DECL_NSIPRINTERINFO
|
|
|
|
nsPrinterInfo() = delete;
|
|
|
|
nsPrinterInfo(nsPrinterBase& aPrinter,
|
|
|
|
const nsPrinterBase::PrinterInfo& aPrinterInfo)
|
|
|
|
: mDefaultSettings(
|
|
|
|
CreatePlatformPrintSettings(aPrinterInfo.mDefaultSettings)) {
|
|
|
|
mPaperList.SetCapacity(aPrinterInfo.mPaperList.Length());
|
|
|
|
for (const PaperInfo& info : aPrinterInfo.mPaperList) {
|
|
|
|
mPaperList.AppendElement(MakeRefPtr<nsPaper>(aPrinter, info));
|
|
|
|
}
|
2020-08-24 03:53:46 +03:00
|
|
|
}
|
2020-11-03 23:35:38 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~nsPrinterInfo() = default;
|
|
|
|
|
|
|
|
nsTArray<RefPtr<nsIPaper>> mPaperList;
|
|
|
|
RefPtr<nsIPrintSettings> mDefaultSettings;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrinterInfo::GetPaperList(nsTArray<RefPtr<nsIPaper>>& aPaperList) {
|
|
|
|
aPaperList = mPaperList.Clone();
|
|
|
|
return NS_OK;
|
2020-08-24 03:53:46 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 23:35:38 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrinterInfo::GetDefaultSettings(nsIPrintSettings** aDefaultSettings) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aDefaultSettings);
|
|
|
|
MOZ_ASSERT(mDefaultSettings);
|
|
|
|
RefPtr<nsIPrintSettings> settings = mDefaultSettings;
|
|
|
|
settings.forget(aDefaultSettings);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION(nsPrinterInfo, mPaperList, mDefaultSettings)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsPrinterInfo)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIPrinterInfo)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPrinterInfo)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsPrinterInfo)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsPrinterInfo)
|
|
|
|
|
2020-08-04 15:06:37 +03:00
|
|
|
template <typename Index, Index Size, typename Value>
|
|
|
|
inline void ImplCycleCollectionTraverse(
|
|
|
|
nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
EnumeratedArray<Index, Size, Value>& aArray, const char* aName,
|
|
|
|
uint32_t aFlags = 0) {
|
|
|
|
aFlags |= CycleCollectionEdgeNameArrayFlag;
|
|
|
|
for (Value& element : aArray) {
|
|
|
|
ImplCycleCollectionTraverse(aCallback, element, aName, aFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Index, Index Size, typename Value>
|
|
|
|
inline void ImplCycleCollectionUnlink(
|
|
|
|
EnumeratedArray<Index, Size, Value>& aArray) {
|
|
|
|
for (Value& element : aArray) {
|
|
|
|
ImplCycleCollectionUnlink(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 01:05:57 +03:00
|
|
|
namespace mozilla {
|
2020-08-05 14:26:13 +03:00
|
|
|
|
|
|
|
template <>
|
|
|
|
void ResolveOrReject(Promise& aPromise, nsPrinterBase&,
|
|
|
|
const MarginDouble& aResult) {
|
|
|
|
auto margin = MakeRefPtr<nsPaperMargin>(aResult);
|
|
|
|
aPromise.MaybeResolve(margin);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void ResolveOrReject(Promise& aPromise, nsPrinterBase& aPrinter,
|
|
|
|
const nsTArray<PaperInfo>& aResult) {
|
|
|
|
nsTArray<RefPtr<nsPaper>> result;
|
|
|
|
result.SetCapacity(aResult.Length());
|
|
|
|
for (const PaperInfo& info : aResult) {
|
|
|
|
result.AppendElement(MakeRefPtr<nsPaper>(aPrinter, info));
|
|
|
|
}
|
|
|
|
aPromise.MaybeResolve(result);
|
|
|
|
}
|
|
|
|
|
2020-08-20 09:11:08 +03:00
|
|
|
template <>
|
|
|
|
void ResolveOrReject(Promise& aPromise, nsPrinterBase& aPrinter,
|
|
|
|
const PrintSettingsInitializer& aResult) {
|
|
|
|
aPromise.MaybeResolve(
|
|
|
|
RefPtr<nsIPrintSettings>(CreatePlatformPrintSettings(aResult)));
|
|
|
|
}
|
|
|
|
|
2020-11-03 23:35:38 +03:00
|
|
|
template <>
|
|
|
|
void ResolveOrReject(Promise& aPromise, nsPrinterBase& aPrinter,
|
|
|
|
const nsPrinterBase::PrinterInfo& aResult) {
|
|
|
|
aPromise.MaybeResolve(MakeRefPtr<nsPrinterInfo>(aPrinter, aResult));
|
|
|
|
}
|
|
|
|
|
2020-08-10 01:05:57 +03:00
|
|
|
} // namespace mozilla
|
2020-08-05 14:26:13 +03:00
|
|
|
|
|
|
|
template <typename T, typename... Args>
|
2020-08-04 15:06:37 +03:00
|
|
|
nsresult nsPrinterBase::AsyncPromiseAttributeGetter(
|
|
|
|
JSContext* aCx, Promise** aResultPromise, AsyncAttribute aAttribute,
|
2020-08-05 14:26:13 +03:00
|
|
|
BackgroundTask<T, Args...> aBackgroundTask, Args... aArgs) {
|
2020-08-04 15:06:37 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2020-08-31 17:36:45 +03:00
|
|
|
|
2020-09-07 21:58:48 +03:00
|
|
|
static constexpr EnumeratedArray<AsyncAttribute, AsyncAttribute::Last,
|
|
|
|
nsLiteralCString>
|
2020-08-31 17:36:45 +03:00
|
|
|
attributeKeys{"SupportsDuplex"_ns, "SupportsColor"_ns,
|
2020-09-07 21:58:48 +03:00
|
|
|
"SupportsMonochrome"_ns, "SupportsCollation"_ns,
|
2020-11-03 23:35:38 +03:00
|
|
|
"PrinterInfo"_ns};
|
2020-08-10 01:05:57 +03:00
|
|
|
return mozilla::AsyncPromiseAttributeGetter(
|
|
|
|
*this, mAsyncAttributePromises[aAttribute], aCx, aResultPromise,
|
2020-08-31 17:36:45 +03:00
|
|
|
attributeKeys[aAttribute], aBackgroundTask, std::forward<Args>(aArgs)...);
|
2020-08-04 15:06:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPrinterBase::GetSupportsDuplex(JSContext* aCx,
|
|
|
|
Promise** aResultPromise) {
|
2020-08-10 01:05:57 +03:00
|
|
|
return AsyncPromiseAttributeGetter(aCx, aResultPromise,
|
|
|
|
AsyncAttribute::SupportsDuplex,
|
|
|
|
&nsPrinterBase::SupportsDuplex);
|
2020-08-04 15:06:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPrinterBase::GetSupportsColor(JSContext* aCx,
|
|
|
|
Promise** aResultPromise) {
|
2020-08-10 01:05:57 +03:00
|
|
|
return AsyncPromiseAttributeGetter(aCx, aResultPromise,
|
|
|
|
AsyncAttribute::SupportsColor,
|
|
|
|
&nsPrinterBase::SupportsColor);
|
2020-08-04 15:06:37 +03:00
|
|
|
}
|
|
|
|
|
2020-09-07 21:58:48 +03:00
|
|
|
NS_IMETHODIMP nsPrinterBase::GetSupportsMonochrome(JSContext* aCx,
|
|
|
|
Promise** aResultPromise) {
|
|
|
|
return AsyncPromiseAttributeGetter(aCx, aResultPromise,
|
|
|
|
AsyncAttribute::SupportsMonochrome,
|
|
|
|
&nsPrinterBase::SupportsMonochrome);
|
|
|
|
}
|
|
|
|
|
2020-08-14 23:41:59 +03:00
|
|
|
NS_IMETHODIMP nsPrinterBase::GetSupportsCollation(JSContext* aCx,
|
|
|
|
Promise** aResultPromise) {
|
|
|
|
return AsyncPromiseAttributeGetter(aCx, aResultPromise,
|
|
|
|
AsyncAttribute::SupportsCollation,
|
|
|
|
&nsPrinterBase::SupportsCollation);
|
|
|
|
}
|
|
|
|
|
2020-11-03 23:35:38 +03:00
|
|
|
NS_IMETHODIMP nsPrinterBase::GetPrinterInfo(JSContext* aCx,
|
|
|
|
Promise** aResultPromise) {
|
2020-08-10 01:05:57 +03:00
|
|
|
return AsyncPromiseAttributeGetter(aCx, aResultPromise,
|
2020-11-03 23:35:38 +03:00
|
|
|
AsyncAttribute::PrinterInfo,
|
|
|
|
&nsPrinterBase::CreatePrinterInfo);
|
2020-08-05 04:24:49 +03:00
|
|
|
}
|
|
|
|
|
2020-09-17 23:05:08 +03:00
|
|
|
void nsPrinterBase::QueryMarginsForPaper(Promise& aPromise,
|
|
|
|
const nsString& aPaperId) {
|
2020-08-31 17:36:45 +03:00
|
|
|
return SpawnPrintBackgroundTask(*this, aPromise, "MarginsForPaper"_ns,
|
2020-08-10 01:05:57 +03:00
|
|
|
&nsPrinterBase::GetMarginsForPaper, aPaperId);
|
2020-08-05 14:26:13 +03:00
|
|
|
}
|
|
|
|
|
2020-08-05 04:24:49 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(nsPrinterBase, mAsyncAttributePromises)
|
2020-08-04 15:06:37 +03:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsPrinterBase)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIPrinter)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPrinter)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsPrinterBase)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsPrinterBase)
|
|
|
|
|
2020-10-14 00:52:04 +03:00
|
|
|
nsPrinterBase::nsPrinterBase(const CommonPaperInfoArray* aPaperInfoArray)
|
|
|
|
: mCommonPaperInfo(aPaperInfoArray) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aPaperInfoArray, "Localized paper info was null");
|
|
|
|
}
|
2020-08-04 15:06:37 +03:00
|
|
|
nsPrinterBase::~nsPrinterBase() = default;
|
2020-10-14 00:52:04 +03:00
|
|
|
|
|
|
|
const PaperInfo* nsPrinterBase::FindCommonPaperSize(
|
|
|
|
const gfx::SizeDouble& aSize) const {
|
|
|
|
for (const PaperInfo& paper : *mCommonPaperInfo) {
|
|
|
|
if (std::abs(paper.mSize.width - aSize.width) <= kPaperSizePointsEpsilon &&
|
|
|
|
std::abs(paper.mSize.height - aSize.height) <=
|
|
|
|
kPaperSizePointsEpsilon) {
|
|
|
|
return &paper;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|