Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: chrome/service/cloud_print/printer_job_handler.cc

Issue 8375039: Create a content::UrlFetcher interface that lives in content/public/common and convert users to i... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: review comments Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/file_util.h" 7 #include "base/file_util.h"
8 #include "base/json/json_reader.h" 8 #include "base/json/json_reader.h"
9 #include "base/md5.h" 9 #include "base/md5.h"
10 #include "base/stringprintf.h" 10 #include "base/stringprintf.h"
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 std::string()); 275 std::string());
276 } else { 276 } else {
277 // We are done here. Go to the Stop state 277 // We are done here. Go to the Stop state
278 MessageLoop::current()->PostTask( 278 MessageLoop::current()->PostTask(
279 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop)); 279 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop));
280 } 280 }
281 } 281 }
282 282
283 // CloudPrintURLFetcher::Delegate implementation. 283 // CloudPrintURLFetcher::Delegate implementation.
284 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleRawResponse( 284 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleRawResponse(
285 const URLFetcher* source, 285 const content::URLFetcher* source,
286 const GURL& url, 286 const GURL& url,
287 const net::URLRequestStatus& status, 287 const net::URLRequestStatus& status,
288 int response_code, 288 int response_code,
289 const net::ResponseCookies& cookies, 289 const net::ResponseCookies& cookies,
290 const std::string& data) { 290 const std::string& data) {
291 // 415 (Unsupported media type) error while fetching data from the server 291 // 415 (Unsupported media type) error while fetching data from the server
292 // means data conversion error. Stop fetching process and mark job as error. 292 // means data conversion error. Stop fetching process and mark job as error.
293 if (next_data_handler_ == (&PrinterJobHandler::HandlePrintDataResponse) && 293 if (next_data_handler_ == (&PrinterJobHandler::HandlePrintDataResponse) &&
294 response_code == RC_UNSUPPORTED_MEDIA_TYPE) { 294 response_code == RC_UNSUPPORTED_MEDIA_TYPE) {
295 MessageLoop::current()->PostTask( 295 MessageLoop::current()->PostTask(
296 FROM_HERE, 296 FROM_HERE,
297 NewRunnableMethod(this, &PrinterJobHandler::JobFailed, 297 NewRunnableMethod(this, &PrinterJobHandler::JobFailed,
298 JOB_DOWNLOAD_FAILED)); 298 JOB_DOWNLOAD_FAILED));
299 return CloudPrintURLFetcher::STOP_PROCESSING; 299 return CloudPrintURLFetcher::STOP_PROCESSING;
300 } 300 }
301 return CloudPrintURLFetcher::CONTINUE_PROCESSING; 301 return CloudPrintURLFetcher::CONTINUE_PROCESSING;
302 } 302 }
303 303
304 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleRawData( 304 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleRawData(
305 const URLFetcher* source, 305 const content::URLFetcher* source,
306 const GURL& url, 306 const GURL& url,
307 const std::string& data) { 307 const std::string& data) {
308 if (!next_data_handler_) 308 if (!next_data_handler_)
309 return CloudPrintURLFetcher::CONTINUE_PROCESSING; 309 return CloudPrintURLFetcher::CONTINUE_PROCESSING;
310 return (this->*next_data_handler_)(source, url, data); 310 return (this->*next_data_handler_)(source, url, data);
311 } 311 }
312 312
313 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleJSONData( 313 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleJSONData(
314 const URLFetcher* source, 314 const content::URLFetcher* source,
315 const GURL& url, 315 const GURL& url,
316 DictionaryValue* json_data, 316 DictionaryValue* json_data,
317 bool succeeded) { 317 bool succeeded) {
318 DCHECK(next_json_data_handler_); 318 DCHECK(next_json_data_handler_);
319 return (this->*next_json_data_handler_)(source, 319 return (this->*next_json_data_handler_)(source,
320 url, 320 url,
321 json_data, 321 json_data,
322 succeeded); 322 succeeded);
323 } 323 }
324 324
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 index != job_status_updater_list_.end(); index++) { 379 index != job_status_updater_list_.end(); index++) {
380 MessageLoop::current()->PostTask( 380 MessageLoop::current()->PostTask(
381 FROM_HERE, NewRunnableMethod(index->get(), 381 FROM_HERE, NewRunnableMethod(index->get(),
382 &JobStatusUpdater::UpdateStatus)); 382 &JobStatusUpdater::UpdateStatus));
383 } 383 }
384 } 384 }
385 385
386 // Begin Response handlers 386 // Begin Response handlers
387 CloudPrintURLFetcher::ResponseAction 387 CloudPrintURLFetcher::ResponseAction
388 PrinterJobHandler::HandlePrinterUpdateResponse( 388 PrinterJobHandler::HandlePrinterUpdateResponse(
389 const URLFetcher* source, 389 const content::URLFetcher* source,
390 const GURL& url, 390 const GURL& url,
391 DictionaryValue* json_data, 391 DictionaryValue* json_data,
392 bool succeeded) { 392 bool succeeded) {
393 VLOG(1) << "CP_PROXY: Handle printer update response, id: " 393 VLOG(1) << "CP_PROXY: Handle printer update response, id: "
394 << printer_info_cloud_.printer_id; 394 << printer_info_cloud_.printer_id;
395 // We are done here. Go to the Stop state 395 // We are done here. Go to the Stop state
396 MessageLoop::current()->PostTask( 396 MessageLoop::current()->PostTask(
397 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop)); 397 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop));
398 return CloudPrintURLFetcher::STOP_PROCESSING; 398 return CloudPrintURLFetcher::STOP_PROCESSING;
399 } 399 }
400 400
401 CloudPrintURLFetcher::ResponseAction 401 CloudPrintURLFetcher::ResponseAction
402 PrinterJobHandler::HandlePrinterDeleteResponse( 402 PrinterJobHandler::HandlePrinterDeleteResponse(
403 const URLFetcher* source, 403 const content::URLFetcher* source,
404 const GURL& url, 404 const GURL& url,
405 DictionaryValue* json_data, 405 DictionaryValue* json_data,
406 bool succeeded) { 406 bool succeeded) {
407 VLOG(1) << "CP_PROXY: Handler printer delete response, id: " 407 VLOG(1) << "CP_PROXY: Handler printer delete response, id: "
408 << printer_info_cloud_.printer_id; 408 << printer_info_cloud_.printer_id;
409 // The printer has been deleted. Shutdown the handler class. 409 // The printer has been deleted. Shutdown the handler class.
410 MessageLoop::current()->PostTask( 410 MessageLoop::current()->PostTask(
411 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Shutdown)); 411 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Shutdown));
412 return CloudPrintURLFetcher::STOP_PROCESSING; 412 return CloudPrintURLFetcher::STOP_PROCESSING;
413 } 413 }
414 414
415 CloudPrintURLFetcher::ResponseAction 415 CloudPrintURLFetcher::ResponseAction
416 PrinterJobHandler::HandleJobMetadataResponse( 416 PrinterJobHandler::HandleJobMetadataResponse(
417 const URLFetcher* source, 417 const content::URLFetcher* source,
418 const GURL& url, 418 const GURL& url,
419 DictionaryValue* json_data, 419 DictionaryValue* json_data,
420 bool succeeded) { 420 bool succeeded) {
421 VLOG(1) << "CP_PROXY: Handle job metadata response, id: " 421 VLOG(1) << "CP_PROXY: Handle job metadata response, id: "
422 << printer_info_cloud_.printer_id; 422 << printer_info_cloud_.printer_id;
423 bool job_available = false; 423 bool job_available = false;
424 if (succeeded) { 424 if (succeeded) {
425 ListValue* job_list = NULL; 425 ListValue* job_list = NULL;
426 if (json_data->GetList(kJobListValue, &job_list) && job_list) { 426 if (json_data->GetList(kJobListValue, &job_list) && job_list) {
427 // Even though it is a job list, for now we are only interested in the 427 // Even though it is a job list, for now we are only interested in the
(...skipping 27 matching lines...) Expand all
455 } 455 }
456 } 456 }
457 // If no jobs are available, go to the Stop state. 457 // If no jobs are available, go to the Stop state.
458 if (!job_available) 458 if (!job_available)
459 MessageLoop::current()->PostTask( 459 MessageLoop::current()->PostTask(
460 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop)); 460 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop));
461 return CloudPrintURLFetcher::STOP_PROCESSING; 461 return CloudPrintURLFetcher::STOP_PROCESSING;
462 } 462 }
463 463
464 CloudPrintURLFetcher::ResponseAction 464 CloudPrintURLFetcher::ResponseAction
465 PrinterJobHandler::HandlePrintTicketResponse(const URLFetcher* source, 465 PrinterJobHandler::HandlePrintTicketResponse(const content::URLFetcher* source,
466 const GURL& url, 466 const GURL& url,
467 const std::string& data) { 467 const std::string& data) {
468 VLOG(1) << "CP_PROXY: Handle print ticket response, id: " 468 VLOG(1) << "CP_PROXY: Handle print ticket response, id: "
469 << printer_info_cloud_.printer_id; 469 << printer_info_cloud_.printer_id;
470 if (print_system_->ValidatePrintTicket(printer_info_.printer_name, data)) { 470 if (print_system_->ValidatePrintTicket(printer_info_.printer_name, data)) {
471 job_details_.print_ticket_ = data; 471 job_details_.print_ticket_ = data;
472 SetNextDataHandler(&PrinterJobHandler::HandlePrintDataResponse); 472 SetNextDataHandler(&PrinterJobHandler::HandlePrintDataResponse);
473 request_ = new CloudPrintURLFetcher; 473 request_ = new CloudPrintURLFetcher;
474 std::string accept_headers = "Accept: "; 474 std::string accept_headers = "Accept: ";
475 accept_headers += print_system_->GetSupportedMimeTypes(); 475 accept_headers += print_system_->GetSupportedMimeTypes();
476 request_->StartGetRequest(GURL(print_data_url_.c_str()), 476 request_->StartGetRequest(GURL(print_data_url_.c_str()),
477 this, 477 this,
478 kJobDataMaxRetryCount, 478 kJobDataMaxRetryCount,
479 accept_headers); 479 accept_headers);
480 } else { 480 } else {
481 // The print ticket was not valid. We are done here. 481 // The print ticket was not valid. We are done here.
482 FailedFetchingJobData(); 482 FailedFetchingJobData();
483 } 483 }
484 return CloudPrintURLFetcher::STOP_PROCESSING; 484 return CloudPrintURLFetcher::STOP_PROCESSING;
485 } 485 }
486 486
487 CloudPrintURLFetcher::ResponseAction 487 CloudPrintURLFetcher::ResponseAction
488 PrinterJobHandler::HandlePrintDataResponse(const URLFetcher* source, 488 PrinterJobHandler::HandlePrintDataResponse(const content::URLFetcher* source,
489 const GURL& url, 489 const GURL& url,
490 const std::string& data) { 490 const std::string& data) {
491 VLOG(1) << "CP_PROXY: Handle print data response, id: " 491 VLOG(1) << "CP_PROXY: Handle print data response, id: "
492 << printer_info_cloud_.printer_id; 492 << printer_info_cloud_.printer_id;
493 Task* next_task = NULL; 493 Task* next_task = NULL;
494 if (file_util::CreateTemporaryFile(&job_details_.print_data_file_path_)) { 494 if (file_util::CreateTemporaryFile(&job_details_.print_data_file_path_)) {
495 int ret = file_util::WriteFile(job_details_.print_data_file_path_, 495 int ret = file_util::WriteFile(job_details_.print_data_file_path_,
496 data.c_str(), 496 data.c_str(),
497 data.length()); 497 data.length());
498 source->response_headers()->GetMimeType( 498 source->GetResponseHeaders()->GetMimeType(
499 &job_details_.print_data_mime_type_); 499 &job_details_.print_data_mime_type_);
500 DCHECK(ret == static_cast<int>(data.length())); 500 DCHECK(ret == static_cast<int>(data.length()));
501 if (ret == static_cast<int>(data.length())) { 501 if (ret == static_cast<int>(data.length())) {
502 next_task = NewRunnableMethod(this, &PrinterJobHandler::StartPrinting); 502 next_task = NewRunnableMethod(this, &PrinterJobHandler::StartPrinting);
503 } 503 }
504 } 504 }
505 // If there was no task allocated above, then there was an error in 505 // If there was no task allocated above, then there was an error in
506 // saving the print data, bail out here. 506 // saving the print data, bail out here.
507 if (!next_task) { 507 if (!next_task) {
508 next_task = NewRunnableMethod(this, &PrinterJobHandler::JobFailed, 508 next_task = NewRunnableMethod(this, &PrinterJobHandler::JobFailed,
509 JOB_DOWNLOAD_FAILED); 509 JOB_DOWNLOAD_FAILED);
510 } 510 }
511 MessageLoop::current()->PostTask(FROM_HERE, next_task); 511 MessageLoop::current()->PostTask(FROM_HERE, next_task);
512 return CloudPrintURLFetcher::STOP_PROCESSING; 512 return CloudPrintURLFetcher::STOP_PROCESSING;
513 } 513 }
514 514
515 CloudPrintURLFetcher::ResponseAction 515 CloudPrintURLFetcher::ResponseAction
516 PrinterJobHandler::HandleSuccessStatusUpdateResponse( 516 PrinterJobHandler::HandleSuccessStatusUpdateResponse(
517 const URLFetcher* source, 517 const content::URLFetcher* source,
518 const GURL& url, 518 const GURL& url,
519 DictionaryValue* json_data, 519 DictionaryValue* json_data,
520 bool succeeded) { 520 bool succeeded) {
521 VLOG(1) << "CP_PROXY: Handle success status update response, id: " 521 VLOG(1) << "CP_PROXY: Handle success status update response, id: "
522 << printer_info_cloud_.printer_id; 522 << printer_info_cloud_.printer_id;
523 // The print job has been spooled locally. We now need to create an object 523 // The print job has been spooled locally. We now need to create an object
524 // that monitors the status of the job and updates the server. 524 // that monitors the status of the job and updates the server.
525 scoped_refptr<JobStatusUpdater> job_status_updater( 525 scoped_refptr<JobStatusUpdater> job_status_updater(
526 new JobStatusUpdater(printer_info_.printer_name, job_details_.job_id_, 526 new JobStatusUpdater(printer_info_.printer_name, job_details_.job_id_,
527 local_job_id_, cloud_print_server_url_, 527 local_job_id_, cloud_print_server_url_,
528 print_system_.get(), this)); 528 print_system_.get(), this));
529 job_status_updater_list_.push_back(job_status_updater); 529 job_status_updater_list_.push_back(job_status_updater);
530 MessageLoop::current()->PostTask( 530 MessageLoop::current()->PostTask(
531 FROM_HERE, NewRunnableMethod(job_status_updater.get(), 531 FROM_HERE, NewRunnableMethod(job_status_updater.get(),
532 &JobStatusUpdater::UpdateStatus)); 532 &JobStatusUpdater::UpdateStatus));
533 if (succeeded) { 533 if (succeeded) {
534 // Since we just printed successfully, we want to look for more jobs. 534 // Since we just printed successfully, we want to look for more jobs.
535 CheckForJobs(kJobFetchReasonQueryMore); 535 CheckForJobs(kJobFetchReasonQueryMore);
536 } 536 }
537 MessageLoop::current()->PostTask( 537 MessageLoop::current()->PostTask(
538 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop)); 538 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop));
539 return CloudPrintURLFetcher::STOP_PROCESSING; 539 return CloudPrintURLFetcher::STOP_PROCESSING;
540 } 540 }
541 541
542 CloudPrintURLFetcher::ResponseAction 542 CloudPrintURLFetcher::ResponseAction
543 PrinterJobHandler::HandleFailureStatusUpdateResponse( 543 PrinterJobHandler::HandleFailureStatusUpdateResponse(
544 const URLFetcher* source, 544 const content::URLFetcher* source,
545 const GURL& url, 545 const GURL& url,
546 DictionaryValue* json_data, 546 DictionaryValue* json_data,
547 bool succeeded) { 547 bool succeeded) {
548 VLOG(1) << "CP_PROXY: Handle failure status update response, id: " 548 VLOG(1) << "CP_PROXY: Handle failure status update response, id: "
549 << printer_info_cloud_.printer_id; 549 << printer_info_cloud_.printer_id;
550 MessageLoop::current()->PostTask( 550 MessageLoop::current()->PostTask(
551 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop)); 551 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop));
552 return CloudPrintURLFetcher::STOP_PROCESSING; 552 return CloudPrintURLFetcher::STOP_PROCESSING;
553 } 553 }
554 // End Response handlers 554 // End Response handlers
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
681 } 681 }
682 682
683 void PrinterJobHandler::OnJobSpoolFailed() { 683 void PrinterJobHandler::OnJobSpoolFailed() {
684 DCHECK(MessageLoop::current() == print_thread_.message_loop()); 684 DCHECK(MessageLoop::current() == print_thread_.message_loop());
685 job_spooler_ = NULL; 685 job_spooler_ = NULL;
686 job_handler_message_loop_proxy_->PostTask(FROM_HERE, 686 job_handler_message_loop_proxy_->PostTask(FROM_HERE,
687 NewRunnableMethod(this, 687 NewRunnableMethod(this,
688 &PrinterJobHandler::JobFailed, 688 &PrinterJobHandler::JobFailed,
689 PRINT_FAILED)); 689 PRINT_FAILED));
690 } 690 }
OLDNEW
« no previous file with comments | « chrome/service/cloud_print/printer_job_handler.h ('k') | chrome/service/gaia/service_gaia_authenticator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698