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

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

Issue 11360151: Move common cloud print methods from service/cloud_print to common/cloud_print. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 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) 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"
11 #include "base/md5.h" 11 #include "base/md5.h"
12 #include "base/stringprintf.h" 12 #include "base/stringprintf.h"
13 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "chrome/common/cloud_print/cloud_print_constants.h"
15 #include "chrome/common/cloud_print/cloud_print_helpers.h" 16 #include "chrome/common/cloud_print/cloud_print_helpers.h"
16 #include "chrome/service/cloud_print/cloud_print_consts.h" 17 #include "chrome/service/cloud_print/cloud_print_consts.h"
msw 2012/11/17 00:22:30 ditto nit to remove this include if unused.
Chen Yu 2012/11/26 12:07:06 Done.
17 #include "chrome/service/cloud_print/cloud_print_helpers.h" 18 #include "chrome/service/cloud_print/cloud_print_helpers.h"
18 #include "chrome/service/cloud_print/job_status_updater.h" 19 #include "chrome/service/cloud_print/job_status_updater.h"
19 #include "googleurl/src/gurl.h" 20 #include "googleurl/src/gurl.h"
20 #include "grit/generated_resources.h" 21 #include "grit/generated_resources.h"
21 #include "net/http/http_response_headers.h" 22 #include "net/http/http_response_headers.h"
22 #include "net/http/http_status_code.h" 23 #include "net/http/http_status_code.h"
23 #include "printing/backend/print_backend.h" 24 #include "printing/backend/print_backend.h"
24 #include "ui/base/l10n/l10n_util.h" 25 #include "ui/base/l10n/l10n_util.h"
25 26
26 PrinterJobHandler::JobDetails::JobDetails() {} 27 PrinterJobHandler::JobDetails::JobDetails() {}
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 PrinterJobHandler::HandleJobMetadataResponse( 251 PrinterJobHandler::HandleJobMetadataResponse(
251 const net::URLFetcher* source, 252 const net::URLFetcher* source,
252 const GURL& url, 253 const GURL& url,
253 DictionaryValue* json_data, 254 DictionaryValue* json_data,
254 bool succeeded) { 255 bool succeeded) {
255 VLOG(1) << "CP_CONNECTOR: Handling job metadata response" 256 VLOG(1) << "CP_CONNECTOR: Handling job metadata response"
256 << ", printer id: " << printer_info_cloud_.printer_id; 257 << ", printer id: " << printer_info_cloud_.printer_id;
257 bool job_available = false; 258 bool job_available = false;
258 if (succeeded) { 259 if (succeeded) {
259 ListValue* job_list = NULL; 260 ListValue* job_list = NULL;
260 if (json_data->GetList(kJobListValue, &job_list) && job_list) { 261 if (json_data->GetList(cloud_print::kJobListValue, &job_list) && job_list) {
msw 2012/11/17 00:22:30 ditto nit: put this file in the cloud_print namesp
Chen Yu 2012/11/26 12:07:06 Done.
261 // Even though it is a job list, for now we are only interested in the 262 // Even though it is a job list, for now we are only interested in the
262 // first job 263 // first job
263 DictionaryValue* job_data = NULL; 264 DictionaryValue* job_data = NULL;
264 if (job_list->GetDictionary(0, &job_data)) { 265 if (job_list->GetDictionary(0, &job_data)) {
265 job_available = true; 266 job_available = true;
266 job_data->GetString(kIdValue, &job_details_.job_id_); 267 job_data->GetString(cloud_print::kIdValue, &job_details_.job_id_);
267 job_data->GetString(kTitleValue, &job_details_.job_title_); 268 job_data->GetString(cloud_print::kTitleValue, &job_details_.job_title_);
268 std::string print_ticket_url; 269 std::string print_ticket_url;
269 job_data->GetString(kTicketUrlValue, &print_ticket_url); 270 job_data->GetString(cloud_print::kTicketUrlValue, &print_ticket_url);
270 job_data->GetString(kFileUrlValue, &print_data_url_); 271 job_data->GetString(cloud_print::kFileUrlValue, &print_data_url_);
271 272
272 // Get tags for print job. 273 // Get tags for print job.
273 job_details_.tags_.clear(); 274 job_details_.tags_.clear();
274 ListValue* tags = NULL; 275 ListValue* tags = NULL;
275 if (job_data->GetList(kTagsValue, &tags)) { 276 if (job_data->GetList(cloud_print::kTagsValue, &tags)) {
276 for (size_t i = 0; i < tags->GetSize(); i++) { 277 for (size_t i = 0; i < tags->GetSize(); i++) {
277 std::string value; 278 std::string value;
278 if (tags->GetString(i, &value)) 279 if (tags->GetString(i, &value))
279 job_details_.tags_.push_back(value); 280 job_details_.tags_.push_back(value);
280 } 281 }
281 } 282 }
282 SetNextDataHandler(&PrinterJobHandler::HandlePrintTicketResponse); 283 SetNextDataHandler(&PrinterJobHandler::HandlePrintTicketResponse);
283 request_ = new CloudPrintURLFetcher; 284 request_ = new CloudPrintURLFetcher;
284 request_->StartGetRequest(GURL(print_ticket_url.c_str()), 285 request_->StartGetRequest(GURL(print_ticket_url.c_str()),
285 this, 286 this,
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 if (!task_in_progress_ && job_check_pending_) { 417 if (!task_in_progress_ && job_check_pending_) {
417 task_in_progress_ = true; 418 task_in_progress_ = true;
418 VLOG(1) << "CP_CONNECTOR: Changed task in progress" 419 VLOG(1) << "CP_CONNECTOR: Changed task in progress"
419 ", printer id: " << printer_info_cloud_.printer_id 420 ", printer id: " << printer_info_cloud_.printer_id
420 << ", task in progress: " << task_in_progress_; 421 << ", task in progress: " << task_in_progress_;
421 job_check_pending_ = false; 422 job_check_pending_ = false;
422 // We need to fetch any pending jobs for this printer 423 // We need to fetch any pending jobs for this printer
423 SetNextJSONHandler(&PrinterJobHandler::HandleJobMetadataResponse); 424 SetNextJSONHandler(&PrinterJobHandler::HandleJobMetadataResponse);
424 request_ = new CloudPrintURLFetcher; 425 request_ = new CloudPrintURLFetcher;
425 request_->StartGetRequest( 426 request_->StartGetRequest(
426 CloudPrintHelpers::GetUrlForJobFetch( 427 cloud_print::GetUrlForJobFetch(
427 cloud_print_server_url_, printer_info_cloud_.printer_id, 428 cloud_print_server_url_, printer_info_cloud_.printer_id,
428 job_fetch_reason_), 429 job_fetch_reason_),
429 this, 430 this,
430 kCloudPrintAPIMaxRetryCount, 431 kCloudPrintAPIMaxRetryCount,
431 std::string()); 432 std::string());
432 last_job_fetch_time_ = base::TimeTicks::Now(); 433 last_job_fetch_time_ = base::TimeTicks::Now();
433 VLOG(1) << "CP_CONNECTOR: Last job fetch time" 434 VLOG(1) << "CP_CONNECTOR: Last job fetch time"
434 << ", printer name: " << printer_info_.printer_name.c_str() 435 << ", printer name: " << printer_info_.printer_name.c_str()
435 << ", timestamp: " << last_job_fetch_time_.ToInternalValue(); 436 << ", timestamp: " << last_job_fetch_time_.ToInternalValue();
436 job_fetch_reason_.clear(); 437 job_fetch_reason_.clear();
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
599 std::string mime_boundary; 600 std::string mime_boundary;
600 cloud_print::CreateMimeBoundaryForUpload(&mime_boundary); 601 cloud_print::CreateMimeBoundaryForUpload(&mime_boundary);
601 602
602 if (succeeded) { 603 if (succeeded) {
603 std::string caps_hash = 604 std::string caps_hash =
604 base::MD5String(caps_and_defaults.printer_capabilities); 605 base::MD5String(caps_and_defaults.printer_capabilities);
605 if (caps_hash != printer_info_cloud_.caps_hash) { 606 if (caps_hash != printer_info_cloud_.caps_hash) {
606 // Hashes don't match, we need to upload new capabilities (the defaults 607 // Hashes don't match, we need to upload new capabilities (the defaults
607 // go for free along with the capabilities) 608 // go for free along with the capabilities)
608 printer_info_cloud_.caps_hash = caps_hash; 609 printer_info_cloud_.caps_hash = caps_hash;
609 cloud_print::AddMultipartValueForUpload(kPrinterCapsValue, 610 cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterCapsValue,
610 caps_and_defaults.printer_capabilities, mime_boundary, 611 caps_and_defaults.printer_capabilities, mime_boundary,
611 caps_and_defaults.caps_mime_type, &post_data); 612 caps_and_defaults.caps_mime_type, &post_data);
612 cloud_print::AddMultipartValueForUpload(kPrinterDefaultsValue, 613 cloud_print::AddMultipartValueForUpload(
614 cloud_print::kPrinterDefaultsValue,
613 caps_and_defaults.printer_defaults, mime_boundary, 615 caps_and_defaults.printer_defaults, mime_boundary,
614 caps_and_defaults.defaults_mime_type, &post_data); 616 caps_and_defaults.defaults_mime_type, &post_data);
615 cloud_print::AddMultipartValueForUpload(kPrinterCapsHashValue, 617 cloud_print::AddMultipartValueForUpload(
616 caps_hash, mime_boundary, std::string(), &post_data); 618 cloud_print::kPrinterCapsHashValue, caps_hash, mime_boundary,
619 std::string(), &post_data);
617 } 620 }
618 } else { 621 } else {
619 LOG(ERROR) << "Failed to get printer caps and defaults" 622 LOG(ERROR) << "Failed to get printer caps and defaults"
620 << ", printer name: " << printer_name; 623 << ", printer name: " << printer_name;
621 } 624 }
622 625
623 std::string tags_hash = CloudPrintHelpers::GetHashOfPrinterTags(printer_info); 626 std::string tags_hash = CloudPrintHelpers::GetHashOfPrinterTags(printer_info);
624 if (tags_hash != printer_info_cloud_.tags_hash) { 627 if (tags_hash != printer_info_cloud_.tags_hash) {
625 printer_info_cloud_.tags_hash = tags_hash; 628 printer_info_cloud_.tags_hash = tags_hash;
626 post_data += CloudPrintHelpers::GetPostDataForPrinterTags(printer_info, 629 post_data += CloudPrintHelpers::GetPostDataForPrinterTags(printer_info,
627 mime_boundary); 630 mime_boundary);
628 // Remove all the existing proxy tags. 631 // Remove all the existing proxy tags.
629 std::string cp_tag_wildcard(kProxyTagPrefix); 632 std::string cp_tag_wildcard(kProxyTagPrefix);
630 cp_tag_wildcard += ".*"; 633 cp_tag_wildcard += ".*";
631 cloud_print::AddMultipartValueForUpload(kPrinterRemoveTagValue, 634 cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterRemoveTagValue,
632 cp_tag_wildcard, mime_boundary, std::string(), &post_data); 635 cp_tag_wildcard, mime_boundary, std::string(), &post_data);
633 } 636 }
634 637
635 if (printer_info.printer_name != printer_info_.printer_name) { 638 if (printer_info.printer_name != printer_info_.printer_name) {
636 cloud_print::AddMultipartValueForUpload(kPrinterNameValue, 639 cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterNameValue,
637 printer_info.printer_name, mime_boundary, std::string(), &post_data); 640 printer_info.printer_name, mime_boundary, std::string(), &post_data);
638 } 641 }
639 if (printer_info.printer_description != printer_info_.printer_description) { 642 if (printer_info.printer_description != printer_info_.printer_description) {
640 cloud_print::AddMultipartValueForUpload(kPrinterDescValue, 643 cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterDescValue,
641 printer_info.printer_description, mime_boundary, 644 printer_info.printer_description, mime_boundary,
642 std::string(), &post_data); 645 std::string(), &post_data);
643 } 646 }
644 if (printer_info.printer_status != printer_info_.printer_status) { 647 if (printer_info.printer_status != printer_info_.printer_status) {
645 cloud_print::AddMultipartValueForUpload(kPrinterStatusValue, 648 cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterStatusValue,
646 base::StringPrintf("%d", printer_info.printer_status), mime_boundary, 649 base::StringPrintf("%d", printer_info.printer_status), mime_boundary,
647 std::string(), &post_data); 650 std::string(), &post_data);
648 } 651 }
649 printer_info_ = printer_info; 652 printer_info_ = printer_info;
650 if (!post_data.empty()) { 653 if (!post_data.empty()) {
651 // Terminate the request body 654 // Terminate the request body
652 post_data.append("--" + mime_boundary + "--\r\n"); 655 post_data.append("--" + mime_boundary + "--\r\n");
653 std::string mime_type("multipart/form-data; boundary="); 656 std::string mime_type("multipart/form-data; boundary=");
654 mime_type += mime_boundary; 657 mime_type += mime_boundary;
655 SetNextJSONHandler(&PrinterJobHandler::HandlePrinterUpdateResponse); 658 SetNextJSONHandler(&PrinterJobHandler::HandlePrinterUpdateResponse);
656 request_ = new CloudPrintURLFetcher; 659 request_ = new CloudPrintURLFetcher;
657 request_->StartPostRequest( 660 request_->StartPostRequest(
658 CloudPrintHelpers::GetUrlForPrinterUpdate( 661 cloud_print::GetUrlForPrinterUpdate(
659 cloud_print_server_url_, printer_info_cloud_.printer_id), 662 cloud_print_server_url_, printer_info_cloud_.printer_id),
660 this, 663 this,
661 kCloudPrintAPIMaxRetryCount, 664 kCloudPrintAPIMaxRetryCount,
662 mime_type, 665 mime_type,
663 post_data, 666 post_data,
664 std::string()); 667 std::string());
665 } else { 668 } else {
666 // We are done here. Go to the Stop state 669 // We are done here. Go to the Stop state
667 VLOG(1) << "CP_CONNECTOR: Stopping printer job handler" 670 VLOG(1) << "CP_CONNECTOR: Stopping printer job handler"
668 << ", printer name: " << printer_name; 671 << ", printer name: " << printer_name;
(...skipping 21 matching lines...) Expand all
690 if (!job_spooler_->Spool(job_details.print_ticket_, 693 if (!job_spooler_->Spool(job_details.print_ticket_,
691 job_details.print_data_file_path_, 694 job_details.print_data_file_path_,
692 job_details.print_data_mime_type_, 695 job_details.print_data_mime_type_,
693 printer_name, 696 printer_name,
694 UTF16ToUTF8(document_name), 697 UTF16ToUTF8(document_name),
695 job_details.tags_, 698 job_details.tags_,
696 this)) { 699 this)) {
697 OnJobSpoolFailed(); 700 OnJobSpoolFailed();
698 } 701 }
699 } 702 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698