Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 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/browser/printing/printer_query.h" | 5 #include "chrome/browser/printing/printer_query.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/threading/thread_restrictions.h" | 8 #include "base/threading/thread_restrictions.h" |
| 9 #include "base/values.h" | |
| 9 #include "chrome/browser/printing/print_job_worker.h" | 10 #include "chrome/browser/printing/print_job_worker.h" |
| 10 | 11 |
| 11 namespace printing { | 12 namespace printing { |
| 12 | 13 |
| 13 PrinterQuery::PrinterQuery() | 14 PrinterQuery::PrinterQuery() |
| 14 : io_message_loop_(MessageLoop::current()), | 15 : io_message_loop_(MessageLoop::current()), |
| 15 ALLOW_THIS_IN_INITIALIZER_LIST(worker_(new PrintJobWorker(this))), | 16 ALLOW_THIS_IN_INITIALIZER_LIST(worker_(new PrintJobWorker(this))), |
| 16 is_print_dialog_box_shown_(false), | 17 is_print_dialog_box_shown_(false), |
| 17 cookie_(PrintSettings::NewCookie()), | 18 cookie_(PrintSettings::NewCookie()), |
| 18 last_status_(PrintingContext::FAILED) { | 19 last_status_(PrintingContext::FAILED) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 } | 72 } |
| 72 | 73 |
| 73 void PrinterQuery::GetSettings(GetSettingsAskParam ask_user_for_settings, | 74 void PrinterQuery::GetSettings(GetSettingsAskParam ask_user_for_settings, |
| 74 gfx::NativeView parent_view, | 75 gfx::NativeView parent_view, |
| 75 int expected_page_count, | 76 int expected_page_count, |
| 76 bool has_selection, | 77 bool has_selection, |
| 77 bool use_overlays, | 78 bool use_overlays, |
| 78 CancelableTask* callback) { | 79 CancelableTask* callback) { |
| 79 DCHECK_EQ(io_message_loop_, MessageLoop::current()); | 80 DCHECK_EQ(io_message_loop_, MessageLoop::current()); |
| 80 DCHECK(!is_print_dialog_box_shown_); | 81 DCHECK(!is_print_dialog_box_shown_); |
| 82 if (!StartWorker(callback)) | |
| 83 return; | |
| 84 | |
| 85 // Real work is done in PrintJobWorker::Init(). | |
| 86 is_print_dialog_box_shown_ = ask_user_for_settings == ASK_USER; | |
| 87 worker_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | |
| 88 worker_.get(), | |
| 89 &PrintJobWorker::GetSettings, | |
| 90 is_print_dialog_box_shown_, | |
| 91 parent_view, | |
| 92 expected_page_count, | |
| 93 has_selection, | |
| 94 use_overlays)); | |
| 95 } | |
| 96 | |
| 97 void PrinterQuery::SetSettings(const DictionaryValue& new_settings, | |
| 98 CancelableTask* callback) { | |
| 99 if (!StartWorker(callback)) | |
| 100 return; | |
| 101 | |
| 102 scoped_ptr<DictionaryValue> new_settings_copy(new_settings.DeepCopy()); | |
|
Lei Zhang
2011/03/03 00:22:44
Probably don't need the scoped_ptr if you are allo
kmadhusu
2011/03/04 19:39:02
Done.
| |
| 103 worker_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | |
| 104 worker_.get(), | |
| 105 &PrintJobWorker::SetSettings, | |
| 106 new_settings_copy.release())); | |
| 107 } | |
| 108 | |
| 109 bool PrinterQuery::StartWorker(CancelableTask* callback) { | |
| 81 DCHECK(!callback_.get()); | 110 DCHECK(!callback_.get()); |
| 82 DCHECK(worker_.get()); | 111 DCHECK(worker_.get()); |
| 83 if (!worker_.get()) | 112 if (!worker_.get()) |
| 84 return; | 113 return false; |
| 114 | |
| 85 // Lazy create the worker thread. There is one worker thread per print job. | 115 // Lazy create the worker thread. There is one worker thread per print job. |
| 86 if (!worker_->message_loop()) { | 116 if (!worker_->message_loop()) { |
| 87 if (!worker_->Start()) { | 117 if (!worker_->Start()) { |
| 88 if (callback) { | 118 if (callback) { |
| 89 callback->Cancel(); | 119 callback->Cancel(); |
| 90 delete callback; | 120 delete callback; |
| 91 } | 121 } |
| 92 NOTREACHED(); | 122 NOTREACHED(); |
| 93 return; | 123 return false; |
| 94 } | 124 } |
| 95 } | 125 } |
| 96 | |
| 97 callback_.reset(callback); | 126 callback_.reset(callback); |
| 98 // Real work is done in PrintJobWorker::Init(). | 127 return true; |
| 99 is_print_dialog_box_shown_ = ask_user_for_settings == ASK_USER; | |
| 100 worker_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | |
| 101 worker_.get(), | |
| 102 &PrintJobWorker::GetSettings, | |
| 103 is_print_dialog_box_shown_, | |
| 104 parent_view, | |
| 105 expected_page_count, | |
| 106 has_selection, | |
| 107 use_overlays)); | |
| 108 } | 128 } |
| 109 | 129 |
| 110 void PrinterQuery::StopWorker() { | 130 void PrinterQuery::StopWorker() { |
| 111 if (worker_.get()) { | 131 if (worker_.get()) { |
| 112 // http://crbug.com/66082: We're blocking on the PrinterQuery's worker | 132 // 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 | 133 // 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. | 134 // thread for an unacceptable time. We should probably fix it. |
| 115 base::ThreadRestrictions::ScopedAllowIO allow_io; | 135 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 116 worker_->Stop(); | 136 worker_->Stop(); |
| 117 worker_.reset(); | 137 worker_.reset(); |
| 118 } | 138 } |
| 119 } | 139 } |
| 120 | 140 |
| 121 bool PrinterQuery::is_callback_pending() const { | 141 bool PrinterQuery::is_callback_pending() const { |
| 122 return callback_.get() != NULL; | 142 return callback_.get() != NULL; |
| 123 } | 143 } |
| 124 | 144 |
| 125 bool PrinterQuery::is_valid() const { | 145 bool PrinterQuery::is_valid() const { |
| 126 return worker_.get() != NULL; | 146 return worker_.get() != NULL; |
| 127 } | 147 } |
| 128 | 148 |
| 129 } // namespace printing | 149 } // namespace printing |
| OLD | NEW |