Bug 1692971 [wpt PR 27638] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / widget / nsPrinterBase.h
blobda16e00277604e98e8289c1d040110afc2009dff
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 #ifndef nsPrinterBase_h__
7 #define nsPrinterBase_h__
9 #include "mozilla/gfx/Rect.h"
10 #include "nsIPrinter.h"
11 #include "nsTArray.h"
12 #include "nsCycleCollectionParticipant.h"
13 #include "nsISupportsImpl.h"
14 #include "nsPrintSettingsImpl.h"
15 #include "mozilla/EnumeratedArray.h"
16 #include "mozilla/Result.h"
18 namespace mozilla {
20 struct PaperInfo;
22 namespace dom {
23 class Promise;
26 } // namespace mozilla
28 class nsPrinterBase : public nsIPrinter {
29 public:
30 using Promise = mozilla::dom::Promise;
31 using MarginDouble = mozilla::gfx::MarginDouble;
32 using PrintSettingsInitializer = mozilla::PrintSettingsInitializer;
34 NS_IMETHOD GetSupportsDuplex(JSContext*, Promise**) final;
35 NS_IMETHOD GetSupportsColor(JSContext*, Promise**) final;
36 NS_IMETHOD GetSupportsMonochrome(JSContext*, Promise**) final;
37 NS_IMETHOD GetSupportsCollation(JSContext*, Promise**) final;
38 NS_IMETHOD GetPrinterInfo(JSContext*, Promise**) final;
40 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
41 NS_DECL_CYCLE_COLLECTION_CLASS(nsPrinterBase)
43 // We require the paper info array
44 nsPrinterBase() = delete;
46 // No copy or move, we're an identity.
47 nsPrinterBase(const nsPrinterBase&) = delete;
48 nsPrinterBase(nsPrinterBase&&) = delete;
50 void QueryMarginsForPaper(Promise&, const nsString& aPaperId);
52 /**
53 * Caches the argument by copying it into mPrintSettingsInitializer.
54 * If mPrintSettingsInitializer is already populated this is a no-op.
56 void CachePrintSettingsInitializer(
57 const PrintSettingsInitializer& aInitializer);
59 // Data to pass through to create the nsPrinterInfo
60 struct PrinterInfo {
61 nsTArray<mozilla::PaperInfo> mPaperList;
62 PrintSettingsInitializer mDefaultSettings;
65 private:
66 enum class AsyncAttribute {
67 // If you change this list you must update attributeKeys in
68 // nsPrinterBase::AsyncPromiseAttributeGetter.
69 SupportsDuplex = 0,
70 SupportsColor,
71 SupportsMonochrome,
72 SupportsCollation,
73 PrinterInfo,
74 // Just a guard.
75 Last,
78 template <typename Result, typename... Args>
79 using BackgroundTask = Result (nsPrinterBase::*)(Args...) const;
81 // Resolves an async attribute via a background task.
82 template <typename T, typename... Args>
83 nsresult AsyncPromiseAttributeGetter(JSContext*, Promise**, AsyncAttribute,
84 BackgroundTask<T, Args...>,
85 Args... aArgs);
87 protected:
88 nsPrinterBase(const mozilla::CommonPaperInfoArray* aPaperInfoArray);
89 virtual ~nsPrinterBase();
91 // Implementation-specific methods. These must not make assumptions about
92 // which thread they run on.
93 virtual bool SupportsDuplex() const = 0;
94 virtual bool SupportsColor() const = 0;
95 virtual bool SupportsMonochrome() const = 0;
96 virtual bool SupportsCollation() const = 0;
97 virtual MarginDouble GetMarginsForPaper(nsString aPaperId) const = 0;
98 virtual PrinterInfo CreatePrinterInfo() const = 0;
99 // Searches our built-in list of commonly used PWG paper sizes for a matching,
100 // localized PaperInfo. Returns null if there is no matching size.
101 const mozilla::PaperInfo* FindCommonPaperSize(
102 const mozilla::gfx::SizeDouble& aSize) const;
104 private:
105 mozilla::EnumeratedArray<AsyncAttribute, AsyncAttribute::Last,
106 RefPtr<Promise>>
107 mAsyncAttributePromises;
108 // List of built-in, commonly used paper sizes.
109 const RefPtr<const mozilla::CommonPaperInfoArray> mCommonPaperInfo;
112 #endif