1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/service/cloud_print/print_system.h"
16 #include "base/bind.h"
17 #include "base/files/file_path.h"
18 #include "base/json/json_reader.h"
19 #include "base/logging.h"
21 #include "base/memory/scoped_ptr.h"
22 #include "base/message_loop/message_loop.h"
23 #include "base/rand_util.h"
24 #include "base/strings/string_number_conversions.h"
25 #include "base/strings/string_util.h"
26 #include "base/strings/utf_string_conversions.h"
27 #include "base/values.h"
28 #include "chrome/common/cloud_print/cloud_print_constants.h"
29 #include "chrome/common/crash_keys.h"
30 #include "chrome/service/cloud_print/cloud_print_service_helpers.h"
31 #include "grit/generated_resources.h"
32 #include "printing/backend/cups_helper.h"
33 #include "printing/backend/print_backend.h"
34 #include "printing/backend/print_backend_consts.h"
35 #include "ui/base/l10n/l10n_util.h"
40 // Print system config options.
41 const char kCUPSPrintServerURLs
[] = "print_server_urls";
42 const char kCUPSUpdateTimeoutMs
[] = "update_timeout_ms";
43 const char kCUPSNotifyDelete
[] = "notify_delete";
44 const char kCUPSSupportedMimeTipes
[] = "supported_mime_types";
46 // Default mime types supported by CUPS
47 // http://www.cups.org/articles.php?L205+TFAQ+Q
48 const char kCUPSDefaultSupportedTypes
[] =
49 "application/pdf,application/postscript,image/jpeg,image/png,image/gif";
51 // Time interval to check for printer's updates.
52 const int kCheckForPrinterUpdatesMinutes
= 5;
55 const int kJobUpdateTimeoutSeconds
= 5;
57 // Job id for dry run (it should not affect CUPS job ids, since 0 job-id is
59 const int kDryRunJobId
= 0;
63 namespace cloud_print
{
65 struct PrintServerInfoCUPS
{
67 scoped_refptr
<printing::PrintBackend
> backend
;
68 printing::PrinterList printers
;
69 // CapsMap cache PPD until the next update and give a fast access to it by
70 // printer name. PPD request is relatively expensive and this should minimize
71 // the number of requests.
72 typedef std::map
<std::string
, printing::PrinterCapsAndDefaults
> CapsMap
;
76 class PrintSystemCUPS
: public PrintSystem
{
78 explicit PrintSystemCUPS(const base::DictionaryValue
* print_system_settings
);
80 // PrintSystem implementation.
81 virtual PrintSystemResult
Init() OVERRIDE
;
82 virtual PrintSystem::PrintSystemResult
EnumeratePrinters(
83 printing::PrinterList
* printer_list
) OVERRIDE
;
84 virtual void GetPrinterCapsAndDefaults(
85 const std::string
& printer_name
,
86 const PrinterCapsAndDefaultsCallback
& callback
) OVERRIDE
;
87 virtual bool IsValidPrinter(const std::string
& printer_name
) OVERRIDE
;
88 virtual bool ValidatePrintTicket(
89 const std::string
& printer_name
,
90 const std::string
& print_ticket_data
) OVERRIDE
;
91 virtual bool GetJobDetails(const std::string
& printer_name
,
93 PrintJobDetails
*job_details
) OVERRIDE
;
94 virtual PrintSystem::PrintServerWatcher
* CreatePrintServerWatcher() OVERRIDE
;
95 virtual PrintSystem::PrinterWatcher
* CreatePrinterWatcher(
96 const std::string
& printer_name
) OVERRIDE
;
97 virtual PrintSystem::JobSpooler
* CreateJobSpooler() OVERRIDE
;
98 virtual std::string
GetSupportedMimeTypes() OVERRIDE
;
101 PlatformJobId
SpoolPrintJob(const std::string
& print_ticket
,
102 const base::FilePath
& print_data_file_path
,
103 const std::string
& print_data_mime_type
,
104 const std::string
& printer_name
,
105 const std::string
& job_title
,
106 const std::vector
<std::string
>& tags
,
108 bool GetPrinterInfo(const std::string
& printer_name
,
109 printing::PrinterBasicInfo
* info
);
110 bool ParsePrintTicket(const std::string
& print_ticket
,
111 std::map
<std::string
, std::string
>* options
);
113 // Synchronous version of GetPrinterCapsAndDefaults.
114 bool GetPrinterCapsAndDefaults(
115 const std::string
& printer_name
,
116 printing::PrinterCapsAndDefaults
* printer_info
);
118 base::TimeDelta
GetUpdateTimeout() const {
119 return update_timeout_
;
122 bool NotifyDelete() const {
123 // Notify about deleted printers only when we
124 // fetched printers list without errors.
125 return notify_delete_
&& printer_enum_succeeded_
;
129 virtual ~PrintSystemCUPS() {}
131 // Following functions are wrappers around corresponding CUPS functions.
132 // <functions>2() are called when print server is specified, and plain
133 // version in another case. There is an issue specifing CUPS_HTTP_DEFAULT
134 // in the <functions>2(), it does not work in CUPS prior to 1.4.
135 int GetJobs(cups_job_t
** jobs
, const GURL
& url
,
136 http_encryption_t encryption
, const char* name
,
137 int myjobs
, int whichjobs
);
138 int PrintFile(const GURL
& url
, http_encryption_t encryption
,
139 const char* name
, const char* filename
,
140 const char* title
, int num_options
, cups_option_t
* options
);
142 void InitPrintBackends(const base::DictionaryValue
* print_system_settings
);
143 void AddPrintServer(const std::string
& url
);
145 void UpdatePrinters();
147 // Full name contains print server url:port and printer name. Short name
148 // is the name of the printer in the CUPS server.
149 std::string
MakeFullPrinterName(const GURL
& url
,
150 const std::string
& short_printer_name
);
151 PrintServerInfoCUPS
* FindServerByFullName(
152 const std::string
& full_printer_name
, std::string
* short_printer_name
);
154 // Helper method to invoke a PrinterCapsAndDefaultsCallback.
155 static void RunCapsCallback(
156 const PrinterCapsAndDefaultsCallback
& callback
,
158 const std::string
& printer_name
,
159 const printing::PrinterCapsAndDefaults
& printer_info
);
161 // PrintServerList contains information about all print servers and backends
162 // this proxy is connected to.
163 typedef std::list
<PrintServerInfoCUPS
> PrintServerList
;
164 PrintServerList print_servers_
;
166 base::TimeDelta update_timeout_
;
168 bool printer_enum_succeeded_
;
170 http_encryption_t cups_encryption_
;
171 std::string supported_mime_types_
;
174 class PrintServerWatcherCUPS
175 : public PrintSystem::PrintServerWatcher
{
177 explicit PrintServerWatcherCUPS(PrintSystemCUPS
* print_system
)
178 : print_system_(print_system
),
182 // PrintSystem::PrintServerWatcher implementation.
183 virtual bool StartWatching(
184 PrintSystem::PrintServerWatcher::Delegate
* delegate
) OVERRIDE
{
185 delegate_
= delegate
;
186 printers_hash_
= GetPrintersHash();
187 base::MessageLoop::current()->PostDelayedTask(
189 base::Bind(&PrintServerWatcherCUPS::CheckForUpdates
, this),
190 print_system_
->GetUpdateTimeout());
194 virtual bool StopWatching() OVERRIDE
{
199 void CheckForUpdates() {
200 if (delegate_
== NULL
)
201 return; // Orphan call. We have been stopped already.
202 VLOG(1) << "CP_CUPS: Checking for new printers";
203 std::string new_hash
= GetPrintersHash();
204 if (printers_hash_
!= new_hash
) {
205 printers_hash_
= new_hash
;
206 delegate_
->OnPrinterAdded();
208 base::MessageLoop::current()->PostDelayedTask(
210 base::Bind(&PrintServerWatcherCUPS::CheckForUpdates
, this),
211 print_system_
->GetUpdateTimeout());
215 virtual ~PrintServerWatcherCUPS() {
220 std::string
GetPrintersHash() {
221 printing::PrinterList printer_list
;
222 print_system_
->EnumeratePrinters(&printer_list
);
224 // Sort printer names.
225 std::vector
<std::string
> printers
;
226 printing::PrinterList::iterator it
;
227 for (it
= printer_list
.begin(); it
!= printer_list
.end(); ++it
)
228 printers
.push_back(it
->printer_name
);
229 std::sort(printers
.begin(), printers
.end());
232 for (size_t i
= 0; i
< printers
.size(); i
++)
233 to_hash
+= printers
[i
];
235 return base::MD5String(to_hash
);
238 scoped_refptr
<PrintSystemCUPS
> print_system_
;
239 PrintSystem::PrintServerWatcher::Delegate
* delegate_
;
240 std::string printers_hash_
;
242 DISALLOW_COPY_AND_ASSIGN(PrintServerWatcherCUPS
);
245 class PrinterWatcherCUPS
246 : public PrintSystem::PrinterWatcher
{
248 PrinterWatcherCUPS(PrintSystemCUPS
* print_system
,
249 const std::string
& printer_name
)
250 : printer_name_(printer_name
),
252 print_system_(print_system
) {
255 // PrintSystem::PrinterWatcher implementation.
256 virtual bool StartWatching(
257 PrintSystem::PrinterWatcher::Delegate
* delegate
) OVERRIDE
{
258 scoped_refptr
<printing::PrintBackend
> print_backend(
259 printing::PrintBackend::CreateInstance(NULL
));
260 crash_keys::ScopedPrinterInfo
crash_key(
261 print_backend
->GetPrinterDriverInfo(printer_name_
));
262 if (delegate_
!= NULL
)
264 delegate_
= delegate
;
265 settings_hash_
= GetSettingsHash();
266 // Schedule next job status update.
267 base::MessageLoop::current()->PostDelayedTask(
269 base::Bind(&PrinterWatcherCUPS::JobStatusUpdate
, this),
270 base::TimeDelta::FromSeconds(kJobUpdateTimeoutSeconds
));
271 // Schedule next printer check.
272 // TODO(gene): Randomize time for the next printer update.
273 base::MessageLoop::current()->PostDelayedTask(
275 base::Bind(&PrinterWatcherCUPS::PrinterUpdate
, this),
276 print_system_
->GetUpdateTimeout());
280 virtual bool StopWatching() OVERRIDE
{
285 virtual bool GetCurrentPrinterInfo(
286 printing::PrinterBasicInfo
* printer_info
) OVERRIDE
{
287 DCHECK(printer_info
);
288 return print_system_
->GetPrinterInfo(printer_name_
, printer_info
);
291 void JobStatusUpdate() {
292 if (delegate_
== NULL
)
293 return; // Orphan call. We have been stopped already.
294 // For CUPS proxy, we are going to fire OnJobChanged notification
295 // periodically. Higher level will check if there are any outstanding
296 // jobs for this printer and check their status. If printer has no
297 // outstanding jobs, OnJobChanged() will do nothing.
298 delegate_
->OnJobChanged();
299 base::MessageLoop::current()->PostDelayedTask(
301 base::Bind(&PrinterWatcherCUPS::JobStatusUpdate
, this),
302 base::TimeDelta::FromSeconds(kJobUpdateTimeoutSeconds
));
305 void PrinterUpdate() {
306 if (delegate_
== NULL
)
307 return; // Orphan call. We have been stopped already.
308 VLOG(1) << "CP_CUPS: Checking for updates"
309 << ", printer name: " << printer_name_
;
310 if (print_system_
->NotifyDelete() &&
311 !print_system_
->IsValidPrinter(printer_name_
)) {
312 delegate_
->OnPrinterDeleted();
313 VLOG(1) << "CP_CUPS: Printer deleted"
314 << ", printer name: " << printer_name_
;
316 std::string new_hash
= GetSettingsHash();
317 if (settings_hash_
!= new_hash
) {
318 settings_hash_
= new_hash
;
319 delegate_
->OnPrinterChanged();
320 VLOG(1) << "CP_CUPS: Printer configuration changed"
321 << ", printer name: " << printer_name_
;
324 base::MessageLoop::current()->PostDelayedTask(
326 base::Bind(&PrinterWatcherCUPS::PrinterUpdate
, this),
327 print_system_
->GetUpdateTimeout());
331 virtual ~PrinterWatcherCUPS() {
336 std::string
GetSettingsHash() {
337 printing::PrinterBasicInfo info
;
338 if (!print_system_
->GetPrinterInfo(printer_name_
, &info
))
339 return std::string();
341 printing::PrinterCapsAndDefaults caps
;
342 if (!print_system_
->GetPrinterCapsAndDefaults(printer_name_
, &caps
))
343 return std::string();
345 std::string
to_hash(info
.printer_name
);
346 to_hash
+= info
.printer_description
;
347 std::map
<std::string
, std::string
>::const_iterator it
;
348 for (it
= info
.options
.begin(); it
!= info
.options
.end(); ++it
) {
349 to_hash
+= it
->first
;
350 to_hash
+= it
->second
;
353 to_hash
+= caps
.printer_capabilities
;
354 to_hash
+= caps
.caps_mime_type
;
355 to_hash
+= caps
.printer_defaults
;
356 to_hash
+= caps
.defaults_mime_type
;
358 return base::MD5String(to_hash
);
360 std::string printer_name_
;
361 PrintSystem::PrinterWatcher::Delegate
* delegate_
;
362 scoped_refptr
<PrintSystemCUPS
> print_system_
;
363 std::string settings_hash_
;
365 DISALLOW_COPY_AND_ASSIGN(PrinterWatcherCUPS
);
368 class JobSpoolerCUPS
: public PrintSystem::JobSpooler
{
370 explicit JobSpoolerCUPS(PrintSystemCUPS
* print_system
)
371 : print_system_(print_system
) {
372 DCHECK(print_system_
.get());
375 // PrintSystem::JobSpooler implementation.
376 virtual bool Spool(const std::string
& print_ticket
,
377 const base::FilePath
& print_data_file_path
,
378 const std::string
& print_data_mime_type
,
379 const std::string
& printer_name
,
380 const std::string
& job_title
,
381 const std::vector
<std::string
>& tags
,
382 JobSpooler::Delegate
* delegate
) OVERRIDE
{
384 bool dry_run
= false;
385 int job_id
= print_system_
->SpoolPrintJob(
386 print_ticket
, print_data_file_path
, print_data_mime_type
,
387 printer_name
, job_title
, tags
, &dry_run
);
388 base::MessageLoop::current()->PostTask(
390 base::Bind(&JobSpoolerCUPS::NotifyDelegate
, delegate
, job_id
, dry_run
));
394 static void NotifyDelegate(JobSpooler::Delegate
* delegate
,
395 int job_id
, bool dry_run
) {
396 if (dry_run
|| job_id
)
397 delegate
->OnJobSpoolSucceeded(job_id
);
399 delegate
->OnJobSpoolFailed();
403 virtual ~JobSpoolerCUPS() {}
406 scoped_refptr
<PrintSystemCUPS
> print_system_
;
408 DISALLOW_COPY_AND_ASSIGN(JobSpoolerCUPS
);
411 PrintSystemCUPS::PrintSystemCUPS(
412 const base::DictionaryValue
* print_system_settings
)
413 : update_timeout_(base::TimeDelta::FromMinutes(
414 kCheckForPrinterUpdatesMinutes
)),
416 printer_enum_succeeded_(false),
417 notify_delete_(true),
418 cups_encryption_(HTTP_ENCRYPT_NEVER
),
419 supported_mime_types_(kCUPSDefaultSupportedTypes
) {
420 if (print_system_settings
) {
422 if (print_system_settings
->GetInteger(kCUPSUpdateTimeoutMs
, &timeout
))
423 update_timeout_
= base::TimeDelta::FromMilliseconds(timeout
);
426 if (print_system_settings
->GetInteger(kCUPSEncryption
, &encryption
))
428 static_cast<http_encryption_t
>(encryption
);
430 bool notify_delete
= true;
431 if (print_system_settings
->GetBoolean(kCUPSNotifyDelete
, ¬ify_delete
))
432 notify_delete_
= notify_delete
;
435 if (print_system_settings
->GetString(kCUPSSupportedMimeTipes
, &types
))
436 supported_mime_types_
= types
;
439 InitPrintBackends(print_system_settings
);
442 void PrintSystemCUPS::InitPrintBackends(
443 const base::DictionaryValue
* print_system_settings
) {
444 const base::ListValue
* url_list
;
445 if (print_system_settings
&&
446 print_system_settings
->GetList(kCUPSPrintServerURLs
, &url_list
)) {
447 for (size_t i
= 0; i
< url_list
->GetSize(); i
++) {
448 std::string print_server_url
;
449 if (url_list
->GetString(i
, &print_server_url
))
450 AddPrintServer(print_server_url
);
454 // If server list is empty, use default print server.
455 if (print_servers_
.empty())
456 AddPrintServer(std::string());
459 void PrintSystemCUPS::AddPrintServer(const std::string
& url
) {
461 LOG(WARNING
) << "No print server specified. Using default print server.";
463 // Get Print backend for the specific print server.
464 base::DictionaryValue backend_settings
;
465 backend_settings
.SetString(kCUPSPrintServerURL
, url
);
467 // Make CUPS requests non-blocking.
468 backend_settings
.SetString(kCUPSBlocking
, kValueFalse
);
470 // Set encryption for backend.
471 backend_settings
.SetInteger(kCUPSEncryption
, cups_encryption_
);
473 PrintServerInfoCUPS print_server
;
474 print_server
.backend
=
475 printing::PrintBackend::CreateInstance(&backend_settings
);
476 print_server
.url
= GURL(url
.c_str());
478 print_servers_
.push_back(print_server
);
481 PrintSystem::PrintSystemResult
PrintSystemCUPS::Init() {
484 return PrintSystemResult(true, std::string());
487 void PrintSystemCUPS::UpdatePrinters() {
488 PrintServerList::iterator it
;
489 printer_enum_succeeded_
= true;
490 for (it
= print_servers_
.begin(); it
!= print_servers_
.end(); ++it
) {
491 if (!it
->backend
->EnumeratePrinters(&it
->printers
))
492 printer_enum_succeeded_
= false;
493 it
->caps_cache
.clear();
494 printing::PrinterList::iterator printer_it
;
495 for (printer_it
= it
->printers
.begin();
496 printer_it
!= it
->printers
.end(); ++printer_it
) {
497 printer_it
->printer_name
= MakeFullPrinterName(it
->url
,
498 printer_it
->printer_name
);
500 VLOG(1) << "CP_CUPS: Updated printers list"
501 << ", server: " << it
->url
502 << ", # of printers: " << it
->printers
.size();
505 // Schedule next update.
506 base::MessageLoop::current()->PostDelayedTask(
508 base::Bind(&PrintSystemCUPS::UpdatePrinters
, this),
512 PrintSystem::PrintSystemResult
PrintSystemCUPS::EnumeratePrinters(
513 printing::PrinterList
* printer_list
) {
514 DCHECK(initialized_
);
515 printer_list
->clear();
516 PrintServerList::iterator it
;
517 for (it
= print_servers_
.begin(); it
!= print_servers_
.end(); ++it
) {
518 printer_list
->insert(printer_list
->end(),
519 it
->printers
.begin(), it
->printers
.end());
521 VLOG(1) << "CP_CUPS: Total printers enumerated: " << printer_list
->size();
522 // TODO(sanjeevr): Maybe some day we want to report the actual server names
523 // for which the enumeration failed.
524 return PrintSystemResult(printer_enum_succeeded_
, std::string());
527 void PrintSystemCUPS::GetPrinterCapsAndDefaults(
528 const std::string
& printer_name
,
529 const PrinterCapsAndDefaultsCallback
& callback
) {
530 printing::PrinterCapsAndDefaults printer_info
;
531 bool succeeded
= GetPrinterCapsAndDefaults(printer_name
, &printer_info
);
532 base::MessageLoop::current()->PostTask(
534 base::Bind(&PrintSystemCUPS::RunCapsCallback
,
541 bool PrintSystemCUPS::IsValidPrinter(const std::string
& printer_name
) {
542 return GetPrinterInfo(printer_name
, NULL
);
545 bool PrintSystemCUPS::ValidatePrintTicket(
546 const std::string
& printer_name
,
547 const std::string
& print_ticket_data
) {
548 DCHECK(initialized_
);
549 scoped_ptr
<base::Value
> ticket_value(
550 base::JSONReader::Read(print_ticket_data
));
551 return ticket_value
!= NULL
&&
552 ticket_value
->IsType(base::Value::TYPE_DICTIONARY
);
555 // Print ticket on linux is a JSON string containing only one dictionary.
556 bool PrintSystemCUPS::ParsePrintTicket(
557 const std::string
& print_ticket
,
558 std::map
<std::string
, std::string
>* options
) {
560 scoped_ptr
<base::Value
> ticket_value(base::JSONReader::Read(print_ticket
));
561 if (ticket_value
== NULL
||
562 !ticket_value
->IsType(base::Value::TYPE_DICTIONARY
)) {
567 base::DictionaryValue
* ticket_dict
=
568 static_cast<base::DictionaryValue
*>(ticket_value
.get());
569 for (base::DictionaryValue::Iterator
it(*ticket_dict
); !it
.IsAtEnd();
572 if (it
.value().GetAsString(&value
))
573 (*options
)[it
.key()] = value
;
579 bool PrintSystemCUPS::GetPrinterCapsAndDefaults(
580 const std::string
& printer_name
,
581 printing::PrinterCapsAndDefaults
* printer_info
) {
582 DCHECK(initialized_
);
583 std::string short_printer_name
;
584 PrintServerInfoCUPS
* server_info
=
585 FindServerByFullName(printer_name
, &short_printer_name
);
589 PrintServerInfoCUPS::CapsMap::iterator caps_it
=
590 server_info
->caps_cache
.find(printer_name
);
591 if (caps_it
!= server_info
->caps_cache
.end()) {
592 *printer_info
= caps_it
->second
;
596 // TODO(gene): Retry multiple times in case of error.
597 crash_keys::ScopedPrinterInfo
crash_key(
598 server_info
->backend
->GetPrinterDriverInfo(short_printer_name
));
599 if (!server_info
->backend
->GetPrinterCapsAndDefaults(short_printer_name
,
604 server_info
->caps_cache
[printer_name
] = *printer_info
;
608 bool PrintSystemCUPS::GetJobDetails(const std::string
& printer_name
,
609 PlatformJobId job_id
,
610 PrintJobDetails
*job_details
) {
611 DCHECK(initialized_
);
614 std::string short_printer_name
;
615 PrintServerInfoCUPS
* server_info
=
616 FindServerByFullName(printer_name
, &short_printer_name
);
620 crash_keys::ScopedPrinterInfo
crash_key(
621 server_info
->backend
->GetPrinterDriverInfo(short_printer_name
));
622 cups_job_t
* jobs
= NULL
;
623 int num_jobs
= GetJobs(&jobs
, server_info
->url
, cups_encryption_
,
624 short_printer_name
.c_str(), 1, -1);
625 bool error
= (num_jobs
== 0) && (cupsLastError() > IPP_OK_EVENTS_COMPLETE
);
627 VLOG(1) << "CP_CUPS: Error getting jobs from CUPS server"
628 << ", printer name:" << printer_name
629 << ", error: " << static_cast<int>(cupsLastError());
633 // Check if the request is for dummy dry run job.
634 // We check this after calling GetJobs API to see if this printer is actually
635 // accessible through CUPS.
636 if (job_id
== kDryRunJobId
) {
637 job_details
->status
= PRINT_JOB_STATUS_COMPLETED
;
638 VLOG(1) << "CP_CUPS: Dry run job succeeded"
639 << ", printer name: " << printer_name
;
644 for (int i
= 0; i
< num_jobs
; i
++) {
645 if (jobs
[i
].id
== job_id
) {
647 switch (jobs
[i
].state
) {
648 case IPP_JOB_PENDING
:
650 case IPP_JOB_PROCESSING
:
651 job_details
->status
= PRINT_JOB_STATUS_IN_PROGRESS
;
653 case IPP_JOB_STOPPED
:
654 case IPP_JOB_CANCELLED
:
655 case IPP_JOB_ABORTED
:
656 job_details
->status
= PRINT_JOB_STATUS_ERROR
;
658 case IPP_JOB_COMPLETED
:
659 job_details
->status
= PRINT_JOB_STATUS_COMPLETED
;
662 job_details
->status
= PRINT_JOB_STATUS_INVALID
;
664 job_details
->platform_status_flags
= jobs
[i
].state
;
666 // We don't have any details on the number of processed pages here.
672 VLOG(1) << "CP_CUPS: Job found"
673 << ", printer name: " << printer_name
674 << ", cups job id: " << job_id
675 << ", cups job status: " << job_details
->status
;
677 LOG(WARNING
) << "CP_CUPS: Job not found"
678 << ", printer name: " << printer_name
679 << ", cups job id: " << job_id
;
681 cupsFreeJobs(num_jobs
, jobs
);
685 bool PrintSystemCUPS::GetPrinterInfo(const std::string
& printer_name
,
686 printing::PrinterBasicInfo
* info
) {
687 DCHECK(initialized_
);
689 VLOG(1) << "CP_CUPS: Getting printer info"
690 << ", printer name: " << printer_name
;
692 std::string short_printer_name
;
693 PrintServerInfoCUPS
* server_info
=
694 FindServerByFullName(printer_name
, &short_printer_name
);
698 printing::PrinterList::iterator it
;
699 for (it
= server_info
->printers
.begin();
700 it
!= server_info
->printers
.end(); ++it
) {
701 if (it
->printer_name
== printer_name
) {
710 PrintSystem::PrintServerWatcher
*
711 PrintSystemCUPS::CreatePrintServerWatcher() {
712 DCHECK(initialized_
);
713 return new PrintServerWatcherCUPS(this);
716 PrintSystem::PrinterWatcher
* PrintSystemCUPS::CreatePrinterWatcher(
717 const std::string
& printer_name
) {
718 DCHECK(initialized_
);
719 DCHECK(!printer_name
.empty());
720 return new PrinterWatcherCUPS(this, printer_name
);
723 PrintSystem::JobSpooler
* PrintSystemCUPS::CreateJobSpooler() {
724 DCHECK(initialized_
);
725 return new JobSpoolerCUPS(this);
728 std::string
PrintSystemCUPS::GetSupportedMimeTypes() {
729 return supported_mime_types_
;
732 scoped_refptr
<PrintSystem
> PrintSystem::CreateInstance(
733 const base::DictionaryValue
* print_system_settings
) {
734 return new PrintSystemCUPS(print_system_settings
);
737 int PrintSystemCUPS::PrintFile(const GURL
& url
, http_encryption_t encryption
,
738 const char* name
, const char* filename
,
739 const char* title
, int num_options
,
740 cups_option_t
* options
) {
741 if (url
.is_empty()) { // Use default (local) print server.
742 return cupsPrintFile(name
, filename
, title
, num_options
, options
);
744 printing::HttpConnectionCUPS
http(url
, encryption
);
745 http
.SetBlocking(false);
746 return cupsPrintFile2(http
.http(), name
, filename
,
747 title
, num_options
, options
);
751 int PrintSystemCUPS::GetJobs(cups_job_t
** jobs
, const GURL
& url
,
752 http_encryption_t encryption
,
753 const char* name
, int myjobs
, int whichjobs
) {
754 if (url
.is_empty()) { // Use default (local) print server.
755 return cupsGetJobs(jobs
, name
, myjobs
, whichjobs
);
757 printing::HttpConnectionCUPS
http(url
, encryption
);
758 http
.SetBlocking(false);
759 return cupsGetJobs2(http
.http(), jobs
, name
, myjobs
, whichjobs
);
763 PlatformJobId
PrintSystemCUPS::SpoolPrintJob(
764 const std::string
& print_ticket
,
765 const base::FilePath
& print_data_file_path
,
766 const std::string
& print_data_mime_type
,
767 const std::string
& printer_name
,
768 const std::string
& job_title
,
769 const std::vector
<std::string
>& tags
,
771 DCHECK(initialized_
);
772 VLOG(1) << "CP_CUPS: Spooling print job, printer name: " << printer_name
;
774 std::string short_printer_name
;
775 PrintServerInfoCUPS
* server_info
=
776 FindServerByFullName(printer_name
, &short_printer_name
);
780 crash_keys::ScopedPrinterInfo
crash_key(
781 server_info
->backend
->GetPrinterDriverInfo(printer_name
));
783 // We need to store options as char* string for the duration of the
784 // cupsPrintFile2 call. We'll use map here to store options, since
785 // Dictionary value from JSON parser returns wchat_t.
786 std::map
<std::string
, std::string
> options
;
787 bool res
= ParsePrintTicket(print_ticket
, &options
);
788 DCHECK(res
); // If print ticket is invalid we still print using defaults.
790 // Check if this is a dry run (test) job.
791 *dry_run
= IsDryRunJob(tags
);
793 VLOG(1) << "CP_CUPS: Dry run job spooled";
797 std::vector
<cups_option_t
> cups_options
;
798 std::map
<std::string
, std::string
>::iterator it
;
800 for (it
= options
.begin(); it
!= options
.end(); ++it
) {
802 opt
.name
= const_cast<char*>(it
->first
.c_str());
803 opt
.value
= const_cast<char*>(it
->second
.c_str());
804 cups_options
.push_back(opt
);
807 int job_id
= PrintFile(server_info
->url
,
809 short_printer_name
.c_str(),
810 print_data_file_path
.value().c_str(),
815 // TODO(alexyu): Output printer id.
816 VLOG(1) << "CP_CUPS: Job spooled"
817 << ", printer name: " << printer_name
818 << ", cups job id: " << job_id
;
823 std::string
PrintSystemCUPS::MakeFullPrinterName(
824 const GURL
& url
, const std::string
& short_printer_name
) {
825 std::string full_name
;
827 full_name
+= url
.host();
828 if (!url
.port().empty()) {
830 full_name
+= url
.port();
833 full_name
+= short_printer_name
;
837 PrintServerInfoCUPS
* PrintSystemCUPS::FindServerByFullName(
838 const std::string
& full_printer_name
, std::string
* short_printer_name
) {
839 size_t front
= full_printer_name
.find("\\\\");
840 size_t separator
= full_printer_name
.find("\\", 2);
841 if (front
== std::string::npos
|| separator
== std::string::npos
) {
842 LOG(WARNING
) << "CP_CUPS: Invalid UNC"
843 << ", printer name: " << full_printer_name
;
846 std::string server
= full_printer_name
.substr(2, separator
- 2);
848 PrintServerList::iterator it
;
849 for (it
= print_servers_
.begin(); it
!= print_servers_
.end(); ++it
) {
850 std::string cur_server
;
851 cur_server
+= it
->url
.host();
852 if (!it
->url
.port().empty()) {
854 cur_server
+= it
->url
.port();
856 if (cur_server
== server
) {
857 *short_printer_name
= full_printer_name
.substr(separator
+ 1);
862 LOG(WARNING
) << "CP_CUPS: Server not found"
863 << ", printer name: " << full_printer_name
;
867 void PrintSystemCUPS::RunCapsCallback(
868 const PrinterCapsAndDefaultsCallback
& callback
,
870 const std::string
& printer_name
,
871 const printing::PrinterCapsAndDefaults
& printer_info
) {
872 callback
.Run(succeeded
, printer_name
, printer_info
);
875 } // namespace cloud_print