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 worker_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 103 worker_.get(), |
| 104 &PrintJobWorker::SetSettings, |
| 105 new_settings.DeepCopy())); |
| 106 } |
| 107 |
| 108 bool PrinterQuery::StartWorker(CancelableTask* callback) { |
81 DCHECK(!callback_.get()); | 109 DCHECK(!callback_.get()); |
82 DCHECK(worker_.get()); | 110 DCHECK(worker_.get()); |
83 if (!worker_.get()) | 111 if (!worker_.get()) |
84 return; | 112 return false; |
| 113 |
85 // Lazy create the worker thread. There is one worker thread per print job. | 114 // Lazy create the worker thread. There is one worker thread per print job. |
86 if (!worker_->message_loop()) { | 115 if (!worker_->message_loop()) { |
87 if (!worker_->Start()) { | 116 if (!worker_->Start()) { |
88 if (callback) { | 117 if (callback) { |
89 callback->Cancel(); | 118 callback->Cancel(); |
90 delete callback; | 119 delete callback; |
91 } | 120 } |
92 NOTREACHED(); | 121 NOTREACHED(); |
93 return; | 122 return false; |
94 } | 123 } |
95 } | 124 } |
96 | |
97 callback_.reset(callback); | 125 callback_.reset(callback); |
98 // Real work is done in PrintJobWorker::Init(). | 126 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 } | 127 } |
109 | 128 |
110 void PrinterQuery::StopWorker() { | 129 void PrinterQuery::StopWorker() { |
111 if (worker_.get()) { | 130 if (worker_.get()) { |
112 // http://crbug.com/66082: We're blocking on the PrinterQuery's worker | 131 // 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 | 132 // 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. | 133 // thread for an unacceptable time. We should probably fix it. |
115 base::ThreadRestrictions::ScopedAllowIO allow_io; | 134 base::ThreadRestrictions::ScopedAllowIO allow_io; |
116 worker_->Stop(); | 135 worker_->Stop(); |
117 worker_.reset(); | 136 worker_.reset(); |
118 } | 137 } |
119 } | 138 } |
120 | 139 |
121 bool PrinterQuery::is_callback_pending() const { | 140 bool PrinterQuery::is_callback_pending() const { |
122 return callback_.get() != NULL; | 141 return callback_.get() != NULL; |
123 } | 142 } |
124 | 143 |
125 bool PrinterQuery::is_valid() const { | 144 bool PrinterQuery::is_valid() const { |
126 return worker_.get() != NULL; | 145 return worker_.get() != NULL; |
127 } | 146 } |
128 | 147 |
129 } // namespace printing | 148 } // namespace printing |
OLD | NEW |