| 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" |
| 8 #include "base/bind_helpers.h" |
| 7 #include "base/md5.h" | 9 #include "base/md5.h" |
| 8 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
| 9 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 10 #include "base/string_split.h" | 12 #include "base/string_split.h" |
| 11 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 12 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 13 #include "base/values.h" | 15 #include "base/values.h" |
| 14 #include "chrome/service/cloud_print/cloud_print_consts.h" | 16 #include "chrome/service/cloud_print/cloud_print_consts.h" |
| 15 #include "chrome/service/cloud_print/cloud_print_helpers.h" | 17 #include "chrome/service/cloud_print/cloud_print_helpers.h" |
| 16 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 task.type = PENDING_PRINTER_REGISTER; | 375 task.type = PENDING_PRINTER_REGISTER; |
| 374 task.printer_info = info; | 376 task.printer_info = info; |
| 375 AddPendingTask(task); | 377 AddPendingTask(task); |
| 376 } | 378 } |
| 377 | 379 |
| 378 void CloudPrintConnector::AddPendingTask(const PendingTask& task) { | 380 void CloudPrintConnector::AddPendingTask(const PendingTask& task) { |
| 379 pending_tasks_.push_back(task); | 381 pending_tasks_.push_back(task); |
| 380 // If this is the only pending task, we need to start the process. | 382 // If this is the only pending task, we need to start the process. |
| 381 if (pending_tasks_.size() == 1) { | 383 if (pending_tasks_.size() == 1) { |
| 382 MessageLoop::current()->PostTask( | 384 MessageLoop::current()->PostTask( |
| 383 FROM_HERE, NewRunnableMethod( | 385 FROM_HERE, base::Bind(&CloudPrintConnector::ProcessPendingTask, this)); |
| 384 this, &CloudPrintConnector::ProcessPendingTask)); | |
| 385 } | 386 } |
| 386 } | 387 } |
| 387 | 388 |
| 388 void CloudPrintConnector::ProcessPendingTask() { | 389 void CloudPrintConnector::ProcessPendingTask() { |
| 389 if (!IsRunning()) | 390 if (!IsRunning()) |
| 390 return; // Orphant call. | 391 return; // Orphant call. |
| 391 if (pending_tasks_.size() == 0) | 392 if (pending_tasks_.size() == 0) |
| 392 return; // No peding tasks. | 393 return; // No peding tasks. |
| 393 | 394 |
| 394 PendingTask task = pending_tasks_.front(); | 395 PendingTask task = pending_tasks_.front(); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 421 const printing::PrinterBasicInfo& info) { | 422 const printing::PrinterBasicInfo& info) { |
| 422 for (JobHandlerMap::iterator it = job_handler_map_.begin(); | 423 for (JobHandlerMap::iterator it = job_handler_map_.begin(); |
| 423 it != job_handler_map_.end(); ++it) { | 424 it != job_handler_map_.end(); ++it) { |
| 424 if (IsSamePrinter(it->second->GetPrinterName(), info.printer_name)) { | 425 if (IsSamePrinter(it->second->GetPrinterName(), info.printer_name)) { |
| 425 // Printer already registered, continue to the next task. | 426 // Printer already registered, continue to the next task. |
| 426 ContinuePendingTaskProcessing(); | 427 ContinuePendingTaskProcessing(); |
| 427 return; | 428 return; |
| 428 } | 429 } |
| 429 } | 430 } |
| 430 | 431 |
| 431 cloud_print::PrintSystem::PrinterCapsAndDefaultsCallback* callback = | 432 // Asynchronously fetch the printer caps and defaults. The story will |
| 432 NewCallback(this, &CloudPrintConnector::OnReceivePrinterCaps); | |
| 433 // Asnchronously fetch the printer caps and defaults. The story will | |
| 434 // continue in OnReceivePrinterCaps. | 433 // continue in OnReceivePrinterCaps. |
| 435 print_system_->GetPrinterCapsAndDefaults( | 434 print_system_->GetPrinterCapsAndDefaults( |
| 436 info.printer_name.c_str(), callback); | 435 info.printer_name.c_str(), |
| 436 base::Bind(&CloudPrintConnector::OnReceivePrinterCaps, |
| 437 base::Unretained(this))); |
| 437 } | 438 } |
| 438 | 439 |
| 439 void CloudPrintConnector::OnPrinterDelete(const std::string& printer_id) { | 440 void CloudPrintConnector::OnPrinterDelete(const std::string& printer_id) { |
| 440 // Remove corresponding printer job handler. | 441 // Remove corresponding printer job handler. |
| 441 JobHandlerMap::iterator it = job_handler_map_.find(printer_id); | 442 JobHandlerMap::iterator it = job_handler_map_.find(printer_id); |
| 442 if (it != job_handler_map_.end()) { | 443 if (it != job_handler_map_.end()) { |
| 443 it->second->Shutdown(); | 444 it->second->Shutdown(); |
| 444 job_handler_map_.erase(it); | 445 job_handler_map_.erase(it); |
| 445 } | 446 } |
| 446 | 447 |
| 447 // TODO(gene): We probably should not try indefinitely here. Just once or | 448 // TODO(gene): We probably should not try indefinitely here. Just once or |
| 448 // twice should be enough. | 449 // twice should be enough. |
| 449 // Bug: http://code.google.com/p/chromium/issues/detail?id=101850 | 450 // Bug: http://code.google.com/p/chromium/issues/detail?id=101850 |
| 450 GURL url = CloudPrintHelpers::GetUrlForPrinterDelete(cloud_print_server_url_, | 451 GURL url = CloudPrintHelpers::GetUrlForPrinterDelete(cloud_print_server_url_, |
| 451 printer_id); | 452 printer_id); |
| 452 StartGetRequest(url, | 453 StartGetRequest(url, |
| 453 kCloudPrintAPIMaxRetryCount, | 454 kCloudPrintAPIMaxRetryCount, |
| 454 &CloudPrintConnector::HandlePrinterDeleteResponse); | 455 &CloudPrintConnector::HandlePrinterDeleteResponse); |
| 455 } | 456 } |
| 456 | 457 |
| 457 void CloudPrintConnector::ContinuePendingTaskProcessing() { | 458 void CloudPrintConnector::ContinuePendingTaskProcessing() { |
| 458 if (pending_tasks_.size() == 0) | 459 if (pending_tasks_.size() == 0) |
| 459 return; // No peding tasks. | 460 return; // No pending tasks. |
| 460 | 461 |
| 461 // Delete current task and repost if we have more task avaialble. | 462 // Delete current task and repost if we have more task available. |
| 462 pending_tasks_.pop_front(); | 463 pending_tasks_.pop_front(); |
| 463 if (pending_tasks_.size() != 0) { | 464 if (pending_tasks_.size() != 0) { |
| 464 MessageLoop::current()->PostTask( | 465 MessageLoop::current()->PostTask( |
| 465 FROM_HERE, NewRunnableMethod( | 466 FROM_HERE, base::Bind(&CloudPrintConnector::ProcessPendingTask, this)); |
| 466 this, &CloudPrintConnector::ProcessPendingTask)); | |
| 467 } | 467 } |
| 468 } | 468 } |
| 469 | 469 |
| 470 void CloudPrintConnector::OnReceivePrinterCaps( | 470 void CloudPrintConnector::OnReceivePrinterCaps( |
| 471 bool succeeded, | 471 bool succeeded, |
| 472 const std::string& printer_name, | 472 const std::string& printer_name, |
| 473 const printing::PrinterCapsAndDefaults& caps_and_defaults) { | 473 const printing::PrinterCapsAndDefaults& caps_and_defaults) { |
| 474 if (!IsRunning()) | 474 if (!IsRunning()) |
| 475 return; // Orphant call. | 475 return; // Orphant call. |
| 476 DCHECK(pending_tasks_.size() > 0 && | 476 DCHECK(pending_tasks_.size() > 0 && |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 543 mime_type, | 543 mime_type, |
| 544 post_data, | 544 post_data, |
| 545 &CloudPrintConnector::HandleRegisterPrinterResponse); | 545 &CloudPrintConnector::HandleRegisterPrinterResponse); |
| 546 } | 546 } |
| 547 | 547 |
| 548 bool CloudPrintConnector::IsSamePrinter(const std::string& name1, | 548 bool CloudPrintConnector::IsSamePrinter(const std::string& name1, |
| 549 const std::string& name2) const { | 549 const std::string& name2) const { |
| 550 return (0 == base::strcasecmp(name1.c_str(), name2.c_str())); | 550 return (0 == base::strcasecmp(name1.c_str(), name2.c_str())); |
| 551 } | 551 } |
| 552 | 552 |
| OLD | NEW |