Commit 2b2d0021 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Tidy PrintSystemCUPS code.

Change-Id: I44ece9059b99217e0ed488a95b6ee3a84abdf0b0
Reviewed-on: https://chromium-review.googlesource.com/c/1316410Reviewed-by: default avatarSean Kau <skau@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#605637}
parent f6c9abc1
...@@ -51,10 +51,11 @@ const char kCUPSDefaultSupportedTypes[] = ...@@ -51,10 +51,11 @@ const char kCUPSDefaultSupportedTypes[] =
"application/pdf,application/postscript,image/jpeg,image/png,image/gif"; "application/pdf,application/postscript,image/jpeg,image/png,image/gif";
// Time interval to check for printer's updates. // Time interval to check for printer's updates.
const int kCheckForPrinterUpdatesMinutes = 5; constexpr base::TimeDelta kCheckForPrinterUpdatesTime =
base::TimeDelta::FromMinutes(5);
// Job update timeout // Job update timeout
const int kJobUpdateTimeoutSeconds = 5; constexpr base::TimeDelta kJobUpdateTimeout = base::TimeDelta::FromSeconds(5);
// Job id for dry run (it should not affect CUPS job ids, since 0 job-id is // Job id for dry run (it should not affect CUPS job ids, since 0 job-id is
// invalid in CUPS. // invalid in CUPS.
...@@ -135,12 +136,19 @@ class PrintSystemCUPS : public PrintSystem { ...@@ -135,12 +136,19 @@ class PrintSystemCUPS : public PrintSystem {
// <functions>2() are called when print server is specified, and plain // <functions>2() are called when print server is specified, and plain
// version in another case. There is an issue specifing CUPS_HTTP_DEFAULT // version in another case. There is an issue specifing CUPS_HTTP_DEFAULT
// in the <functions>2(), it does not work in CUPS prior to 1.4. // in the <functions>2(), it does not work in CUPS prior to 1.4.
int GetJobs(cups_job_t** jobs, const GURL& url, static int GetJobs(cups_job_t** jobs,
http_encryption_t encryption, const char* name, const GURL& url,
int myjobs, int whichjobs); http_encryption_t encryption,
int PrintFile(const GURL& url, http_encryption_t encryption, const char* name,
const char* name, const char* filename, int myjobs,
const char* title, int num_options, cups_option_t* options); int whichjobs);
static int PrintFile(const GURL& url,
http_encryption_t encryption,
const char* name,
const char* filename,
const char* title,
int num_options,
cups_option_t* options);
void InitPrintBackends(const base::DictionaryValue* print_system_settings); void InitPrintBackends(const base::DictionaryValue* print_system_settings);
void AddPrintServer(const std::string& url); void AddPrintServer(const std::string& url);
...@@ -149,8 +157,8 @@ class PrintSystemCUPS : public PrintSystem { ...@@ -149,8 +157,8 @@ class PrintSystemCUPS : public PrintSystem {
// Full name contains print server url:port and printer name. Short name // Full name contains print server url:port and printer name. Short name
// is the name of the printer in the CUPS server. // is the name of the printer in the CUPS server.
std::string MakeFullPrinterName(const GURL& url, static std::string MakeFullPrinterName(const GURL& url,
const std::string& short_printer_name); const std::string& short_printer_name);
PrintServerInfoCUPS* FindServerByFullName( PrintServerInfoCUPS* FindServerByFullName(
const std::string& full_printer_name, std::string* short_printer_name); const std::string& full_printer_name, std::string* short_printer_name);
...@@ -165,12 +173,12 @@ class PrintSystemCUPS : public PrintSystem { ...@@ -165,12 +173,12 @@ class PrintSystemCUPS : public PrintSystem {
// connected to. // connected to.
std::vector<PrintServerInfoCUPS> print_servers_; std::vector<PrintServerInfoCUPS> print_servers_;
base::TimeDelta update_timeout_; base::TimeDelta update_timeout_ = kCheckForPrinterUpdatesTime;
bool initialized_; bool initialized_ = false;
bool printer_enum_succeeded_; bool printer_enum_succeeded_ = false;
bool notify_delete_; bool notify_delete_ = true;
http_encryption_t cups_encryption_; http_encryption_t cups_encryption_ = HTTP_ENCRYPT_NEVER;
std::string supported_mime_types_; std::string supported_mime_types_ = kCUPSDefaultSupportedTypes;
}; };
class PrintServerWatcherCUPS class PrintServerWatcherCUPS
...@@ -261,7 +269,7 @@ class PrinterWatcherCUPS ...@@ -261,7 +269,7 @@ class PrinterWatcherCUPS
// Schedule next job status update. // Schedule next job status update.
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, base::BindOnce(&PrinterWatcherCUPS::JobStatusUpdate, this), FROM_HERE, base::BindOnce(&PrinterWatcherCUPS::JobStatusUpdate, this),
base::TimeDelta::FromSeconds(kJobUpdateTimeoutSeconds)); kJobUpdateTimeout);
// Schedule next printer check. // Schedule next printer check.
// TODO(gene): Randomize time for the next printer update. // TODO(gene): Randomize time for the next printer update.
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
...@@ -292,7 +300,7 @@ class PrinterWatcherCUPS ...@@ -292,7 +300,7 @@ class PrinterWatcherCUPS
delegate_->OnJobChanged(); delegate_->OnJobChanged();
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, base::BindOnce(&PrinterWatcherCUPS::JobStatusUpdate, this), FROM_HERE, base::BindOnce(&PrinterWatcherCUPS::JobStatusUpdate, this),
base::TimeDelta::FromSeconds(kJobUpdateTimeoutSeconds)); kJobUpdateTimeout);
} }
void PrinterUpdate() { void PrinterUpdate() {
...@@ -400,14 +408,7 @@ class JobSpoolerCUPS : public PrintSystem::JobSpooler { ...@@ -400,14 +408,7 @@ class JobSpoolerCUPS : public PrintSystem::JobSpooler {
}; };
PrintSystemCUPS::PrintSystemCUPS( PrintSystemCUPS::PrintSystemCUPS(
const base::DictionaryValue* print_system_settings) const base::DictionaryValue* print_system_settings) {
: update_timeout_(base::TimeDelta::FromMinutes(
kCheckForPrinterUpdatesMinutes)),
initialized_(false),
printer_enum_succeeded_(false),
notify_delete_(true),
cups_encryption_(HTTP_ENCRYPT_NEVER),
supported_mime_types_(kCUPSDefaultSupportedTypes) {
if (print_system_settings) { if (print_system_settings) {
int timeout; int timeout;
if (print_system_settings->GetInteger(kCUPSUpdateTimeoutMs, &timeout)) if (print_system_settings->GetInteger(kCUPSUpdateTimeoutMs, &timeout))
...@@ -415,8 +416,7 @@ PrintSystemCUPS::PrintSystemCUPS( ...@@ -415,8 +416,7 @@ PrintSystemCUPS::PrintSystemCUPS(
int encryption; int encryption;
if (print_system_settings->GetInteger(kCUPSEncryption, &encryption)) if (print_system_settings->GetInteger(kCUPSEncryption, &encryption))
cups_encryption_ = cups_encryption_ = static_cast<http_encryption_t>(encryption);
static_cast<http_encryption_t>(encryption);
bool notify_delete = true; bool notify_delete = true;
if (print_system_settings->GetBoolean(kCUPSNotifyDelete, &notify_delete)) if (print_system_settings->GetBoolean(kCUPSNotifyDelete, &notify_delete))
...@@ -545,11 +545,10 @@ bool PrintSystemCUPS::ParsePrintTicket( ...@@ -545,11 +545,10 @@ bool PrintSystemCUPS::ParsePrintTicket(
return false; return false;
options->clear(); options->clear();
for (base::DictionaryValue::Iterator it(*ticket_value); !it.IsAtEnd(); for (const auto& it : *ticket_value) {
it.Advance()) {
std::string value; std::string value;
if (it.value().GetAsString(&value)) if (it.second->GetAsString(&value))
(*options)[it.key()] = value; (*options)[it.first] = value;
} }
return true; return true;
...@@ -647,15 +646,16 @@ bool PrintSystemCUPS::GetJobDetails(const std::string& printer_name, ...@@ -647,15 +646,16 @@ bool PrintSystemCUPS::GetJobDetails(const std::string& printer_name,
} }
} }
if (found) if (found) {
VLOG(1) << "CP_CUPS: Job found" VLOG(1) << "CP_CUPS: Job found"
<< ", printer name: " << printer_name << ", printer name: " << printer_name
<< ", cups job id: " << job_id << ", cups job id: " << job_id
<< ", cups job status: " << job_details->status; << ", cups job status: " << job_details->status;
else } else {
LOG(WARNING) << "CP_CUPS: Job not found" LOG(WARNING) << "CP_CUPS: Job not found"
<< ", printer name: " << printer_name << ", printer name: " << printer_name
<< ", cups job id: " << job_id; << ", cups job id: " << job_id;
}
cupsFreeJobs(num_jobs, jobs); cupsFreeJobs(num_jobs, jobs);
return found; return found;
...@@ -715,9 +715,13 @@ scoped_refptr<PrintSystem> PrintSystem::CreateInstance( ...@@ -715,9 +715,13 @@ scoped_refptr<PrintSystem> PrintSystem::CreateInstance(
return base::MakeRefCounted<PrintSystemCUPS>(print_system_settings); return base::MakeRefCounted<PrintSystemCUPS>(print_system_settings);
} }
int PrintSystemCUPS::PrintFile(const GURL& url, http_encryption_t encryption, // static
const char* name, const char* filename, int PrintSystemCUPS::PrintFile(const GURL& url,
const char* title, int num_options, http_encryption_t encryption,
const char* name,
const char* filename,
const char* title,
int num_options,
cups_option_t* options) { cups_option_t* options) {
// Use default (local) print server. // Use default (local) print server.
if (url.is_empty()) if (url.is_empty())
...@@ -729,9 +733,13 @@ int PrintSystemCUPS::PrintFile(const GURL& url, http_encryption_t encryption, ...@@ -729,9 +733,13 @@ int PrintSystemCUPS::PrintFile(const GURL& url, http_encryption_t encryption,
options); options);
} }
int PrintSystemCUPS::GetJobs(cups_job_t** jobs, const GURL& url, // static
int PrintSystemCUPS::GetJobs(cups_job_t** jobs,
const GURL& url,
http_encryption_t encryption, http_encryption_t encryption,
const char* name, int myjobs, int whichjobs) { const char* name,
int myjobs,
int whichjobs) {
// Use default (local) print server. // Use default (local) print server.
if (url.is_empty()) if (url.is_empty())
return cupsGetJobs(jobs, name, myjobs, whichjobs); return cupsGetJobs(jobs, name, myjobs, whichjobs);
...@@ -776,7 +784,6 @@ PlatformJobId PrintSystemCUPS::SpoolPrintJob( ...@@ -776,7 +784,6 @@ PlatformJobId PrintSystemCUPS::SpoolPrintJob(
} }
std::vector<cups_option_t> cups_options; std::vector<cups_option_t> cups_options;
for (const auto& it : options) { for (const auto& it : options) {
cups_option_t opt; cups_option_t opt;
opt.name = const_cast<char*>(it.first.c_str()); opt.name = const_cast<char*>(it.first.c_str());
...@@ -798,6 +805,7 @@ PlatformJobId PrintSystemCUPS::SpoolPrintJob( ...@@ -798,6 +805,7 @@ PlatformJobId PrintSystemCUPS::SpoolPrintJob(
return job_id; return job_id;
} }
// static
std::string PrintSystemCUPS::MakeFullPrinterName( std::string PrintSystemCUPS::MakeFullPrinterName(
const GURL& url, const std::string& short_printer_name) { const GURL& url, const std::string& short_printer_name) {
std::string full_name; std::string full_name;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment