Bug 1756130 [wpt PR 32898] - [CSP] Enhance unsafe-eval test to check both realms...
[gecko.git] / widget / nsPrinterBase.h
blobabd4f82705c80ee6b44bb86b52e0a6ef438a59b7
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 CopyFromWithValidation(nsIPrintSettings*, JSContext*,
35 Promise**) override;
36 NS_IMETHOD GetSupportsDuplex(JSContext*, Promise**) final;
37 NS_IMETHOD GetSupportsColor(JSContext*, Promise**) final;
38 NS_IMETHOD GetSupportsMonochrome(JSContext*, Promise**) final;
39 NS_IMETHOD GetSupportsCollation(JSContext*, Promise**) final;
40 NS_IMETHOD GetPrinterInfo(JSContext*, Promise**) final;
42 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
43 NS_DECL_CYCLE_COLLECTION_CLASS(nsPrinterBase)
45 // We require the paper info array
46 nsPrinterBase() = delete;
48 // No copy or move, we're an identity.
49 nsPrinterBase(const nsPrinterBase&) = delete;
50 nsPrinterBase(nsPrinterBase&&) = delete;
52 void QueryMarginsForPaper(Promise&, const nsString& aPaperId);
54 /**
55 * Caches the argument by copying it into mPrintSettingsInitializer.
56 * If mPrintSettingsInitializer is already populated this is a no-op.
58 void CachePrintSettingsInitializer(
59 const PrintSettingsInitializer& aInitializer);
61 // Data to pass through to create the nsPrinterInfo
62 struct PrinterInfo {
63 nsTArray<mozilla::PaperInfo> mPaperList;
64 PrintSettingsInitializer mDefaultSettings;
67 private:
68 enum class AsyncAttribute {
69 // If you change this list you must update attributeKeys in
70 // nsPrinterBase::AsyncPromiseAttributeGetter.
71 SupportsDuplex = 0,
72 SupportsColor,
73 SupportsMonochrome,
74 SupportsCollation,
75 PrinterInfo,
76 // Just a guard.
77 Last,
80 template <typename Result, typename... Args>
81 using BackgroundTask = Result (nsPrinterBase::*)(Args...) const;
83 // Resolves an async attribute via a background task.
84 template <typename T, typename... Args>
85 nsresult AsyncPromiseAttributeGetter(JSContext*, Promise**, AsyncAttribute,
86 BackgroundTask<T, Args...>,
87 Args... aArgs);
89 protected:
90 nsPrinterBase(const mozilla::CommonPaperInfoArray* aPaperInfoArray);
91 virtual ~nsPrinterBase();
93 // Implementation-specific methods. These must not make assumptions about
94 // which thread they run on.
95 virtual bool SupportsDuplex() const = 0;
96 virtual bool SupportsColor() const = 0;
97 virtual bool SupportsMonochrome() const = 0;
98 virtual bool SupportsCollation() const = 0;
99 virtual MarginDouble GetMarginsForPaper(nsString aPaperId) const = 0;
100 virtual PrinterInfo CreatePrinterInfo() const = 0;
101 // Searches our built-in list of commonly used PWG paper sizes for a matching,
102 // localized PaperInfo. Returns null if there is no matching size.
103 const mozilla::PaperInfo* FindCommonPaperSize(
104 const mozilla::gfx::SizeDouble& aSize) const;
106 private:
107 mozilla::EnumeratedArray<AsyncAttribute, AsyncAttribute::Last,
108 RefPtr<Promise>>
109 mAsyncAttributePromises;
110 // List of built-in, commonly used paper sizes.
111 const RefPtr<const mozilla::CommonPaperInfoArray> mCommonPaperInfo;
114 #endif