| OLD | NEW |
| 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/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/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 12 #include "base/string_split.h" | 12 #include "base/string_split.h" |
| 13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.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_helpers.h" |
| 16 #include "chrome/service/cloud_print/cloud_print_consts.h" | 17 #include "chrome/service/cloud_print/cloud_print_consts.h" |
| 17 #include "chrome/service/cloud_print/cloud_print_helpers.h" | 18 #include "chrome/service/cloud_print/cloud_print_helpers.h" |
| 18 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
| 19 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
| 20 | 21 |
| 21 CloudPrintConnector::CloudPrintConnector( | 22 CloudPrintConnector::CloudPrintConnector( |
| 22 Client* client, | 23 Client* client, |
| 23 const std::string& proxy_id, | 24 const std::string& proxy_id, |
| 24 const GURL& cloud_print_server_url, | 25 const GURL& cloud_print_server_url, |
| 25 const DictionaryValue* print_system_settings) | 26 const DictionaryValue* print_system_settings) |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 if (message.empty()) | 171 if (message.empty()) |
| 171 message = l10n_util::GetStringFUTF8(IDS_CLOUD_PRINT_ENUM_FAILED, | 172 message = l10n_util::GetStringFUTF8(IDS_CLOUD_PRINT_ENUM_FAILED, |
| 172 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT)); | 173 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT)); |
| 173 // There was a failure enumerating printers. Send a message to the server. | 174 // There was a failure enumerating printers. Send a message to the server. |
| 174 ReportUserMessage(kEnumPrintersFailedMessageId, message); | 175 ReportUserMessage(kEnumPrintersFailedMessageId, message); |
| 175 } | 176 } |
| 176 | 177 |
| 177 // Go through the list of the cloud printers and init print job handlers. | 178 // Go through the list of the cloud printers and init print job handlers. |
| 178 ListValue* printer_list = NULL; | 179 ListValue* printer_list = NULL; |
| 179 // There may be no "printers" value in the JSON | 180 // There may be no "printers" value in the JSON |
| 180 if (json_data->GetList(kPrinterListValue, &printer_list) && printer_list) { | 181 if (json_data->GetList(cloud_print::kPrinterListValue, &printer_list) |
| 182 && printer_list) { |
| 181 for (size_t index = 0; index < printer_list->GetSize(); index++) { | 183 for (size_t index = 0; index < printer_list->GetSize(); index++) { |
| 182 DictionaryValue* printer_data = NULL; | 184 DictionaryValue* printer_data = NULL; |
| 183 if (printer_list->GetDictionary(index, &printer_data)) { | 185 if (printer_list->GetDictionary(index, &printer_data)) { |
| 184 std::string printer_name; | 186 std::string printer_name; |
| 185 printer_data->GetString(kNameValue, &printer_name); | 187 printer_data->GetString(kNameValue, &printer_name); |
| 186 if (RemovePrinterFromList(printer_name, &local_printers)) { | 188 if (RemovePrinterFromList(printer_name, &local_printers)) { |
| 187 InitJobHandlerForPrinter(printer_data); | 189 InitJobHandlerForPrinter(printer_data); |
| 188 } else { | 190 } else { |
| 189 // Cloud printer is not found on the local system. | 191 // Cloud printer is not found on the local system. |
| 190 if (full_list) { // Delete only if we get the full list of printer. | 192 if (full_list) { // Delete only if we get the full list of printer. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 CloudPrintConnector::HandleRegisterPrinterResponse( | 228 CloudPrintConnector::HandleRegisterPrinterResponse( |
| 227 const content::URLFetcher* source, | 229 const content::URLFetcher* source, |
| 228 const GURL& url, | 230 const GURL& url, |
| 229 DictionaryValue* json_data, | 231 DictionaryValue* json_data, |
| 230 bool succeeded) { | 232 bool succeeded) { |
| 231 VLOG(1) << "CP_CONNECTOR: Handler printer register response, succeeded:" | 233 VLOG(1) << "CP_CONNECTOR: Handler printer register response, succeeded:" |
| 232 << succeeded << " url: " << url; | 234 << succeeded << " url: " << url; |
| 233 if (succeeded) { | 235 if (succeeded) { |
| 234 ListValue* printer_list = NULL; | 236 ListValue* printer_list = NULL; |
| 235 // There should be a "printers" value in the JSON | 237 // There should be a "printers" value in the JSON |
| 236 if (json_data->GetList(kPrinterListValue, &printer_list)) { | 238 if (json_data->GetList(cloud_print::kPrinterListValue, &printer_list)) { |
| 237 DictionaryValue* printer_data = NULL; | 239 DictionaryValue* printer_data = NULL; |
| 238 if (printer_list->GetDictionary(0, &printer_data)) | 240 if (printer_list->GetDictionary(0, &printer_data)) |
| 239 InitJobHandlerForPrinter(printer_data); | 241 InitJobHandlerForPrinter(printer_data); |
| 240 } | 242 } |
| 241 } | 243 } |
| 242 ContinuePendingTaskProcessing(); // Continue processing background tasks. | 244 ContinuePendingTaskProcessing(); // Continue processing background tasks. |
| 243 return CloudPrintURLFetcher::STOP_PROCESSING; | 245 return CloudPrintURLFetcher::STOP_PROCESSING; |
| 244 } | 246 } |
| 245 | 247 |
| 246 | 248 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 270 request_ = new CloudPrintURLFetcher; | 272 request_ = new CloudPrintURLFetcher; |
| 271 request_->StartPostRequest( | 273 request_->StartPostRequest( |
| 272 url, this, max_retries, mime_type, post_data, std::string()); | 274 url, this, max_retries, mime_type, post_data, std::string()); |
| 273 } | 275 } |
| 274 | 276 |
| 275 void CloudPrintConnector::ReportUserMessage(const std::string& message_id, | 277 void CloudPrintConnector::ReportUserMessage(const std::string& message_id, |
| 276 const std::string& failure_msg) { | 278 const std::string& failure_msg) { |
| 277 // This is a fire and forget type of function. | 279 // This is a fire and forget type of function. |
| 278 // Result of this request will be ignored. | 280 // Result of this request will be ignored. |
| 279 std::string mime_boundary; | 281 std::string mime_boundary; |
| 280 CloudPrintHelpers::CreateMimeBoundaryForUpload(&mime_boundary); | 282 cloud_print::CreateMimeBoundaryForUpload(&mime_boundary); |
| 281 GURL url = CloudPrintHelpers::GetUrlForUserMessage(cloud_print_server_url_, | 283 GURL url = CloudPrintHelpers::GetUrlForUserMessage(cloud_print_server_url_, |
| 282 message_id); | 284 message_id); |
| 283 std::string post_data; | 285 std::string post_data; |
| 284 CloudPrintHelpers::AddMultipartValueForUpload(kMessageTextValue, | 286 cloud_print::AddMultipartValueForUpload(kMessageTextValue, failure_msg, |
| 285 failure_msg, | 287 mime_boundary, std::string(), &post_data); |
| 286 mime_boundary, | |
| 287 std::string(), | |
| 288 &post_data); | |
| 289 // Terminate the request body | 288 // Terminate the request body |
| 290 post_data.append("--" + mime_boundary + "--\r\n"); | 289 post_data.append("--" + mime_boundary + "--\r\n"); |
| 291 std::string mime_type("multipart/form-data; boundary="); | 290 std::string mime_type("multipart/form-data; boundary="); |
| 292 mime_type += mime_boundary; | 291 mime_type += mime_boundary; |
| 293 user_message_request_ = new CloudPrintURLFetcher; | 292 user_message_request_ = new CloudPrintURLFetcher; |
| 294 user_message_request_->StartPostRequest(url, this, 1, mime_type, post_data, | 293 user_message_request_->StartPostRequest(url, this, 1, mime_type, post_data, |
| 295 std::string()); | 294 std::string()); |
| 296 } | 295 } |
| 297 | 296 |
| 298 bool CloudPrintConnector::RemovePrinterFromList( | 297 bool CloudPrintConnector::RemovePrinterFromList( |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 489 ReportUserMessage(kGetPrinterCapsFailedMessageId, status_message); | 488 ReportUserMessage(kGetPrinterCapsFailedMessageId, status_message); |
| 490 | 489 |
| 491 ContinuePendingTaskProcessing(); // Skip this printer registration. | 490 ContinuePendingTaskProcessing(); // Skip this printer registration. |
| 492 return; | 491 return; |
| 493 } | 492 } |
| 494 | 493 |
| 495 const printing::PrinterBasicInfo& info = pending_tasks_.front().printer_info; | 494 const printing::PrinterBasicInfo& info = pending_tasks_.front().printer_info; |
| 496 DCHECK(IsSamePrinter(info.printer_name, printer_name)); | 495 DCHECK(IsSamePrinter(info.printer_name, printer_name)); |
| 497 | 496 |
| 498 std::string mime_boundary; | 497 std::string mime_boundary; |
| 499 CloudPrintHelpers::CreateMimeBoundaryForUpload(&mime_boundary); | 498 cloud_print::CreateMimeBoundaryForUpload(&mime_boundary); |
| 500 std::string post_data; | 499 std::string post_data; |
| 501 | 500 |
| 502 CloudPrintHelpers::AddMultipartValueForUpload(kProxyIdValue, proxy_id_, | 501 cloud_print::AddMultipartValueForUpload(kProxyIdValue, proxy_id_, |
| 503 mime_boundary, | 502 mime_boundary, std::string(), &post_data); |
| 504 std::string(), &post_data); | 503 cloud_print::AddMultipartValueForUpload(kPrinterNameValue, info.printer_name, |
| 505 CloudPrintHelpers::AddMultipartValueForUpload(kPrinterNameValue, | 504 mime_boundary, std::string(), &post_data); |
| 506 info.printer_name, | 505 cloud_print::AddMultipartValueForUpload(kPrinterDescValue, |
| 507 mime_boundary, | 506 info.printer_description, mime_boundary, std::string() , &post_data); |
| 508 std::string(), &post_data); | 507 cloud_print::AddMultipartValueForUpload(kPrinterStatusValue, |
| 509 CloudPrintHelpers::AddMultipartValueForUpload(kPrinterDescValue, | 508 base::StringPrintf("%d", info.printer_status), |
| 510 info.printer_description, | |
| 511 mime_boundary, | |
| 512 std::string() , &post_data); | |
| 513 CloudPrintHelpers::AddMultipartValueForUpload( | |
| 514 kPrinterStatusValue, base::StringPrintf("%d", info.printer_status), | |
| 515 mime_boundary, std::string(), &post_data); | 509 mime_boundary, std::string(), &post_data); |
| 516 // Add printer options as tags. | 510 // Add printer options as tags. |
| 517 CloudPrintHelpers::GenerateMultipartPostDataForPrinterTags(info.options, | 511 CloudPrintHelpers::GenerateMultipartPostDataForPrinterTags(info.options, |
| 518 mime_boundary, | 512 mime_boundary, |
| 519 &post_data); | 513 &post_data); |
| 520 | 514 |
| 521 CloudPrintHelpers::AddMultipartValueForUpload( | 515 cloud_print::AddMultipartValueForUpload(kPrinterCapsValue, |
| 522 kPrinterCapsValue, caps_and_defaults.printer_capabilities, | 516 caps_and_defaults.printer_capabilities, mime_boundary, |
| 523 mime_boundary, caps_and_defaults.caps_mime_type, | 517 caps_and_defaults.caps_mime_type, &post_data); |
| 524 &post_data); | 518 cloud_print::AddMultipartValueForUpload(kPrinterDefaultsValue, |
| 525 CloudPrintHelpers::AddMultipartValueForUpload( | 519 caps_and_defaults.printer_defaults, mime_boundary, |
| 526 kPrinterDefaultsValue, caps_and_defaults.printer_defaults, | 520 caps_and_defaults.defaults_mime_type, &post_data); |
| 527 mime_boundary, caps_and_defaults.defaults_mime_type, | |
| 528 &post_data); | |
| 529 // Send a hash of the printer capabilities to the server. We will use this | 521 // Send a hash of the printer capabilities to the server. We will use this |
| 530 // later to check if the capabilities have changed | 522 // later to check if the capabilities have changed |
| 531 CloudPrintHelpers::AddMultipartValueForUpload( | 523 cloud_print::AddMultipartValueForUpload(kPrinterCapsHashValue, |
| 532 kPrinterCapsHashValue, | |
| 533 base::MD5String(caps_and_defaults.printer_capabilities), | 524 base::MD5String(caps_and_defaults.printer_capabilities), |
| 534 mime_boundary, std::string(), &post_data); | 525 mime_boundary, std::string(), &post_data); |
| 535 | 526 |
| 536 // Terminate the request body | 527 // Terminate the request body |
| 537 post_data.append("--" + mime_boundary + "--\r\n"); | 528 post_data.append("--" + mime_boundary + "--\r\n"); |
| 538 std::string mime_type("multipart/form-data; boundary="); | 529 std::string mime_type("multipart/form-data; boundary="); |
| 539 mime_type += mime_boundary; | 530 mime_type += mime_boundary; |
| 540 | 531 |
| 541 GURL post_url = CloudPrintHelpers::GetUrlForPrinterRegistration( | 532 GURL post_url = CloudPrintHelpers::GetUrlForPrinterRegistration( |
| 542 cloud_print_server_url_); | 533 cloud_print_server_url_); |
| 543 StartPostRequest(post_url, | 534 StartPostRequest(post_url, |
| 544 kCloudPrintAPIMaxRetryCount, | 535 kCloudPrintAPIMaxRetryCount, |
| 545 mime_type, | 536 mime_type, |
| 546 post_data, | 537 post_data, |
| 547 &CloudPrintConnector::HandleRegisterPrinterResponse); | 538 &CloudPrintConnector::HandleRegisterPrinterResponse); |
| 548 } | 539 } |
| 549 | 540 |
| 550 bool CloudPrintConnector::IsSamePrinter(const std::string& name1, | 541 bool CloudPrintConnector::IsSamePrinter(const std::string& name1, |
| 551 const std::string& name2) const { | 542 const std::string& name2) const { |
| 552 return (0 == base::strcasecmp(name1.c_str(), name2.c_str())); | 543 return (0 == base::strcasecmp(name1.c_str(), name2.c_str())); |
| 553 } | 544 } |
| 554 | 545 |
| OLD | NEW |