Skip to content

Commit

Permalink
Enable the cups backend only when --enable-native-cups is specified.
Browse files Browse the repository at this point in the history
CUPS printers will now be selectable in the print menus.

NOTE: This does not enable printing to said printers.

BUG=607668

TEST=Launch chrome with --enable-native-cups flag, open print preview,
change printers.  See that CUPS printers are listed as available
printers.

Review-Url: https://codereview.chromium.org/1934013002
Cr-Commit-Position: refs/heads/master@{#402654}
  • Loading branch information
skau authored and Commit bot committed Jun 29, 2016
1 parent 709246a commit 8ffe775
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 49 deletions.
5 changes: 5 additions & 0 deletions chrome/browser/chromeos/chrome_browser_main_chromeos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
#include "net/socket/ssl_server_socket.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context_getter.h"
#include "printing/backend/print_backend.h"
#include "ui/base/ime/chromeos/ime_keyboard.h"
#include "ui/base/ime/chromeos/input_method_manager.h"
#include "ui/base/touch/touch_device.h"
Expand Down Expand Up @@ -505,6 +506,10 @@ void ChromeBrowserMainPartsChromeos::PreProfileInit() {
WizardController::SetZeroDelays();
}

// Enable/disable native CUPS integration
printing::PrintBackend::SetNativeCupsEnabled(
parsed_command_line().HasSwitch(::switches::kEnableNativeCups));

power_prefs_.reset(new PowerPrefs(PowerPolicyController::Get()));

// In Aura builds this will initialize ash::Shell.
Expand Down
16 changes: 6 additions & 10 deletions printing/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,6 @@ component("printing") {
]
}

if (is_chromeos) {
sources += [
"printing_context_no_system_dialog.cc",
"printing_context_no_system_dialog.h",
]
}

if (use_cups) {
configs += [ ":cups" ]

Expand Down Expand Up @@ -177,15 +170,18 @@ component("printing") {
"backend/cups_helper.cc",
"backend/cups_helper.h",
"backend/print_backend_cups.cc",
"backend/print_backend_cups.h",
]
}

