More code cleanup in print_backend_win.cc
- Make sure ScopedProvider cannot be copied. - Remove out-parameter from GetDeviceCapabilityArray(). - Use base::HeapArray instead of calling std::make_unique<T[]>(). Change-Id: I9dade099778c0d90dda9b35319fdc0652c87ecb7 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5840730 Reviewed-by: Alan Screen <awscreen@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org> Cr-Commit-Position: refs/heads/main@{#1352305}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
2cd2134e9c
commit
cd68e592c1
@ -16,7 +16,9 @@
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "base/containers/heap_array.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/memory/free_deleter.h"
|
||||
#include "base/numerics/safe_conversions.h"
|
||||
@ -47,6 +49,8 @@ namespace {
|
||||
class ScopedProvider {
|
||||
public:
|
||||
explicit ScopedProvider(HPTPROVIDER provider) : provider_(provider) {}
|
||||
ScopedProvider(const ScopedProvider&) = delete;
|
||||
ScopedProvider& operator=(const ScopedProvider&) = delete;
|
||||
|
||||
// Once the object is destroyed, it automatically closes the provider by
|
||||
// calling the XPSModule API.
|
||||
@ -94,24 +98,25 @@ HRESULT StreamOnHGlobalToString(IStream* stream, std::string* out) {
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void GetDeviceCapabilityArray(const wchar_t* printer,
|
||||
const wchar_t* port,
|
||||
WORD id,
|
||||
std::vector<T>* result) {
|
||||
std::vector<T> GetDeviceCapabilityArray(const wchar_t* printer,
|
||||
const wchar_t* port,
|
||||
WORD id) {
|
||||
int count = DeviceCapabilities(printer, port, id, nullptr, nullptr);
|
||||
if (count <= 0)
|
||||
return;
|
||||
if (count <= 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<T> tmp;
|
||||
tmp.resize(count * 2);
|
||||
std::vector<T> results;
|
||||
results.resize(count * 2);
|
||||
count = DeviceCapabilities(printer, port, id,
|
||||
reinterpret_cast<LPTSTR>(tmp.data()), nullptr);
|
||||
if (count <= 0)
|
||||
return;
|
||||
reinterpret_cast<LPTSTR>(results.data()), nullptr);
|
||||
if (count <= 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
CHECK_LE(static_cast<size_t>(count), tmp.size());
|
||||
tmp.resize(count);
|
||||
result->swap(tmp);
|
||||
CHECK_LE(static_cast<size_t>(count), results.size());
|
||||
results.resize(count);
|
||||
return results;
|
||||
}
|
||||
|
||||
gfx::Size GetDefaultDpi(HDC hdc) {
|
||||
@ -161,14 +166,12 @@ bool LoadPaper(const wchar_t* printer,
|
||||
DCHECK_EQ(sizeof(PaperName), sizeof(wchar_t) * kMaxPaperName);
|
||||
|
||||
// Paper
|
||||
std::vector<PaperName> names;
|
||||
GetDeviceCapabilityArray(printer, port, DC_PAPERNAMES, &names);
|
||||
|
||||
std::vector<POINT> sizes;
|
||||
GetDeviceCapabilityArray(printer, port, DC_PAPERSIZE, &sizes);
|
||||
|
||||
std::vector<WORD> ids;
|
||||
GetDeviceCapabilityArray(printer, port, DC_PAPERS, &ids);
|
||||
std::vector<PaperName> names =
|
||||
GetDeviceCapabilityArray<PaperName>(printer, port, DC_PAPERNAMES);
|
||||
std::vector<POINT> sizes =
|
||||
GetDeviceCapabilityArray<POINT>(printer, port, DC_PAPERSIZE);
|
||||
std::vector<WORD> ids =
|
||||
GetDeviceCapabilityArray<WORD>(printer, port, DC_PAPERS);
|
||||
|
||||
DCHECK_EQ(ids.size(), sizes.size());
|
||||
DCHECK_EQ(names.size(), sizes.size());
|
||||
@ -271,9 +274,8 @@ void LoadDpi(const wchar_t* printer,
|
||||
const wchar_t* port,
|
||||
const DEVMODE* devmode,
|
||||
PrinterSemanticCapsAndDefaults* caps) {
|
||||
std::vector<POINT> dpis;
|
||||
GetDeviceCapabilityArray(printer, port, DC_ENUMRESOLUTIONS, &dpis);
|
||||
|
||||
std::vector<POINT> dpis =
|
||||
GetDeviceCapabilityArray<POINT>(printer, port, DC_ENUMRESOLUTIONS);
|
||||
for (size_t i = 0; i < dpis.size(); ++i)
|
||||
caps->dpis.push_back(gfx::Size(dpis[i].x, dpis[i].y));
|
||||
|
||||
@ -342,9 +344,10 @@ mojom::ResultCode PrintBackendWin::EnumeratePrinters(
|
||||
return GetResultCodeFromSystemErrorCode(code);
|
||||
}
|
||||
|
||||
auto printer_info_buffer = std::make_unique<BYTE[]>(bytes_needed);
|
||||
if (!EnumPrinters(kFlags, nullptr, kLevel, printer_info_buffer.get(),
|
||||
bytes_needed, &bytes_needed, &count_returned)) {
|
||||
auto printer_info_buffer = base::HeapArray<BYTE>::Uninit(bytes_needed);
|
||||
if (!EnumPrinters(kFlags, nullptr, kLevel, printer_info_buffer.data(),
|
||||
printer_info_buffer.size(), &bytes_needed,
|
||||
&count_returned)) {
|
||||
NOTREACHED_IN_MIGRATION();
|
||||
return GetResultCodeFromSystemErrorCode(logging::GetLastSystemErrorCode());
|
||||
}
|
||||
@ -354,8 +357,8 @@ mojom::ResultCode PrintBackendWin::EnumeratePrinters(
|
||||
std::string default_printer;
|
||||
GetDefaultPrinterName(default_printer);
|
||||
|
||||
PRINTER_INFO_4* printer_info =
|
||||
reinterpret_cast<PRINTER_INFO_4*>(printer_info_buffer.get());
|
||||
const auto* printer_info =
|
||||
reinterpret_cast<PRINTER_INFO_4*>(printer_info_buffer.data());
|
||||
for (DWORD index = 0; index < count_returned; index++) {
|
||||
ScopedPrinterHandle printer;
|
||||
if (!printer.OpenPrinterWithName(printer_info[index].pPrinterName)) {
|
||||
|
Reference in New Issue
Block a user