| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_SERVICE_CLOUD_PRINT_PRINTER_JOB_HANDLER_H_ | 5 #ifndef CHROME_SERVICE_CLOUD_PRINT_PRINTER_JOB_HANDLER_H_ |
| 6 #define CHROME_SERVICE_CLOUD_PRINT_PRINTER_JOB_HANDLER_H_ | 6 #define CHROME_SERVICE_CLOUD_PRINT_PRINTER_JOB_HANDLER_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/ref_counted.h" | 12 #include "base/ref_counted.h" |
| 13 #include "base/message_loop_proxy.h" |
| 13 #include "base/thread.h" | 14 #include "base/thread.h" |
| 14 #include "chrome/service/cloud_print/job_status_updater.h" | 15 #include "chrome/service/cloud_print/job_status_updater.h" |
| 15 #include "chrome/service/cloud_print/print_system.h" | 16 #include "chrome/service/cloud_print/print_system.h" |
| 16 #include "chrome/common/net/url_fetcher.h" | 17 #include "chrome/common/net/url_fetcher.h" |
| 17 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
| 18 #include "net/url_request/url_request_status.h" | 19 #include "net/url_request/url_request_status.h" |
| 19 | 20 |
| 20 // A class that handles cloud print jobs for a particular printer. This class | 21 // A class that handles cloud print jobs for a particular printer. This class |
| 21 // imlements a state machine that transitions from Start to various states. The | 22 // imlements a state machine that transitions from Start to various states. The |
| 22 // various states are shown in the below diagram. | 23 // various states are shown in the below diagram. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 // | | 55 // | |
| 55 // | | 56 // | |
| 56 // Stop | 57 // Stop |
| 57 // (If there are pending tasks go back to Start) | 58 // (If there are pending tasks go back to Start) |
| 58 | 59 |
| 59 typedef URLFetcher::Delegate URLFetcherDelegate; | 60 typedef URLFetcher::Delegate URLFetcherDelegate; |
| 60 | 61 |
| 61 class PrinterJobHandler : public base::RefCountedThreadSafe<PrinterJobHandler>, | 62 class PrinterJobHandler : public base::RefCountedThreadSafe<PrinterJobHandler>, |
| 62 public URLFetcherDelegate, | 63 public URLFetcherDelegate, |
| 63 public JobStatusUpdaterDelegate, | 64 public JobStatusUpdaterDelegate, |
| 64 public cloud_print::PrinterWatcherDelegate { | 65 public cloud_print::PrinterWatcherDelegate, |
| 66 public cloud_print::JobSpoolerDelegate { |
| 65 enum PrintJobError { | 67 enum PrintJobError { |
| 66 SUCCESS, | 68 SUCCESS, |
| 67 JOB_DOWNLOAD_FAILED, | 69 JOB_DOWNLOAD_FAILED, |
| 68 INVALID_JOB_DATA, | 70 INVALID_JOB_DATA, |
| 69 PRINT_FAILED, | 71 PRINT_FAILED, |
| 70 }; | 72 }; |
| 71 struct JobDetails { | 73 struct JobDetails { |
| 72 std::string job_id_; | 74 std::string job_id_; |
| 73 std::string job_title_; | 75 std::string job_title_; |
| 74 std::string print_ticket_; | 76 std::string print_ticket_; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 int response_code, | 116 int response_code, |
| 115 const ResponseCookies& cookies, | 117 const ResponseCookies& cookies, |
| 116 const std::string& data); | 118 const std::string& data); |
| 117 // JobStatusUpdater::Delegate implementation | 119 // JobStatusUpdater::Delegate implementation |
| 118 virtual bool OnJobCompleted(JobStatusUpdater* updater); | 120 virtual bool OnJobCompleted(JobStatusUpdater* updater); |
| 119 // cloud_print::PrinterWatcherDelegate implementation | 121 // cloud_print::PrinterWatcherDelegate implementation |
| 120 virtual void OnPrinterDeleted(); | 122 virtual void OnPrinterDeleted(); |
| 121 virtual void OnPrinterChanged(); | 123 virtual void OnPrinterChanged(); |
| 122 virtual void OnJobChanged(); | 124 virtual void OnJobChanged(); |
| 123 | 125 |
| 126 // cloud_print::JobSpoolerDelegate implementation. |
| 127 // Called on print_thread_. |
| 128 virtual void OnJobSpoolSucceeded(const cloud_print::PlatformJobId& job_id); |
| 129 virtual void OnJobSpoolFailed(); |
| 130 |
| 124 // End Delegate implementations | 131 // End Delegate implementations |
| 125 | 132 |
| 126 private: | 133 private: |
| 127 // Prototype for a response handler. The return value indicates whether the | 134 // Prototype for a response handler. The return value indicates whether the |
| 128 // request should be retried, false means "retry", true means "do not retry" | 135 // request should be retried, false means "retry", true means "do not retry" |
| 129 typedef bool (PrinterJobHandler::*ResponseHandler)( | 136 typedef bool (PrinterJobHandler::*ResponseHandler)( |
| 130 const URLFetcher* source, const GURL& url, | 137 const URLFetcher* source, const GURL& url, |
| 131 const URLRequestStatus& status, int response_code, | 138 const URLRequestStatus& status, int response_code, |
| 132 const ResponseCookies& cookies, const std::string& data); | 139 const ResponseCookies& cookies, const std::string& data); |
| 133 // Prototype for a failure handler. This handler will be executed if all | 140 // Prototype for a failure handler. This handler will be executed if all |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 // to decide which handler to call. | 207 // to decide which handler to call. |
| 201 void FetchURL(const GURL& url); | 208 void FetchURL(const GURL& url); |
| 202 | 209 |
| 203 void JobFailed(PrintJobError error); | 210 void JobFailed(PrintJobError error); |
| 204 void JobSpooled(cloud_print::PlatformJobId local_job_id); | 211 void JobSpooled(cloud_print::PlatformJobId local_job_id); |
| 205 // Returns false if printer info is up to date and no updating is needed. | 212 // Returns false if printer info is up to date and no updating is needed. |
| 206 bool UpdatePrinterInfo(); | 213 bool UpdatePrinterInfo(); |
| 207 bool HavePendingTasks(); | 214 bool HavePendingTasks(); |
| 208 void FailedFetchingJobData(); | 215 void FailedFetchingJobData(); |
| 209 | 216 |
| 210 static void DoPrint(const JobDetails& job_details, | 217 // Called on print_thread_. |
| 211 const std::string& printer_name, | 218 void DoPrint(const JobDetails& job_details, |
| 212 scoped_refptr<cloud_print::PrintSystem> print_system, | 219 const std::string& printer_name); |
| 213 PrinterJobHandler* job_handler, | |
| 214 MessageLoop* job_message_loop); | |
| 215 | 220 |
| 216 scoped_ptr<URLFetcher> request_; | 221 scoped_ptr<URLFetcher> request_; |
| 217 scoped_refptr<cloud_print::PrintSystem> print_system_; | 222 scoped_refptr<cloud_print::PrintSystem> print_system_; |
| 218 cloud_print::PrinterBasicInfo printer_info_; | 223 cloud_print::PrinterBasicInfo printer_info_; |
| 219 std::string printer_id_; | 224 std::string printer_id_; |
| 220 std::string auth_token_; | 225 std::string auth_token_; |
| 221 std::string last_caps_hash_; | 226 std::string last_caps_hash_; |
| 222 GURL cloud_print_server_url_; | 227 GURL cloud_print_server_url_; |
| 223 std::string print_data_url_; | 228 std::string print_data_url_; |
| 224 JobDetails job_details_; | 229 JobDetails job_details_; |
| 225 Delegate* delegate_; | 230 Delegate* delegate_; |
| 226 // Once the job has been spooled to the local spooler, this specifies the | 231 // Once the job has been spooled to the local spooler, this specifies the |
| 227 // job id of the job on the local spooler. | 232 // job id of the job on the local spooler. |
| 228 cloud_print::PlatformJobId local_job_id_; | 233 cloud_print::PlatformJobId local_job_id_; |
| 229 ResponseHandler next_response_handler_; | 234 ResponseHandler next_response_handler_; |
| 230 FailureHandler next_failure_handler_; | 235 FailureHandler next_failure_handler_; |
| 231 // The number of consecutive times that connecting to the server failed. | 236 // The number of consecutive times that connecting to the server failed. |
| 232 int server_error_count_; | 237 int server_error_count_; |
| 233 // The thread on which the actual print operation happens | 238 // The thread on which the actual print operation happens |
| 234 base::Thread print_thread_; | 239 base::Thread print_thread_; |
| 240 // The Job spooler object. This is only non-NULL during a print operation. |
| 241 // It lives and dies on |print_thread_| |
| 242 scoped_refptr<cloud_print::PrintSystem::JobSpooler> job_spooler_; |
| 243 // The message loop proxy representing the thread on which this object |
| 244 // was created. Used by the print thread. |
| 245 scoped_refptr<base::MessageLoopProxy> job_handler_message_loop_proxy_; |
| 246 |
| 235 // There may be pending tasks in the message queue when Shutdown is called. | 247 // There may be pending tasks in the message queue when Shutdown is called. |
| 236 // We set this flag so as to do nothing in those tasks. | 248 // We set this flag so as to do nothing in those tasks. |
| 237 bool shutting_down_; | 249 bool shutting_down_; |
| 238 | 250 |
| 239 // Flags that specify various pending server updates | 251 // Flags that specify various pending server updates |
| 240 bool server_job_available_; | 252 bool server_job_available_; |
| 241 bool printer_update_pending_; | 253 bool printer_update_pending_; |
| 242 bool printer_delete_pending_; | 254 bool printer_delete_pending_; |
| 243 | 255 |
| 244 // Some task in the state machine is in progress. | 256 // Some task in the state machine is in progress. |
| 245 bool task_in_progress_; | 257 bool task_in_progress_; |
| 246 scoped_refptr<cloud_print::PrintSystem::PrinterWatcher> printer_watcher_; | 258 scoped_refptr<cloud_print::PrintSystem::PrinterWatcher> printer_watcher_; |
| 247 typedef std::list< scoped_refptr<JobStatusUpdater> > JobStatusUpdaterList; | 259 typedef std::list< scoped_refptr<JobStatusUpdater> > JobStatusUpdaterList; |
| 248 JobStatusUpdaterList job_status_updater_list_; | 260 JobStatusUpdaterList job_status_updater_list_; |
| 249 | 261 |
| 250 DISALLOW_COPY_AND_ASSIGN(PrinterJobHandler); | 262 DISALLOW_COPY_AND_ASSIGN(PrinterJobHandler); |
| 251 }; | 263 }; |
| 252 | 264 |
| 253 // This typedef is to workaround the issue with certain versions of | 265 // This typedef is to workaround the issue with certain versions of |
| 254 // Visual Studio where it gets confused between multiple Delegate | 266 // Visual Studio where it gets confused between multiple Delegate |
| 255 // classes and gives a C2500 error. (I saw this error on the try bots - | 267 // classes and gives a C2500 error. (I saw this error on the try bots - |
| 256 // the workaround was not needed for my machine). | 268 // the workaround was not needed for my machine). |
| 257 typedef PrinterJobHandler::Delegate PrinterJobHandlerDelegate; | 269 typedef PrinterJobHandler::Delegate PrinterJobHandlerDelegate; |
| 258 | 270 |
| 259 #endif // CHROME_SERVICE_CLOUD_PRINT_PRINTER_JOB_HANDLER_H_ | 271 #endif // CHROME_SERVICE_CLOUD_PRINT_PRINTER_JOB_HANDLER_H_ |
| 260 | 272 |
| OLD | NEW |