| 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/browser/printing/print_job.h" | 5 #include "chrome/browser/printing/print_job.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/timer.h" | 9 #include "base/timer.h" |
| 10 #include "chrome/browser/printing/print_job_worker.h" | 10 #include "chrome/browser/printing/print_job_worker.h" |
| 11 #include "chrome/common/chrome_notification_types.h" |
| 11 #include "content/common/notification_service.h" | 12 #include "content/common/notification_service.h" |
| 12 #include "printing/printed_document.h" | 13 #include "printing/printed_document.h" |
| 13 #include "printing/printed_page.h" | 14 #include "printing/printed_page.h" |
| 14 | 15 |
| 15 using base::TimeDelta; | 16 using base::TimeDelta; |
| 16 | 17 |
| 17 namespace printing { | 18 namespace printing { |
| 18 | 19 |
| 19 PrintJob::PrintJob() | 20 PrintJob::PrintJob() |
| 20 : ui_message_loop_(MessageLoop::current()), | 21 : ui_message_loop_(MessageLoop::current()), |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 source_ = source; | 53 source_ = source; |
| 53 worker_.reset(job->DetachWorker(this)); | 54 worker_.reset(job->DetachWorker(this)); |
| 54 settings_ = job->settings(); | 55 settings_ = job->settings(); |
| 55 | 56 |
| 56 PrintedDocument* new_doc = | 57 PrintedDocument* new_doc = |
| 57 new PrintedDocument(settings_, source_, job->cookie()); | 58 new PrintedDocument(settings_, source_, job->cookie()); |
| 58 new_doc->set_page_count(page_count); | 59 new_doc->set_page_count(page_count); |
| 59 UpdatePrintedDocument(new_doc); | 60 UpdatePrintedDocument(new_doc); |
| 60 | 61 |
| 61 // Don't forget to register to our own messages. | 62 // Don't forget to register to our own messages. |
| 62 registrar_.Add(this, NotificationType::PRINT_JOB_EVENT, | 63 registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT, |
| 63 Source<PrintJob>(this)); | 64 Source<PrintJob>(this)); |
| 64 } | 65 } |
| 65 | 66 |
| 66 void PrintJob::Observe(NotificationType type, | 67 void PrintJob::Observe(int type, |
| 67 const NotificationSource& source, | 68 const NotificationSource& source, |
| 68 const NotificationDetails& details) { | 69 const NotificationDetails& details) { |
| 69 DCHECK_EQ(ui_message_loop_, MessageLoop::current()); | 70 DCHECK_EQ(ui_message_loop_, MessageLoop::current()); |
| 70 switch (type.value) { | 71 switch (type) { |
| 71 case NotificationType::PRINT_JOB_EVENT: { | 72 case chrome::NOTIFICATION_PRINT_JOB_EVENT: { |
| 72 OnNotifyPrintJobEvent(*Details<JobEventDetails>(details).ptr()); | 73 OnNotifyPrintJobEvent(*Details<JobEventDetails>(details).ptr()); |
| 73 break; | 74 break; |
| 74 } | 75 } |
| 75 default: { | 76 default: { |
| 76 break; | 77 break; |
| 77 } | 78 } |
| 78 } | 79 } |
| 79 } | 80 } |
| 80 | 81 |
| 81 void PrintJob::GetSettingsDone(const PrintSettings& new_settings, | 82 void PrintJob::GetSettingsDone(const PrintSettings& new_settings, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 // Real work is done in PrintJobWorker::StartPrinting(). | 118 // Real work is done in PrintJobWorker::StartPrinting(). |
| 118 worker_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 119 worker_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 119 worker_.get(), &PrintJobWorker::StartPrinting, document_)); | 120 worker_.get(), &PrintJobWorker::StartPrinting, document_)); |
| 120 // Set the flag right now. | 121 // Set the flag right now. |
| 121 is_job_pending_ = true; | 122 is_job_pending_ = true; |
| 122 | 123 |
| 123 // Tell everyone! | 124 // Tell everyone! |
| 124 scoped_refptr<JobEventDetails> details( | 125 scoped_refptr<JobEventDetails> details( |
| 125 new JobEventDetails(JobEventDetails::NEW_DOC, document_.get(), NULL)); | 126 new JobEventDetails(JobEventDetails::NEW_DOC, document_.get(), NULL)); |
| 126 NotificationService::current()->Notify( | 127 NotificationService::current()->Notify( |
| 127 NotificationType::PRINT_JOB_EVENT, | 128 chrome::NOTIFICATION_PRINT_JOB_EVENT, |
| 128 Source<PrintJob>(this), | 129 Source<PrintJob>(this), |
| 129 Details<JobEventDetails>(details.get())); | 130 Details<JobEventDetails>(details.get())); |
| 130 } | 131 } |
| 131 | 132 |
| 132 void PrintJob::Stop() { | 133 void PrintJob::Stop() { |
| 133 DCHECK_EQ(ui_message_loop_, MessageLoop::current()); | 134 DCHECK_EQ(ui_message_loop_, MessageLoop::current()); |
| 134 | 135 |
| 135 // Be sure to live long enough. | 136 // Be sure to live long enough. |
| 136 scoped_refptr<PrintJob> handle(this); | 137 scoped_refptr<PrintJob> handle(this); |
| 137 | 138 |
| 138 MessageLoop* worker_loop = worker_->message_loop(); | 139 MessageLoop* worker_loop = worker_->message_loop(); |
| 139 if (worker_loop) { | 140 if (worker_loop) { |
| 140 ControlledWorkerShutdown(); | 141 ControlledWorkerShutdown(); |
| 141 | 142 |
| 142 is_job_pending_ = false; | 143 is_job_pending_ = false; |
| 143 registrar_.Remove(this, NotificationType::PRINT_JOB_EVENT, | 144 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_EVENT, |
| 144 Source<PrintJob>(this)); | 145 Source<PrintJob>(this)); |
| 145 } | 146 } |
| 146 // Flush the cached document. | 147 // Flush the cached document. |
| 147 UpdatePrintedDocument(NULL); | 148 UpdatePrintedDocument(NULL); |
| 148 } | 149 } |
| 149 | 150 |
| 150 void PrintJob::Cancel() { | 151 void PrintJob::Cancel() { |
| 151 if (is_canceling_) | 152 if (is_canceling_) |
| 152 return; | 153 return; |
| 153 is_canceling_ = true; | 154 is_canceling_ = true; |
| 154 | 155 |
| 155 // Be sure to live long enough. | 156 // Be sure to live long enough. |
| 156 scoped_refptr<PrintJob> handle(this); | 157 scoped_refptr<PrintJob> handle(this); |
| 157 | 158 |
| 158 DCHECK_EQ(ui_message_loop_, MessageLoop::current()); | 159 DCHECK_EQ(ui_message_loop_, MessageLoop::current()); |
| 159 MessageLoop* worker_loop = worker_.get() ? worker_->message_loop() : NULL; | 160 MessageLoop* worker_loop = worker_.get() ? worker_->message_loop() : NULL; |
| 160 if (worker_loop) { | 161 if (worker_loop) { |
| 161 // Call this right now so it renders the context invalid. Do not use | 162 // Call this right now so it renders the context invalid. Do not use |
| 162 // InvokeLater since it would take too much time. | 163 // InvokeLater since it would take too much time. |
| 163 worker_->Cancel(); | 164 worker_->Cancel(); |
| 164 } | 165 } |
| 165 // Make sure a Cancel() is broadcast. | 166 // Make sure a Cancel() is broadcast. |
| 166 scoped_refptr<JobEventDetails> details( | 167 scoped_refptr<JobEventDetails> details( |
| 167 new JobEventDetails(JobEventDetails::FAILED, NULL, NULL)); | 168 new JobEventDetails(JobEventDetails::FAILED, NULL, NULL)); |
| 168 NotificationService::current()->Notify( | 169 NotificationService::current()->Notify( |
| 169 NotificationType::PRINT_JOB_EVENT, | 170 chrome::NOTIFICATION_PRINT_JOB_EVENT, |
| 170 Source<PrintJob>(this), | 171 Source<PrintJob>(this), |
| 171 Details<JobEventDetails>(details.get())); | 172 Details<JobEventDetails>(details.get())); |
| 172 Stop(); | 173 Stop(); |
| 173 is_canceling_ = false; | 174 is_canceling_ = false; |
| 174 } | 175 } |
| 175 | 176 |
| 176 bool PrintJob::FlushJob(int timeout_ms) { | 177 bool PrintJob::FlushJob(int timeout_ms) { |
| 177 // Make sure the object outlive this message loop. | 178 // Make sure the object outlive this message loop. |
| 178 scoped_refptr<PrintJob> handle(this); | 179 scoped_refptr<PrintJob> handle(this); |
| 179 | 180 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 // Be sure to live long enough. The instance could be destroyed by the | 267 // Be sure to live long enough. The instance could be destroyed by the |
| 267 // JOB_DONE broadcast. | 268 // JOB_DONE broadcast. |
| 268 scoped_refptr<PrintJob> handle(this); | 269 scoped_refptr<PrintJob> handle(this); |
| 269 | 270 |
| 270 // Stop the worker thread. | 271 // Stop the worker thread. |
| 271 Stop(); | 272 Stop(); |
| 272 | 273 |
| 273 scoped_refptr<JobEventDetails> details( | 274 scoped_refptr<JobEventDetails> details( |
| 274 new JobEventDetails(JobEventDetails::JOB_DONE, document_.get(), NULL)); | 275 new JobEventDetails(JobEventDetails::JOB_DONE, document_.get(), NULL)); |
| 275 NotificationService::current()->Notify( | 276 NotificationService::current()->Notify( |
| 276 NotificationType::PRINT_JOB_EVENT, | 277 chrome::NOTIFICATION_PRINT_JOB_EVENT, |
| 277 Source<PrintJob>(this), | 278 Source<PrintJob>(this), |
| 278 Details<JobEventDetails>(details.get())); | 279 Details<JobEventDetails>(details.get())); |
| 279 } | 280 } |
| 280 | 281 |
| 281 void PrintJob::ControlledWorkerShutdown() { | 282 void PrintJob::ControlledWorkerShutdown() { |
| 282 DCHECK_EQ(ui_message_loop_, MessageLoop::current()); | 283 DCHECK_EQ(ui_message_loop_, MessageLoop::current()); |
| 283 | 284 |
| 284 // The deadlock this code works around is specific to window messaging on | 285 // The deadlock this code works around is specific to window messaging on |
| 285 // Windows, so we aren't likely to need it on any other platforms. | 286 // Windows, so we aren't likely to need it on any other platforms. |
| 286 #if defined(OS_WIN) | 287 #if defined(OS_WIN) |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 | 347 |
| 347 PrintedDocument* JobEventDetails::document() const { | 348 PrintedDocument* JobEventDetails::document() const { |
| 348 return document_; | 349 return document_; |
| 349 } | 350 } |
| 350 | 351 |
| 351 PrintedPage* JobEventDetails::page() const { | 352 PrintedPage* JobEventDetails::page() const { |
| 352 return page_; | 353 return page_; |
| 353 } | 354 } |
| 354 | 355 |
| 355 } // namespace printing | 356 } // namespace printing |
| OLD | NEW |