Home | History | Annotate | Download | only in cloud_print
      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.
      4 
      5 #ifndef CHROME_SERVICE_CLOUD_PRINT_PRINT_SYSTEM_H_
      6 #define CHROME_SERVICE_CLOUD_PRINT_PRINT_SYSTEM_H_
      7 
      8 #include <map>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/callback.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "printing/backend/print_backend.h"
     15 
     16 namespace base {
     17 class DictionaryValue;
     18 class FilePath;
     19 }
     20 
     21 namespace printing {
     22 struct PrinterBasicInfo;
     23 struct PrinterCapsAndDefaults;
     24 }
     25 
     26 // This is the interface for platform-specific code for cloud print
     27 namespace cloud_print {
     28 
     29 typedef int PlatformJobId;
     30 
     31 enum PrintJobStatus {
     32   PRINT_JOB_STATUS_INVALID,
     33   PRINT_JOB_STATUS_IN_PROGRESS,
     34   PRINT_JOB_STATUS_ERROR,
     35   PRINT_JOB_STATUS_COMPLETED,
     36   PRINT_JOB_STATUS_MAX,
     37 };
     38 
     39 struct PrintJobDetails {
     40   PrintJobDetails();
     41 
     42   void Clear();
     43 
     44   bool operator ==(const PrintJobDetails& other) const {
     45     return (status == other.status) &&
     46            (platform_status_flags == other.platform_status_flags) &&
     47            (status_message == other.status_message) &&
     48            (total_pages == other.total_pages) &&
     49            (pages_printed == other.pages_printed);
     50   }
     51 
     52   bool operator !=(const PrintJobDetails& other) const {
     53     return !(*this == other);
     54   }
     55 
     56   PrintJobStatus status;
     57   int platform_status_flags;
     58   std::string status_message;
     59   int total_pages;
     60   int pages_printed;
     61 };
     62 
     63 // PrintSystem class will provide interface for different printing systems
     64 // (Windows, CUPS) to implement. User will call CreateInstance() to
     65 // obtain available printing system.
     66 // Please note, that PrintSystem is not platform specific, but rather
     67 // print system specific. For example, CUPS is available on both Linux and Mac,
     68 // but not available on ChromeOS, etc. This design allows us to add more
     69 // functionality on some platforms, while reusing core (CUPS) functions.
     70 class PrintSystem : public base::RefCountedThreadSafe<PrintSystem> {
     71  public:
     72   class PrintServerWatcher
     73       : public base::RefCountedThreadSafe<PrintServerWatcher> {
     74    public:
     75     // Callback interface for new printer notifications.
     76     class Delegate {
     77       public:
     78         virtual void OnPrinterAdded() = 0;
     79         // TODO(gene): Do we need OnPrinterDeleted notification here?
     80 
     81       protected:
     82         virtual ~Delegate() {}
     83     };
     84 
     85     virtual bool StartWatching(PrintServerWatcher::Delegate* delegate) = 0;
     86     virtual bool StopWatching() = 0;
     87 
     88    protected:
     89     friend class base::RefCountedThreadSafe<PrintServerWatcher>;
     90     virtual ~PrintServerWatcher();
     91   };
     92 
     93   class PrinterWatcher : public base::RefCountedThreadSafe<PrinterWatcher> {
     94    public:
     95     // Callback interface for printer updates notifications.
     96     class Delegate {
     97       public:
     98         virtual void OnPrinterDeleted() = 0;
     99         virtual void OnPrinterChanged() = 0;
    100         virtual void OnJobChanged() = 0;
    101 
    102       protected:
    103         virtual ~Delegate() {}
    104     };
    105 
    106     virtual bool StartWatching(PrinterWatcher::Delegate* delegate) = 0;
    107     virtual bool StopWatching() = 0;
    108     virtual bool GetCurrentPrinterInfo(
    109         printing::PrinterBasicInfo* printer_info) = 0;
    110 
    111    protected:
    112     friend class base::RefCountedThreadSafe<PrinterWatcher>;
    113     virtual ~PrinterWatcher();
    114   };
    115 
    116   class JobSpooler : public base::RefCountedThreadSafe<JobSpooler> {
    117    public:
    118     // Callback interface for JobSpooler notifications.
    119     class Delegate {
    120      public:
    121       virtual void OnJobSpoolSucceeded(const PlatformJobId& job_id) = 0;
    122       virtual void OnJobSpoolFailed() = 0;
    123 
    124      protected:
    125       virtual ~Delegate() {}
    126     };
    127 
    128     // Spool job to the printer asynchronously. Caller will be notified via
    129     // |delegate|. Note that only one print job can be in progress at any given
    130     // time. Subsequent calls to Spool (before the Delegate::OnJobSpoolSucceeded
    131     // or Delegate::OnJobSpoolFailed methods are called) can fail.
    132     virtual bool Spool(const std::string& print_ticket,
    133                        const base::FilePath& print_data_file_path,
    134                        const std::string& print_data_mime_type,
    135                        const std::string& printer_name,
    136                        const std::string& job_title,
    137                        const std::vector<std::string>& tags,
    138                        JobSpooler::Delegate* delegate) = 0;
    139    protected:
    140     friend class base::RefCountedThreadSafe<JobSpooler>;
    141     virtual ~JobSpooler();
    142   };
    143 
    144   class PrintSystemResult {
    145    public:
    146     PrintSystemResult(bool succeeded, const std::string& message)
    147         : succeeded_(succeeded), message_(message) { }
    148     bool succeeded() const { return succeeded_; }
    149     std::string message() const { return message_; }
    150 
    151    private:
    152     PrintSystemResult() {}
    153 
    154     bool succeeded_;
    155     std::string message_;
    156   };
    157 
    158   typedef base::Callback<void(bool,
    159                               const std::string&,
    160                               const printing::PrinterCapsAndDefaults&)>
    161       PrinterCapsAndDefaultsCallback;
    162 
    163   // Initialize print system. This need to be called before any other function
    164   // of PrintSystem.
    165   virtual PrintSystemResult Init() = 0;
    166 
    167   // Enumerates the list of installed local and network printers.
    168   virtual PrintSystemResult EnumeratePrinters(
    169       printing::PrinterList* printer_list) = 0;
    170 
    171   // Gets the capabilities and defaults for a specific printer asynchronously.
    172   virtual void GetPrinterCapsAndDefaults(
    173       const std::string& printer_name,
    174       const PrinterCapsAndDefaultsCallback& callback) = 0;
    175 
    176   // Returns true if printer_name points to a valid printer.
    177   virtual bool IsValidPrinter(const std::string& printer_name) = 0;
    178 
    179   // Returns true if ticket is valid.
    180   virtual bool ValidatePrintTicket(const std::string& printer_name,
    181                                    const std::string& print_ticket_data) = 0;
    182 
    183   // Get details for already spooled job.
    184   virtual bool GetJobDetails(const std::string& printer_name,
    185                              PlatformJobId job_id,
    186                              PrintJobDetails* job_details) = 0;
    187 
    188   // Factory methods to create corresponding watcher. Callee is responsible
    189   // for deleting objects. Return NULL if failed.
    190   virtual PrintServerWatcher* CreatePrintServerWatcher() = 0;
    191   virtual PrinterWatcher* CreatePrinterWatcher(
    192       const std::string& printer_name) = 0;
    193   virtual JobSpooler* CreateJobSpooler() = 0;
    194 
    195   // Returns a comma separated list of mimetypes for print data that are
    196   // supported by this print system. The format of this string is the same as
    197   // that used for the HTTP Accept: header.
    198   virtual std::string GetSupportedMimeTypes() = 0;
    199 
    200   // Generate unique for proxy.
    201   static std::string GenerateProxyId();
    202 
    203   // Call this function to obtain printing system for specified print server.
    204   // If print settings are NULL, default settings will be used.
    205   // Return NULL if no print system available.
    206   static scoped_refptr<PrintSystem> CreateInstance(
    207       const base::DictionaryValue* print_system_settings);
    208 
    209  protected:
    210   friend class base::RefCountedThreadSafe<PrintSystem>;
    211   virtual ~PrintSystem();
    212 };
    213 
    214 }  // namespace cloud_print
    215 
    216 #endif  // CHROME_SERVICE_CLOUD_PRINT_PRINT_SYSTEM_H_
    217