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: ~Delegate()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: ~Delegate()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: ~Delegate()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 std::string& print_ticket_mime_type, 134 const base::FilePath& print_data_file_path, 135 const std::string& print_data_mime_type, 136 const std::string& printer_name, 137 const std::string& job_title, 138 const std::vector<std::string>& tags, 139 JobSpooler::Delegate* delegate) = 0; 140 protected: 141 friend class base::RefCountedThreadSafe<JobSpooler>; 142 virtual ~JobSpooler(); 143 }; 144 145 class PrintSystemResult { 146 public: PrintSystemResult(bool succeeded,const std::string & message)147 PrintSystemResult(bool succeeded, const std::string& message) 148 : succeeded_(succeeded), message_(message) { } succeeded()149 bool succeeded() const { return succeeded_; } message()150 std::string message() const { return message_; } 151 152 private: PrintSystemResult()153 PrintSystemResult() {} 154 155 bool succeeded_; 156 std::string message_; 157 }; 158 159 typedef base::Callback<void(bool, 160 const std::string&, 161 const printing::PrinterCapsAndDefaults&)> 162 PrinterCapsAndDefaultsCallback; 163 164 // Initialize print system. This need to be called before any other function 165 // of PrintSystem. 166 virtual PrintSystemResult Init() = 0; 167 168 // Enumerates the list of installed local and network printers. 169 virtual PrintSystemResult EnumeratePrinters( 170 printing::PrinterList* printer_list) = 0; 171 172 // Gets the capabilities and defaults for a specific printer asynchronously. 173 virtual void GetPrinterCapsAndDefaults( 174 const std::string& printer_name, 175 const PrinterCapsAndDefaultsCallback& callback) = 0; 176 177 // Returns true if printer_name points to a valid printer. 178 virtual bool IsValidPrinter(const std::string& printer_name) = 0; 179 180 // Returns true if ticket is valid. 181 virtual bool ValidatePrintTicket( 182 const std::string& printer_name, 183 const std::string& print_ticket_data, 184 const std::string& print_ticket_mime_type) = 0; 185 186 // Get details for already spooled job. 187 virtual bool GetJobDetails(const std::string& printer_name, 188 PlatformJobId job_id, 189 PrintJobDetails* job_details) = 0; 190 191 // Factory methods to create corresponding watcher. Callee is responsible 192 // for deleting objects. Return NULL if failed. 193 virtual PrintServerWatcher* CreatePrintServerWatcher() = 0; 194 virtual PrinterWatcher* CreatePrinterWatcher( 195 const std::string& printer_name) = 0; 196 virtual JobSpooler* CreateJobSpooler() = 0; 197 198 // Returns a true if connector should use CDD for capabilities and CJT as 199 // print ticket. 200 virtual bool UseCddAndCjt() = 0; 201 202 // Returns a comma separated list of mimetypes for print data that are 203 // supported by this print system. The format of this string is the same as 204 // that used for the HTTP Accept: header. 205 virtual std::string GetSupportedMimeTypes() = 0; 206 207 // Generate unique for proxy. 208 static std::string GenerateProxyId(); 209 210 // Call this function to obtain printing system for specified print server. 211 // If print settings are NULL, default settings will be used. 212 // Return NULL if no print system available. 213 static scoped_refptr<PrintSystem> CreateInstance( 214 const base::DictionaryValue* print_system_settings); 215 216 protected: 217 friend class base::RefCountedThreadSafe<PrintSystem>; 218 virtual ~PrintSystem(); 219 }; 220 221 } // namespace cloud_print 222 223 #endif // CHROME_SERVICE_CLOUD_PRINT_PRINT_SYSTEM_H_ 224