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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <list> | 9 #include <list> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
13 #include "base/ref_counted.h" | 13 #include "base/ref_counted.h" |
14 #include "base/message_loop_proxy.h" | 14 #include "base/message_loop_proxy.h" |
15 #include "base/thread.h" | 15 #include "base/thread.h" |
| 16 #include "chrome/service/cloud_print/cloud_print_url_fetcher.h" |
16 #include "chrome/service/cloud_print/job_status_updater.h" | 17 #include "chrome/service/cloud_print/job_status_updater.h" |
17 #include "chrome/common/net/url_fetcher.h" | |
18 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
19 #include "net/url_request/url_request_status.h" | 19 #include "net/url_request/url_request_status.h" |
20 #include "printing/backend/print_backend.h" | 20 #include "printing/backend/print_backend.h" |
21 | 21 |
| 22 class URLFetcher; |
22 // A class that handles cloud print jobs for a particular printer. This class | 23 // A class that handles cloud print jobs for a particular printer. This class |
23 // imlements a state machine that transitions from Start to various states. The | 24 // imlements a state machine that transitions from Start to various states. The |
24 // various states are shown in the below diagram. | 25 // various states are shown in the below diagram. |
25 // the status on the server. | 26 // the status on the server. |
26 | 27 |
27 // Start --> No pending tasks --> Done | 28 // Start --> No pending tasks --> Done |
28 // | | 29 // | |
29 // | | 30 // | |
30 // | Have Pending tasks | 31 // | Have Pending tasks |
31 // | | 32 // | |
(...skipping 19 matching lines...) Expand all Loading... |
51 // | | 52 // | |
52 // | | 53 // | |
53 // | | 54 // | |
54 // | | 55 // | |
55 // | | 56 // | |
56 // | | 57 // | |
57 // | | 58 // | |
58 // Stop | 59 // Stop |
59 // (If there are pending tasks go back to Start) | 60 // (If there are pending tasks go back to Start) |
60 | 61 |
61 typedef URLFetcher::Delegate URLFetcherDelegate; | |
62 | |
63 class PrinterJobHandler : public base::RefCountedThreadSafe<PrinterJobHandler>, | 62 class PrinterJobHandler : public base::RefCountedThreadSafe<PrinterJobHandler>, |
64 public URLFetcherDelegate, | 63 public CloudPrintURLFetcherDelegate, |
65 public JobStatusUpdaterDelegate, | 64 public JobStatusUpdaterDelegate, |
66 public cloud_print::PrinterWatcherDelegate, | 65 public cloud_print::PrinterWatcherDelegate, |
67 public cloud_print::JobSpoolerDelegate { | 66 public cloud_print::JobSpoolerDelegate { |
68 enum PrintJobError { | 67 enum PrintJobError { |
69 SUCCESS, | 68 SUCCESS, |
70 JOB_DOWNLOAD_FAILED, | 69 JOB_DOWNLOAD_FAILED, |
71 INVALID_JOB_DATA, | 70 INVALID_JOB_DATA, |
72 PRINT_FAILED, | 71 PRINT_FAILED, |
73 }; | 72 }; |
74 struct JobDetails { | 73 struct JobDetails { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 ~PrinterJobHandler(); | 112 ~PrinterJobHandler(); |
114 bool Initialize(); | 113 bool Initialize(); |
115 // Notifies the JobHandler that a job is available | 114 // Notifies the JobHandler that a job is available |
116 void NotifyJobAvailable(); | 115 void NotifyJobAvailable(); |
117 // Shutdown everything (the process is exiting). | 116 // Shutdown everything (the process is exiting). |
118 void Shutdown(); | 117 void Shutdown(); |
119 // End public interface | 118 // End public interface |
120 | 119 |
121 // Begin Delegate implementations | 120 // Begin Delegate implementations |
122 | 121 |
123 // URLFetcher::Delegate implementation. | 122 // CloudPrintURLFetcher::Delegate implementation. |
124 virtual void OnURLFetchComplete(const URLFetcher* source, const GURL& url, | 123 virtual CloudPrintURLFetcher::ResponseAction HandleRawData( |
125 const URLRequestStatus& status, | 124 const URLFetcher* source, |
126 int response_code, | 125 const GURL& url, |
127 const ResponseCookies& cookies, | 126 const std::string& data); |
128 const std::string& data); | 127 virtual CloudPrintURLFetcher::ResponseAction HandleJSONData( |
| 128 const URLFetcher* source, |
| 129 const GURL& url, |
| 130 DictionaryValue* json_data, |
| 131 bool succeeded); |
| 132 virtual void OnRequestGiveUp(); |
| 133 virtual void OnRequestAuthError(); |
| 134 |
129 // JobStatusUpdater::Delegate implementation | 135 // JobStatusUpdater::Delegate implementation |
130 virtual bool OnJobCompleted(JobStatusUpdater* updater); | 136 virtual bool OnJobCompleted(JobStatusUpdater* updater); |
131 virtual void OnAuthError(); | 137 virtual void OnAuthError(); |
132 | 138 |
133 // cloud_print::PrinterWatcherDelegate implementation | 139 // cloud_print::PrinterWatcherDelegate implementation |
134 virtual void OnPrinterDeleted(); | 140 virtual void OnPrinterDeleted(); |
135 virtual void OnPrinterChanged(); | 141 virtual void OnPrinterChanged(); |
136 virtual void OnJobChanged(); | 142 virtual void OnJobChanged(); |
137 | 143 |
138 // cloud_print::JobSpoolerDelegate implementation. | 144 // cloud_print::JobSpoolerDelegate implementation. |
139 // Called on print_thread_. | 145 // Called on print_thread_. |
140 virtual void OnJobSpoolSucceeded(const cloud_print::PlatformJobId& job_id); | 146 virtual void OnJobSpoolSucceeded(const cloud_print::PlatformJobId& job_id); |
141 virtual void OnJobSpoolFailed(); | 147 virtual void OnJobSpoolFailed(); |
142 | 148 |
143 // End Delegate implementations | 149 // End Delegate implementations |
144 | 150 |
145 private: | 151 private: |
146 // Prototype for a response handler. The return value indicates whether the | 152 // Prototype for a JSON data handler. |
147 // request should be retried, false means "retry", true means "do not retry" | 153 typedef CloudPrintURLFetcher::ResponseAction |
148 typedef bool (PrinterJobHandler::*ResponseHandler)( | 154 (PrinterJobHandler::*JSONDataHandler)(const URLFetcher* source, |
149 const URLFetcher* source, const GURL& url, | 155 const GURL& url, |
150 const URLRequestStatus& status, int response_code, | 156 DictionaryValue* json_data, |
151 const ResponseCookies& cookies, const std::string& data); | 157 bool succeeded); |
152 // Prototype for a failure handler. This handler will be executed if all | 158 // Prototype for a data handler. |
153 // attempts to fetch url failed. | 159 typedef CloudPrintURLFetcher::ResponseAction |
154 typedef void (PrinterJobHandler::*FailureHandler)(); | 160 (PrinterJobHandler::*DataHandler)(const URLFetcher* source, |
| 161 const GURL& url, |
| 162 const std::string& data); |
155 // Begin request handlers for each state in the state machine | 163 // Begin request handlers for each state in the state machine |
156 bool HandlePrinterUpdateResponse(const URLFetcher* source, const GURL& url, | 164 CloudPrintURLFetcher::ResponseAction HandlePrinterUpdateResponse( |
157 const URLRequestStatus& status, | 165 const URLFetcher* source, |
158 int response_code, | 166 const GURL& url, |
159 const ResponseCookies& cookies, | 167 DictionaryValue* json_data, |
160 const std::string& data); | 168 bool succeeded); |
161 bool HandlePrinterDeleteResponse(const URLFetcher* source, const GURL& url, | 169 |
162 const URLRequestStatus& status, | 170 CloudPrintURLFetcher::ResponseAction HandlePrinterDeleteResponse( |
163 int response_code, | 171 const URLFetcher* source, |
164 const ResponseCookies& cookies, | 172 const GURL& url, |
165 const std::string& data); | 173 DictionaryValue* json_data, |
166 bool HandleJobMetadataResponse(const URLFetcher* source, const GURL& url, | 174 bool succeeded); |
167 const URLRequestStatus& status, | 175 |
168 int response_code, | 176 CloudPrintURLFetcher::ResponseAction HandleJobMetadataResponse( |
169 const ResponseCookies& cookies, | 177 const URLFetcher* source, |
170 const std::string& data); | 178 const GURL& url, |
171 bool HandlePrintTicketResponse(const URLFetcher* source, | 179 DictionaryValue* json_data, |
172 const GURL& url, | 180 bool succeeded); |
173 const URLRequestStatus& status, | 181 |
174 int response_code, | 182 CloudPrintURLFetcher::ResponseAction HandlePrintTicketResponse( |
175 const ResponseCookies& cookies, | 183 const URLFetcher* source, |
176 const std::string& data); | 184 const GURL& url, |
177 bool HandlePrintDataResponse(const URLFetcher* source, | 185 const std::string& data); |
178 const GURL& url, | 186 |
179 const URLRequestStatus& status, | 187 CloudPrintURLFetcher::ResponseAction HandlePrintDataResponse( |
180 int response_code, | 188 const URLFetcher* source, |
181 const ResponseCookies& cookies, | 189 const GURL& url, |
182 const std::string& data); | 190 const std::string& data); |
183 bool HandleSuccessStatusUpdateResponse(const URLFetcher* source, | 191 |
184 const GURL& url, | 192 CloudPrintURLFetcher::ResponseAction HandleSuccessStatusUpdateResponse( |
185 const URLRequestStatus& status, | 193 const URLFetcher* source, |
186 int response_code, | 194 const GURL& url, |
187 const ResponseCookies& cookies, | 195 DictionaryValue* json_data, |
188 const std::string& data); | 196 bool succeeded); |
189 bool HandleFailureStatusUpdateResponse(const URLFetcher* source, | 197 |
190 const GURL& url, | 198 CloudPrintURLFetcher::ResponseAction HandleFailureStatusUpdateResponse( |
191 const URLRequestStatus& status, | 199 const URLFetcher* source, |
192 int response_code, | 200 const GURL& url, |
193 const ResponseCookies& cookies, | 201 DictionaryValue* json_data, |
194 const std::string& data); | 202 bool succeeded); |
195 // End request handlers for each state in the state machine | 203 // End request handlers for each state in the state machine |
196 | 204 |
197 // Start the state machine. Based on the flags set this could mean updating | 205 // Start the state machine. Based on the flags set this could mean updating |
198 // printer information, deleting the printer from the server or looking for | 206 // printer information, deleting the printer from the server or looking for |
199 // new print jobs | 207 // new print jobs |
200 void Start(); | 208 void Start(); |
201 | 209 |
202 // End the state machine. If there are pending tasks, we will post a Start | 210 // End the state machine. If there are pending tasks, we will post a Start |
203 // again. | 211 // again. |
204 void Stop(); | 212 void Stop(); |
205 | 213 |
206 void StartPrinting(); | 214 void StartPrinting(); |
207 void HandleServerError(const GURL& url); | 215 void HandleServerError(const GURL& url); |
208 void Reset(); | 216 void Reset(); |
209 void UpdateJobStatus(cloud_print::PrintJobStatus status, PrintJobError error); | 217 void UpdateJobStatus(cloud_print::PrintJobStatus status, PrintJobError error); |
210 | 218 |
211 // This function should be used to go from one state to another. It will | 219 // Sets the next response handler to the specifed JSON data handler. |
212 // retry to fetch url (up to a limit) if response handler will return false. | 220 void SetNextJSONHandler(JSONDataHandler handler); |
213 // Calling this function will zero failure counter. | 221 // Sets the next response handler to the specifed data handler. |
214 void MakeServerRequest(const GURL& url, | 222 void SetNextDataHandler(DataHandler handler); |
215 ResponseHandler response_handler, | |
216 FailureHandler failure_handler); | |
217 // This function should be used ONLY from MakeServerRequest and | |
218 // HandleServerError. It is using stored handlers and failure counter | |
219 // to decide which handler to call. | |
220 void FetchURL(const GURL& url); | |
221 | 223 |
222 void JobFailed(PrintJobError error); | 224 void JobFailed(PrintJobError error); |
223 void JobSpooled(cloud_print::PlatformJobId local_job_id); | 225 void JobSpooled(cloud_print::PlatformJobId local_job_id); |
224 // Returns false if printer info is up to date and no updating is needed. | 226 // Returns false if printer info is up to date and no updating is needed. |
225 bool UpdatePrinterInfo(); | 227 bool UpdatePrinterInfo(); |
226 bool HavePendingTasks(); | 228 bool HavePendingTasks(); |
227 void FailedFetchingJobData(); | 229 void FailedFetchingJobData(); |
228 | 230 |
229 // Called on print_thread_. | 231 // Called on print_thread_. |
230 void DoPrint(const JobDetails& job_details, | 232 void DoPrint(const JobDetails& job_details, |
231 const std::string& printer_name); | 233 const std::string& printer_name); |
232 | 234 |
233 scoped_ptr<URLFetcher> request_; | 235 scoped_refptr<CloudPrintURLFetcher> request_; |
234 scoped_refptr<cloud_print::PrintSystem> print_system_; | 236 scoped_refptr<cloud_print::PrintSystem> print_system_; |
235 printing::PrinterBasicInfo printer_info_; | 237 printing::PrinterBasicInfo printer_info_; |
236 PrinterInfoFromCloud printer_info_cloud_; | 238 PrinterInfoFromCloud printer_info_cloud_; |
237 std::string auth_token_; | 239 std::string auth_token_; |
238 GURL cloud_print_server_url_; | 240 GURL cloud_print_server_url_; |
239 std::string print_data_url_; | 241 std::string print_data_url_; |
240 JobDetails job_details_; | 242 JobDetails job_details_; |
241 Delegate* delegate_; | 243 Delegate* delegate_; |
242 // Once the job has been spooled to the local spooler, this specifies the | 244 // Once the job has been spooled to the local spooler, this specifies the |
243 // job id of the job on the local spooler. | 245 // job id of the job on the local spooler. |
244 cloud_print::PlatformJobId local_job_id_; | 246 cloud_print::PlatformJobId local_job_id_; |
245 ResponseHandler next_response_handler_; | 247 |
246 FailureHandler next_failure_handler_; | 248 // The next response handler can either be a JSONDataHandler or a |
| 249 // DataHandler (depending on the current request being made). |
| 250 JSONDataHandler next_json_data_handler_; |
| 251 DataHandler next_data_handler_; |
247 // The number of consecutive times that connecting to the server failed. | 252 // The number of consecutive times that connecting to the server failed. |
248 int server_error_count_; | 253 int server_error_count_; |
249 // The thread on which the actual print operation happens | 254 // The thread on which the actual print operation happens |
250 base::Thread print_thread_; | 255 base::Thread print_thread_; |
251 // The Job spooler object. This is only non-NULL during a print operation. | 256 // The Job spooler object. This is only non-NULL during a print operation. |
252 // It lives and dies on |print_thread_| | 257 // It lives and dies on |print_thread_| |
253 scoped_refptr<cloud_print::PrintSystem::JobSpooler> job_spooler_; | 258 scoped_refptr<cloud_print::PrintSystem::JobSpooler> job_spooler_; |
254 // The message loop proxy representing the thread on which this object | 259 // The message loop proxy representing the thread on which this object |
255 // was created. Used by the print thread. | 260 // was created. Used by the print thread. |
256 scoped_refptr<base::MessageLoopProxy> job_handler_message_loop_proxy_; | 261 scoped_refptr<base::MessageLoopProxy> job_handler_message_loop_proxy_; |
(...skipping 16 matching lines...) Expand all Loading... |
273 DISALLOW_COPY_AND_ASSIGN(PrinterJobHandler); | 278 DISALLOW_COPY_AND_ASSIGN(PrinterJobHandler); |
274 }; | 279 }; |
275 | 280 |
276 // This typedef is to workaround the issue with certain versions of | 281 // This typedef is to workaround the issue with certain versions of |
277 // Visual Studio where it gets confused between multiple Delegate | 282 // Visual Studio where it gets confused between multiple Delegate |
278 // classes and gives a C2500 error. (I saw this error on the try bots - | 283 // classes and gives a C2500 error. (I saw this error on the try bots - |
279 // the workaround was not needed for my machine). | 284 // the workaround was not needed for my machine). |
280 typedef PrinterJobHandler::Delegate PrinterJobHandlerDelegate; | 285 typedef PrinterJobHandler::Delegate PrinterJobHandlerDelegate; |
281 | 286 |
282 #endif // CHROME_SERVICE_CLOUD_PRINT_PRINTER_JOB_HANDLER_H_ | 287 #endif // CHROME_SERVICE_CLOUD_PRINT_PRINTER_JOB_HANDLER_H_ |
OLD | NEW |