| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/browser/printing/printer_query.h" | 5 #include "chrome/browser/printing/printer_query.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" |
| 7 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 8 #include "chrome/browser/printing/print_job_worker.h" | 9 #include "chrome/browser/printing/print_job_worker.h" |
| 9 | 10 |
| 10 #ifdef _MSC_VER | |
| 11 #pragma warning(disable:4355) // 'this' : used in base member initializer list | |
| 12 #endif | |
| 13 | |
| 14 namespace printing { | 11 namespace printing { |
| 15 | 12 |
| 16 PrinterQuery::PrinterQuery() | 13 PrinterQuery::PrinterQuery(MessageLoop* ui_message_loop) |
| 17 : ui_message_loop_(MessageLoop::current()), | 14 : ui_message_loop_(ui_message_loop), |
| 18 worker_(new PrintJobWorker(this)), | 15 message_loop_(MessageLoop::current()), |
| 16 ALLOW_THIS_IN_INITIALIZER_LIST(worker_(new PrintJobWorker(this))), |
| 19 is_print_dialog_box_shown_(false), | 17 is_print_dialog_box_shown_(false), |
| 20 last_status_(PrintingContext::FAILED), | 18 last_status_(PrintingContext::FAILED), |
| 21 cookie_(PrintSettings::NewCookie()) { | 19 cookie_(PrintSettings::NewCookie()) { |
| 20 DCHECK(ui_message_loop_); |
| 22 } | 21 } |
| 23 | 22 |
| 24 PrinterQuery::~PrinterQuery() { | 23 PrinterQuery::~PrinterQuery() { |
| 25 // The job should be finished (or at least canceled) when it is destroyed. | 24 // The job should be finished (or at least canceled) when it is destroyed. |
| 26 DCHECK(!is_print_dialog_box_shown_); | 25 DCHECK(!is_print_dialog_box_shown_); |
| 27 // If this fires, it is that this pending printer context has leaked. | 26 // If this fires, it is that this pending printer context has leaked. |
| 28 DCHECK(!worker_.get()); | 27 DCHECK(!worker_.get()); |
| 29 if (callback_.get()) { | 28 if (callback_.get()) { |
| 30 // Be sure to cancel it. | 29 // Be sure to cancel it. |
| 31 callback_->Cancel(); | 30 callback_->Cancel(); |
| 32 } | 31 } |
| 33 // It may get deleted in a different thread that the one that created it. | 32 // It may get deleted in a different thread that the one that created it. |
| 34 // That's fine so don't DCHECK_EQ(ui_message_loop_, MessageLoop::current()); | 33 // That's fine so don't DCHECK_EQ(message_loop_, MessageLoop::current()); |
| 35 } | 34 } |
| 36 | 35 |
| 37 void PrinterQuery::GetSettingsDone(const PrintSettings& new_settings, | 36 void PrinterQuery::GetSettingsDone(const PrintSettings& new_settings, |
| 38 PrintingContext::Result result) { | 37 PrintingContext::Result result) { |
| 39 is_print_dialog_box_shown_ = false; | 38 is_print_dialog_box_shown_ = false; |
| 40 last_status_ = result; | 39 last_status_ = result; |
| 41 if (result != PrintingContext::FAILED) { | 40 if (result != PrintingContext::FAILED) { |
| 42 settings_ = new_settings; | 41 settings_ = new_settings; |
| 43 cookie_ = PrintSettings::NewCookie(); | 42 cookie_ = PrintSettings::NewCookie(); |
| 44 } else { | 43 } else { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 58 if (!worker_.get()) | 57 if (!worker_.get()) |
| 59 return NULL; | 58 return NULL; |
| 60 worker_->SetNewOwner(new_owner); | 59 worker_->SetNewOwner(new_owner); |
| 61 return worker_.release(); | 60 return worker_.release(); |
| 62 } | 61 } |
| 63 | 62 |
| 64 void PrinterQuery::GetSettings(GetSettingsAskParam ask_user_for_settings, | 63 void PrinterQuery::GetSettings(GetSettingsAskParam ask_user_for_settings, |
| 65 HWND parent_window, | 64 HWND parent_window, |
| 66 int expected_page_count, | 65 int expected_page_count, |
| 67 CancelableTask* callback) { | 66 CancelableTask* callback) { |
| 68 DCHECK_EQ(ui_message_loop_, MessageLoop::current()); | 67 DCHECK_EQ(message_loop_, MessageLoop::current()); |
| 69 DCHECK(!is_print_dialog_box_shown_); | 68 DCHECK(!is_print_dialog_box_shown_); |
| 70 DCHECK(!callback_.get()); | 69 DCHECK(!callback_.get()); |
| 71 DCHECK(worker_.get()); | 70 DCHECK(worker_.get()); |
| 72 if (!worker_.get()) | 71 if (!worker_.get()) |
| 73 return; | 72 return; |
| 74 // Lazy create the worker thread. There is one worker thread per print job. | 73 |
| 75 if (!worker_->message_loop()) { | 74 MessageLoop* message_loop; |
| 76 if (!worker_->Start()) { | 75 if (ask_user_for_settings == DEFAULTS) { |
| 77 if (callback) { | 76 // Lazy create the worker thread. There is one worker thread per print job. |
| 78 callback->Cancel(); | 77 if (!worker_->message_loop()) { |
| 79 delete callback; | 78 if (!worker_->Start()) { |
| 79 if (callback) { |
| 80 callback->Cancel(); |
| 81 delete callback; |
| 82 } |
| 83 callback_.reset(NULL); |
| 84 NOTREACHED(); |
| 85 return; |
| 80 } | 86 } |
| 81 NOTREACHED(); | |
| 82 return; | |
| 83 } | 87 } |
| 88 message_loop = worker_->message_loop(); |
| 89 } else if (ask_user_for_settings == ASK_USER) { |
| 90 // No need for a worker thread in that case. |
| 91 message_loop = ui_message_loop_; |
| 92 } else { |
| 93 callback_.reset(NULL); |
| 94 NOTREACHED(); |
| 95 return; |
| 84 } | 96 } |
| 85 | 97 DCHECK(message_loop); |
| 98 is_print_dialog_box_shown_ = ask_user_for_settings == ASK_USER; |
| 86 callback_.reset(callback); | 99 callback_.reset(callback); |
| 87 // Real work is done in PrintJobWorker::Init(). | 100 // Real work is done in PrintJobWorker::Init(). |
| 88 is_print_dialog_box_shown_ = ask_user_for_settings == ASK_USER; | 101 message_loop->PostTask(FROM_HERE, NewRunnableMethod( |
| 89 worker_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | |
| 90 worker_.get(), | 102 worker_.get(), |
| 91 &PrintJobWorker::GetSettings, | 103 &PrintJobWorker::GetSettings, |
| 92 is_print_dialog_box_shown_, | 104 is_print_dialog_box_shown_, |
| 93 parent_window, | 105 parent_window, |
| 94 expected_page_count)); | 106 expected_page_count)); |
| 95 } | 107 } |
| 96 | 108 |
| 97 void PrinterQuery::StopWorker() { | 109 void PrinterQuery::StopWorker() { |
| 98 if (worker_.get()) { | 110 if (worker_.get()) { |
| 99 worker_->Stop(); | 111 worker_->Stop(); |
| 100 worker_.reset(); | 112 worker_.reset(); |
| 101 } | 113 } |
| 102 } | 114 } |
| 103 | 115 |
| 104 bool PrinterQuery::is_print_dialog_box_shown() const { | 116 bool PrinterQuery::is_print_dialog_box_shown() const { |
| 105 return is_print_dialog_box_shown_; | 117 return is_print_dialog_box_shown_; |
| 106 } | 118 } |
| 107 | 119 |
| 108 bool PrinterQuery::is_callback_pending() const { | 120 bool PrinterQuery::is_callback_pending() const { |
| 109 return callback_.get() != NULL; | 121 return callback_.get() != NULL; |
| 110 } | 122 } |
| 111 | 123 |
| 112 bool PrinterQuery::is_valid() const { | 124 bool PrinterQuery::is_valid() const { |
| 113 return worker_.get() != NULL; | 125 return worker_.get() != NULL; |
| 114 } | 126 } |
| 115 | 127 |
| 128 |
| 129 DefaultSettingsPrinterQueryTask::DefaultSettingsPrinterQueryTask( |
| 130 MessageLoop* reply_message_loop) |
| 131 : reply_task_(new ReplyTask()), |
| 132 reply_message_loop_(reply_message_loop) { |
| 133 if (!reply_message_loop_) |
| 134 reply_message_loop_ = MessageLoop::current(); |
| 135 } |
| 136 |
| 137 void DefaultSettingsPrinterQueryTask::Run() { |
| 138 DCHECK(reply_task_); |
| 139 // Run the lengthy operation: |
| 140 reply_task_->result_.Run(); |
| 141 |
| 142 // Post back the result. |
| 143 reply_message_loop_->PostTask(FROM_HERE, reply_task_); |
| 144 // |this| will be deleted upon return. |
| 145 } |
| 146 |
| 147 void DefaultSettingsPrinterQueryTask::Cancel() { |
| 148 reply_task_->Cancel(); |
| 149 // |this| will be deleted upon return. |
| 150 } |
| 151 |
| 152 DefaultSettingsPrinterQueryTask::Result::Result() |
| 153 : result(PrintingContext::FAILED) { |
| 154 } |
| 155 |
| 156 void DefaultSettingsPrinterQueryTask::Result::Run() { |
| 157 result = printing_context.UseDefaultSettings(); |
| 158 } |
| 159 |
| 160 DefaultSettingsPrinterQueryTask::ReplyTask::ReplyTask() { |
| 161 } |
| 162 |
| 163 void DefaultSettingsPrinterQueryTask::ReplyTask::Run() { |
| 164 DCHECK(reply_task_.get()); |
| 165 reply_task_->Run(); |
| 166 reply_task_.reset(NULL); |
| 167 // |this| will be deleted upon return. |
| 168 } |
| 169 |
| 170 void DefaultSettingsPrinterQueryTask::ReplyTask::Cancel() { |
| 171 DCHECK(reply_task_.get()); |
| 172 reply_task_->Cancel(); |
| 173 reply_task_.reset(NULL); |
| 174 // |this| will be deleted upon return. |
| 175 } |
| 176 |
| 116 } // namespace printing | 177 } // namespace printing |
| OLD | NEW |