Chromium Code Reviews| 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 #include "chrome/service/cloud_print/cloud_print_proxy_backend.h" | 5 #include "chrome/service/cloud_print/cloud_print_proxy_backend.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 // from the cloud print server. | 129 // from the cloud print server. |
| 130 void GetRegisteredPrinters(); | 130 void GetRegisteredPrinters(); |
| 131 // Removes the given printer from the list. Returns false if the printer | 131 // Removes the given printer from the list. Returns false if the printer |
| 132 // did not exist in the list. | 132 // did not exist in the list. |
| 133 bool RemovePrinterFromList(const std::string& printer_name); | 133 bool RemovePrinterFromList(const std::string& printer_name); |
| 134 // Initializes a job handler object for the specified printer. The job | 134 // Initializes a job handler object for the specified printer. The job |
| 135 // handler is responsible for checking for pending print jobs for this | 135 // handler is responsible for checking for pending print jobs for this |
| 136 // printer and print them. | 136 // printer and print them. |
| 137 void InitJobHandlerForPrinter(DictionaryValue* printer_data); | 137 void InitJobHandlerForPrinter(DictionaryValue* printer_data); |
| 138 | 138 |
| 139 // Callback method for GetPrinterCapsAndDefaults. | |
| 140 void OnReceivePrinterCaps( | |
| 141 bool succeeded, | |
| 142 const std::string& printer_name, | |
| 143 const printing::PrinterCapsAndDefaults& caps_and_defaults); | |
| 144 | |
| 139 void HandlePrinterNotification(const std::string& printer_id); | 145 void HandlePrinterNotification(const std::string& printer_id); |
| 140 void PollForJobs(); | 146 void PollForJobs(); |
| 141 // Schedules a task to poll for jobs. Does nothing if a task is already | 147 // Schedules a task to poll for jobs. Does nothing if a task is already |
| 142 // scheduled. | 148 // scheduled. |
| 143 void ScheduleJobPoll(); | 149 void ScheduleJobPoll(); |
| 144 | 150 |
| 145 // Our parent CloudPrintProxyBackend | 151 // Our parent CloudPrintProxyBackend |
| 146 CloudPrintProxyBackend* backend_; | 152 CloudPrintProxyBackend* backend_; |
| 147 | 153 |
| 148 GURL cloud_print_server_url_; | 154 GURL cloud_print_server_url_; |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 417 kCloudPrintAPIMaxRetryCount); | 423 kCloudPrintAPIMaxRetryCount); |
| 418 } | 424 } |
| 419 | 425 |
| 420 void CloudPrintProxyBackend::Core::RegisterNextPrinter() { | 426 void CloudPrintProxyBackend::Core::RegisterNextPrinter() { |
| 421 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 427 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 422 // For the next printer to be uploaded, create a multi-part post request to | 428 // For the next printer to be uploaded, create a multi-part post request to |
| 423 // upload the printer capabilities and the printer defaults. | 429 // upload the printer capabilities and the printer defaults. |
| 424 if (next_upload_index_ < printer_list_.size()) { | 430 if (next_upload_index_ < printer_list_.size()) { |
| 425 const printing::PrinterBasicInfo& info = | 431 const printing::PrinterBasicInfo& info = |
| 426 printer_list_.at(next_upload_index_); | 432 printer_list_.at(next_upload_index_); |
| 427 bool have_printer_info = true; | |
| 428 // If we are retrying a previous upload, we don't need to fetch the caps | 433 // If we are retrying a previous upload, we don't need to fetch the caps |
| 429 // and defaults again. | 434 // and defaults again. |
| 430 if (info.printer_name != last_uploaded_printer_name_) { | 435 if (info.printer_name != last_uploaded_printer_name_) { |
| 431 have_printer_info = | 436 cloud_print::PrintSystem::PrinterCapsAndDefaultsCallback* callback = |
| 432 print_system_->GetPrinterCapsAndDefaults( | 437 NewCallback(this, |
| 433 info.printer_name.c_str(), &last_uploaded_printer_info_); | 438 &CloudPrintProxyBackend::Core::OnReceivePrinterCaps); |
| 434 } | 439 // Asnchronously fetch the printer caps and defaults. The story will |
| 435 if (have_printer_info) { | 440 // continue in OnReceivePrinterCaps. |
| 436 last_uploaded_printer_name_ = info.printer_name; | 441 print_system_->GetPrinterCapsAndDefaults( |
| 437 std::string mime_boundary; | 442 info.printer_name.c_str(), callback); |
| 438 CloudPrintHelpers::CreateMimeBoundaryForUpload(&mime_boundary); | |
| 439 std::string post_data; | |
| 440 CloudPrintHelpers::AddMultipartValueForUpload(kProxyIdValue, proxy_id_, | |
| 441 mime_boundary, | |
| 442 std::string(), &post_data); | |
| 443 CloudPrintHelpers::AddMultipartValueForUpload(kPrinterNameValue, | |
| 444 info.printer_name, | |
| 445 mime_boundary, | |
| 446 std::string(), &post_data); | |
| 447 CloudPrintHelpers::AddMultipartValueForUpload(kPrinterDescValue, | |
| 448 info.printer_description, | |
| 449 mime_boundary, | |
| 450 std::string() , &post_data); | |
| 451 CloudPrintHelpers::AddMultipartValueForUpload( | |
| 452 kPrinterStatusValue, StringPrintf("%d", info.printer_status), | |
| 453 mime_boundary, std::string(), &post_data); | |
| 454 // Add printer options as tags. | |
| 455 CloudPrintHelpers::GenerateMultipartPostDataForPrinterTags(info.options, | |
| 456 mime_boundary, | |
| 457 &post_data); | |
| 458 | |
| 459 CloudPrintHelpers::AddMultipartValueForUpload( | |
| 460 kPrinterCapsValue, last_uploaded_printer_info_.printer_capabilities, | |
| 461 mime_boundary, last_uploaded_printer_info_.caps_mime_type, | |
| 462 &post_data); | |
| 463 CloudPrintHelpers::AddMultipartValueForUpload( | |
| 464 kPrinterDefaultsValue, last_uploaded_printer_info_.printer_defaults, | |
| 465 mime_boundary, last_uploaded_printer_info_.defaults_mime_type, | |
| 466 &post_data); | |
| 467 // Send a hash of the printer capabilities to the server. We will use this | |
| 468 // later to check if the capabilities have changed | |
| 469 CloudPrintHelpers::AddMultipartValueForUpload( | |
| 470 kPrinterCapsHashValue, | |
| 471 MD5String(last_uploaded_printer_info_.printer_capabilities), | |
| 472 mime_boundary, std::string(), &post_data); | |
| 473 // Terminate the request body | |
| 474 post_data.append("--" + mime_boundary + "--\r\n"); | |
| 475 std::string mime_type("multipart/form-data; boundary="); | |
| 476 mime_type += mime_boundary; | |
| 477 GURL register_url = CloudPrintHelpers::GetUrlForPrinterRegistration( | |
| 478 cloud_print_server_url_); | |
| 479 | |
| 480 next_response_handler_ = | |
| 481 &CloudPrintProxyBackend::Core::HandleRegisterPrinterResponse; | |
| 482 request_ = new CloudPrintURLFetcher; | |
| 483 request_->StartPostRequest(register_url, this, auth_token_, | |
| 484 kCloudPrintAPIMaxRetryCount, mime_type, | |
| 485 post_data); | |
| 486 | |
| 487 } else { | 443 } else { |
| 488 LOG(ERROR) << "CP_PROXY: Failed to get printer info for: " << | 444 OnReceivePrinterCaps(true, |
|
gene
2010/12/20 22:52:31
Use PostTask here
sanjeevr
2010/12/21 21:28:45
As we discussed, I don't think this is needed.
| |
| 489 info.printer_name; | 445 last_uploaded_printer_name_, |
| 490 next_upload_index_++; | 446 last_uploaded_printer_info_); |
| 491 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(this, | |
| 492 &CloudPrintProxyBackend::Core::RegisterNextPrinter)); | |
| 493 } | 447 } |
| 494 } else { | 448 } else { |
| 495 EndRegistration(); | 449 EndRegistration(); |
| 496 } | 450 } |
| 497 } | 451 } |
| 498 | 452 |
| 453 void CloudPrintProxyBackend::Core::OnReceivePrinterCaps( | |
| 454 bool succeeded, | |
| 455 const std::string& printer_name, | |
| 456 const printing::PrinterCapsAndDefaults& caps_and_defaults) { | |
| 457 DCHECK(next_upload_index_ < printer_list_.size()); | |
| 458 if (succeeded) { | |
| 459 const printing::PrinterBasicInfo& info = | |
| 460 printer_list_.at(next_upload_index_); | |
| 461 | |
| 462 last_uploaded_printer_name_ = info.printer_name; | |
| 463 last_uploaded_printer_info_ = caps_and_defaults; | |
| 464 | |
| 465 std::string mime_boundary; | |
| 466 CloudPrintHelpers::CreateMimeBoundaryForUpload(&mime_boundary); | |
| 467 std::string post_data; | |
| 468 CloudPrintHelpers::AddMultipartValueForUpload(kProxyIdValue, proxy_id_, | |
| 469 mime_boundary, | |
| 470 std::string(), &post_data); | |
| 471 CloudPrintHelpers::AddMultipartValueForUpload(kPrinterNameValue, | |
| 472 info.printer_name, | |
| 473 mime_boundary, | |
| 474 std::string(), &post_data); | |
| 475 CloudPrintHelpers::AddMultipartValueForUpload(kPrinterDescValue, | |
| 476 info.printer_description, | |
| 477 mime_boundary, | |
| 478 std::string() , &post_data); | |
| 479 CloudPrintHelpers::AddMultipartValueForUpload( | |
| 480 kPrinterStatusValue, StringPrintf("%d", info.printer_status), | |
| 481 mime_boundary, std::string(), &post_data); | |
| 482 // Add printer options as tags. | |
| 483 CloudPrintHelpers::GenerateMultipartPostDataForPrinterTags(info.options, | |
| 484 mime_boundary, | |
| 485 &post_data); | |
| 486 | |
| 487 CloudPrintHelpers::AddMultipartValueForUpload( | |
| 488 kPrinterCapsValue, last_uploaded_printer_info_.printer_capabilities, | |
| 489 mime_boundary, last_uploaded_printer_info_.caps_mime_type, | |
| 490 &post_data); | |
| 491 CloudPrintHelpers::AddMultipartValueForUpload( | |
| 492 kPrinterDefaultsValue, last_uploaded_printer_info_.printer_defaults, | |
| 493 mime_boundary, last_uploaded_printer_info_.defaults_mime_type, | |
| 494 &post_data); | |
| 495 // Send a hash of the printer capabilities to the server. We will use this | |
| 496 // later to check if the capabilities have changed | |
| 497 CloudPrintHelpers::AddMultipartValueForUpload( | |
| 498 kPrinterCapsHashValue, | |
| 499 MD5String(last_uploaded_printer_info_.printer_capabilities), | |
| 500 mime_boundary, std::string(), &post_data); | |
| 501 // Terminate the request body | |
| 502 post_data.append("--" + mime_boundary + "--\r\n"); | |
| 503 std::string mime_type("multipart/form-data; boundary="); | |
| 504 mime_type += mime_boundary; | |
| 505 GURL register_url = CloudPrintHelpers::GetUrlForPrinterRegistration( | |
| 506 cloud_print_server_url_); | |
| 507 | |
| 508 next_response_handler_ = | |
| 509 &CloudPrintProxyBackend::Core::HandleRegisterPrinterResponse; | |
| 510 request_ = new CloudPrintURLFetcher; | |
| 511 request_->StartPostRequest(register_url, this, auth_token_, | |
| 512 kCloudPrintAPIMaxRetryCount, mime_type, | |
| 513 post_data); | |
| 514 } else { | |
| 515 LOG(ERROR) << "CP_PROXY: Failed to get printer info for: " << | |
| 516 printer_name; | |
| 517 next_upload_index_++; | |
| 518 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(this, | |
| 519 &CloudPrintProxyBackend::Core::RegisterNextPrinter)); | |
| 520 } | |
| 521 } | |
| 522 | |
| 499 void CloudPrintProxyBackend::Core::HandlePrinterNotification( | 523 void CloudPrintProxyBackend::Core::HandlePrinterNotification( |
| 500 const std::string& printer_id) { | 524 const std::string& printer_id) { |
| 501 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 525 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 502 VLOG(1) << "CP_PROXY: Handle printer notification, id: " << printer_id; | 526 VLOG(1) << "CP_PROXY: Handle printer notification, id: " << printer_id; |
| 503 JobHandlerMap::iterator index = job_handler_map_.find(printer_id); | 527 JobHandlerMap::iterator index = job_handler_map_.find(printer_id); |
| 504 if (index != job_handler_map_.end()) | 528 if (index != job_handler_map_.end()) |
| 505 index->second->CheckForJobs(kJobFetchReasonNotified); | 529 index->second->CheckForJobs(kJobFetchReasonNotified); |
| 506 } | 530 } |
| 507 | 531 |
| 508 void CloudPrintProxyBackend::Core::PollForJobs() { | 532 void CloudPrintProxyBackend::Core::PollForJobs() { |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 755 VLOG(1) << "CP_PROXY: Printer job handle shutdown, id " << printer_id; | 779 VLOG(1) << "CP_PROXY: Printer job handle shutdown, id " << printer_id; |
| 756 job_handler_map_.erase(printer_id); | 780 job_handler_map_.erase(printer_id); |
| 757 } | 781 } |
| 758 | 782 |
| 759 void CloudPrintProxyBackend::Core::OnAuthError() { | 783 void CloudPrintProxyBackend::Core::OnAuthError() { |
| 760 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 784 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 761 VLOG(1) << "CP_PROXY: Auth Error"; | 785 VLOG(1) << "CP_PROXY: Auth Error"; |
| 762 backend_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, | 786 backend_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, |
| 763 &Core::NotifyAuthenticationFailed)); | 787 &Core::NotifyAuthenticationFailed)); |
| 764 } | 788 } |
| OLD | NEW |