OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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_BROWSER_PRINTING_CLOUD_PRINT_PRINTER_JOB_HANDLER_H_ |
| 6 #define CHROME_BROWSER_PRINTING_CLOUD_PRINT_PRINTER_JOB_HANDLER_H_ |
| 7 |
| 8 #include <list> |
| 9 #include <string> |
| 10 |
| 11 #include "base/file_path.h" |
| 12 #include "base/ref_counted.h" |
| 13 #include "base/thread.h" |
| 14 #include "chrome/browser/printing/cloud_print/job_status_updater.h" |
| 15 #include "chrome/browser/printing/cloud_print/printer_info.h" |
| 16 #include "chrome/browser/net/url_fetcher.h" |
| 17 #include "net/url_request/url_request_status.h" |
| 18 |
| 19 // A class that handles cloud print jobs for a particular printer. This class |
| 20 // imlements a state machine that transitions from Start to various states. The |
| 21 // various states are shown in the below diagram. |
| 22 // the status on the server. |
| 23 |
| 24 // Start --> No pending tasks --> Done |
| 25 // | |
| 26 // | |
| 27 // | Have Pending tasks |
| 28 // | |
| 29 // | |
| 30 // <----Delete Pending -- | ---Update Pending-----> |
| 31 // | | | |
| 32 // | | | |
| 33 // | | | |
| 34 // Delete Printer from server | Update Printer info on server |
| 35 // Shutdown | Go to Stop |
| 36 // | |
| 37 // | Job Available |
| 38 // | |
| 39 // | |
| 40 // Fetch Next Job Metadata |
| 41 // Fetch Print Ticket |
| 42 // Fetch Print Data |
| 43 // Spool Print Job |
| 44 // Create Job StatusUpdater for job |
| 45 // Mark job as "in progress" on server |
| 46 // (On any unrecoverable error in any of the above steps go to Stop) |
| 47 // Go to Stop |
| 48 // | |
| 49 // | |
| 50 // | |
| 51 // | |
| 52 // | |
| 53 // | |
| 54 // | |
| 55 // Stop |
| 56 // (If there are pending tasks go back to Start) |
| 57 |
| 58 typedef URLFetcher::Delegate URLFetcherDelegate; |
| 59 |
| 60 class PrinterJobHandler : public base::RefCountedThreadSafe<PrinterJobHandler>, |
| 61 public URLFetcherDelegate, |
| 62 public JobStatusUpdaterDelegate, |
| 63 public cloud_print::PrinterChangeNotifierDelegate { |
| 64 enum PrintJobError { |
| 65 SUCCESS, |
| 66 JOB_DOWNLOAD_FAILED, |
| 67 INVALID_JOB_DATA, |
| 68 PRINT_FAILED, |
| 69 }; |
| 70 struct JobDetails { |
| 71 std::string job_id_; |
| 72 std::string job_title_; |
| 73 std::string print_ticket_; |
| 74 FilePath print_data_file_path_; |
| 75 std::string print_data_mime_type_; |
| 76 void Clear() { |
| 77 job_id_.clear(); |
| 78 job_title_.clear(); |
| 79 print_ticket_.clear(); |
| 80 print_data_mime_type_.clear(); |
| 81 print_data_file_path_ = FilePath(); |
| 82 } |
| 83 }; |
| 84 |
| 85 public: |
| 86 class Delegate { |
| 87 public: |
| 88 virtual void OnPrinterJobHandlerShutdown( |
| 89 PrinterJobHandler* job_handler, const std::string& printer_id) = 0; |
| 90 }; |
| 91 |
| 92 // Begin public interface |
| 93 PrinterJobHandler(const cloud_print::PrinterBasicInfo& printer_info, |
| 94 const std::string& printer_id, |
| 95 const std::string& caps_hash, |
| 96 const std::string& auth_token, |
| 97 Delegate* delegate); |
| 98 ~PrinterJobHandler(); |
| 99 bool Initialize(); |
| 100 // Notifies the JobHandler that a job is available |
| 101 void NotifyJobAvailable(); |
| 102 // Shutdown everything (the process is exiting). |
| 103 void Shutdown(); |
| 104 // End public interface |
| 105 |
| 106 // Begin Delegate implementations |
| 107 |
| 108 // URLFetcher::Delegate implementation. |
| 109 virtual void OnURLFetchComplete(const URLFetcher* source, const GURL& url, |
| 110 const URLRequestStatus& status, |
| 111 int response_code, |
| 112 const ResponseCookies& cookies, |
| 113 const std::string& data); |
| 114 // JobStatusUpdater::Delegate implementation |
| 115 virtual bool OnJobCompleted(JobStatusUpdater* updater); |
| 116 // cloud_print::PrinterChangeNotifier::Delegate implementation |
| 117 virtual void OnPrinterAdded(); |
| 118 virtual void OnPrinterDeleted(); |
| 119 virtual void OnPrinterChanged(); |
| 120 virtual void OnJobChanged(); |
| 121 |
| 122 // End Delegate implementations |
| 123 |
| 124 private: |
| 125 // Prototype for a response handler. The return value indicates whether the |
| 126 // request should be retried, false means "retry", true means "do not retry" |
| 127 typedef bool (PrinterJobHandler::*ResponseHandler)( |
| 128 const URLFetcher* source, const GURL& url, |
| 129 const URLRequestStatus& status, int response_code, |
| 130 const ResponseCookies& cookies, const std::string& data); |
| 131 // Begin request handlers for each state in the state machine |
| 132 bool HandlePrinterUpdateResponse(const URLFetcher* source, const GURL& url, |
| 133 const URLRequestStatus& status, |
| 134 int response_code, |
| 135 const ResponseCookies& cookies, |
| 136 const std::string& data); |
| 137 bool HandlePrinterDeleteResponse(const URLFetcher* source, const GURL& url, |
| 138 const URLRequestStatus& status, |
| 139 int response_code, |
| 140 const ResponseCookies& cookies, |
| 141 const std::string& data); |
| 142 bool HandleJobMetadataResponse(const URLFetcher* source, const GURL& url, |
| 143 const URLRequestStatus& status, |
| 144 int response_code, |
| 145 const ResponseCookies& cookies, |
| 146 const std::string& data); |
| 147 bool HandlePrintTicketResponse(const URLFetcher* source, |
| 148 const GURL& url, |
| 149 const URLRequestStatus& status, |
| 150 int response_code, |
| 151 const ResponseCookies& cookies, |
| 152 const std::string& data); |
| 153 bool HandlePrintDataResponse(const URLFetcher* source, |
| 154 const GURL& url, |
| 155 const URLRequestStatus& status, |
| 156 int response_code, |
| 157 const ResponseCookies& cookies, |
| 158 const std::string& data); |
| 159 bool HandleSuccessStatusUpdateResponse(const URLFetcher* source, |
| 160 const GURL& url, |
| 161 const URLRequestStatus& status, |
| 162 int response_code, |
| 163 const ResponseCookies& cookies, |
| 164 const std::string& data); |
| 165 bool HandleFailureStatusUpdateResponse(const URLFetcher* source, |
| 166 const GURL& url, |
| 167 const URLRequestStatus& status, |
| 168 int response_code, |
| 169 const ResponseCookies& cookies, |
| 170 const std::string& data); |
| 171 // End request handlers for each state in the state machine |
| 172 |
| 173 // Start the state machine. Based on the flags set this could mean updating |
| 174 // printer information, deleting the printer from the server or looking for |
| 175 // new print jobs |
| 176 void Start(); |
| 177 |
| 178 // End the state machine. If there are pending tasks, we will post a Start |
| 179 // again. |
| 180 void Stop(); |
| 181 |
| 182 void StartPrinting(); |
| 183 void HandleServerError(const GURL& url); |
| 184 void Reset(); |
| 185 void UpdateJobStatus(cloud_print::PrintJobStatus status, PrintJobError error); |
| 186 void MakeServerRequest(const GURL& url, ResponseHandler response_handler); |
| 187 void JobFailed(PrintJobError error); |
| 188 void JobSpooled(cloud_print::PlatformJobId local_job_id); |
| 189 // Returns false if printer info is up to date and no updating is needed. |
| 190 bool UpdatePrinterInfo(); |
| 191 bool HavePendingTasks(); |
| 192 |
| 193 static void DoPrint(const JobDetails& job_details, |
| 194 const std::string& printer_name, |
| 195 PrinterJobHandler* job_handler, |
| 196 MessageLoop* job_message_loop); |
| 197 |
| 198 |
| 199 scoped_ptr<URLFetcher> request_; |
| 200 cloud_print::PrinterBasicInfo printer_info_; |
| 201 std::string printer_id_; |
| 202 std::string auth_token_; |
| 203 std::string last_caps_hash_; |
| 204 std::string print_data_url_; |
| 205 JobDetails job_details_; |
| 206 Delegate* delegate_; |
| 207 // Once the job has been spooled to the local spooler, this specifies the |
| 208 // job id of the job on the local spooler. |
| 209 cloud_print::PlatformJobId local_job_id_; |
| 210 ResponseHandler next_response_handler_; |
| 211 // The number of consecutive times that connecting to the server failed. |
| 212 int server_error_count_; |
| 213 // The thread on which the actual print operation happens |
| 214 base::Thread print_thread_; |
| 215 // There may be pending tasks in the message queue when Shutdown is called. |
| 216 // We set this flag so as to do nothing in those tasks. |
| 217 bool shutting_down_; |
| 218 |
| 219 // Flags that specify various pending server updates |
| 220 bool server_job_available_; |
| 221 bool printer_update_pending_; |
| 222 bool printer_delete_pending_; |
| 223 |
| 224 // Some task in the state machine is in progress. |
| 225 bool task_in_progress_; |
| 226 cloud_print::PrinterChangeNotifier printer_change_notifier_; |
| 227 typedef std::list< scoped_refptr<JobStatusUpdater> > JobStatusUpdaterList; |
| 228 JobStatusUpdaterList job_status_updater_list_; |
| 229 |
| 230 DISALLOW_COPY_AND_ASSIGN(PrinterJobHandler); |
| 231 }; |
| 232 |
| 233 // This typedef is to workaround the issue with certain versions of |
| 234 // Visual Studio where it gets confused between multiple Delegate |
| 235 // classes and gives a C2500 error. (I saw this error on the try bots - |
| 236 // the workaround was not needed for my machine). |
| 237 typedef PrinterJobHandler::Delegate PrinterJobHandlerDelegate; |
| 238 |
| 239 #endif // CHROME_BROWSER_PRINTING_CLOUD_PRINT_PRINTER_JOB_HANDLER_H_ |
| 240 |
OLD | NEW |