Bug 1852149 Part 1 - Add margin-rule pref r=firefox-style-system-reviewers,emilio
[gecko.git] / widget / nsPrinterListBase.h
blob9f922d74b534ba2c1c87767467c3886869d76239
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 nsPrinterListBase_h__
7 #define nsPrinterListBase_h__
9 #include "nsIPrinterList.h"
11 #include "nsCycleCollectionParticipant.h"
12 #include "nsISupportsImpl.h"
13 #include "nsPaper.h"
14 #include "nsString.h"
16 class nsPrinterListBase : public nsIPrinterList {
17 public:
18 using Promise = mozilla::dom::Promise;
20 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
21 NS_DECL_CYCLE_COLLECTION_CLASS(nsPrinterListBase)
22 NS_IMETHOD GetSystemDefaultPrinterName(nsAString& aName) final {
23 return SystemDefaultPrinterName(aName);
25 NS_IMETHOD GetPrinters(JSContext*, Promise**) final;
26 NS_IMETHOD GetPrinterByName(const nsAString& aPrinterName, JSContext* aCx,
27 Promise** aResult) final;
28 NS_IMETHOD GetPrinterBySystemName(const nsAString& aPrinterName,
29 JSContext* aCx, Promise** aResult) final;
30 NS_IMETHOD GetNamedOrDefaultPrinter(const nsAString& aPrinterName,
31 JSContext* aCx, Promise** aResult) final;
32 NS_IMETHOD GetFallbackPaperList(JSContext*, Promise**) final;
34 struct PrinterInfo {
35 // Both windows and CUPS: The name of the printer.
36 nsString mName;
37 // CUPS only: Handle to owned cups_dest_t.
38 void* mCupsHandle = nullptr;
41 // Called off the main thread, collect information to create an appropriate
42 // list of printers.
43 virtual nsTArray<PrinterInfo> Printers() const = 0;
45 // Create an nsIPrinter object given the information we obtained from the
46 // background thread.
47 virtual RefPtr<nsIPrinter> CreatePrinter(PrinterInfo) const = 0;
49 mozilla::Maybe<PrinterInfo> NamedOrDefaultPrinter(nsString aName) const;
51 // No copy or move, we're an identity.
52 nsPrinterListBase(const nsPrinterListBase&) = delete;
53 nsPrinterListBase(nsPrinterListBase&&) = delete;
55 protected:
56 nsPrinterListBase();
57 virtual ~nsPrinterListBase();
59 // This could be implemented in terms of Printers() and then searching the
60 // returned printer info for a printer of the given name, but we expect
61 // backends to have more efficient methods of implementing this.
62 virtual mozilla::Maybe<PrinterInfo> PrinterByName(nsString aName) const = 0;
64 // Same as NamedPrinter, but uses the system name.
65 // Depending on whether or not there is a more efficient way to address the
66 // printer for a given backend, this may or may not be equivalent to
67 // NamedPrinter.
68 virtual mozilla::Maybe<PrinterInfo> PrinterBySystemName(
69 nsString aName) const = 0;
71 // This is implemented separately from the IDL interface version so that it
72 // can be made const, which allows it to be used while resolving promises.
73 virtual nsresult SystemDefaultPrinterName(nsAString&) const = 0;
75 // Return "paper" sizes to be supported by the Save to PDF destination;
76 // for actual printer drivers the list is retrieved from nsIPrinter.
77 nsTArray<RefPtr<nsPaper>> FallbackPaperList() const;
79 // Constructs mCommonPaperInfo by localizing the sizes in
80 // nsPaper::kCommonPaperSizes and creating corresponding PaperInfo.
81 void EnsureCommonPaperInfo(JSContext* aCx);
83 RefPtr<Promise> mPrintersPromise;
84 // PaperInfo for our fallback sizes and common size localization.
85 // This field contains the same data for every instance of this class.
86 // It's unfortunate that this needs to be a member rather than static data
87 // like nsPaper::kCommonPaperSizes, but that's because PaperInfo contains
88 // localized data, and we need a JSContext to do the localization.
89 RefPtr<const mozilla::CommonPaperInfoArray> mCommonPaperInfo;
92 #endif