printer_query.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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
  3. // found in the LICENSE file.
  4. #include "chrome/browser/printing/printer_query.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/bind_helpers.h"
  9. #include "base/message_loop/message_loop.h"
  10. #include "base/threading/thread_restrictions.h"
  11. #include "base/values.h"
  12. #include "chrome/browser/printing/print_job_worker.h"
  13. namespace printing {
  14. PrinterQuery::PrinterQuery(int render_process_id, int render_frame_id)
  15. : worker_(new PrintJobWorker(render_process_id, render_frame_id, this)),
  16. is_print_dialog_box_shown_(false),
  17. cookie_(PrintSettings::NewCookie()),
  18. last_status_(PrintingContext::FAILED) {
  19. DCHECK(base::MessageLoopForIO::IsCurrent());
  20. }
  21. PrinterQuery::~PrinterQuery() {
  22. // The job should be finished (or at least canceled) when it is destroyed.
  23. DCHECK(!is_print_dialog_box_shown_);
  24. // If this fires, it is that this pending printer context has leaked.
  25. DCHECK(!worker_);
  26. }
  27. void PrinterQuery::GetSettingsDone(const PrintSettings& new_settings,
  28. PrintingContext::Result result) {
  29. is_print_dialog_box_shown_ = false;
  30. last_status_ = result;
  31. if (result != PrintingContext::FAILED) {
  32. settings_ = new_settings;
  33. cookie_ = PrintSettings::NewCookie();
  34. } else {
  35. // Failure.
  36. cookie_ = 0;
  37. }
  38. if (!callback_.is_null()) {
  39. // This may cause reentrancy like to call StopWorker().
  40. callback_.Run();
  41. callback_.Reset();
  42. }
  43. }
  44. std::unique_ptr<PrintJobWorker> PrinterQuery::DetachWorker(
  45. PrintJobWorkerOwner* new_owner) {
  46. DCHECK(callback_.is_null());
  47. DCHECK(worker_);
  48. worker_->SetNewOwner(new_owner);
  49. return std::move(worker_);
  50. }
  51. const PrintSettings& PrinterQuery::settings() const {
  52. return settings_;
  53. }
  54. int PrinterQuery::cookie() const {
  55. return cookie_;
  56. }
  57. void PrinterQuery::GetSettings(GetSettingsAskParam ask_user_for_settings,
  58. int expected_page_count,
  59. bool has_selection,
  60. MarginType margin_type,
  61. bool is_scripted,
  62. bool is_modifiable,
  63. const base::Closure& callback) {
  64. DCHECK(RunsTasksInCurrentSequence());
  65. DCHECK(!is_print_dialog_box_shown_ || !is_scripted);
  66. StartWorker(callback);
  67. // Real work is done in PrintJobWorker::GetSettings().
  68. is_print_dialog_box_shown_ =
  69. ask_user_for_settings == GetSettingsAskParam::ASK_USER;
  70. worker_->PostTask(
  71. FROM_HERE,
  72. base::Bind(&PrintJobWorker::GetSettings, base::Unretained(worker_.get()),
  73. is_print_dialog_box_shown_, expected_page_count, has_selection,
  74. margin_type, is_scripted, is_modifiable, base::string16()));
  75. }
  76. void PrinterQuery::GetSettings(GetSettingsAskParam ask_user_for_settings,
  77. int expected_page_count,
  78. bool has_selection,
  79. MarginType margin_type,
  80. bool is_scripted,
  81. bool is_modifiable,
  82. const base::string16& device_name,
  83. const base::Closure& callback) {
  84. DCHECK(RunsTasksInCurrentSequence());
  85. DCHECK(!is_print_dialog_box_shown_);
  86. StartWorker(callback);
  87. is_print_dialog_box_shown_ = false;
  88. worker_->PostTask(
  89. FROM_HERE,
  90. base::Bind(&PrintJobWorker::GetSettings, base::Unretained(worker_.get()),
  91. is_print_dialog_box_shown_, expected_page_count, has_selection,
  92. margin_type, is_scripted, is_modifiable, device_name));
  93. }
  94. void PrinterQuery::SetSettings(
  95. std::unique_ptr<base::DictionaryValue> new_settings,
  96. const base::Closure& callback) {
  97. StartWorker(callback);
  98. worker_->PostTask(FROM_HERE, base::Bind(&PrintJobWorker::SetSettings,
  99. base::Unretained(worker_.get()),
  100. base::Passed(&new_settings)));
  101. }
  102. void PrinterQuery::StartWorker(const base::Closure& callback) {
  103. DCHECK(callback_.is_null());
  104. DCHECK(worker_);
  105. // Lazily create the worker thread. There is one worker thread per print job.
  106. if (!worker_->IsRunning())
  107. worker_->Start();
  108. callback_ = callback;
  109. }
  110. void PrinterQuery::StopWorker() {
  111. if (worker_) {
  112. // http://crbug.com/66082: We're blocking on the PrinterQuery's worker
  113. // thread. It's not clear to me if this may result in blocking the current
  114. // thread for an unacceptable time. We should probably fix it.
  115. base::ThreadRestrictions::ScopedAllowIO allow_io;
  116. worker_->Stop();
  117. worker_.reset();
  118. }
  119. }
  120. bool PrinterQuery::is_callback_pending() const {
  121. return !callback_.is_null();
  122. }
  123. bool PrinterQuery::is_valid() const {
  124. return !!worker_;
  125. }
  126. } // namespace printing