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" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 DCHECK(printer_ids); | 83 DCHECK(printer_ids); |
84 printer_ids->clear(); | 84 printer_ids->clear(); |
85 for (JobHandlerMap::const_iterator iter = job_handler_map_.begin(); | 85 for (JobHandlerMap::const_iterator iter = job_handler_map_.begin(); |
86 iter != job_handler_map_.end(); ++iter) { | 86 iter != job_handler_map_.end(); ++iter) { |
87 printer_ids->push_back(iter->first); | 87 printer_ids->push_back(iter->first); |
88 } | 88 } |
89 } | 89 } |
90 | 90 |
91 void CloudPrintConnector::RegisterPrinters( | 91 void CloudPrintConnector::RegisterPrinters( |
92 const printing::PrinterList& printers) { | 92 const printing::PrinterList& printers) { |
93 if (!settings_.connect_new_printers() || !IsRunning()) | 93 if (!IsRunning()) |
94 return; | 94 return; |
95 printing::PrinterList::const_iterator it; | 95 printing::PrinterList::const_iterator it; |
96 for (it = printers.begin(); it != printers.end(); ++it) { | 96 for (it = printers.begin(); it != printers.end(); ++it) { |
97 if (!settings_.IsPrinterBlacklisted(it->printer_name)) | 97 if (settings_.ShouldConnect(it->printer_name)) |
98 AddPendingRegisterTask(*it); | 98 AddPendingRegisterTask(*it, settings_.GetDisplayName(it->printer_name)); |
99 } | 99 } |
100 } | 100 } |
101 | 101 |
102 // Check for jobs for specific printer | 102 // Check for jobs for specific printer |
103 void CloudPrintConnector::CheckForJobs(const std::string& reason, | 103 void CloudPrintConnector::CheckForJobs(const std::string& reason, |
104 const std::string& printer_id) { | 104 const std::string& printer_id) { |
105 if (!IsRunning()) | 105 if (!IsRunning()) |
106 return; | 106 return; |
107 if (!printer_id.empty()) { | 107 if (!printer_id.empty()) { |
108 JobHandlerMap::iterator index = job_handler_map_.find(printer_id); | 108 JobHandlerMap::iterator index = job_handler_map_.find(printer_id); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 ListValue* printer_list = NULL; | 205 ListValue* printer_list = NULL; |
206 // There may be no "printers" value in the JSON | 206 // There may be no "printers" value in the JSON |
207 if (json_data->GetList(kPrinterListValue, &printer_list) && printer_list) { | 207 if (json_data->GetList(kPrinterListValue, &printer_list) && printer_list) { |
208 for (size_t index = 0; index < printer_list->GetSize(); index++) { | 208 for (size_t index = 0; index < printer_list->GetSize(); index++) { |
209 DictionaryValue* printer_data = NULL; | 209 DictionaryValue* printer_data = NULL; |
210 if (printer_list->GetDictionary(index, &printer_data)) { | 210 if (printer_list->GetDictionary(index, &printer_data)) { |
211 std::string printer_name; | 211 std::string printer_name; |
212 printer_data->GetString(kNameValue, &printer_name); | 212 printer_data->GetString(kNameValue, &printer_name); |
213 std::string printer_id; | 213 std::string printer_id; |
214 printer_data->GetString(kIdValue, &printer_id); | 214 printer_data->GetString(kIdValue, &printer_id); |
215 if (settings_.IsPrinterBlacklisted(printer_name)) { | 215 |
| 216 if (!settings_.ShouldConnect(printer_name)) { |
216 VLOG(1) << "CP_CONNECTOR: Deleting " << printer_name << | 217 VLOG(1) << "CP_CONNECTOR: Deleting " << printer_name << |
217 " id: " << printer_id << " as blacklisted"; | 218 " id: " << printer_id << " as blacklisted"; |
218 AddPendingDeleteTask(printer_id); | 219 AddPendingDeleteTask(printer_id); |
219 } else if (RemovePrinterFromList(printer_name, &local_printers)) { | 220 } else if (RemovePrinterFromList(printer_name, &local_printers)) { |
220 InitJobHandlerForPrinter(printer_data); | 221 InitJobHandlerForPrinter(printer_data); |
221 } else { | 222 } else { |
222 // Cloud printer is not found on the local system. | 223 // Cloud printer is not found on the local system. |
223 if (full_list || settings_.delete_on_enum_fail()) { | 224 if (full_list || settings_.delete_on_enum_fail()) { |
224 // Delete if we get the full list of printers or | 225 // Delete if we get the full list of printers or |
225 // |delete_on_enum_fail_| is set. | 226 // |delete_on_enum_fail_| is set. |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 } | 393 } |
393 | 394 |
394 void CloudPrintConnector::AddPendingDeleteTask(const std::string& id) { | 395 void CloudPrintConnector::AddPendingDeleteTask(const std::string& id) { |
395 PendingTask task; | 396 PendingTask task; |
396 task.type = PENDING_PRINTER_DELETE; | 397 task.type = PENDING_PRINTER_DELETE; |
397 task.printer_id = id; | 398 task.printer_id = id; |
398 AddPendingTask(task); | 399 AddPendingTask(task); |
399 } | 400 } |
400 | 401 |
401 void CloudPrintConnector::AddPendingRegisterTask( | 402 void CloudPrintConnector::AddPendingRegisterTask( |
402 const printing::PrinterBasicInfo& info) { | 403 const printing::PrinterBasicInfo& info, |
| 404 const std::string& display_name) { |
403 PendingTask task; | 405 PendingTask task; |
404 task.type = PENDING_PRINTER_REGISTER; | 406 task.type = PENDING_PRINTER_REGISTER; |
405 task.printer_info = info; | 407 task.printer_info = info; |
| 408 task.display_name = display_name; |
406 AddPendingTask(task); | 409 AddPendingTask(task); |
407 } | 410 } |
408 | 411 |
409 void CloudPrintConnector::AddPendingTask(const PendingTask& task) { | 412 void CloudPrintConnector::AddPendingTask(const PendingTask& task) { |
410 pending_tasks_.push_back(task); | 413 pending_tasks_.push_back(task); |
411 // If this is the only pending task, we need to start the process. | 414 // If this is the only pending task, we need to start the process. |
412 if (pending_tasks_.size() == 1) { | 415 if (pending_tasks_.size() == 1) { |
413 MessageLoop::current()->PostTask( | 416 MessageLoop::current()->PostTask( |
414 FROM_HERE, base::Bind(&CloudPrintConnector::ProcessPendingTask, this)); | 417 FROM_HERE, base::Bind(&CloudPrintConnector::ProcessPendingTask, this)); |
415 } | 418 } |
416 } | 419 } |
417 | 420 |
418 void CloudPrintConnector::ProcessPendingTask() { | 421 void CloudPrintConnector::ProcessPendingTask() { |
419 if (!IsRunning()) | 422 if (!IsRunning()) |
420 return; // Orphant call. | 423 return; // Orphant call. |
421 if (pending_tasks_.size() == 0) | 424 if (pending_tasks_.size() == 0) |
422 return; // No peding tasks. | 425 return; // No peding tasks. |
423 | 426 |
424 PendingTask task = pending_tasks_.front(); | 427 PendingTask task = pending_tasks_.front(); |
425 | 428 |
426 switch (task.type) { | 429 switch (task.type) { |
427 case PENDING_PRINTERS_AVAILABLE : | 430 case PENDING_PRINTERS_AVAILABLE : |
428 OnPrintersAvailable(); | 431 OnPrintersAvailable(); |
429 break; | 432 break; |
430 case PENDING_PRINTER_REGISTER : | 433 case PENDING_PRINTER_REGISTER : |
431 OnPrinterRegister(task.printer_info); | 434 OnPrinterRegister(task.printer_info, task.display_name); |
432 break; | 435 break; |
433 case PENDING_PRINTER_DELETE : | 436 case PENDING_PRINTER_DELETE : |
434 OnPrinterDelete(task.printer_id); | 437 OnPrinterDelete(task.printer_id); |
435 break; | 438 break; |
436 default: | 439 default: |
437 NOTREACHED(); | 440 NOTREACHED(); |
438 } | 441 } |
439 } | 442 } |
440 | 443 |
441 void CloudPrintConnector::ContinuePendingTaskProcessing() { | 444 void CloudPrintConnector::ContinuePendingTaskProcessing() { |
(...skipping 10 matching lines...) Expand all Loading... |
452 | 455 |
453 void CloudPrintConnector::OnPrintersAvailable() { | 456 void CloudPrintConnector::OnPrintersAvailable() { |
454 GURL printer_list_url = GetUrlForPrinterList( | 457 GURL printer_list_url = 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, const std::string& display_name) { |
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) { |
465 if (IsSamePrinter(it->second->GetPrinterName(), info.printer_name)) { | 468 if (IsSamePrinter(it->second->GetPrinterName(), info.printer_name)) { |
466 // Printer already registered, continue to the next task. | 469 // Printer already registered, continue to the next task. |
467 ContinuePendingTaskProcessing(); | 470 ContinuePendingTaskProcessing(); |
468 return; | 471 return; |
469 } | 472 } |
470 } | 473 } |
471 | 474 |
472 // Asynchronously fetch the printer caps and defaults. The story will | 475 // Asynchronously fetch the printer caps and defaults. The story will |
473 // continue in OnReceivePrinterCaps. | 476 // continue in OnReceivePrinterCaps. |
474 print_system_->GetPrinterCapsAndDefaults( | 477 print_system_->GetPrinterCapsAndDefaults( |
475 info.printer_name.c_str(), | 478 info.printer_name.c_str(), |
476 base::Bind(&CloudPrintConnector::OnReceivePrinterCaps, | 479 base::Bind(&CloudPrintConnector::OnReceivePrinterCaps, |
477 base::Unretained(this))); | 480 base::Unretained(this), display_name)); |
478 } | 481 } |
479 | 482 |
480 void CloudPrintConnector::OnPrinterDelete(const std::string& printer_id) { | 483 void CloudPrintConnector::OnPrinterDelete(const std::string& printer_id) { |
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 = GetUrlForPrinterDelete( | 494 GURL url = 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( |
| 502 const std::string& display_name, |
499 bool succeeded, | 503 bool succeeded, |
500 const std::string& printer_name, | 504 const std::string& printer_name, |
501 const printing::PrinterCapsAndDefaults& caps_and_defaults) { | 505 const printing::PrinterCapsAndDefaults& caps_and_defaults) { |
502 if (!IsRunning()) | 506 if (!IsRunning()) |
503 return; // Orphant call. | 507 return; // Orphant call. |
504 DCHECK(pending_tasks_.size() > 0 && | 508 DCHECK(pending_tasks_.size() > 0 && |
505 pending_tasks_.front().type == PENDING_PRINTER_REGISTER); | 509 pending_tasks_.front().type == PENDING_PRINTER_REGISTER); |
506 | 510 |
507 if (!succeeded) { | 511 if (!succeeded) { |
508 LOG(ERROR) << "CP_CONNECTOR: Failed to get printer info" | 512 LOG(ERROR) << "CP_CONNECTOR: Failed to get printer info" |
(...skipping 14 matching lines...) Expand all Loading... |
523 DCHECK(IsSamePrinter(info.printer_name, printer_name)); | 527 DCHECK(IsSamePrinter(info.printer_name, printer_name)); |
524 | 528 |
525 std::string mime_boundary; | 529 std::string mime_boundary; |
526 CreateMimeBoundaryForUpload(&mime_boundary); | 530 CreateMimeBoundaryForUpload(&mime_boundary); |
527 std::string post_data; | 531 std::string post_data; |
528 | 532 |
529 AddMultipartValueForUpload(kProxyIdValue, | 533 AddMultipartValueForUpload(kProxyIdValue, |
530 settings_.proxy_id(), mime_boundary, std::string(), &post_data); | 534 settings_.proxy_id(), mime_boundary, std::string(), &post_data); |
531 AddMultipartValueForUpload(kPrinterNameValue, | 535 AddMultipartValueForUpload(kPrinterNameValue, |
532 info.printer_name, mime_boundary, std::string(), &post_data); | 536 info.printer_name, mime_boundary, std::string(), &post_data); |
| 537 if (!display_name.empty()) { |
| 538 AddMultipartValueForUpload(kPrinterDisplayNameValue, |
| 539 display_name, mime_boundary, std::string(), &post_data); |
| 540 } |
533 AddMultipartValueForUpload(kPrinterDescValue, | 541 AddMultipartValueForUpload(kPrinterDescValue, |
534 info.printer_description, mime_boundary, std::string(), &post_data); | 542 info.printer_description, mime_boundary, std::string(), &post_data); |
535 AddMultipartValueForUpload(kPrinterStatusValue, | 543 AddMultipartValueForUpload(kPrinterStatusValue, |
536 base::StringPrintf("%d", info.printer_status), | 544 base::StringPrintf("%d", info.printer_status), |
537 mime_boundary, std::string(), &post_data); | 545 mime_boundary, std::string(), &post_data); |
538 post_data += GetPostDataForPrinterInfo(info, mime_boundary); | 546 post_data += GetPostDataForPrinterInfo(info, mime_boundary); |
539 AddMultipartValueForUpload(kPrinterCapsValue, | 547 AddMultipartValueForUpload(kPrinterCapsValue, |
540 caps_and_defaults.printer_capabilities, mime_boundary, | 548 caps_and_defaults.printer_capabilities, mime_boundary, |
541 caps_and_defaults.caps_mime_type, &post_data); | 549 caps_and_defaults.caps_mime_type, &post_data); |
542 AddMultipartValueForUpload(kPrinterDefaultsValue, | 550 AddMultipartValueForUpload(kPrinterDefaultsValue, |
(...skipping 17 matching lines...) Expand all Loading... |
560 post_data, | 568 post_data, |
561 &CloudPrintConnector::HandleRegisterPrinterResponse); | 569 &CloudPrintConnector::HandleRegisterPrinterResponse); |
562 } | 570 } |
563 | 571 |
564 bool CloudPrintConnector::IsSamePrinter(const std::string& name1, | 572 bool CloudPrintConnector::IsSamePrinter(const std::string& name1, |
565 const std::string& name2) const { | 573 const std::string& name2) const { |
566 return (0 == base::strcasecmp(name1.c_str(), name2.c_str())); | 574 return (0 == base::strcasecmp(name1.c_str(), name2.c_str())); |
567 } | 575 } |
568 | 576 |
569 } // namespace cloud_print | 577 } // namespace cloud_print |
OLD | NEW |