Skip to content
Permalink
Browse files

Pass locale to PrintBackend::CreateInstance()

To allow for localization of default paper size, the locale should be
known by the print backend. Change calls of CreateInstance() to take
the locale. When CreateInstance() is called from the browser process,
pass the locale; otherwise, pass an empty string.

Bug: 890904
Change-Id: I0238f1d5fe6ca3bc59715a1c126237627144a8be
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1952523
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#722201}
  • Loading branch information
Daniel Hosseinian Commit Bot
Daniel Hosseinian authored and Commit Bot committed Dec 5, 2019
1 parent 93364ef commit 733106e85daaf3b27f922e4006166c8ad06df85a
@@ -18,6 +18,7 @@
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/post_task.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chromeos/printing/cups_print_job.h"
#include "chrome/browser/chromeos/printing/cups_print_job_manager.h"
@@ -179,7 +180,8 @@ void OnSetSettingsDoneOnIOThread(std::unique_ptr<printing::PrinterQuery> query,
std::unique_ptr<printing::PrinterSemanticCapsAndDefaults>
FetchCapabilitiesOnBlockingTaskRunner(const std::string& printer_id) {
scoped_refptr<printing::PrintBackend> backend(
printing::PrintBackend::CreateInstance(nullptr));
printing::PrintBackend::CreateInstance(
nullptr, g_browser_process->GetApplicationLocale()));
auto caps = std::make_unique<printing::PrinterSemanticCapsAndDefaults>();
if (!backend->GetPrinterSemanticCapsAndDefaults(printer_id, caps.get())) {
LOG(ERROR) << "Failed to get caps for " << printer_id;
@@ -214,8 +214,8 @@ void PrintJobWorker::UpdatePrintSettings(base::Value new_settings,
// not thread-safe and have to be accessed on the UI thread.
base::ScopedAllowBlocking allow_blocking;
#endif
scoped_refptr<PrintBackend> print_backend =
PrintBackend::CreateInstance(nullptr);
scoped_refptr<PrintBackend> print_backend = PrintBackend::CreateInstance(
nullptr, g_browser_process->GetApplicationLocale());
std::string printer_name = *new_settings.FindStringKey(kSettingDeviceName);
crash_key = std::make_unique<crash_keys::ScopedPrinterInfo>(
print_backend->GetPrinterDriverInfo(printer_name));
@@ -15,6 +15,7 @@
#include "base/metrics/histogram_macros.h"
#include "base/task/post_task.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/printing/cups_print_job_manager.h"
#include "chrome/browser/chromeos/printing/cups_print_job_manager_factory.h"
#include "chrome/browser/chromeos/printing/cups_printers_manager.h"
@@ -69,7 +70,8 @@ void AddPrintersToList(const std::vector<chromeos::Printer>& printers,
base::Value FetchCapabilitiesAsync(const std::string& device_name,
const PrinterBasicInfo& basic_info,
bool has_secure_protocol) {
auto print_backend = PrintBackend::CreateInstance(nullptr);
auto print_backend = PrintBackend::CreateInstance(
nullptr, g_browser_process->GetApplicationLocale());
return GetSettingsOnBlockingTaskRunner(
device_name, basic_info, PrinterSemanticCapsAndDefaults::Papers(),
has_secure_protocol, print_backend);
@@ -14,6 +14,7 @@
#include "base/task/post_task.h"
#include "base/threading/scoped_blocking_call.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/ui/webui/print_preview/print_preview_utils.h"
#include "components/printing/browser/printer_capabilities.h"
#include "content/public/browser/browser_thread.h"
@@ -48,8 +49,8 @@ scoped_refptr<base::TaskRunner> CreatePrinterHandlerTaskRunner() {
PrinterList EnumeratePrintersAsync() {
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK);
scoped_refptr<PrintBackend> print_backend(
PrintBackend::CreateInstance(nullptr));
scoped_refptr<PrintBackend> print_backend(PrintBackend::CreateInstance(
nullptr, g_browser_process->GetApplicationLocale()));

PrinterList printer_list;
print_backend->EnumeratePrinters(&printer_list);
@@ -65,8 +66,8 @@ base::Value FetchCapabilitiesAsync(const std::string& device_name) {

base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK);
scoped_refptr<PrintBackend> print_backend(
PrintBackend::CreateInstance(nullptr));
scoped_refptr<PrintBackend> print_backend(PrintBackend::CreateInstance(
nullptr, g_browser_process->GetApplicationLocale()));

VLOG(1) << "Get printer capabilities start for " << device_name;

@@ -84,8 +85,8 @@ base::Value FetchCapabilitiesAsync(const std::string& device_name) {
std::string GetDefaultPrinterAsync() {
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK);
scoped_refptr<PrintBackend> print_backend(
PrintBackend::CreateInstance(nullptr));
scoped_refptr<PrintBackend> print_backend(PrintBackend::CreateInstance(
nullptr, g_browser_process->GetApplicationLocale()));

std::string default_printer = print_backend->GetDefaultPrinterName();
VLOG(1) << "Default Printer: " << default_printer;
@@ -259,7 +259,8 @@ class PrinterWatcherCUPS
// PrintSystem::PrinterWatcher implementation.
bool StartWatching(PrintSystem::PrinterWatcher::Delegate* delegate) override {
scoped_refptr<printing::PrintBackend> print_backend(
printing::PrintBackend::CreateInstance(nullptr));
printing::PrintBackend::CreateInstance(nullptr,
/*locale=*/std::string()));
crash_keys::ScopedPrinterInfo crash_key(
print_backend->GetPrinterDriverInfo(printer_name_));
if (delegate_)
@@ -462,8 +463,8 @@ void PrintSystemCUPS::AddPrintServer(const std::string& url) {
backend_settings.SetInteger(kCUPSEncryption, cups_encryption_);

PrintServerInfoCUPS print_server;
print_server.backend =
printing::PrintBackend::CreateInstance(&backend_settings);
print_server.backend = printing::PrintBackend::CreateInstance(
&backend_settings, /*locale=*/std::string());
print_server.url = GURL(url.c_str());

print_servers_.push_back(print_server);
@@ -64,7 +64,8 @@ class PrintSystemWatcherWin : public base::win::ObjectWatcher::Delegate {

bool Start(const std::string& printer_name, Delegate* delegate) {
scoped_refptr<printing::PrintBackend> print_backend(
printing::PrintBackend::CreateInstance(nullptr));
printing::PrintBackend::CreateInstance(nullptr,
/*locale=*/std::string()));
printer_info_ = print_backend->GetPrinterDriverInfo(printer_name);
crash_keys::ScopedPrinterInfo crash_key(printer_info_);

@@ -239,7 +240,8 @@ class JobSpoolerWin : public PrintSystem::JobSpooler {
JobSpooler::Delegate* delegate) override {
// TODO(gene): add tags handling.
scoped_refptr<printing::PrintBackend> print_backend(
printing::PrintBackend::CreateInstance(nullptr));
printing::PrintBackend::CreateInstance(nullptr,
/*locale=*/std::string()));
crash_keys::ScopedPrinterInfo crash_key(
print_backend->GetPrinterDriverInfo(printer_name));
return core_->Spool(print_ticket, print_ticket_mime_type,
@@ -659,7 +661,9 @@ class PrintSystemWin : public PrintSystem {
};

PrintSystemWin::PrintSystemWin()
: print_backend_(printing::PrintBackend::CreateInstance(nullptr)) {}
: print_backend_(
printing::PrintBackend::CreateInstance(nullptr,
/*locale=*/std::string())) {}

PrintSystem::PrintSystemResult PrintSystemWin::Init() {
use_cdd_ = !base::CommandLine::ForCurrentProcess()->HasSwitch(
@@ -45,7 +45,7 @@ bool PrintingHandler::OnMessageReceived(const IPC::Message& message) {
void PrintingHandler::OnGetPrinterCapsAndDefaults(
const std::string& printer_name) {
scoped_refptr<PrintBackend> print_backend =
PrintBackend::CreateInstance(nullptr);
PrintBackend::CreateInstance(nullptr, /*locale=*/std::string());
PrinterCapsAndDefaults printer_info;

crash_keys::ScopedPrinterInfo crash_key(
@@ -64,7 +64,7 @@ void PrintingHandler::OnGetPrinterCapsAndDefaults(
void PrintingHandler::OnGetPrinterSemanticCapsAndDefaults(
const std::string& printer_name) {
scoped_refptr<PrintBackend> print_backend =
PrintBackend::CreateInstance(nullptr);
PrintBackend::CreateInstance(nullptr, /*locale=*/std::string());
PrinterSemanticCapsAndDefaults printer_info;

crash_keys::ScopedPrinterInfo crash_key(
@@ -51,14 +51,17 @@ PrinterCapsAndDefaults::PrinterCapsAndDefaults(

PrinterCapsAndDefaults::~PrinterCapsAndDefaults() = default;

PrintBackend::PrintBackend(const std::string& locale) : locale_(locale) {}

PrintBackend::~PrintBackend() = default;

// static
scoped_refptr<PrintBackend> PrintBackend::CreateInstance(
const base::DictionaryValue* print_backend_settings) {
const base::DictionaryValue* print_backend_settings,
const std::string& locale) {
return g_print_backend_for_test
? g_print_backend_for_test
: PrintBackend::CreateInstanceImpl(print_backend_settings);
: PrintBackend::CreateInstanceImpl(print_backend_settings, locale);
}

// static
@@ -179,19 +179,27 @@ class PRINTING_EXPORT PrintBackend
// Allocates a print backend. If |print_backend_settings| is nullptr, default
// settings will be used.
static scoped_refptr<PrintBackend> CreateInstance(
const base::DictionaryValue* print_backend_settings);
const base::DictionaryValue* print_backend_settings,
const std::string& locale);

// Test method to override the print backend for testing. Caller should
// retain ownership.
static void SetPrintBackendForTesting(PrintBackend* print_backend);

protected:
friend class base::RefCountedThreadSafe<PrintBackend>;
explicit PrintBackend(const std::string& locale);
virtual ~PrintBackend();

// Provide the actual backend for CreateInstance().
static scoped_refptr<PrintBackend> CreateInstanceImpl(
const base::DictionaryValue* print_backend_settings);
const base::DictionaryValue* print_backend_settings,
const std::string& locale);

const std::string& locale() const { return locale_; }

private:
const std::string locale_;
};

} // namespace printing
@@ -43,7 +43,7 @@ std::unique_ptr<CupsConnection> CreateConnection(
// implementation for use on ChromeOS.
class PrintBackendChromeOS : public PrintBackend {
public:
PrintBackendChromeOS();
explicit PrintBackendChromeOS(const std::string& locale);

// PrintBackend implementation.
bool EnumeratePrinters(PrinterList* printer_list) override;
@@ -60,7 +60,8 @@ class PrintBackendChromeOS : public PrintBackend {
~PrintBackendChromeOS() override = default;
};

PrintBackendChromeOS::PrintBackendChromeOS() = default;
PrintBackendChromeOS::PrintBackendChromeOS(const std::string& locale)
: PrintBackend(locale) {}

bool PrintBackendChromeOS::EnumeratePrinters(PrinterList* printer_list) {
return true;
@@ -95,12 +96,13 @@ bool PrintBackendChromeOS::IsValidPrinter(const std::string& printer_name) {

// static
scoped_refptr<PrintBackend> PrintBackend::CreateInstanceImpl(
const base::DictionaryValue* print_backend_settings) {
const base::DictionaryValue* print_backend_settings,
const std::string& locale) {
#if defined(USE_CUPS)
return base::WrapRefCounted(
new PrintBackendCupsIpp(CreateConnection(print_backend_settings)));
return base::MakeRefCounted<PrintBackendCupsIpp>(
CreateConnection(print_backend_settings), locale);
#else
return base::MakeRefCounted<PrintBackendChromeOS>();
return base::MakeRefCounted<PrintBackendChromeOS>(locale);
#endif // defined(USE_CUPS)
}

@@ -14,6 +14,7 @@
#include "base/files/file_util.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/no_destructor.h"
#include "base/strings/string_number_conversions.h"
#include "base/synchronization/lock.h"
@@ -35,8 +36,10 @@ const char kCUPSPrinterTypeOpt[] = "printer-type";

PrintBackendCUPS::PrintBackendCUPS(const GURL& print_server_url,
http_encryption_t encryption,
bool blocking)
: print_server_url_(print_server_url),
bool blocking,
const std::string& locale)
: PrintBackend(locale),
print_server_url_(print_server_url),
cups_encryption_(encryption),
blocking_(blocking) {}

@@ -211,7 +214,8 @@ bool PrintBackendCUPS::IsValidPrinter(const std::string& printer_name) {

#if !defined(OS_CHROMEOS)
scoped_refptr<PrintBackend> PrintBackend::CreateInstanceImpl(
const base::DictionaryValue* print_backend_settings) {
const base::DictionaryValue* print_backend_settings,
const std::string& locale) {
std::string print_server_url_str, cups_blocking;
int encryption = HTTP_ENCRYPT_NEVER;
if (print_backend_settings) {
@@ -223,9 +227,9 @@ scoped_refptr<PrintBackend> PrintBackend::CreateInstanceImpl(
print_backend_settings->GetInteger(kCUPSEncryption, &encryption);
}
GURL print_server_url(print_server_url_str);
return new PrintBackendCUPS(print_server_url,
static_cast<http_encryption_t>(encryption),
cups_blocking == kValueTrue);
return base::MakeRefCounted<PrintBackendCUPS>(
print_server_url, static_cast<http_encryption_t>(encryption),
cups_blocking == kValueTrue, locale);
}
#endif // !defined(OS_CHROMEOS)

@@ -20,7 +20,8 @@ class PrintBackendCUPS : public PrintBackend {
public:
PrintBackendCUPS(const GURL& print_server_url,
http_encryption_t encryption,
bool blocking);
bool blocking,
const std::string& locale);

// This static function is exposed here for use in the tests.
PRINTING_EXPORT static bool PrinterBasicInfoFromCUPS(
@@ -22,10 +22,11 @@
namespace printing {

PrintBackendCupsIpp::PrintBackendCupsIpp(
std::unique_ptr<CupsConnection> cups_connection)
: cups_connection_(std::move(cups_connection)) {}
std::unique_ptr<CupsConnection> cups_connection,
const std::string& locale)
: PrintBackend(locale), cups_connection_(std::move(cups_connection)) {}

PrintBackendCupsIpp::~PrintBackendCupsIpp() {}
PrintBackendCupsIpp::~PrintBackendCupsIpp() = default;

bool PrintBackendCupsIpp::EnumeratePrinters(PrinterList* printer_list) {
DCHECK(printer_list);
@@ -15,7 +15,8 @@ namespace printing {

class PrintBackendCupsIpp : public PrintBackend {
public:
explicit PrintBackendCupsIpp(std::unique_ptr<CupsConnection> connection);
PrintBackendCupsIpp(std::unique_ptr<CupsConnection> connection,
const std::string& locale);

private:
~PrintBackendCupsIpp() override;
@@ -16,7 +16,8 @@ namespace printing {

class DummyPrintBackend : public PrintBackend {
public:
DummyPrintBackend() {}
explicit DummyPrintBackend(const std::string& locale)
: PrintBackend(locale) {}

bool EnumeratePrinters(PrinterList* printer_list) override { return false; }

@@ -48,15 +49,16 @@ class DummyPrintBackend : public PrintBackend {
}

private:
~DummyPrintBackend() override {}
~DummyPrintBackend() override = default;

DISALLOW_COPY_AND_ASSIGN(DummyPrintBackend);
};

// static
scoped_refptr<PrintBackend> PrintBackend::CreateInstanceImpl(
const base::DictionaryValue* print_backend_settings) {
return new DummyPrintBackend();
const base::DictionaryValue* print_backend_settings,
const std::string& locale) {
return base::MakeRefCounted<DummyPrintBackend>(locale);
}

} // namespace printing
@@ -166,7 +166,7 @@ void LoadDpi(const wchar_t* printer,

class PrintBackendWin : public PrintBackend {
public:
PrintBackendWin() {}
explicit PrintBackendWin(const std::string& locale) : PrintBackend(locale) {}

// PrintBackend implementation.
bool EnumeratePrinters(PrinterList* printer_list) override;
@@ -182,7 +182,7 @@ class PrintBackendWin : public PrintBackend {
bool IsValidPrinter(const std::string& printer_name) override;

protected:
~PrintBackendWin() override {}
~PrintBackendWin() override = default;
};

bool PrintBackendWin::EnumeratePrinters(PrinterList* printer_list) {
@@ -392,8 +392,9 @@ bool PrintBackendWin::IsValidPrinter(const std::string& printer_name) {

// static
scoped_refptr<PrintBackend> PrintBackend::CreateInstanceImpl(
const base::DictionaryValue* print_backend_settings) {
return base::MakeRefCounted<PrintBackendWin>();
const base::DictionaryValue* print_backend_settings,
const std::string& locale) {
return base::MakeRefCounted<PrintBackendWin>(locale);
}

} // namespace printing
@@ -12,7 +12,7 @@

namespace printing {

TestPrintBackend::TestPrintBackend() = default;
TestPrintBackend::TestPrintBackend() : PrintBackend(/*locale=*/std::string()) {}

TestPrintBackend::~TestPrintBackend() = default;

0 comments on commit 733106e

Please sign in to comment.
You can’t perform that action at this time.