1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
|
// Copyright (c) 2012 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.
#include "chrome/service/cloud_print/print_system.h"
#include <cups/cups.h>
#include <cups/ppd.h>
#include <dlfcn.h>
#include <errno.h>
#include <pthread.h>
#include <algorithm>
#include <list>
#include <map>
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/md5.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/common/cloud_print/cloud_print_constants.h"
#include "chrome/common/crash_keys.h"
#include "chrome/service/cloud_print/cloud_print_service_helpers.h"
#include "grit/generated_resources.h"
#include "printing/backend/cups_helper.h"
#include "printing/backend/print_backend.h"
#include "printing/backend/print_backend_consts.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"
namespace {
// Print system config options.
const char kCUPSPrintServerURLs[] = "print_server_urls";
const char kCUPSUpdateTimeoutMs[] = "update_timeout_ms";
const char kCUPSNotifyDelete[] = "notify_delete";
const char kCUPSSupportedMimeTipes[] = "supported_mime_types";
// Default mime types supported by CUPS
// http://www.cups.org/articles.php?L205+TFAQ+Q
const char kCUPSDefaultSupportedTypes[] =
"application/pdf,application/postscript,image/jpeg,image/png,image/gif";
// Time interval to check for printer's updates.
const int kCheckForPrinterUpdatesMinutes = 5;
// Job update timeout
const int kJobUpdateTimeoutSeconds = 5;
// Job id for dry run (it should not affect CUPS job ids, since 0 job-id is
// invalid in CUPS.
const int kDryRunJobId = 0;
} // namespace
namespace cloud_print {
struct PrintServerInfoCUPS {
GURL url;
scoped_refptr<printing::PrintBackend> backend;
printing::PrinterList printers;
// CapsMap cache PPD until the next update and give a fast access to it by
// printer name. PPD request is relatively expensive and this should minimize
// the number of requests.
typedef std::map<std::string, printing::PrinterCapsAndDefaults> CapsMap;
CapsMap caps_cache;
};
class PrintSystemCUPS : public PrintSystem {
public:
explicit PrintSystemCUPS(const base::DictionaryValue* print_system_settings);
// PrintSystem implementation.
virtual PrintSystemResult Init() OVERRIDE;
virtual PrintSystem::PrintSystemResult EnumeratePrinters(
printing::PrinterList* printer_list) OVERRIDE;
virtual void GetPrinterCapsAndDefaults(
const std::string& printer_name,
const PrinterCapsAndDefaultsCallback& callback) OVERRIDE;
virtual bool IsValidPrinter(const std::string& printer_name) OVERRIDE;
virtual bool ValidatePrintTicket(
const std::string& printer_name,
const std::string& print_ticket_data,
const std::string& print_ticket_mime_type) OVERRIDE;
virtual bool GetJobDetails(const std::string& printer_name,
PlatformJobId job_id,
PrintJobDetails *job_details) OVERRIDE;
virtual PrintSystem::PrintServerWatcher* CreatePrintServerWatcher() OVERRIDE;
virtual PrintSystem::PrinterWatcher* CreatePrinterWatcher(
const std::string& printer_name) OVERRIDE;
virtual PrintSystem::JobSpooler* CreateJobSpooler() OVERRIDE;
virtual bool UseCddAndCjt() OVERRIDE;
virtual std::string GetSupportedMimeTypes() OVERRIDE;
// Helper functions.
PlatformJobId SpoolPrintJob(const std::string& print_ticket,
const base::FilePath& print_data_file_path,
const std::string& print_data_mime_type,
const std::string& printer_name,
const std::string& job_title,
const std::vector<std::string>& tags,
bool* dry_run);
bool GetPrinterInfo(const std::string& printer_name,
printing::PrinterBasicInfo* info);
bool ParsePrintTicket(const std::string& print_ticket,
std::map<std::string, std::string>* options);
// Synchronous version of GetPrinterCapsAndDefaults.
bool GetPrinterCapsAndDefaults(
const std::string& printer_name,
printing::PrinterCapsAndDefaults* printer_info);
base::TimeDelta GetUpdateTimeout() const {
return update_timeout_;
}
bool NotifyDelete() const {
// Notify about deleted printers only when we
// fetched printers list without errors.
return notify_delete_ && printer_enum_succeeded_;
}
private:
virtual ~PrintSystemCUPS() {}
// 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 specifing CUPS_HTTP_DEFAULT
// in the <functions>2(), it does not work in CUPS prior to 1.4.
int GetJobs(cups_job_t** jobs, const GURL& url,
http_encryption_t encryption, const char* name,
int myjobs, int whichjobs);
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 AddPrintServer(const std::string& url);
void UpdatePrinters();
// Full name contains print server url:port and printer name. Short name
// is the name of the printer in the CUPS server.
std::string MakeFullPrinterName(const GURL& url,
const std::string& short_printer_name);
PrintServerInfoCUPS* FindServerByFullName(
const std::string& full_printer_name, std::string* short_printer_name);
// Helper method to invoke a PrinterCapsAndDefaultsCallback.
static void RunCapsCallback(
const PrinterCapsAndDefaultsCallback& callback,
bool succeeded,
const std::string& printer_name,
const printing::PrinterCapsAndDefaults& printer_info);
// PrintServerList contains information about all print servers and backends
// this proxy is connected to.
typedef std::list<PrintServerInfoCUPS> PrintServerList;
PrintServerList print_servers_;
base::TimeDelta update_timeout_;
bool initialized_;
bool printer_enum_succeeded_;
bool notify_delete_;
http_encryption_t cups_encryption_;
std::string supported_mime_types_;
};
class PrintServerWatcherCUPS
: public PrintSystem::PrintServerWatcher {
public:
explicit PrintServerWatcherCUPS(PrintSystemCUPS* print_system)
: print_system_(print_system),
delegate_(NULL) {
}
// PrintSystem::PrintServerWatcher implementation.
virtual bool StartWatching(
PrintSystem::PrintServerWatcher::Delegate* delegate) OVERRIDE {
delegate_ = delegate;
printers_hash_ = GetPrintersHash();
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&PrintServerWatcherCUPS::CheckForUpdates, this),
print_system_->GetUpdateTimeout());
return true;
}
virtual bool StopWatching() OVERRIDE {
delegate_ = NULL;
return true;
}
void CheckForUpdates() {
if (delegate_ == NULL)
return; // Orphan call. We have been stopped already.
VLOG(1) << "CP_CUPS: Checking for new printers";
std::string new_hash = GetPrintersHash();
if (printers_hash_ != new_hash) {
printers_hash_ = new_hash;
delegate_->OnPrinterAdded();
}
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&PrintServerWatcherCUPS::CheckForUpdates, this),
print_system_->GetUpdateTimeout());
}
protected:
virtual ~PrintServerWatcherCUPS() {
StopWatching();
}
private:
std::string GetPrintersHash() {
printing::PrinterList printer_list;
print_system_->EnumeratePrinters(&printer_list);
// Sort printer names.
std::vector<std::string> printers;
printing::PrinterList::iterator it;
for (it = printer_list.begin(); it != printer_list.end(); ++it)
printers.push_back(it->printer_name);
std::sort(printers.begin(), printers.end());
std::string to_hash;
for (size_t i = 0; i < printers.size(); i++)
to_hash += printers[i];
return base::MD5String(to_hash);
}
scoped_refptr<PrintSystemCUPS> print_system_;
PrintSystem::PrintServerWatcher::Delegate* delegate_;
std::string printers_hash_;
DISALLOW_COPY_AND_ASSIGN(PrintServerWatcherCUPS);
};
class PrinterWatcherCUPS
: public PrintSystem::PrinterWatcher {
public:
PrinterWatcherCUPS(PrintSystemCUPS* print_system,
const std::string& printer_name)
: printer_name_(printer_name),
delegate_(NULL),
print_system_(print_system) {
}
// PrintSystem::PrinterWatcher implementation.
virtual bool StartWatching(
PrintSystem::PrinterWatcher::Delegate* delegate) OVERRIDE{
scoped_refptr<printing::PrintBackend> print_backend(
printing::PrintBackend::CreateInstance(NULL));
crash_keys::ScopedPrinterInfo crash_key(
print_backend->GetPrinterDriverInfo(printer_name_));
if (delegate_ != NULL)
StopWatching();
delegate_ = delegate;
settings_hash_ = GetSettingsHash();
// Schedule next job status update.
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&PrinterWatcherCUPS::JobStatusUpdate, this),
base::TimeDelta::FromSeconds(kJobUpdateTimeoutSeconds));
// Schedule next printer check.
// TODO(gene): Randomize time for the next printer update.
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&PrinterWatcherCUPS::PrinterUpdate, this),
print_system_->GetUpdateTimeout());
return true;
}
virtual bool StopWatching() OVERRIDE{
delegate_ = NULL;
return true;
}
virtual bool GetCurrentPrinterInfo(
printing::PrinterBasicInfo* printer_info) OVERRIDE {
DCHECK(printer_info);
return print_system_->GetPrinterInfo(printer_name_, printer_info);
}
void JobStatusUpdate() {
if (delegate_ == NULL)
return; // Orphan call. We have been stopped already.
// For CUPS proxy, we are going to fire OnJobChanged notification
// periodically. Higher level will check if there are any outstanding
// jobs for this printer and check their status. If printer has no
// outstanding jobs, OnJobChanged() will do nothing.
delegate_->OnJobChanged();
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&PrinterWatcherCUPS::JobStatusUpdate, this),
base::TimeDelta::FromSeconds(kJobUpdateTimeoutSeconds));
}
void PrinterUpdate() {
if (delegate_ == NULL)
return; // Orphan call. We have been stopped already.
VLOG(1) << "CP_CUPS: Checking for updates"
<< ", printer name: " << printer_name_;
if (print_system_->NotifyDelete() &&
!print_system_->IsValidPrinter(printer_name_)) {
delegate_->OnPrinterDeleted();
VLOG(1) << "CP_CUPS: Printer deleted"
<< ", printer name: " << printer_name_;
} else {
std::string new_hash = GetSettingsHash();
if (settings_hash_ != new_hash) {
settings_hash_ = new_hash;
delegate_->OnPrinterChanged();
VLOG(1) << "CP_CUPS: Printer configuration changed"
<< ", printer name: " << printer_name_;
}
}
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&PrinterWatcherCUPS::PrinterUpdate, this),
print_system_->GetUpdateTimeout());
}
protected:
virtual ~PrinterWatcherCUPS() {
StopWatching();
}
private:
std::string GetSettingsHash() {
printing::PrinterBasicInfo info;
if (!print_system_->GetPrinterInfo(printer_name_, &info))
return std::string();
printing::PrinterCapsAndDefaults caps;
if (!print_system_->GetPrinterCapsAndDefaults(printer_name_, &caps))
return std::string();
std::string to_hash(info.printer_name);
to_hash += info.printer_description;
std::map<std::string, std::string>::const_iterator it;
for (it = info.options.begin(); it != info.options.end(); ++it) {
to_hash += it->first;
to_hash += it->second;
}
to_hash += caps.printer_capabilities;
to_hash += caps.caps_mime_type;
to_hash += caps.printer_defaults;
to_hash += caps.defaults_mime_type;
return base::MD5String(to_hash);
}
std::string printer_name_;
PrintSystem::PrinterWatcher::Delegate* delegate_;
scoped_refptr<PrintSystemCUPS> print_system_;
std::string settings_hash_;
DISALLOW_COPY_AND_ASSIGN(PrinterWatcherCUPS);
};
class JobSpoolerCUPS : public PrintSystem::JobSpooler {
public:
explicit JobSpoolerCUPS(PrintSystemCUPS* print_system)
: print_system_(print_system) {
DCHECK(print_system_.get());
}
// PrintSystem::JobSpooler implementation.
virtual bool Spool(const std::string& print_ticket,
const std::string& print_ticket_mime_type,
const base::FilePath& print_data_file_path,
const std::string& print_data_mime_type,
const std::string& printer_name,
const std::string& job_title,
const std::vector<std::string>& tags,
JobSpooler::Delegate* delegate) OVERRIDE{
DCHECK(delegate);
bool dry_run = false;
int job_id = print_system_->SpoolPrintJob(
print_ticket, print_data_file_path, print_data_mime_type,
printer_name, job_title, tags, &dry_run);
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&JobSpoolerCUPS::NotifyDelegate, delegate, job_id, dry_run));
return true;
}
static void NotifyDelegate(JobSpooler::Delegate* delegate,
int job_id, bool dry_run) {
if (dry_run || job_id)
delegate->OnJobSpoolSucceeded(job_id);
else
delegate->OnJobSpoolFailed();
}
protected:
virtual ~JobSpoolerCUPS() {}
private:
scoped_refptr<PrintSystemCUPS> print_system_;
DISALLOW_COPY_AND_ASSIGN(JobSpoolerCUPS);
};
PrintSystemCUPS::PrintSystemCUPS(
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) {
int timeout;
if (print_system_settings->GetInteger(kCUPSUpdateTimeoutMs, &timeout))
update_timeout_ = base::TimeDelta::FromMilliseconds(timeout);
int encryption;
if (print_system_settings->GetInteger(kCUPSEncryption, &encryption))
cups_encryption_ =
static_cast<http_encryption_t>(encryption);
bool notify_delete = true;
if (print_system_settings->GetBoolean(kCUPSNotifyDelete, ¬ify_delete))
notify_delete_ = notify_delete;
std::string types;
if (print_system_settings->GetString(kCUPSSupportedMimeTipes, &types))
supported_mime_types_ = types;
}
InitPrintBackends(print_system_settings);
}
void PrintSystemCUPS::InitPrintBackends(
const base::DictionaryValue* print_system_settings) {
const base::ListValue* url_list;
if (print_system_settings &&
print_system_settings->GetList(kCUPSPrintServerURLs, &url_list)) {
for (size_t i = 0; i < url_list->GetSize(); i++) {
std::string print_server_url;
if (url_list->GetString(i, &print_server_url))
AddPrintServer(print_server_url);
}
}
// If server list is empty, use default print server.
if (print_servers_.empty())
AddPrintServer(std::string());
}
void PrintSystemCUPS::AddPrintServer(const std::string& url) {
if (url.empty())
LOG(WARNING) << "No print server specified. Using default print server.";
// Get Print backend for the specific print server.
base::DictionaryValue backend_settings;
backend_settings.SetString(kCUPSPrintServerURL, url);
// Make CUPS requests non-blocking.
backend_settings.SetString(kCUPSBlocking, kValueFalse);
// Set encryption for backend.
backend_settings.SetInteger(kCUPSEncryption, cups_encryption_);
PrintServerInfoCUPS print_server;
print_server.backend =
printing::PrintBackend::CreateInstance(&backend_settings);
print_server.url = GURL(url.c_str());
print_servers_.push_back(print_server);
}
PrintSystem::PrintSystemResult PrintSystemCUPS::Init() {
UpdatePrinters();
initialized_ = true;
return PrintSystemResult(true, std::string());
}
void PrintSystemCUPS::UpdatePrinters() {
PrintServerList::iterator it;
printer_enum_succeeded_ = true;
for (it = print_servers_.begin(); it != print_servers_.end(); ++it) {
if (!it->backend->EnumeratePrinters(&it->printers))
printer_enum_succeeded_ = false;
it->caps_cache.clear();
printing::PrinterList::iterator printer_it;
for (printer_it = it->printers.begin();
printer_it != it->printers.end(); ++printer_it) {
printer_it->printer_name = MakeFullPrinterName(it->url,
printer_it->printer_name);
}
VLOG(1) << "CP_CUPS: Updated printers list"
<< ", server: " << it->url
<< ", # of printers: " << it->printers.size();
}
// Schedule next update.
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&PrintSystemCUPS::UpdatePrinters, this),
GetUpdateTimeout());
}
PrintSystem::PrintSystemResult PrintSystemCUPS::EnumeratePrinters(
printing::PrinterList* printer_list) {
DCHECK(initialized_);
printer_list->clear();
PrintServerList::iterator it;
for (it = print_servers_.begin(); it != print_servers_.end(); ++it) {
printer_list->insert(printer_list->end(),
it->printers.begin(), it->printers.end());
}
VLOG(1) << "CP_CUPS: Total printers enumerated: " << printer_list->size();
// TODO(sanjeevr): Maybe some day we want to report the actual server names
// for which the enumeration failed.
return PrintSystemResult(printer_enum_succeeded_, std::string());
}
void PrintSystemCUPS::GetPrinterCapsAndDefaults(
const std::string& printer_name,
const PrinterCapsAndDefaultsCallback& callback) {
printing::PrinterCapsAndDefaults printer_info;
bool succeeded = GetPrinterCapsAndDefaults(printer_name, &printer_info);
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&PrintSystemCUPS::RunCapsCallback,
callback,
succeeded,
printer_name,
printer_info));
}
bool PrintSystemCUPS::IsValidPrinter(const std::string& printer_name) {
return GetPrinterInfo(printer_name, NULL);
}
bool PrintSystemCUPS::ValidatePrintTicket(
const std::string& printer_name,
const std::string& print_ticket_data,
const std::string& print_ticket_mime_type) {
DCHECK(initialized_);
scoped_ptr<base::Value> ticket_value(
base::JSONReader::Read(print_ticket_data));
return ticket_value != NULL &&
ticket_value->IsType(base::Value::TYPE_DICTIONARY);
}
// Print ticket on linux is a JSON string containing only one dictionary.
bool PrintSystemCUPS::ParsePrintTicket(
const std::string& print_ticket,
std::map<std::string, std::string>* options) {
DCHECK(options);
scoped_ptr<base::Value> ticket_value(base::JSONReader::Read(print_ticket));
if (ticket_value == NULL ||
!ticket_value->IsType(base::Value::TYPE_DICTIONARY)) {
return false;
}
options->clear();
base::DictionaryValue* ticket_dict =
static_cast<base::DictionaryValue*>(ticket_value.get());
for (base::DictionaryValue::Iterator it(*ticket_dict); !it.IsAtEnd();
it.Advance()) {
std::string value;
if (it.value().GetAsString(&value))
(*options)[it.key()] = value;
}
return true;
}
bool PrintSystemCUPS::GetPrinterCapsAndDefaults(
const std::string& printer_name,
printing::PrinterCapsAndDefaults* printer_info) {
DCHECK(initialized_);
std::string short_printer_name;
PrintServerInfoCUPS* server_info =
FindServerByFullName(printer_name, &short_printer_name);
if (!server_info)
return false;
PrintServerInfoCUPS::CapsMap::iterator caps_it =
server_info->caps_cache.find(printer_name);
if (caps_it != server_info->caps_cache.end()) {
*printer_info = caps_it->second;
return true;
}
// TODO(gene): Retry multiple times in case of error.
crash_keys::ScopedPrinterInfo crash_key(
server_info->backend->GetPrinterDriverInfo(short_printer_name));
if (!server_info->backend->GetPrinterCapsAndDefaults(short_printer_name,
printer_info) ) {
return false;
}
server_info->caps_cache[printer_name] = *printer_info;
return true;
}
bool PrintSystemCUPS::GetJobDetails(const std::string& printer_name,
PlatformJobId job_id,
PrintJobDetails *job_details) {
DCHECK(initialized_);
DCHECK(job_details);
std::string short_printer_name;
PrintServerInfoCUPS* server_info =
FindServerByFullName(printer_name, &short_printer_name);
if (!server_info)
return false;
crash_keys::ScopedPrinterInfo crash_key(
server_info->backend->GetPrinterDriverInfo(short_printer_name));
cups_job_t* jobs = NULL;
int num_jobs = GetJobs(&jobs, server_info->url, cups_encryption_,
short_printer_name.c_str(), 1, -1);
bool error = (num_jobs == 0) && (cupsLastError() > IPP_OK_EVENTS_COMPLETE);
if (error) {
VLOG(1) << "CP_CUPS: Error getting jobs from CUPS server"
<< ", printer name:" << printer_name
<< ", error: " << static_cast<int>(cupsLastError());
return false;
}
// Check if the request is for dummy dry run job.
// We check this after calling GetJobs API to see if this printer is actually
// accessible through CUPS.
if (job_id == kDryRunJobId) {
job_details->status = PRINT_JOB_STATUS_COMPLETED;
VLOG(1) << "CP_CUPS: Dry run job succeeded"
<< ", printer name: " << printer_name;
return true;
}
bool found = false;
for (int i = 0; i < num_jobs; i++) {
if (jobs[i].id == job_id) {
found = true;
switch (jobs[i].state) {
case IPP_JOB_PENDING :
case IPP_JOB_HELD :
case IPP_JOB_PROCESSING :
job_details->status = PRINT_JOB_STATUS_IN_PROGRESS;
break;
case IPP_JOB_STOPPED :
case IPP_JOB_CANCELLED :
case IPP_JOB_ABORTED :
job_details->status = PRINT_JOB_STATUS_ERROR;
break;
case IPP_JOB_COMPLETED :
job_details->status = PRINT_JOB_STATUS_COMPLETED;
break;
default:
job_details->status = PRINT_JOB_STATUS_INVALID;
}
job_details->platform_status_flags = jobs[i].state;
// We don't have any details on the number of processed pages here.
break;
}
}
if (found)
VLOG(1) << "CP_CUPS: Job found"
<< ", printer name: " << printer_name
<< ", cups job id: " << job_id
<< ", cups job status: " << job_details->status;
else
LOG(WARNING) << "CP_CUPS: Job not found"
<< ", printer name: " << printer_name
<< ", cups job id: " << job_id;
cupsFreeJobs(num_jobs, jobs);
return found;
}
bool PrintSystemCUPS::GetPrinterInfo(const std::string& printer_name,
printing::PrinterBasicInfo* info) {
DCHECK(initialized_);
if (info)
VLOG(1) << "CP_CUPS: Getting printer info"
<< ", printer name: " << printer_name;
std::string short_printer_name;
PrintServerInfoCUPS* server_info =
FindServerByFullName(printer_name, &short_printer_name);
if (!server_info)
return false;
printing::PrinterList::iterator it;
for (it = server_info->printers.begin();
it != server_info->printers.end(); ++it) {
if (it->printer_name == printer_name) {
if (info)
*info = *it;
return true;
}
}
return false;
}
PrintSystem::PrintServerWatcher*
PrintSystemCUPS::CreatePrintServerWatcher() {
DCHECK(initialized_);
return new PrintServerWatcherCUPS(this);
}
PrintSystem::PrinterWatcher* PrintSystemCUPS::CreatePrinterWatcher(
const std::string& printer_name) {
DCHECK(initialized_);
DCHECK(!printer_name.empty());
return new PrinterWatcherCUPS(this, printer_name);
}
PrintSystem::JobSpooler* PrintSystemCUPS::CreateJobSpooler() {
DCHECK(initialized_);
return new JobSpoolerCUPS(this);
}
bool PrintSystemCUPS::UseCddAndCjt() {
return false;
}
std::string PrintSystemCUPS::GetSupportedMimeTypes() {
return supported_mime_types_;
}
scoped_refptr<PrintSystem> PrintSystem::CreateInstance(
const base::DictionaryValue* print_system_settings) {
return new PrintSystemCUPS(print_system_settings);
}
int PrintSystemCUPS::PrintFile(const GURL& url, http_encryption_t encryption,
const char* name, const char* filename,
const char* title, int num_options,
cups_option_t* options) {
if (url.is_empty()) { // Use default (local) print server.
return cupsPrintFile(name, filename, title, num_options, options);
} else {
printing::HttpConnectionCUPS http(url, encryption);
http.SetBlocking(false);
return cupsPrintFile2(http.http(), name, filename,
title, num_options, options);
}
}
int PrintSystemCUPS::GetJobs(cups_job_t** jobs, const GURL& url,
http_encryption_t encryption,
const char* name, int myjobs, int whichjobs) {
if (url.is_empty()) { // Use default (local) print server.
return cupsGetJobs(jobs, name, myjobs, whichjobs);
} else {
printing::HttpConnectionCUPS http(url, encryption);
http.SetBlocking(false);
return cupsGetJobs2(http.http(), jobs, name, myjobs, whichjobs);
}
}
PlatformJobId PrintSystemCUPS::SpoolPrintJob(
const std::string& print_ticket,
const base::FilePath& print_data_file_path,
const std::string& print_data_mime_type,
const std::string& printer_name,
const std::string& job_title,
const std::vector<std::string>& tags,
bool* dry_run) {
DCHECK(initialized_);
VLOG(1) << "CP_CUPS: Spooling print job, printer name: " << printer_name;
std::string short_printer_name;
PrintServerInfoCUPS* server_info =
FindServerByFullName(printer_name, &short_printer_name);
if (!server_info)
return false;
crash_keys::ScopedPrinterInfo crash_key(
server_info->backend->GetPrinterDriverInfo(printer_name));
// We need to store options as char* string for the duration of the
// cupsPrintFile2 call. We'll use map here to store options, since
// Dictionary value from JSON parser returns wchat_t.
std::map<std::string, std::string> options;
bool res = ParsePrintTicket(print_ticket, &options);
DCHECK(res); // If print ticket is invalid we still print using defaults.
// Check if this is a dry run (test) job.
*dry_run = IsDryRunJob(tags);
if (*dry_run) {
VLOG(1) << "CP_CUPS: Dry run job spooled";
return kDryRunJobId;
}
std::vector<cups_option_t> cups_options;
std::map<std::string, std::string>::iterator it;
for (it = options.begin(); it != options.end(); ++it) {
cups_option_t opt;
opt.name = const_cast<char*>(it->first.c_str());
opt.value = const_cast<char*>(it->second.c_str());
cups_options.push_back(opt);
}
int job_id = PrintFile(server_info->url,
cups_encryption_,
short_printer_name.c_str(),
print_data_file_path.value().c_str(),
job_title.c_str(),
cups_options.size(),
&(cups_options[0]));
// TODO(alexyu): Output printer id.
VLOG(1) << "CP_CUPS: Job spooled"
<< ", printer name: " << printer_name
<< ", cups job id: " << job_id;
return job_id;
}
std::string PrintSystemCUPS::MakeFullPrinterName(
const GURL& url, const std::string& short_printer_name) {
std::string full_name;
full_name += "\\\\";
full_name += url.host();
if (!url.port().empty()) {
full_name += ":";
full_name += url.port();
}
full_name += "\\";
full_name += short_printer_name;
return full_name;
}
PrintServerInfoCUPS* PrintSystemCUPS::FindServerByFullName(
const std::string& full_printer_name, std::string* short_printer_name) {
size_t front = full_printer_name.find("\\\\");
size_t separator = full_printer_name.find("\\", 2);
if (front == std::string::npos || separator == std::string::npos) {
LOG(WARNING) << "CP_CUPS: Invalid UNC"
<< ", printer name: " << full_printer_name;
return NULL;
}
std::string server = full_printer_name.substr(2, separator - 2);
PrintServerList::iterator it;
for (it = print_servers_.begin(); it != print_servers_.end(); ++it) {
std::string cur_server;
cur_server += it->url.host();
if (!it->url.port().empty()) {
cur_server += ":";
cur_server += it->url.port();
}
if (cur_server == server) {
*short_printer_name = full_printer_name.substr(separator + 1);
return &(*it);
}
}
LOG(WARNING) << "CP_CUPS: Server not found"
<< ", printer name: " << full_printer_name;
return NULL;
}
void PrintSystemCUPS::RunCapsCallback(
const PrinterCapsAndDefaultsCallback& callback,
bool succeeded,
const std::string& printer_name,
const printing::PrinterCapsAndDefaults& printer_info) {
callback.Run(succeeded, printer_name, printer_info);
}
} // namespace cloud_print
|