| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include <vector> | 10 #include <vector> |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 // | | 54 // | |
| 55 // | | 55 // | |
| 56 // | | 56 // | |
| 57 // | | 57 // | |
| 58 // | | 58 // | |
| 59 // | | 59 // | |
| 60 // | | 60 // | |
| 61 // Stop | 61 // Stop |
| 62 // (If there are pending tasks go back to Start) | 62 // (If there are pending tasks go back to Start) |
| 63 | 63 |
| 64 namespace cloud_print { |
| 65 |
| 64 class PrinterJobHandler : public base::RefCountedThreadSafe<PrinterJobHandler>, | 66 class PrinterJobHandler : public base::RefCountedThreadSafe<PrinterJobHandler>, |
| 65 public CloudPrintURLFetcherDelegate, | 67 public CloudPrintURLFetcherDelegate, |
| 66 public JobStatusUpdaterDelegate, | 68 public JobStatusUpdaterDelegate, |
| 67 public cloud_print::PrinterWatcherDelegate, | 69 public PrinterWatcherDelegate, |
| 68 public cloud_print::JobSpoolerDelegate { | 70 public JobSpoolerDelegate { |
| 69 public: | 71 public: |
| 70 class Delegate { | 72 class Delegate { |
| 71 public: | 73 public: |
| 72 // Notify delegate about authentication error. | 74 // Notify delegate about authentication error. |
| 73 virtual void OnAuthError() = 0; | 75 virtual void OnAuthError() = 0; |
| 74 // Notify delegate that printer has been deleted. | 76 // Notify delegate that printer has been deleted. |
| 75 virtual void OnPrinterDeleted(const std::string& printer_name) = 0; | 77 virtual void OnPrinterDeleted(const std::string& printer_name) = 0; |
| 76 | 78 |
| 77 protected: | 79 protected: |
| 78 virtual ~Delegate() {} | 80 virtual ~Delegate() {} |
| 79 }; | 81 }; |
| 80 | 82 |
| 81 struct PrinterInfoFromCloud { | 83 struct PrinterInfoFromCloud { |
| 82 std::string printer_id; | 84 std::string printer_id; |
| 83 std::string caps_hash; | 85 std::string caps_hash; |
| 84 std::string tags_hash; | 86 std::string tags_hash; |
| 85 }; | 87 }; |
| 86 | 88 |
| 87 // Begin public interface | 89 // Begin public interface |
| 88 PrinterJobHandler(const printing::PrinterBasicInfo& printer_info, | 90 PrinterJobHandler(const printing::PrinterBasicInfo& printer_info, |
| 89 const PrinterInfoFromCloud& printer_info_from_server, | 91 const PrinterInfoFromCloud& printer_info_from_server, |
| 90 const GURL& cloud_print_server_url, | 92 const GURL& cloud_print_server_url, |
| 91 cloud_print::PrintSystem* print_system, | 93 PrintSystem* print_system, |
| 92 Delegate* delegate); | 94 Delegate* delegate); |
| 93 | 95 |
| 94 bool Initialize(); | 96 bool Initialize(); |
| 95 | 97 |
| 96 std::string GetPrinterName() const; | 98 std::string GetPrinterName() const; |
| 97 | 99 |
| 98 // Requests a job check. |reason| is the reason for fetching the job. Used | 100 // Requests a job check. |reason| is the reason for fetching the job. Used |
| 99 // for logging and diagnostc purposes. | 101 // for logging and diagnostc purposes. |
| 100 void CheckForJobs(const std::string& reason); | 102 void CheckForJobs(const std::string& reason); |
| 101 | 103 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 125 base::DictionaryValue* json_data, | 127 base::DictionaryValue* json_data, |
| 126 bool succeeded) OVERRIDE; | 128 bool succeeded) OVERRIDE; |
| 127 virtual void OnRequestGiveUp() OVERRIDE; | 129 virtual void OnRequestGiveUp() OVERRIDE; |
| 128 virtual CloudPrintURLFetcher::ResponseAction OnRequestAuthError() OVERRIDE; | 130 virtual CloudPrintURLFetcher::ResponseAction OnRequestAuthError() OVERRIDE; |
| 129 virtual std::string GetAuthHeader() OVERRIDE; | 131 virtual std::string GetAuthHeader() OVERRIDE; |
| 130 | 132 |
| 131 // JobStatusUpdater::Delegate implementation | 133 // JobStatusUpdater::Delegate implementation |
| 132 virtual bool OnJobCompleted(JobStatusUpdater* updater) OVERRIDE; | 134 virtual bool OnJobCompleted(JobStatusUpdater* updater) OVERRIDE; |
| 133 virtual void OnAuthError() OVERRIDE; | 135 virtual void OnAuthError() OVERRIDE; |
| 134 | 136 |
| 135 // cloud_print::PrinterWatcherDelegate implementation | 137 // PrinterWatcherDelegate implementation |
| 136 virtual void OnPrinterDeleted() OVERRIDE; | 138 virtual void OnPrinterDeleted() OVERRIDE; |
| 137 virtual void OnPrinterChanged() OVERRIDE; | 139 virtual void OnPrinterChanged() OVERRIDE; |
| 138 virtual void OnJobChanged() OVERRIDE; | 140 virtual void OnJobChanged() OVERRIDE; |
| 139 | 141 |
| 140 // cloud_print::JobSpoolerDelegate implementation. | 142 // JobSpoolerDelegate implementation. |
| 141 // Called on print_thread_. | 143 // Called on print_thread_. |
| 142 virtual void OnJobSpoolSucceeded( | 144 virtual void OnJobSpoolSucceeded(const PlatformJobId& job_id) OVERRIDE; |
| 143 const cloud_print::PlatformJobId& job_id) OVERRIDE; | |
| 144 virtual void OnJobSpoolFailed() OVERRIDE; | 145 virtual void OnJobSpoolFailed() OVERRIDE; |
| 145 | 146 |
| 146 // End Delegate implementations | 147 // End Delegate implementations |
| 147 | 148 |
| 148 private: | 149 private: |
| 149 friend class base::RefCountedThreadSafe<PrinterJobHandler>; | 150 friend class base::RefCountedThreadSafe<PrinterJobHandler>; |
| 150 | 151 |
| 151 enum PrintJobError { | 152 enum PrintJobError { |
| 152 SUCCESS, | 153 SUCCESS, |
| 153 JOB_DOWNLOAD_FAILED, | 154 JOB_DOWNLOAD_FAILED, |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 // printer information, deleting the printer from the server or looking for | 223 // printer information, deleting the printer from the server or looking for |
| 223 // new print jobs | 224 // new print jobs |
| 224 void Start(); | 225 void Start(); |
| 225 | 226 |
| 226 // End the state machine. If there are pending tasks, we will post a Start | 227 // End the state machine. If there are pending tasks, we will post a Start |
| 227 // again. | 228 // again. |
| 228 void Stop(); | 229 void Stop(); |
| 229 | 230 |
| 230 void StartPrinting(); | 231 void StartPrinting(); |
| 231 void Reset(); | 232 void Reset(); |
| 232 void UpdateJobStatus(cloud_print::PrintJobStatus status, PrintJobError error); | 233 void UpdateJobStatus(PrintJobStatus status, PrintJobError error); |
| 233 | 234 |
| 234 // Sets the next response handler to the specifed JSON data handler. | 235 // Sets the next response handler to the specifed JSON data handler. |
| 235 void SetNextJSONHandler(JSONDataHandler handler); | 236 void SetNextJSONHandler(JSONDataHandler handler); |
| 236 // Sets the next response handler to the specifed data handler. | 237 // Sets the next response handler to the specifed data handler. |
| 237 void SetNextDataHandler(DataHandler handler); | 238 void SetNextDataHandler(DataHandler handler); |
| 238 | 239 |
| 239 void JobFailed(PrintJobError error); | 240 void JobFailed(PrintJobError error); |
| 240 void JobSpooled(cloud_print::PlatformJobId local_job_id); | 241 void JobSpooled(PlatformJobId local_job_id); |
| 241 // Returns false if printer info is up to date and no updating is needed. | 242 // Returns false if printer info is up to date and no updating is needed. |
| 242 bool UpdatePrinterInfo(); | 243 bool UpdatePrinterInfo(); |
| 243 bool HavePendingTasks(); | 244 bool HavePendingTasks(); |
| 244 void FailedFetchingJobData(); | 245 void FailedFetchingJobData(); |
| 245 | 246 |
| 246 // Callback that asynchronously receives printer caps and defaults. | 247 // Callback that asynchronously receives printer caps and defaults. |
| 247 void OnReceivePrinterCaps( | 248 void OnReceivePrinterCaps( |
| 248 bool succeeded, | 249 bool succeeded, |
| 249 const std::string& printer_name, | 250 const std::string& printer_name, |
| 250 const printing::PrinterCapsAndDefaults& caps_and_defaults); | 251 const printing::PrinterCapsAndDefaults& caps_and_defaults); |
| 251 | 252 |
| 252 // Called on print_thread_. | 253 // Called on print_thread_. |
| 253 void DoPrint(const JobDetails& job_details, | 254 void DoPrint(const JobDetails& job_details, |
| 254 const std::string& printer_name); | 255 const std::string& printer_name); |
| 255 | 256 |
| 256 scoped_refptr<CloudPrintURLFetcher> request_; | 257 scoped_refptr<CloudPrintURLFetcher> request_; |
| 257 scoped_refptr<cloud_print::PrintSystem> print_system_; | 258 scoped_refptr<PrintSystem> print_system_; |
| 258 printing::PrinterBasicInfo printer_info_; | 259 printing::PrinterBasicInfo printer_info_; |
| 259 PrinterInfoFromCloud printer_info_cloud_; | 260 PrinterInfoFromCloud printer_info_cloud_; |
| 260 GURL cloud_print_server_url_; | 261 GURL cloud_print_server_url_; |
| 261 std::string print_data_url_; | 262 std::string print_data_url_; |
| 262 JobDetails job_details_; | 263 JobDetails job_details_; |
| 263 Delegate* delegate_; | 264 Delegate* delegate_; |
| 264 // Once the job has been spooled to the local spooler, this specifies the | 265 // Once the job has been spooled to the local spooler, this specifies the |
| 265 // job id of the job on the local spooler. | 266 // job id of the job on the local spooler. |
| 266 cloud_print::PlatformJobId local_job_id_; | 267 PlatformJobId local_job_id_; |
| 267 | 268 |
| 268 // The next response handler can either be a JSONDataHandler or a | 269 // The next response handler can either be a JSONDataHandler or a |
| 269 // DataHandler (depending on the current request being made). | 270 // DataHandler (depending on the current request being made). |
| 270 JSONDataHandler next_json_data_handler_; | 271 JSONDataHandler next_json_data_handler_; |
| 271 DataHandler next_data_handler_; | 272 DataHandler next_data_handler_; |
| 272 // The number of consecutive times that connecting to the server failed. | 273 // The number of consecutive times that connecting to the server failed. |
| 273 int server_error_count_; | 274 int server_error_count_; |
| 274 // The thread on which the actual print operation happens | 275 // The thread on which the actual print operation happens |
| 275 base::Thread print_thread_; | 276 base::Thread print_thread_; |
| 276 // The Job spooler object. This is only non-NULL during a print operation. | 277 // The Job spooler object. This is only non-NULL during a print operation. |
| 277 // It lives and dies on |print_thread_| | 278 // It lives and dies on |print_thread_| |
| 278 scoped_refptr<cloud_print::PrintSystem::JobSpooler> job_spooler_; | 279 scoped_refptr<PrintSystem::JobSpooler> job_spooler_; |
| 279 // The message loop proxy representing the thread on which this object | 280 // The message loop proxy representing the thread on which this object |
| 280 // was created. Used by the print thread. | 281 // was created. Used by the print thread. |
| 281 scoped_refptr<base::MessageLoopProxy> job_handler_message_loop_proxy_; | 282 scoped_refptr<base::MessageLoopProxy> job_handler_message_loop_proxy_; |
| 282 | 283 |
| 283 // There may be pending tasks in the message queue when Shutdown is called. | 284 // There may be pending tasks in the message queue when Shutdown is called. |
| 284 // We set this flag so as to do nothing in those tasks. | 285 // We set this flag so as to do nothing in those tasks. |
| 285 bool shutting_down_; | 286 bool shutting_down_; |
| 286 | 287 |
| 287 // A string indicating the reason we are fetching jobs from the server | 288 // A string indicating the reason we are fetching jobs from the server |
| 288 // (used to specify the reason in the fetch URL). | 289 // (used to specify the reason in the fetch URL). |
| 289 std::string job_fetch_reason_; | 290 std::string job_fetch_reason_; |
| 290 // Flags that specify various pending server updates | 291 // Flags that specify various pending server updates |
| 291 bool job_check_pending_; | 292 bool job_check_pending_; |
| 292 bool printer_update_pending_; | 293 bool printer_update_pending_; |
| 293 | 294 |
| 294 // Some task in the state machine is in progress. | 295 // Some task in the state machine is in progress. |
| 295 bool task_in_progress_; | 296 bool task_in_progress_; |
| 296 scoped_refptr<cloud_print::PrintSystem::PrinterWatcher> printer_watcher_; | 297 scoped_refptr<PrintSystem::PrinterWatcher> printer_watcher_; |
| 297 typedef std::list< scoped_refptr<JobStatusUpdater> > JobStatusUpdaterList; | 298 typedef std::list< scoped_refptr<JobStatusUpdater> > JobStatusUpdaterList; |
| 298 JobStatusUpdaterList job_status_updater_list_; | 299 JobStatusUpdaterList job_status_updater_list_; |
| 299 | 300 |
| 300 base::TimeTicks last_job_fetch_time_; | 301 base::TimeTicks last_job_fetch_time_; |
| 301 base::WeakPtrFactory<PrinterJobHandler> weak_ptr_factory_; | 302 base::WeakPtrFactory<PrinterJobHandler> weak_ptr_factory_; |
| 302 | 303 |
| 303 DISALLOW_COPY_AND_ASSIGN(PrinterJobHandler); | 304 DISALLOW_COPY_AND_ASSIGN(PrinterJobHandler); |
| 304 }; | 305 }; |
| 305 | 306 |
| 306 // This typedef is to workaround the issue with certain versions of | 307 // This typedef is to workaround the issue with certain versions of |
| 307 // Visual Studio where it gets confused between multiple Delegate | 308 // Visual Studio where it gets confused between multiple Delegate |
| 308 // classes and gives a C2500 error. (I saw this error on the try bots - | 309 // classes and gives a C2500 error. (I saw this error on the try bots - |
| 309 // the workaround was not needed for my machine). | 310 // the workaround was not needed for my machine). |
| 310 typedef PrinterJobHandler::Delegate PrinterJobHandlerDelegate; | 311 typedef PrinterJobHandler::Delegate PrinterJobHandlerDelegate; |
| 311 | 312 |
| 313 } // namespace cloud_print |
| 314 |
| 312 #endif // CHROME_SERVICE_CLOUD_PRINT_PRINTER_JOB_HANDLER_H_ | 315 #endif // CHROME_SERVICE_CLOUD_PRINT_PRINTER_JOB_HANDLER_H_ |
| OLD | NEW |