2020-08-04 12:06:37 +00: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 11:26:13 +00:00
|
|
|
#include "nsPaperMargin.h"
|
|
|
|
#include <utility>
|
2020-08-05 01:24:49 +00:00
|
|
|
#include "nsPaper.h"
|
2020-08-20 06:11:08 +00:00
|
|
|
#include "nsIPrintSettings.h"
|
2020-08-09 22:05:57 +00:00
|
|
|
#include "PrintBackgroundTask.h"
|
2020-08-04 12:06:37 +00:00
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
2020-08-05 11:26:13 +00:00
|
|
|
using mozilla::dom::Promise;
|
|
|
|
using mozilla::gfx::MarginDouble;
|
2020-08-04 12:06:37 +00:00
|
|
|
|
2020-08-24 00:53:46 +00:00
|
|
|
void nsPrinterBase::CachePrintSettingsInitializer(
|
|
|
|
const PrintSettingsInitializer& aInitializer) {
|
|
|
|
if (mPrintSettingsInitializer.isNothing()) {
|
|
|
|
mPrintSettingsInitializer.emplace(aInitializer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:06:37 +00: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-09 22:05:57 +00:00
|
|
|
namespace mozilla {
|
2020-08-05 11:26:13 +00: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 06:11:08 +00:00
|
|
|
template <>
|
|
|
|
void ResolveOrReject(Promise& aPromise, nsPrinterBase& aPrinter,
|
|
|
|
const PrintSettingsInitializer& aResult) {
|
2020-08-24 00:53:46 +00:00
|
|
|
aPrinter.CachePrintSettingsInitializer(aResult);
|
2020-08-20 06:11:08 +00:00
|
|
|
aPromise.MaybeResolve(
|
|
|
|
RefPtr<nsIPrintSettings>(CreatePlatformPrintSettings(aResult)));
|
|
|
|
}
|
|
|
|
|
2020-08-09 22:05:57 +00:00
|
|
|
} // namespace mozilla
|
2020-08-05 11:26:13 +00:00
|
|
|
|
|
|
|
template <typename T, typename... Args>
|
2020-08-04 12:06:37 +00:00
|
|
|
nsresult nsPrinterBase::AsyncPromiseAttributeGetter(
|
|
|
|
JSContext* aCx, Promise** aResultPromise, AsyncAttribute aAttribute,
|
2020-08-05 11:26:13 +00:00
|
|
|
BackgroundTask<T, Args...> aBackgroundTask, Args... aArgs) {
|
2020-08-04 12:06:37 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2020-08-31 14:36:45 +00:00
|
|
|
|
|
|
|
static const EnumeratedArray<AsyncAttribute, AsyncAttribute::Last, nsCString>
|
|
|
|
attributeKeys{"SupportsDuplex"_ns, "SupportsColor"_ns,
|
|
|
|
"SupportsCollation"_ns, "PaperList"_ns};
|
2020-08-09 22:05:57 +00:00
|
|
|
return mozilla::AsyncPromiseAttributeGetter(
|
|
|
|
*this, mAsyncAttributePromises[aAttribute], aCx, aResultPromise,
|
2020-08-31 14:36:45 +00:00
|
|
|
attributeKeys[aAttribute], aBackgroundTask, std::forward<Args>(aArgs)...);
|
2020-08-04 12:06:37 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 06:11:08 +00:00
|
|
|
NS_IMETHODIMP nsPrinterBase::CreateDefaultSettings(JSContext* aCx,
|
|
|
|
Promise** aResultPromise) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2020-08-24 00:53:46 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aResultPromise);
|
|
|
|
|
|
|
|
if (mPrintSettingsInitializer.isSome()) {
|
|
|
|
ErrorResult rv;
|
|
|
|
RefPtr<dom::Promise> promise =
|
|
|
|
dom::Promise::Create(xpc::CurrentNativeGlobal(aCx), rv);
|
|
|
|
|
|
|
|
if (MOZ_UNLIKELY(rv.Failed())) {
|
|
|
|
*aResultPromise = nullptr;
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<nsIPrintSettings> settings(
|
|
|
|
CreatePlatformPrintSettings(mPrintSettingsInitializer.ref()));
|
|
|
|
promise->MaybeResolve(settings);
|
|
|
|
|
|
|
|
promise.forget(aResultPromise);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-08-20 06:11:08 +00:00
|
|
|
return PrintBackgroundTaskPromise(*this, aCx, aResultPromise,
|
2020-08-31 14:36:45 +00:00
|
|
|
"DefaultSettings"_ns,
|
2020-08-20 06:11:08 +00:00
|
|
|
&nsPrinterBase::DefaultSettings);
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:06:37 +00:00
|
|
|
NS_IMETHODIMP nsPrinterBase::GetSupportsDuplex(JSContext* aCx,
|
|
|
|
Promise** aResultPromise) {
|
2020-08-09 22:05:57 +00:00
|
|
|
return AsyncPromiseAttributeGetter(aCx, aResultPromise,
|
|
|
|
AsyncAttribute::SupportsDuplex,
|
|
|
|
&nsPrinterBase::SupportsDuplex);
|
2020-08-04 12:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPrinterBase::GetSupportsColor(JSContext* aCx,
|
|
|
|
Promise** aResultPromise) {
|
2020-08-09 22:05:57 +00:00
|
|
|
return AsyncPromiseAttributeGetter(aCx, aResultPromise,
|
|
|
|
AsyncAttribute::SupportsColor,
|
|
|
|
&nsPrinterBase::SupportsColor);
|
2020-08-04 12:06:37 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 20:41:59 +00:00
|
|
|
NS_IMETHODIMP nsPrinterBase::GetSupportsCollation(JSContext* aCx,
|
|
|
|
Promise** aResultPromise) {
|
|
|
|
return AsyncPromiseAttributeGetter(aCx, aResultPromise,
|
|
|
|
AsyncAttribute::SupportsCollation,
|
|
|
|
&nsPrinterBase::SupportsCollation);
|
|
|
|
}
|
|
|
|
|
2020-08-05 01:24:49 +00:00
|
|
|
NS_IMETHODIMP nsPrinterBase::GetPaperList(JSContext* aCx,
|
|
|
|
Promise** aResultPromise) {
|
2020-08-09 22:05:57 +00:00
|
|
|
return AsyncPromiseAttributeGetter(aCx, aResultPromise,
|
|
|
|
AsyncAttribute::PaperList,
|
|
|
|
&nsPrinterBase::PaperList);
|
2020-08-05 01:24:49 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 11:26:13 +00:00
|
|
|
void nsPrinterBase::QueryMarginsForPaper(Promise& aPromise, uint64_t aPaperId) {
|
2020-08-31 14:36:45 +00:00
|
|
|
return SpawnPrintBackgroundTask(*this, aPromise, "MarginsForPaper"_ns,
|
2020-08-09 22:05:57 +00:00
|
|
|
&nsPrinterBase::GetMarginsForPaper, aPaperId);
|
2020-08-05 11:26:13 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 01:24:49 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(nsPrinterBase, mAsyncAttributePromises)
|
2020-08-04 12:06:37 +00: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)
|
|
|
|
|
|
|
|
nsPrinterBase::nsPrinterBase() = default;
|
|
|
|
nsPrinterBase::~nsPrinterBase() = default;
|