summaryrefslogtreecommitdiffstats
path: root/widget/nsPrinterListBase.cpp
blob: a362474cb5b54b12768a2fbe98cdb2fb124a3575 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* -*- 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 "nsPrinterListBase.h"
#include "PrintBackgroundTask.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/gfx/Rect.h"
#include "mozilla/IntegerRange.h"
#include "mozilla/intl/Localization.h"
#include "mozilla/Maybe.h"
#include "mozilla/RefPtr.h"
#include "xpcpublic.h"

using namespace mozilla;

using mozilla::ErrorResult;
using mozilla::intl::Localization;
using PrinterInfo = nsPrinterListBase::PrinterInfo;
using MarginDouble = mozilla::gfx::MarginDouble;

nsPrinterListBase::nsPrinterListBase() = default;
nsPrinterListBase::~nsPrinterListBase() = default;

NS_IMPL_CYCLE_COLLECTION(nsPrinterListBase, mPrintersPromise)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsPrinterListBase)
  NS_INTERFACE_MAP_ENTRY(nsIPrinterList)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPrinterList)
NS_INTERFACE_MAP_END

NS_IMPL_CYCLE_COLLECTING_ADDREF(nsPrinterListBase)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsPrinterListBase)

namespace mozilla {

template <>
void ResolveOrReject(dom::Promise& aPromise, nsPrinterListBase& aList,
                     const nsTArray<PrinterInfo>& aInfo) {
  nsTArray<RefPtr<nsIPrinter>> printers;
  printers.SetCapacity(aInfo.Length());
  for (auto& info : aInfo) {
    printers.AppendElement(aList.CreatePrinter(info));
  }
  aPromise.MaybeResolve(printers);
}

template <>
void ResolveOrReject(dom::Promise& aPromise, nsPrinterListBase& aList,
                     const Maybe<PrinterInfo>& aInfo) {
  if (aInfo) {
    aPromise.MaybeResolve(aList.CreatePrinter(aInfo.value()));
  } else {
    aPromise.MaybeRejectWithNotFoundError("Printer not found");
  }
}

}  // namespace mozilla

NS_IMETHODIMP nsPrinterListBase::GetPrinters(JSContext* aCx,
                                             Promise** aResult) {
  EnsureCommonPaperInfo(aCx);
  return mozilla::AsyncPromiseAttributeGetter(*this, mPrintersPromise, aCx,
                                              aResult, "Printers"_ns,
                                              &nsPrinterListBase::Printers);
}

NS_IMETHODIMP nsPrinterListBase::GetPrinterByName(const nsAString& aPrinterName,
                                                  JSContext* aCx,
                                                  Promise** aResult) {
  EnsureCommonPaperInfo(aCx);
  return PrintBackgroundTaskPromise(*this, aCx, aResult, "PrinterByName"_ns,
                                    &nsPrinterListBase::PrinterByName,
                                    nsString{aPrinterName});
}

NS_IMETHODIMP nsPrinterListBase::GetPrinterBySystemName(
    const nsAString& aPrinterName, JSContext* aCx, Promise** aResult) {
  EnsureCommonPaperInfo(aCx);
  return PrintBackgroundTaskPromise(
      *this, aCx, aResult, "PrinterBySystemName"_ns,
      &nsPrinterListBase::PrinterBySystemName, nsString{aPrinterName});
}

NS_IMETHODIMP nsPrinterListBase::GetNamedOrDefaultPrinter(
    const nsAString& aPrinterName, JSContext* aCx, Promise** aResult) {
  EnsureCommonPaperInfo(aCx);
  return PrintBackgroundTaskPromise(
      *this, aCx, aResult, "NamedOrDefaultPrinter"_ns,
      &nsPrinterListBase::NamedOrDefaultPrinter, nsString{aPrinterName});
}

Maybe<PrinterInfo> nsPrinterListBase::NamedOrDefaultPrinter(
    nsString aName) const {
  if (Maybe<PrinterInfo> value = PrinterByName(std::move(aName))) {
    return value;
  }

  // Since the name had to be passed by-value, we can re-use it to fetch the
  // default printer name, potentially avoiding an extra string allocation.
  if (NS_SUCCEEDED(SystemDefaultPrinterName(aName))) {
    return PrinterByName(std::move(aName));
  }

  return Nothing();
}

NS_IMETHODIMP nsPrinterListBase::GetFallbackPaperList(JSContext* aCx,
                                                      Promise** aResult) {
  ErrorResult rv;
  nsCOMPtr<nsIGlobalObject> global = xpc::CurrentNativeGlobal(aCx);
  RefPtr<Promise> promise = Promise::Create(global, rv);
  if (MOZ_UNLIKELY(rv.Failed())) {
    *aResult = nullptr;
    return rv.StealNSResult();
  }

  EnsureCommonPaperInfo(aCx);
  nsTArray<RefPtr<nsPaper>> papers;
  papers.SetCapacity(nsPaper::kNumCommonPaperSizes);
  for (const auto& info : *mCommonPaperInfo) {
    papers.AppendElement(MakeRefPtr<nsPaper>(info));
  }

  promise->MaybeResolve(papers);
  promise.forget(aResult);
  return NS_OK;
}

void nsPrinterListBase::EnsureCommonPaperInfo(JSContext* aCx) {
  MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
  if (mCommonPaperInfo) {
    return;
  }
  RefPtr<CommonPaperInfoArray> localizedPaperInfo =
      MakeRefPtr<CommonPaperInfoArray>();
  CommonPaperInfoArray& paperArray = *localizedPaperInfo;
  // Apply localization to the names while constructing the PaperInfo, if
  // available (otherwise leave them as the internal keys, which are at least
  // somewhat recognizable).
  IgnoredErrorResult rv;
  nsTArray<nsCString> resIds = {
      "toolkit/printing/printUI.ftl"_ns,
  };
  RefPtr<Localization> l10n = Localization::Create(resIds, true);

  for (auto i : IntegerRange(nsPaper::kNumCommonPaperSizes)) {
    const CommonPaperSize& size = nsPaper::kCommonPaperSizes[i];
    PaperInfo& info = paperArray[i];

    nsAutoCString key{"printui-paper-"};
    key.Append(size.mLocalizableNameKey);
    nsAutoCString name;
    l10n->FormatValueSync(key, {}, name, rv);

    // Fill out the info with our PWG size and the localized name.
    info.mId = size.mPWGName;
    CopyUTF8toUTF16(
        (rv.Failed() || name.IsEmpty())
            ? static_cast<const nsCString&>(size.mLocalizableNameKey)
            : name,
        info.mName);
    info.mSize = size.mSize;
    info.mUnwriteableMargin = Some(MarginDouble{});
  }
  mCommonPaperInfo = std::move(localizedPaperInfo);
}