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 #include "chrome/service/cloud_print/printer_job_handler.h" | 5 #include "chrome/service/cloud_print/printer_job_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 shutting_down_ = true; | 93 shutting_down_ = true; |
94 while (!job_status_updater_list_.empty()) { | 94 while (!job_status_updater_list_.empty()) { |
95 // Calling Stop() will cause the OnJobCompleted to be called which will | 95 // Calling Stop() will cause the OnJobCompleted to be called which will |
96 // remove the updater object from the list. | 96 // remove the updater object from the list. |
97 job_status_updater_list_.front()->Stop(); | 97 job_status_updater_list_.front()->Stop(); |
98 } | 98 } |
99 } | 99 } |
100 | 100 |
101 // CloudPrintURLFetcher::Delegate implementation. | 101 // CloudPrintURLFetcher::Delegate implementation. |
102 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleRawResponse( | 102 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleRawResponse( |
103 const content::URLFetcher* source, | 103 const net::URLFetcher* source, |
104 const GURL& url, | 104 const GURL& url, |
105 const net::URLRequestStatus& status, | 105 const net::URLRequestStatus& status, |
106 int response_code, | 106 int response_code, |
107 const net::ResponseCookies& cookies, | 107 const net::ResponseCookies& cookies, |
108 const std::string& data) { | 108 const std::string& data) { |
109 // 415 (Unsupported media type) error while fetching data from the server | 109 // 415 (Unsupported media type) error while fetching data from the server |
110 // means data conversion error. Stop fetching process and mark job as error. | 110 // means data conversion error. Stop fetching process and mark job as error. |
111 if (next_data_handler_ == (&PrinterJobHandler::HandlePrintDataResponse) && | 111 if (next_data_handler_ == (&PrinterJobHandler::HandlePrintDataResponse) && |
112 response_code == net::HTTP_UNSUPPORTED_MEDIA_TYPE) { | 112 response_code == net::HTTP_UNSUPPORTED_MEDIA_TYPE) { |
113 MessageLoop::current()->PostTask( | 113 MessageLoop::current()->PostTask( |
114 FROM_HERE, | 114 FROM_HERE, |
115 base::Bind(&PrinterJobHandler::JobFailed, this, JOB_DOWNLOAD_FAILED)); | 115 base::Bind(&PrinterJobHandler::JobFailed, this, JOB_DOWNLOAD_FAILED)); |
116 return CloudPrintURLFetcher::STOP_PROCESSING; | 116 return CloudPrintURLFetcher::STOP_PROCESSING; |
117 } | 117 } |
118 return CloudPrintURLFetcher::CONTINUE_PROCESSING; | 118 return CloudPrintURLFetcher::CONTINUE_PROCESSING; |
119 } | 119 } |
120 | 120 |
121 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleRawData( | 121 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleRawData( |
122 const content::URLFetcher* source, | 122 const net::URLFetcher* source, |
123 const GURL& url, | 123 const GURL& url, |
124 const std::string& data) { | 124 const std::string& data) { |
125 if (!next_data_handler_) | 125 if (!next_data_handler_) |
126 return CloudPrintURLFetcher::CONTINUE_PROCESSING; | 126 return CloudPrintURLFetcher::CONTINUE_PROCESSING; |
127 return (this->*next_data_handler_)(source, url, data); | 127 return (this->*next_data_handler_)(source, url, data); |
128 } | 128 } |
129 | 129 |
130 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleJSONData( | 130 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleJSONData( |
131 const content::URLFetcher* source, | 131 const net::URLFetcher* source, |
132 const GURL& url, | 132 const GURL& url, |
133 DictionaryValue* json_data, | 133 DictionaryValue* json_data, |
134 bool succeeded) { | 134 bool succeeded) { |
135 DCHECK(next_json_data_handler_); | 135 DCHECK(next_json_data_handler_); |
136 return (this->*next_json_data_handler_)(source, url, json_data, succeeded); | 136 return (this->*next_json_data_handler_)(source, url, json_data, succeeded); |
137 } | 137 } |
138 | 138 |
139 void PrinterJobHandler::OnRequestGiveUp() { | 139 void PrinterJobHandler::OnRequestGiveUp() { |
140 // The only time we call CloudPrintURLFetcher::StartGetRequest() with a | 140 // The only time we call CloudPrintURLFetcher::StartGetRequest() with a |
141 // specified number of retries, is when we are trying to fetch print job | 141 // specified number of retries, is when we are trying to fetch print job |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 } | 217 } |
218 | 218 |
219 PrinterJobHandler::~PrinterJobHandler() { | 219 PrinterJobHandler::~PrinterJobHandler() { |
220 if (printer_watcher_) | 220 if (printer_watcher_) |
221 printer_watcher_->StopWatching(); | 221 printer_watcher_->StopWatching(); |
222 } | 222 } |
223 | 223 |
224 // Begin Response handlers | 224 // Begin Response handlers |
225 CloudPrintURLFetcher::ResponseAction | 225 CloudPrintURLFetcher::ResponseAction |
226 PrinterJobHandler::HandlePrinterUpdateResponse( | 226 PrinterJobHandler::HandlePrinterUpdateResponse( |
227 const content::URLFetcher* source, | 227 const net::URLFetcher* source, |
228 const GURL& url, | 228 const GURL& url, |
229 DictionaryValue* json_data, | 229 DictionaryValue* json_data, |
230 bool succeeded) { | 230 bool succeeded) { |
231 VLOG(1) << "CP_CONNECTOR: Handle printer update response, id: " | 231 VLOG(1) << "CP_CONNECTOR: Handle printer update response, id: " |
232 << printer_info_cloud_.printer_id; | 232 << printer_info_cloud_.printer_id; |
233 // We are done here. Go to the Stop state | 233 // We are done here. Go to the Stop state |
234 MessageLoop::current()->PostTask( | 234 MessageLoop::current()->PostTask( |
235 FROM_HERE, base::Bind(&PrinterJobHandler::Stop, this)); | 235 FROM_HERE, base::Bind(&PrinterJobHandler::Stop, this)); |
236 return CloudPrintURLFetcher::STOP_PROCESSING; | 236 return CloudPrintURLFetcher::STOP_PROCESSING; |
237 } | 237 } |
238 | 238 |
239 CloudPrintURLFetcher::ResponseAction | 239 CloudPrintURLFetcher::ResponseAction |
240 PrinterJobHandler::HandleJobMetadataResponse( | 240 PrinterJobHandler::HandleJobMetadataResponse( |
241 const content::URLFetcher* source, | 241 const net::URLFetcher* source, |
242 const GURL& url, | 242 const GURL& url, |
243 DictionaryValue* json_data, | 243 DictionaryValue* json_data, |
244 bool succeeded) { | 244 bool succeeded) { |
245 VLOG(1) << "CP_CONNECTOR: Handle job metadata response, id: " | 245 VLOG(1) << "CP_CONNECTOR: Handle job metadata response, id: " |
246 << printer_info_cloud_.printer_id; | 246 << printer_info_cloud_.printer_id; |
247 bool job_available = false; | 247 bool job_available = false; |
248 if (succeeded) { | 248 if (succeeded) { |
249 ListValue* job_list = NULL; | 249 ListValue* job_list = NULL; |
250 if (json_data->GetList(kJobListValue, &job_list) && job_list) { | 250 if (json_data->GetList(kJobListValue, &job_list) && job_list) { |
251 // Even though it is a job list, for now we are only interested in the | 251 // Even though it is a job list, for now we are only interested in the |
(...skipping 27 matching lines...) Expand all Loading... |
279 } | 279 } |
280 } | 280 } |
281 // If no jobs are available, go to the Stop state. | 281 // If no jobs are available, go to the Stop state. |
282 if (!job_available) | 282 if (!job_available) |
283 MessageLoop::current()->PostTask( | 283 MessageLoop::current()->PostTask( |
284 FROM_HERE, base::Bind(&PrinterJobHandler::Stop, this)); | 284 FROM_HERE, base::Bind(&PrinterJobHandler::Stop, this)); |
285 return CloudPrintURLFetcher::STOP_PROCESSING; | 285 return CloudPrintURLFetcher::STOP_PROCESSING; |
286 } | 286 } |
287 | 287 |
288 CloudPrintURLFetcher::ResponseAction | 288 CloudPrintURLFetcher::ResponseAction |
289 PrinterJobHandler::HandlePrintTicketResponse(const content::URLFetcher* source, | 289 PrinterJobHandler::HandlePrintTicketResponse(const net::URLFetcher* source, |
290 const GURL& url, | 290 const GURL& url, |
291 const std::string& data) { | 291 const std::string& data) { |
292 VLOG(1) << "CP_CONNECTOR: Handle print ticket response, id: " | 292 VLOG(1) << "CP_CONNECTOR: Handle print ticket response, id: " |
293 << printer_info_cloud_.printer_id; | 293 << printer_info_cloud_.printer_id; |
294 if (print_system_->ValidatePrintTicket(printer_info_.printer_name, data)) { | 294 if (print_system_->ValidatePrintTicket(printer_info_.printer_name, data)) { |
295 job_details_.print_ticket_ = data; | 295 job_details_.print_ticket_ = data; |
296 SetNextDataHandler(&PrinterJobHandler::HandlePrintDataResponse); | 296 SetNextDataHandler(&PrinterJobHandler::HandlePrintDataResponse); |
297 request_ = new CloudPrintURLFetcher; | 297 request_ = new CloudPrintURLFetcher; |
298 std::string accept_headers = "Accept: "; | 298 std::string accept_headers = "Accept: "; |
299 accept_headers += print_system_->GetSupportedMimeTypes(); | 299 accept_headers += print_system_->GetSupportedMimeTypes(); |
300 request_->StartGetRequest(GURL(print_data_url_.c_str()), | 300 request_->StartGetRequest(GURL(print_data_url_.c_str()), |
301 this, | 301 this, |
302 kJobDataMaxRetryCount, | 302 kJobDataMaxRetryCount, |
303 accept_headers); | 303 accept_headers); |
304 } else { | 304 } else { |
305 // The print ticket was not valid. We are done here. | 305 // The print ticket was not valid. We are done here. |
306 FailedFetchingJobData(); | 306 FailedFetchingJobData(); |
307 } | 307 } |
308 return CloudPrintURLFetcher::STOP_PROCESSING; | 308 return CloudPrintURLFetcher::STOP_PROCESSING; |
309 } | 309 } |
310 | 310 |
311 CloudPrintURLFetcher::ResponseAction | 311 CloudPrintURLFetcher::ResponseAction |
312 PrinterJobHandler::HandlePrintDataResponse(const content::URLFetcher* source, | 312 PrinterJobHandler::HandlePrintDataResponse(const net::URLFetcher* source, |
313 const GURL& url, | 313 const GURL& url, |
314 const std::string& data) { | 314 const std::string& data) { |
315 VLOG(1) << "CP_CONNECTOR: Handle print data response, id: " | 315 VLOG(1) << "CP_CONNECTOR: Handle print data response, id: " |
316 << printer_info_cloud_.printer_id; | 316 << printer_info_cloud_.printer_id; |
317 base::Closure next_task; | 317 base::Closure next_task; |
318 if (file_util::CreateTemporaryFile(&job_details_.print_data_file_path_)) { | 318 if (file_util::CreateTemporaryFile(&job_details_.print_data_file_path_)) { |
319 int ret = file_util::WriteFile(job_details_.print_data_file_path_, | 319 int ret = file_util::WriteFile(job_details_.print_data_file_path_, |
320 data.c_str(), | 320 data.c_str(), |
321 data.length()); | 321 data.length()); |
322 source->GetResponseHeaders()->GetMimeType( | 322 source->GetResponseHeaders()->GetMimeType( |
323 &job_details_.print_data_mime_type_); | 323 &job_details_.print_data_mime_type_); |
324 DCHECK(ret == static_cast<int>(data.length())); | 324 DCHECK(ret == static_cast<int>(data.length())); |
325 if (ret == static_cast<int>(data.length())) { | 325 if (ret == static_cast<int>(data.length())) { |
326 next_task = base::Bind(&PrinterJobHandler::StartPrinting, this); | 326 next_task = base::Bind(&PrinterJobHandler::StartPrinting, this); |
327 } | 327 } |
328 } | 328 } |
329 // If there was no task allocated above, then there was an error in | 329 // If there was no task allocated above, then there was an error in |
330 // saving the print data, bail out here. | 330 // saving the print data, bail out here. |
331 if (next_task.is_null()) { | 331 if (next_task.is_null()) { |
332 next_task = base::Bind(&PrinterJobHandler::JobFailed, this, | 332 next_task = base::Bind(&PrinterJobHandler::JobFailed, this, |
333 JOB_DOWNLOAD_FAILED); | 333 JOB_DOWNLOAD_FAILED); |
334 } | 334 } |
335 MessageLoop::current()->PostTask(FROM_HERE, next_task); | 335 MessageLoop::current()->PostTask(FROM_HERE, next_task); |
336 return CloudPrintURLFetcher::STOP_PROCESSING; | 336 return CloudPrintURLFetcher::STOP_PROCESSING; |
337 } | 337 } |
338 | 338 |
339 CloudPrintURLFetcher::ResponseAction | 339 CloudPrintURLFetcher::ResponseAction |
340 PrinterJobHandler::HandleSuccessStatusUpdateResponse( | 340 PrinterJobHandler::HandleSuccessStatusUpdateResponse( |
341 const content::URLFetcher* source, | 341 const net::URLFetcher* source, |
342 const GURL& url, | 342 const GURL& url, |
343 DictionaryValue* json_data, | 343 DictionaryValue* json_data, |
344 bool succeeded) { | 344 bool succeeded) { |
345 VLOG(1) << "CP_CONNECTOR: Handle success status update response, id: " | 345 VLOG(1) << "CP_CONNECTOR: Handle success status update response, id: " |
346 << printer_info_cloud_.printer_id; | 346 << printer_info_cloud_.printer_id; |
347 // The print job has been spooled locally. We now need to create an object | 347 // The print job has been spooled locally. We now need to create an object |
348 // that monitors the status of the job and updates the server. | 348 // that monitors the status of the job and updates the server. |
349 scoped_refptr<JobStatusUpdater> job_status_updater( | 349 scoped_refptr<JobStatusUpdater> job_status_updater( |
350 new JobStatusUpdater(printer_info_.printer_name, job_details_.job_id_, | 350 new JobStatusUpdater(printer_info_.printer_name, job_details_.job_id_, |
351 local_job_id_, cloud_print_server_url_, | 351 local_job_id_, cloud_print_server_url_, |
352 print_system_.get(), this)); | 352 print_system_.get(), this)); |
353 job_status_updater_list_.push_back(job_status_updater); | 353 job_status_updater_list_.push_back(job_status_updater); |
354 MessageLoop::current()->PostTask( | 354 MessageLoop::current()->PostTask( |
355 FROM_HERE, base::Bind(&JobStatusUpdater::UpdateStatus, | 355 FROM_HERE, base::Bind(&JobStatusUpdater::UpdateStatus, |
356 job_status_updater.get())); | 356 job_status_updater.get())); |
357 if (succeeded) { | 357 if (succeeded) { |
358 // Since we just printed successfully, we want to look for more jobs. | 358 // Since we just printed successfully, we want to look for more jobs. |
359 CheckForJobs(kJobFetchReasonQueryMore); | 359 CheckForJobs(kJobFetchReasonQueryMore); |
360 } | 360 } |
361 MessageLoop::current()->PostTask( | 361 MessageLoop::current()->PostTask( |
362 FROM_HERE, base::Bind(&PrinterJobHandler::Stop, this)); | 362 FROM_HERE, base::Bind(&PrinterJobHandler::Stop, this)); |
363 return CloudPrintURLFetcher::STOP_PROCESSING; | 363 return CloudPrintURLFetcher::STOP_PROCESSING; |
364 } | 364 } |
365 | 365 |
366 CloudPrintURLFetcher::ResponseAction | 366 CloudPrintURLFetcher::ResponseAction |
367 PrinterJobHandler::HandleFailureStatusUpdateResponse( | 367 PrinterJobHandler::HandleFailureStatusUpdateResponse( |
368 const content::URLFetcher* source, | 368 const net::URLFetcher* source, |
369 const GURL& url, | 369 const GURL& url, |
370 DictionaryValue* json_data, | 370 DictionaryValue* json_data, |
371 bool succeeded) { | 371 bool succeeded) { |
372 VLOG(1) << "CP_CONNECTOR: Handle failure status update response, id: " | 372 VLOG(1) << "CP_CONNECTOR: Handle failure status update response, id: " |
373 << printer_info_cloud_.printer_id; | 373 << printer_info_cloud_.printer_id; |
374 MessageLoop::current()->PostTask( | 374 MessageLoop::current()->PostTask( |
375 FROM_HERE, base::Bind(&PrinterJobHandler::Stop, this)); | 375 FROM_HERE, base::Bind(&PrinterJobHandler::Stop, this)); |
376 return CloudPrintURLFetcher::STOP_PROCESSING; | 376 return CloudPrintURLFetcher::STOP_PROCESSING; |
377 } | 377 } |
378 | 378 |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
637 if (!job_spooler_ || !job_spooler_->Spool(job_details.print_ticket_, | 637 if (!job_spooler_ || !job_spooler_->Spool(job_details.print_ticket_, |
638 job_details.print_data_file_path_, | 638 job_details.print_data_file_path_, |
639 job_details.print_data_mime_type_, | 639 job_details.print_data_mime_type_, |
640 printer_name, | 640 printer_name, |
641 job_details.job_title_, | 641 job_details.job_title_, |
642 job_details.tags_, | 642 job_details.tags_, |
643 this)) { | 643 this)) { |
644 OnJobSpoolFailed(); | 644 OnJobSpoolFailed(); |
645 } | 645 } |
646 } | 646 } |
OLD | NEW |