if (is_chromeos) {
# PRINT_BACKEND_AVAILABLE disables the default dummy implementation
# of the print backend and enables a custom implementation instead.
defines += [ "PRINT_BACKEND_AVAILABLE" ]

sources += [ "backend/print_backend_chromeos.cc" ]
sources += [
"backend/print_backend_chromeos.cc",
"printing_context_no_system_dialog.cc",
"printing_context_no_system_dialog.h",
]
} else if (is_linux) { # Non-ChromeOS Linux.
sources += [
"printing_context_linux.cc",
Expand Down
14 changes: 14 additions & 0 deletions printing/backend/print_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

#include "printing/backend/print_backend.h"

namespace {
bool g_native_cups_enabled = false;
} // namespace

namespace printing {

PrinterBasicInfo::PrinterBasicInfo()
Expand Down Expand Up @@ -40,4 +44,14 @@ PrinterCapsAndDefaults::~PrinterCapsAndDefaults() {}

PrintBackend::~PrintBackend() {}

// static
bool PrintBackend::GetNativeCupsEnabled() {
return g_native_cups_enabled;
}

// static
void PrintBackend::SetNativeCupsEnabled(bool enabled) {
g_native_cups_enabled = enabled;
}

} // namespace printing
5 changes: 5 additions & 0 deletions printing/backend/print_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ class PRINTING_EXPORT PrintBackend
static scoped_refptr<PrintBackend> CreateInstance(
const base::DictionaryValue* print_backend_settings);

// Returns the value of the native cups flag
static bool GetNativeCupsEnabled();

static void SetNativeCupsEnabled(bool enabled);

protected:
friend class base::RefCountedThreadSafe<PrintBackend>;
virtual ~PrintBackend();
Expand Down
28 changes: 27 additions & 1 deletion printing/backend/print_backend_chromeos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
#include "printing/backend/print_backend.h"

#include "base/logging.h"
#include "base/values.h"
#include "printing/backend/print_backend_consts.h"
#include "url/gurl.h"

#if defined(USE_CUPS)
#include "printing/backend/print_backend_cups.h"
#endif // defined(USE_CUPS)

namespace printing {

Expand Down Expand Up @@ -72,8 +79,27 @@ bool PrintBackendChromeOS::IsValidPrinter(const std::string& printer_name) {

scoped_refptr<PrintBackend> PrintBackend::CreateInstance(
const base::DictionaryValue* print_backend_settings) {
if (GetNativeCupsEnabled()) {
#if defined(USE_CUPS)
std::string print_server_url_str;
std::string cups_blocking;
int encryption = HTTP_ENCRYPT_NEVER;
if (print_backend_settings) {
print_backend_settings->GetString(kCUPSPrintServerURL,
&print_server_url_str);

print_backend_settings->GetString(kCUPSBlocking, &cups_blocking);

print_backend_settings->GetInteger(kCUPSEncryption, &encryption);
}
GURL print_server_url(print_server_url_str.c_str());
return new PrintBackendCUPS(print_server_url,
static_cast<http_encryption_t>(encryption),
cups_blocking == kValueTrue);
#endif // defined(USE_CUPS)
}

return new PrintBackendChromeOS();
}

} // namespace printing

43 changes: 5 additions & 38 deletions printing/backend/print_backend_cups.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "printing/backend/print_backend.h"
#include "printing/backend/print_backend_cups.h"

#include <dlfcn.h>
#include <errno.h>
#include <pthread.h>

#include <string>

#include "base/debug/leak_annotations.h"
#include "base/files/file_util.h"
#include "base/lazy_instance.h"
Expand Down Expand Up @@ -68,43 +70,6 @@ bool PrinterBasicInfoFromCUPS(const cups_dest_t& printer,

} // namespace

class PrintBackendCUPS : public PrintBackend {
public:
PrintBackendCUPS(const GURL& print_server_url,
http_encryption_t encryption, bool blocking);

private:
~PrintBackendCUPS() override {}

// PrintBackend implementation.
bool EnumeratePrinters(PrinterList* printer_list) override;
std::string GetDefaultPrinterName() override;
bool GetPrinterBasicInfo(const std::string& printer_name,
PrinterBasicInfo* printer_info) override;
bool GetPrinterSemanticCapsAndDefaults(
const std::string& printer_name,
PrinterSemanticCapsAndDefaults* printer_info) override;
bool GetPrinterCapsAndDefaults(const std::string& printer_name,
PrinterCapsAndDefaults* printer_info) override;
std::string GetPrinterDriverInfo(const std::string& printer_name) override;
bool IsValidPrinter(const std::string& printer_name) override;

// The following functions are wrappers around corresponding CUPS functions.
// <functions>2() are called when print server is specified, and plain version
// in another case. There is an issue specifying CUPS_HTTP_DEFAULT in the
// functions>2(), it does not work in CUPS prior to 1.4.
int GetDests(cups_dest_t** dests);
base::FilePath GetPPD(const char* name);

// Wrapper around cupsGetNamedDest(). Returned result should be freed with
// cupsFreeDests().
cups_dest_t* GetNamedDest(const std::string& printer_name);

GURL print_server_url_;
http_encryption_t cups_encryption_;
bool blocking_;
};

PrintBackendCUPS::PrintBackendCUPS(const GURL& print_server_url,
http_encryption_t encryption,
bool blocking)
Expand Down Expand Up @@ -230,6 +195,7 @@ bool PrintBackendCUPS::IsValidPrinter(const std::string& printer_name) {
return true;
}

#if !defined(OS_CHROMEOS)
scoped_refptr<PrintBackend> PrintBackend::CreateInstance(
const base::DictionaryValue* print_backend_settings) {
std::string print_server_url_str, cups_blocking;
Expand All @@ -248,6 +214,7 @@ scoped_refptr<PrintBackend> PrintBackend::CreateInstance(
static_cast<http_encryption_t>(encryption),
cups_blocking == kValueTrue);
}
#endif // !defined(OS_CHROMEOS)

int PrintBackendCUPS::GetDests(cups_dest_t** dests) {
if (print_server_url_.is_empty()) { // Use default (local) print server.
Expand Down
57 changes: 57 additions & 0 deletions printing/backend/print_backend_cups.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef PRINTING_BACKEND_PRINT_BACKEND_CUPS_H_
#define PRINTING_BACKEND_PRINT_BACKEND_CUPS_H_

#include <string>

#include "base/files/file_util.h"
#include "printing/backend/cups_helper.h"
#include "printing/backend/print_backend.h"
#include "url/gurl.h"

namespace printing {

class PrintBackendCUPS : public PrintBackend {
public:
PrintBackendCUPS(const GURL& print_server_url,
http_encryption_t encryption,
bool blocking);

private:
~PrintBackendCUPS() override {}

// PrintBackend implementation.
bool EnumeratePrinters(PrinterList* printer_list) override;
std::string GetDefaultPrinterName() override;
bool GetPrinterBasicInfo(const std::string& printer_name,
PrinterBasicInfo* printer_info) override;
bool GetPrinterSemanticCapsAndDefaults(
const std::string& printer_name,
PrinterSemanticCapsAndDefaults* printer_info) override;
bool GetPrinterCapsAndDefaults(const std::string& printer_name,
PrinterCapsAndDefaults* printer_info) override;
std::string GetPrinterDriverInfo(const std::string& printer_name) override;
bool IsValidPrinter(const std::string& printer_name) override;

// The following functions are wrappers around corresponding CUPS functions.
// <functions>2() are called when print server is specified, and plain version
// in another case. There is an issue specifying CUPS_HTTP_DEFAULT in the
// functions>2(), it does not work in CUPS prior to 1.4.
int GetDests(cups_dest_t** dests);
base::FilePath GetPPD(const char* name);

// Wrapper around cupsGetNamedDest(). Returned result should be freed with
// cupsFreeDests().
cups_dest_t* GetNamedDest(const std::string& printer_name);

GURL print_server_url_;
http_encryption_t cups_encryption_;
bool blocking_;
};

} // namespace printing

#endif // PRINTING_BACKEND_PRINT_BACKEND_CUPS_H_

0 comments on commit 8ffe775

Please sign in to comment.