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/thread.h" | 13 #include "base/thread.h" |
14 #include "chrome/service/cloud_print/job_status_updater.h" | 14 #include "chrome/service/cloud_print/job_status_updater.h" |
15 #include "chrome/service/cloud_print/printer_info.h" | 15 #include "chrome/service/cloud_print/print_system.h" |
16 #include "chrome/common/net/url_fetcher.h" | 16 #include "chrome/common/net/url_fetcher.h" |
17 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
18 #include "net/url_request/url_request_status.h" | 18 #include "net/url_request/url_request_status.h" |
19 | 19 |
20 // A class that handles cloud print jobs for a particular printer. This class | 20 // 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 | 21 // imlements a state machine that transitions from Start to various states. The |
22 // various states are shown in the below diagram. | 22 // various states are shown in the below diagram. |
23 // the status on the server. | 23 // the status on the server. |
24 | 24 |
25 // Start --> No pending tasks --> Done | 25 // Start --> No pending tasks --> Done |
(...skipping 28 matching lines...) Expand all Loading... |
54 // | | 54 // | |
55 // | | 55 // | |
56 // Stop | 56 // Stop |
57 // (If there are pending tasks go back to Start) | 57 // (If there are pending tasks go back to Start) |
58 | 58 |
59 typedef URLFetcher::Delegate URLFetcherDelegate; | 59 typedef URLFetcher::Delegate URLFetcherDelegate; |
60 | 60 |
61 class PrinterJobHandler : public base::RefCountedThreadSafe<PrinterJobHandler>, | 61 class PrinterJobHandler : public base::RefCountedThreadSafe<PrinterJobHandler>, |
62 public URLFetcherDelegate, | 62 public URLFetcherDelegate, |
63 public JobStatusUpdaterDelegate, | 63 public JobStatusUpdaterDelegate, |
64 public cloud_print::PrinterChangeNotifierDelegate { | 64 public cloud_print::PrinterWatcherDelegate { |
65 enum PrintJobError { | 65 enum PrintJobError { |
66 SUCCESS, | 66 SUCCESS, |
67 JOB_DOWNLOAD_FAILED, | 67 JOB_DOWNLOAD_FAILED, |
68 INVALID_JOB_DATA, | 68 INVALID_JOB_DATA, |
69 PRINT_FAILED, | 69 PRINT_FAILED, |
70 }; | 70 }; |
71 struct JobDetails { | 71 struct JobDetails { |
72 std::string job_id_; | 72 std::string job_id_; |
73 std::string job_title_; | 73 std::string job_title_; |
74 std::string print_ticket_; | 74 std::string print_ticket_; |
(...skipping 10 matching lines...) Expand all Loading... |
85 | 85 |
86 public: | 86 public: |
87 class Delegate { | 87 class Delegate { |
88 public: | 88 public: |
89 virtual void OnPrinterJobHandlerShutdown( | 89 virtual void OnPrinterJobHandlerShutdown( |
90 PrinterJobHandler* job_handler, const std::string& printer_id) = 0; | 90 PrinterJobHandler* job_handler, const std::string& printer_id) = 0; |
91 }; | 91 }; |
92 | 92 |
93 // Begin public interface | 93 // Begin public interface |
94 PrinterJobHandler(const cloud_print::PrinterBasicInfo& printer_info, | 94 PrinterJobHandler(const cloud_print::PrinterBasicInfo& printer_info, |
95 const std::string& printer_id, | 95 const std::string& printer_id, |
96 const std::string& caps_hash, | 96 const std::string& caps_hash, |
97 const std::string& auth_token, | 97 const std::string& auth_token, |
98 const GURL& cloud_print_server_url, | 98 const GURL& cloud_print_server_url, |
99 Delegate* delegate); | 99 cloud_print::PrintSystem* print_system, |
| 100 Delegate* delegate); |
100 ~PrinterJobHandler(); | 101 ~PrinterJobHandler(); |
101 bool Initialize(); | 102 bool Initialize(); |
102 // Notifies the JobHandler that a job is available | 103 // Notifies the JobHandler that a job is available |
103 void NotifyJobAvailable(); | 104 void NotifyJobAvailable(); |
104 // Shutdown everything (the process is exiting). | 105 // Shutdown everything (the process is exiting). |
105 void Shutdown(); | 106 void Shutdown(); |
106 // End public interface | 107 // End public interface |
107 | 108 |
108 // Begin Delegate implementations | 109 // Begin Delegate implementations |
109 | 110 |
110 // URLFetcher::Delegate implementation. | 111 // URLFetcher::Delegate implementation. |
111 virtual void OnURLFetchComplete(const URLFetcher* source, const GURL& url, | 112 virtual void OnURLFetchComplete(const URLFetcher* source, const GURL& url, |
112 const URLRequestStatus& status, | 113 const URLRequestStatus& status, |
113 int response_code, | 114 int response_code, |
114 const ResponseCookies& cookies, | 115 const ResponseCookies& cookies, |
115 const std::string& data); | 116 const std::string& data); |
116 // JobStatusUpdater::Delegate implementation | 117 // JobStatusUpdater::Delegate implementation |
117 virtual bool OnJobCompleted(JobStatusUpdater* updater); | 118 virtual bool OnJobCompleted(JobStatusUpdater* updater); |
118 // cloud_print::PrinterChangeNotifier::Delegate implementation | 119 // cloud_print::PrinterWatcherDelegate implementation |
119 virtual void OnPrinterAdded(); | |
120 virtual void OnPrinterDeleted(); | 120 virtual void OnPrinterDeleted(); |
121 virtual void OnPrinterChanged(); | 121 virtual void OnPrinterChanged(); |
122 virtual void OnJobChanged(); | 122 virtual void OnJobChanged(); |
123 | 123 |
124 // End Delegate implementations | 124 // End Delegate implementations |
125 | 125 |
126 private: | 126 private: |
127 // Prototype for a response handler. The return value indicates whether the | 127 // Prototype for a response handler. The return value indicates whether the |
128 // request should be retried, false means "retry", true means "do not retry" | 128 // request should be retried, false means "retry", true means "do not retry" |
129 typedef bool (PrinterJobHandler::*ResponseHandler)( | 129 typedef bool (PrinterJobHandler::*ResponseHandler)( |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 void UpdateJobStatus(cloud_print::PrintJobStatus status, PrintJobError error); | 187 void UpdateJobStatus(cloud_print::PrintJobStatus status, PrintJobError error); |
188 void MakeServerRequest(const GURL& url, ResponseHandler response_handler); | 188 void MakeServerRequest(const GURL& url, ResponseHandler response_handler); |
189 void JobFailed(PrintJobError error); | 189 void JobFailed(PrintJobError error); |
190 void JobSpooled(cloud_print::PlatformJobId local_job_id); | 190 void JobSpooled(cloud_print::PlatformJobId local_job_id); |
191 // Returns false if printer info is up to date and no updating is needed. | 191 // Returns false if printer info is up to date and no updating is needed. |
192 bool UpdatePrinterInfo(); | 192 bool UpdatePrinterInfo(); |
193 bool HavePendingTasks(); | 193 bool HavePendingTasks(); |
194 | 194 |
195 static void DoPrint(const JobDetails& job_details, | 195 static void DoPrint(const JobDetails& job_details, |
196 const std::string& printer_name, | 196 const std::string& printer_name, |
| 197 scoped_refptr<cloud_print::PrintSystem> print_system, |
197 PrinterJobHandler* job_handler, | 198 PrinterJobHandler* job_handler, |
198 MessageLoop* job_message_loop); | 199 MessageLoop* job_message_loop); |
199 | 200 |
200 | |
201 scoped_ptr<URLFetcher> request_; | 201 scoped_ptr<URLFetcher> request_; |
| 202 scoped_refptr<cloud_print::PrintSystem> print_system_; |
202 cloud_print::PrinterBasicInfo printer_info_; | 203 cloud_print::PrinterBasicInfo printer_info_; |
203 std::string printer_id_; | 204 std::string printer_id_; |
204 std::string auth_token_; | 205 std::string auth_token_; |
205 std::string last_caps_hash_; | 206 std::string last_caps_hash_; |
206 GURL cloud_print_server_url_; | 207 GURL cloud_print_server_url_; |
207 std::string print_data_url_; | 208 std::string print_data_url_; |
208 JobDetails job_details_; | 209 JobDetails job_details_; |
209 Delegate* delegate_; | 210 Delegate* delegate_; |
210 // Once the job has been spooled to the local spooler, this specifies the | 211 // Once the job has been spooled to the local spooler, this specifies the |
211 // job id of the job on the local spooler. | 212 // job id of the job on the local spooler. |
212 cloud_print::PlatformJobId local_job_id_; | 213 cloud_print::PlatformJobId local_job_id_; |
213 ResponseHandler next_response_handler_; | 214 ResponseHandler next_response_handler_; |
214 // The number of consecutive times that connecting to the server failed. | 215 // The number of consecutive times that connecting to the server failed. |
215 int server_error_count_; | 216 int server_error_count_; |
216 // The thread on which the actual print operation happens | 217 // The thread on which the actual print operation happens |
217 base::Thread print_thread_; | 218 base::Thread print_thread_; |
218 // There may be pending tasks in the message queue when Shutdown is called. | 219 // There may be pending tasks in the message queue when Shutdown is called. |
219 // We set this flag so as to do nothing in those tasks. | 220 // We set this flag so as to do nothing in those tasks. |
220 bool shutting_down_; | 221 bool shutting_down_; |
221 | 222 |
222 // Flags that specify various pending server updates | 223 // Flags that specify various pending server updates |
223 bool server_job_available_; | 224 bool server_job_available_; |
224 bool printer_update_pending_; | 225 bool printer_update_pending_; |
225 bool printer_delete_pending_; | 226 bool printer_delete_pending_; |
226 | 227 |
227 // Some task in the state machine is in progress. | 228 // Some task in the state machine is in progress. |
228 bool task_in_progress_; | 229 bool task_in_progress_; |
229 cloud_print::PrinterChangeNotifier printer_change_notifier_; | 230 scoped_refptr<cloud_print::PrintSystem::PrinterWatcher> printer_watcher_; |
230 typedef std::list< scoped_refptr<JobStatusUpdater> > JobStatusUpdaterList; | 231 typedef std::list< scoped_refptr<JobStatusUpdater> > JobStatusUpdaterList; |
231 JobStatusUpdaterList job_status_updater_list_; | 232 JobStatusUpdaterList job_status_updater_list_; |
232 | 233 |
233 DISALLOW_COPY_AND_ASSIGN(PrinterJobHandler); | 234 DISALLOW_COPY_AND_ASSIGN(PrinterJobHandler); |
234 }; | 235 }; |
235 | 236 |
236 // This typedef is to workaround the issue with certain versions of | 237 // This typedef is to workaround the issue with certain versions of |
237 // Visual Studio where it gets confused between multiple Delegate | 238 // Visual Studio where it gets confused between multiple Delegate |
238 // classes and gives a C2500 error. (I saw this error on the try bots - | 239 // classes and gives a C2500 error. (I saw this error on the try bots - |
239 // the workaround was not needed for my machine). | 240 // the workaround was not needed for my machine). |
240 typedef PrinterJobHandler::Delegate PrinterJobHandlerDelegate; | 241 typedef PrinterJobHandler::Delegate PrinterJobHandlerDelegate; |
241 | 242 |
242 #endif // CHROME_SERVICE_CLOUD_PRINT_PRINTER_JOB_HANDLER_H_ | 243 #endif // CHROME_SERVICE_CLOUD_PRINT_PRINTER_JOB_HANDLER_H_ |
243 | 244 |
OLD | NEW |