| 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/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_consts.h" |
| 16 #include "chrome/common/cloud_print/cloud_print_helpers.h" | 17 #include "chrome/common/cloud_print/cloud_print_helpers.h" |
| 17 #include "chrome/service/cloud_print/cloud_print_consts.h" | 18 #include "chrome/service/cloud_print/cloud_print_consts.h" |
| 18 #include "chrome/service/cloud_print/cloud_print_helpers.h" | 19 #include "chrome/service/cloud_print/cloud_print_helpers.h" |
| 19 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 21 | 22 |
| 22 CloudPrintConnector::CloudPrintConnector(Client* client, | 23 CloudPrintConnector::CloudPrintConnector(Client* client, |
| 23 const ConnectorSettings& settings) | 24 const ConnectorSettings& settings) |
| 24 : client_(client), | 25 : client_(client), |
| 25 next_response_handler_(NULL) { | 26 next_response_handler_(NULL) { |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 | 202 |
| 202 // Go through the list of the cloud printers and init print job handlers. | 203 // Go through the list of the cloud printers and init print job handlers. |
| 203 ListValue* printer_list = NULL; | 204 ListValue* printer_list = NULL; |
| 204 // There may be no "printers" value in the JSON | 205 // There may be no "printers" value in the JSON |
| 205 if (json_data->GetList(cloud_print::kPrinterListValue, &printer_list) | 206 if (json_data->GetList(cloud_print::kPrinterListValue, &printer_list) |
| 206 && printer_list) { | 207 && printer_list) { |
| 207 for (size_t index = 0; index < printer_list->GetSize(); index++) { | 208 for (size_t index = 0; index < printer_list->GetSize(); index++) { |
| 208 DictionaryValue* printer_data = NULL; | 209 DictionaryValue* printer_data = NULL; |
| 209 if (printer_list->GetDictionary(index, &printer_data)) { | 210 if (printer_list->GetDictionary(index, &printer_data)) { |
| 210 std::string printer_name; | 211 std::string printer_name; |
| 211 printer_data->GetString(kNameValue, &printer_name); | 212 printer_data->GetString(cloud_print::kNameValue, &printer_name); |
| 212 std::string printer_id; | 213 std::string printer_id; |
| 213 printer_data->GetString(kIdValue, &printer_id); | 214 printer_data->GetString(cloud_print::kIdValue, &printer_id); |
| 214 if (settings_.IsPrinterBlacklisted(printer_name)) { | 215 if (settings_.IsPrinterBlacklisted(printer_name)) { |
| 215 VLOG(1) << "CP_CONNECTOR: Deleting " << printer_name << | 216 VLOG(1) << "CP_CONNECTOR: Deleting " << printer_name << |
| 216 " id: " << printer_id << " as blacklisted"; | 217 " id: " << printer_id << " as blacklisted"; |
| 217 AddPendingDeleteTask(printer_id); | 218 AddPendingDeleteTask(printer_id); |
| 218 } else if (RemovePrinterFromList(printer_name, &local_printers)) { | 219 } else if (RemovePrinterFromList(printer_name, &local_printers)) { |
| 219 InitJobHandlerForPrinter(printer_data); | 220 InitJobHandlerForPrinter(printer_data); |
| 220 } else { | 221 } else { |
| 221 // Cloud printer is not found on the local system. | 222 // Cloud printer is not found on the local system. |
| 222 if (full_list || settings_.delete_on_enum_fail()) { | 223 if (full_list || settings_.delete_on_enum_fail()) { |
| 223 // Delete if we get the full list of printers or | 224 // Delete if we get the full list of printers or |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 request_->StartPostRequest( | 303 request_->StartPostRequest( |
| 303 url, this, max_retries, mime_type, post_data, std::string()); | 304 url, this, max_retries, mime_type, post_data, std::string()); |
| 304 } | 305 } |
| 305 | 306 |
| 306 void CloudPrintConnector::ReportUserMessage(const std::string& message_id, | 307 void CloudPrintConnector::ReportUserMessage(const std::string& message_id, |
| 307 const std::string& failure_msg) { | 308 const std::string& failure_msg) { |
| 308 // This is a fire and forget type of function. | 309 // This is a fire and forget type of function. |
| 309 // Result of this request will be ignored. | 310 // Result of this request will be ignored. |
| 310 std::string mime_boundary; | 311 std::string mime_boundary; |
| 311 cloud_print::CreateMimeBoundaryForUpload(&mime_boundary); | 312 cloud_print::CreateMimeBoundaryForUpload(&mime_boundary); |
| 312 GURL url = CloudPrintHelpers::GetUrlForUserMessage(settings_.server_url(), | 313 GURL url = cloud_print::GetUrlForUserMessage(settings_.server_url(), |
| 313 message_id); | 314 message_id); |
| 314 std::string post_data; | 315 std::string post_data; |
| 315 cloud_print::AddMultipartValueForUpload(kMessageTextValue, failure_msg, | 316 cloud_print::AddMultipartValueForUpload(cloud_print::kMessageTextValue, |
| 316 mime_boundary, std::string(), &post_data); | 317 failure_msg, mime_boundary, std::string(), &post_data); |
| 317 // Terminate the request body | 318 // Terminate the request body |
| 318 post_data.append("--" + mime_boundary + "--\r\n"); | 319 post_data.append("--" + mime_boundary + "--\r\n"); |
| 319 std::string mime_type("multipart/form-data; boundary="); | 320 std::string mime_type("multipart/form-data; boundary="); |
| 320 mime_type += mime_boundary; | 321 mime_type += mime_boundary; |
| 321 user_message_request_ = new CloudPrintURLFetcher; | 322 user_message_request_ = new CloudPrintURLFetcher; |
| 322 user_message_request_->StartPostRequest(url, this, 1, mime_type, post_data, | 323 user_message_request_->StartPostRequest(url, this, 1, mime_type, post_data, |
| 323 std::string()); | 324 std::string()); |
| 324 } | 325 } |
| 325 | 326 |
| 326 bool CloudPrintConnector::RemovePrinterFromList( | 327 bool CloudPrintConnector::RemovePrinterFromList( |
| 327 const std::string& printer_name, | 328 const std::string& printer_name, |
| 328 printing::PrinterList* printer_list) { | 329 printing::PrinterList* printer_list) { |
| 329 for (printing::PrinterList::iterator index = printer_list->begin(); | 330 for (printing::PrinterList::iterator index = printer_list->begin(); |
| 330 index != printer_list->end(); index++) { | 331 index != printer_list->end(); index++) { |
| 331 if (IsSamePrinter(index->printer_name, printer_name)) { | 332 if (IsSamePrinter(index->printer_name, printer_name)) { |
| 332 index = printer_list->erase(index); | 333 index = printer_list->erase(index); |
| 333 return true; | 334 return true; |
| 334 } | 335 } |
| 335 } | 336 } |
| 336 return false; | 337 return false; |
| 337 } | 338 } |
| 338 | 339 |
| 339 void CloudPrintConnector::InitJobHandlerForPrinter( | 340 void CloudPrintConnector::InitJobHandlerForPrinter( |
| 340 DictionaryValue* printer_data) { | 341 DictionaryValue* printer_data) { |
| 341 DCHECK(printer_data); | 342 DCHECK(printer_data); |
| 342 PrinterJobHandler::PrinterInfoFromCloud printer_info_cloud; | 343 PrinterJobHandler::PrinterInfoFromCloud printer_info_cloud; |
| 343 printer_data->GetString(kIdValue, &printer_info_cloud.printer_id); | 344 printer_data->GetString(cloud_print::kIdValue, |
| 345 &printer_info_cloud.printer_id); |
| 344 DCHECK(!printer_info_cloud.printer_id.empty()); | 346 DCHECK(!printer_info_cloud.printer_id.empty()); |
| 345 VLOG(1) << "CP_CONNECTOR: Init job handler" | 347 VLOG(1) << "CP_CONNECTOR: Init job handler" |
| 346 << ", printer id: " << printer_info_cloud.printer_id; | 348 << ", printer id: " << printer_info_cloud.printer_id; |
| 347 JobHandlerMap::iterator index = job_handler_map_.find( | 349 JobHandlerMap::iterator index = job_handler_map_.find( |
| 348 printer_info_cloud.printer_id); | 350 printer_info_cloud.printer_id); |
| 349 if (index != job_handler_map_.end()) | 351 if (index != job_handler_map_.end()) |
| 350 return; // Nothing to do if we already have a job handler for this printer. | 352 return; // Nothing to do if we already have a job handler for this printer. |
| 351 | 353 |
| 352 printing::PrinterBasicInfo printer_info; | 354 printing::PrinterBasicInfo printer_info; |
| 353 printer_data->GetString(kNameValue, &printer_info.printer_name); | 355 printer_data->GetString(cloud_print::kNameValue, &printer_info.printer_name); |
| 354 DCHECK(!printer_info.printer_name.empty()); | 356 DCHECK(!printer_info.printer_name.empty()); |
| 355 printer_data->GetString(kPrinterDescValue, | 357 printer_data->GetString(cloud_print::kPrinterDescValue, |
| 356 &printer_info.printer_description); | 358 &printer_info.printer_description); |
| 357 // Printer status is a string value which actually contains an integer. | 359 // Printer status is a string value which actually contains an integer. |
| 358 std::string printer_status; | 360 std::string printer_status; |
| 359 if (printer_data->GetString(kPrinterStatusValue, &printer_status)) { | 361 if (printer_data->GetString( |
| 362 cloud_print::kPrinterStatusValue, &printer_status)) { |
| 360 base::StringToInt(printer_status, &printer_info.printer_status); | 363 base::StringToInt(printer_status, &printer_info.printer_status); |
| 361 } | 364 } |
| 362 printer_data->GetString(kPrinterCapsHashValue, | 365 printer_data->GetString(cloud_print::kPrinterCapsHashValue, |
| 363 &printer_info_cloud.caps_hash); | 366 &printer_info_cloud.caps_hash); |
| 364 ListValue* tags_list = NULL; | 367 ListValue* tags_list = NULL; |
| 365 if (printer_data->GetList(kTagsValue, &tags_list) && tags_list) { | 368 if (printer_data->GetList(cloud_print::kTagsValue, &tags_list) && tags_list) { |
| 366 for (size_t index = 0; index < tags_list->GetSize(); index++) { | 369 for (size_t index = 0; index < tags_list->GetSize(); index++) { |
| 367 std::string tag; | 370 std::string tag; |
| 368 if (tags_list->GetString(index, &tag) && | 371 if (tags_list->GetString(index, &tag) && |
| 369 StartsWithASCII(tag, kTagsHashTagName, false)) { | 372 StartsWithASCII(tag, kTagsHashTagName, false)) { |
| 370 std::vector<std::string> tag_parts; | 373 std::vector<std::string> tag_parts; |
| 371 base::SplitStringDontTrim(tag, '=', &tag_parts); | 374 base::SplitStringDontTrim(tag, '=', &tag_parts); |
| 372 DCHECK_EQ(tag_parts.size(), 2U); | 375 DCHECK_EQ(tag_parts.size(), 2U); |
| 373 if (tag_parts.size() == 2) | 376 if (tag_parts.size() == 2) |
| 374 printer_info_cloud.tags_hash = tag_parts[1]; | 377 printer_info_cloud.tags_hash = tag_parts[1]; |
| 375 } | 378 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 | 447 |
| 445 // Delete current task and repost if we have more task available. | 448 // Delete current task and repost if we have more task available. |
| 446 pending_tasks_.pop_front(); | 449 pending_tasks_.pop_front(); |
| 447 if (pending_tasks_.size() != 0) { | 450 if (pending_tasks_.size() != 0) { |
| 448 MessageLoop::current()->PostTask( | 451 MessageLoop::current()->PostTask( |
| 449 FROM_HERE, base::Bind(&CloudPrintConnector::ProcessPendingTask, this)); | 452 FROM_HERE, base::Bind(&CloudPrintConnector::ProcessPendingTask, this)); |
| 450 } | 453 } |
| 451 } | 454 } |
| 452 | 455 |
| 453 void CloudPrintConnector::OnPrintersAvailable() { | 456 void CloudPrintConnector::OnPrintersAvailable() { |
| 454 GURL printer_list_url = CloudPrintHelpers::GetUrlForPrinterList( | 457 GURL printer_list_url = cloud_print::GetUrlForPrinterList( |
| 455 settings_.server_url(), settings_.proxy_id()); | 458 settings_.server_url(), settings_.proxy_id()); |
| 456 StartGetRequest(printer_list_url, | 459 StartGetRequest(printer_list_url, |
| 457 kCloudPrintRegisterMaxRetryCount, | 460 kCloudPrintRegisterMaxRetryCount, |
| 458 &CloudPrintConnector::HandlePrinterListResponse); | 461 &CloudPrintConnector::HandlePrinterListResponse); |
| 459 } | 462 } |
| 460 | 463 |
| 461 void CloudPrintConnector::OnPrinterRegister( | 464 void CloudPrintConnector::OnPrinterRegister( |
| 462 const printing::PrinterBasicInfo& info) { | 465 const printing::PrinterBasicInfo& info) { |
| 463 for (JobHandlerMap::iterator it = job_handler_map_.begin(); | 466 for (JobHandlerMap::iterator it = job_handler_map_.begin(); |
| 464 it != job_handler_map_.end(); ++it) { | 467 it != job_handler_map_.end(); ++it) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 481 // Remove corresponding printer job handler. | 484 // Remove corresponding printer job handler. |
| 482 JobHandlerMap::iterator it = job_handler_map_.find(printer_id); | 485 JobHandlerMap::iterator it = job_handler_map_.find(printer_id); |
| 483 if (it != job_handler_map_.end()) { | 486 if (it != job_handler_map_.end()) { |
| 484 it->second->Shutdown(); | 487 it->second->Shutdown(); |
| 485 job_handler_map_.erase(it); | 488 job_handler_map_.erase(it); |
| 486 } | 489 } |
| 487 | 490 |
| 488 // TODO(gene): We probably should not try indefinitely here. Just once or | 491 // TODO(gene): We probably should not try indefinitely here. Just once or |
| 489 // twice should be enough. | 492 // twice should be enough. |
| 490 // Bug: http://code.google.com/p/chromium/issues/detail?id=101850 | 493 // Bug: http://code.google.com/p/chromium/issues/detail?id=101850 |
| 491 GURL url = CloudPrintHelpers::GetUrlForPrinterDelete( | 494 GURL url = cloud_print::GetUrlForPrinterDelete( |
| 492 settings_.server_url(), printer_id, "printer_deleted"); | 495 settings_.server_url(), printer_id, "printer_deleted"); |
| 493 StartGetRequest(url, | 496 StartGetRequest(url, |
| 494 kCloudPrintAPIMaxRetryCount, | 497 kCloudPrintAPIMaxRetryCount, |
| 495 &CloudPrintConnector::HandlePrinterDeleteResponse); | 498 &CloudPrintConnector::HandlePrinterDeleteResponse); |
| 496 } | 499 } |
| 497 | 500 |
| 498 void CloudPrintConnector::OnReceivePrinterCaps( | 501 void CloudPrintConnector::OnReceivePrinterCaps( |
| 499 bool succeeded, | 502 bool succeeded, |
| 500 const std::string& printer_name, | 503 const std::string& printer_name, |
| 501 const printing::PrinterCapsAndDefaults& caps_and_defaults) { | 504 const printing::PrinterCapsAndDefaults& caps_and_defaults) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 519 return; | 522 return; |
| 520 } | 523 } |
| 521 | 524 |
| 522 const printing::PrinterBasicInfo& info = pending_tasks_.front().printer_info; | 525 const printing::PrinterBasicInfo& info = pending_tasks_.front().printer_info; |
| 523 DCHECK(IsSamePrinter(info.printer_name, printer_name)); | 526 DCHECK(IsSamePrinter(info.printer_name, printer_name)); |
| 524 | 527 |
| 525 std::string mime_boundary; | 528 std::string mime_boundary; |
| 526 cloud_print::CreateMimeBoundaryForUpload(&mime_boundary); | 529 cloud_print::CreateMimeBoundaryForUpload(&mime_boundary); |
| 527 std::string post_data; | 530 std::string post_data; |
| 528 | 531 |
| 529 cloud_print::AddMultipartValueForUpload(kProxyIdValue, settings_.proxy_id(), | 532 cloud_print::AddMultipartValueForUpload(cloud_print::kProxyIdValue, |
| 530 mime_boundary, std::string(), &post_data); | 533 settings_.proxy_id(), mime_boundary, std::string(), &post_data); |
| 531 cloud_print::AddMultipartValueForUpload(kPrinterNameValue, info.printer_name, | 534 cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterNameValue, |
| 532 mime_boundary, std::string(), &post_data); | 535 info.printer_name, mime_boundary, std::string(), &post_data); |
| 533 cloud_print::AddMultipartValueForUpload(kPrinterDescValue, | 536 cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterDescValue, |
| 534 info.printer_description, mime_boundary, std::string() , &post_data); | 537 info.printer_description, mime_boundary, std::string(), &post_data); |
| 535 cloud_print::AddMultipartValueForUpload(kPrinterStatusValue, | 538 cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterStatusValue, |
| 536 base::StringPrintf("%d", info.printer_status), | 539 base::StringPrintf("%d", info.printer_status), |
| 537 mime_boundary, std::string(), &post_data); | 540 mime_boundary, std::string(), &post_data); |
| 538 post_data += CloudPrintHelpers::GetPostDataForPrinterTags(info, | 541 post_data += CloudPrintHelpers::GetPostDataForPrinterTags(info, |
| 539 mime_boundary); | 542 mime_boundary); |
| 540 cloud_print::AddMultipartValueForUpload(kPrinterCapsValue, | 543 cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterCapsValue, |
| 541 caps_and_defaults.printer_capabilities, mime_boundary, | 544 caps_and_defaults.printer_capabilities, mime_boundary, |
| 542 caps_and_defaults.caps_mime_type, &post_data); | 545 caps_and_defaults.caps_mime_type, &post_data); |
| 543 cloud_print::AddMultipartValueForUpload(kPrinterDefaultsValue, | 546 cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterDefaultsValue, |
| 544 caps_and_defaults.printer_defaults, mime_boundary, | 547 caps_and_defaults.printer_defaults, mime_boundary, |
| 545 caps_and_defaults.defaults_mime_type, &post_data); | 548 caps_and_defaults.defaults_mime_type, &post_data); |
| 546 // Send a hash of the printer capabilities to the server. We will use this | 549 // Send a hash of the printer capabilities to the server. We will use this |
| 547 // later to check if the capabilities have changed | 550 // later to check if the capabilities have changed |
| 548 cloud_print::AddMultipartValueForUpload(kPrinterCapsHashValue, | 551 cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterCapsHashValue, |
| 549 base::MD5String(caps_and_defaults.printer_capabilities), | 552 base::MD5String(caps_and_defaults.printer_capabilities), |
| 550 mime_boundary, std::string(), &post_data); | 553 mime_boundary, std::string(), &post_data); |
| 551 | 554 |
| 552 // Terminate the request body | 555 // Terminate the request body |
| 553 post_data.append("--" + mime_boundary + "--\r\n"); | 556 post_data.append("--" + mime_boundary + "--\r\n"); |
| 554 std::string mime_type("multipart/form-data; boundary="); | 557 std::string mime_type("multipart/form-data; boundary="); |
| 555 mime_type += mime_boundary; | 558 mime_type += mime_boundary; |
| 556 | 559 |
| 557 GURL post_url = CloudPrintHelpers::GetUrlForPrinterRegistration( | 560 GURL post_url = cloud_print::GetUrlForPrinterRegistration( |
| 558 settings_.server_url()); | 561 settings_.server_url()); |
| 559 StartPostRequest(post_url, | 562 StartPostRequest(post_url, |
| 560 kCloudPrintAPIMaxRetryCount, | 563 kCloudPrintAPIMaxRetryCount, |
| 561 mime_type, | 564 mime_type, |
| 562 post_data, | 565 post_data, |
| 563 &CloudPrintConnector::HandleRegisterPrinterResponse); | 566 &CloudPrintConnector::HandleRegisterPrinterResponse); |
| 564 } | 567 } |
| 565 | 568 |
| 566 bool CloudPrintConnector::IsSamePrinter(const std::string& name1, | 569 bool CloudPrintConnector::IsSamePrinter(const std::string& name1, |
| 567 const std::string& name2) const { | 570 const std::string& name2) const { |
| 568 return (0 == base::strcasecmp(name1.c_str(), name2.c_str())); | 571 return (0 == base::strcasecmp(name1.c_str(), name2.c_str())); |
| 569 } | 572 } |
| OLD | NEW |