| 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/cloud_print_connector.h" | 5 #include "chrome/service/cloud_print/cloud_print_connector.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/md5.h" | 9 #include "base/md5.h" |
| 10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
| 11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/common/cloud_print/cloud_print_constants.h" | 16 #include "chrome/common/cloud_print/cloud_print_constants.h" |
| 17 #include "chrome/common/cloud_print/cloud_print_helpers.h" | 17 #include "chrome/common/cloud_print/cloud_print_helpers.h" |
| 18 #include "chrome/service/cloud_print/cloud_print_helpers.h" | 18 #include "chrome/service/cloud_print/cloud_print_helpers.h" |
| 19 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
| 20 #include "net/base/mime_util.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 21 | 22 |
| 22 namespace cloud_print { | 23 namespace cloud_print { |
| 23 | 24 |
| 24 CloudPrintConnector::CloudPrintConnector(Client* client, | 25 CloudPrintConnector::CloudPrintConnector(Client* client, |
| 25 const ConnectorSettings& settings) | 26 const ConnectorSettings& settings) |
| 26 : client_(client), | 27 : client_(client), |
| 27 next_response_handler_(NULL) { | 28 next_response_handler_(NULL) { |
| 28 settings_.CopyFrom(settings); | 29 settings_.CopyFrom(settings); |
| 29 } | 30 } |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 } | 307 } |
| 307 | 308 |
| 308 void CloudPrintConnector::ReportUserMessage(const std::string& message_id, | 309 void CloudPrintConnector::ReportUserMessage(const std::string& message_id, |
| 309 const std::string& failure_msg) { | 310 const std::string& failure_msg) { |
| 310 // This is a fire and forget type of function. | 311 // This is a fire and forget type of function. |
| 311 // Result of this request will be ignored. | 312 // Result of this request will be ignored. |
| 312 std::string mime_boundary; | 313 std::string mime_boundary; |
| 313 CreateMimeBoundaryForUpload(&mime_boundary); | 314 CreateMimeBoundaryForUpload(&mime_boundary); |
| 314 GURL url = GetUrlForUserMessage(settings_.server_url(), message_id); | 315 GURL url = GetUrlForUserMessage(settings_.server_url(), message_id); |
| 315 std::string post_data; | 316 std::string post_data; |
| 316 AddMultipartValueForUpload(kMessageTextValue, failure_msg, mime_boundary, | 317 net::AddMultipartValueForUpload(kMessageTextValue, failure_msg, mime_boundary, |
| 317 std::string(), &post_data); | 318 std::string(), &post_data); |
| 318 // Terminate the request body | 319 net::AddMultipartTerminateForUpload(mime_boundary, &post_data); |
| 319 post_data.append("--" + mime_boundary + "--\r\n"); | |
| 320 std::string mime_type("multipart/form-data; boundary="); | 320 std::string mime_type("multipart/form-data; boundary="); |
| 321 mime_type += mime_boundary; | 321 mime_type += mime_boundary; |
| 322 user_message_request_ = CloudPrintURLFetcher::Create(); | 322 user_message_request_ = CloudPrintURLFetcher::Create(); |
| 323 user_message_request_->StartPostRequest(url, this, 1, mime_type, post_data, | 323 user_message_request_->StartPostRequest(url, this, 1, mime_type, post_data, |
| 324 std::string()); | 324 std::string()); |
| 325 } | 325 } |
| 326 | 326 |
| 327 bool CloudPrintConnector::RemovePrinterFromList( | 327 bool CloudPrintConnector::RemovePrinterFromList( |
| 328 const std::string& printer_name, | 328 const std::string& printer_name, |
| 329 printing::PrinterList* printer_list) { | 329 printing::PrinterList* printer_list) { |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 520 return; | 520 return; |
| 521 } | 521 } |
| 522 | 522 |
| 523 const printing::PrinterBasicInfo& info = pending_tasks_.front().printer_info; | 523 const printing::PrinterBasicInfo& info = pending_tasks_.front().printer_info; |
| 524 DCHECK(IsSamePrinter(info.printer_name, printer_name)); | 524 DCHECK(IsSamePrinter(info.printer_name, printer_name)); |
| 525 | 525 |
| 526 std::string mime_boundary; | 526 std::string mime_boundary; |
| 527 CreateMimeBoundaryForUpload(&mime_boundary); | 527 CreateMimeBoundaryForUpload(&mime_boundary); |
| 528 std::string post_data; | 528 std::string post_data; |
| 529 | 529 |
| 530 AddMultipartValueForUpload(kProxyIdValue, | 530 net::AddMultipartValueForUpload(kProxyIdValue, |
| 531 settings_.proxy_id(), mime_boundary, std::string(), &post_data); | 531 settings_.proxy_id(), mime_boundary, std::string(), &post_data); |
| 532 AddMultipartValueForUpload(kPrinterNameValue, | 532 net::AddMultipartValueForUpload(kPrinterNameValue, |
| 533 info.printer_name, mime_boundary, std::string(), &post_data); | 533 info.printer_name, mime_boundary, std::string(), &post_data); |
| 534 AddMultipartValueForUpload(kPrinterDescValue, | 534 net::AddMultipartValueForUpload(kPrinterDescValue, |
| 535 info.printer_description, mime_boundary, std::string(), &post_data); | 535 info.printer_description, mime_boundary, std::string(), &post_data); |
| 536 AddMultipartValueForUpload(kPrinterStatusValue, | 536 net::AddMultipartValueForUpload(kPrinterStatusValue, |
| 537 base::StringPrintf("%d", info.printer_status), | 537 base::StringPrintf("%d", info.printer_status), |
| 538 mime_boundary, std::string(), &post_data); | 538 mime_boundary, std::string(), &post_data); |
| 539 post_data += GetPostDataForPrinterInfo(info, mime_boundary); | 539 post_data += GetPostDataForPrinterInfo(info, mime_boundary); |
| 540 AddMultipartValueForUpload(kPrinterCapsValue, | 540 net::AddMultipartValueForUpload(kPrinterCapsValue, |
| 541 caps_and_defaults.printer_capabilities, mime_boundary, | 541 caps_and_defaults.printer_capabilities, mime_boundary, |
| 542 caps_and_defaults.caps_mime_type, &post_data); | 542 caps_and_defaults.caps_mime_type, &post_data); |
| 543 AddMultipartValueForUpload(kPrinterDefaultsValue, | 543 net::AddMultipartValueForUpload(kPrinterDefaultsValue, |
| 544 caps_and_defaults.printer_defaults, mime_boundary, | 544 caps_and_defaults.printer_defaults, mime_boundary, |
| 545 caps_and_defaults.defaults_mime_type, &post_data); | 545 caps_and_defaults.defaults_mime_type, &post_data); |
| 546 // Send a hash of the printer capabilities to the server. We will use this | 546 // Send a hash of the printer capabilities to the server. We will use this |
| 547 // later to check if the capabilities have changed | 547 // later to check if the capabilities have changed |
| 548 AddMultipartValueForUpload(kPrinterCapsHashValue, | 548 net::AddMultipartValueForUpload(kPrinterCapsHashValue, |
| 549 base::MD5String(caps_and_defaults.printer_capabilities), | 549 base::MD5String(caps_and_defaults.printer_capabilities), |
| 550 mime_boundary, std::string(), &post_data); | 550 mime_boundary, std::string(), &post_data); |
| 551 | 551 net::AddMultipartTerminateForUpload(mime_boundary, &post_data); |
| 552 // Terminate the request body | |
| 553 post_data.append("--" + mime_boundary + "--\r\n"); | |
| 554 std::string mime_type("multipart/form-data; boundary="); | 552 std::string mime_type("multipart/form-data; boundary="); |
| 555 mime_type += mime_boundary; | 553 mime_type += mime_boundary; |
| 556 | 554 |
| 557 GURL post_url = GetUrlForPrinterRegistration(settings_.server_url()); | 555 GURL post_url = GetUrlForPrinterRegistration(settings_.server_url()); |
| 558 StartPostRequest(post_url, | 556 StartPostRequest(post_url, |
| 559 kCloudPrintAPIMaxRetryCount, | 557 kCloudPrintAPIMaxRetryCount, |
| 560 mime_type, | 558 mime_type, |
| 561 post_data, | 559 post_data, |
| 562 &CloudPrintConnector::HandleRegisterPrinterResponse); | 560 &CloudPrintConnector::HandleRegisterPrinterResponse); |
| 563 } | 561 } |
| 564 | 562 |
| 565 bool CloudPrintConnector::IsSamePrinter(const std::string& name1, | 563 bool CloudPrintConnector::IsSamePrinter(const std::string& name1, |
| 566 const std::string& name2) const { | 564 const std::string& name2) const { |
| 567 return (0 == base::strcasecmp(name1.c_str(), name2.c_str())); | 565 return (0 == base::strcasecmp(name1.c_str(), name2.c_str())); |
| 568 } | 566 } |
| 569 | 567 |
| 570 } // namespace cloud_print | 568 } // namespace cloud_print |
| OLD | NEW |