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