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