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_worker.h" | 5 #include "chrome/browser/printing/print_job_worker.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 // Helper function to ensure |owner| is valid until at least |callback| returns. | 26 // Helper function to ensure |owner| is valid until at least |callback| returns. |
27 void HoldRefCallback(const scoped_refptr<printing::PrintJobWorkerOwner>& owner, | 27 void HoldRefCallback(const scoped_refptr<printing::PrintJobWorkerOwner>& owner, |
28 const base::Closure& callback) { | 28 const base::Closure& callback) { |
29 callback.Run(); | 29 callback.Run(); |
30 } | 30 } |
31 | 31 |
32 } // namespace | 32 } // namespace |
33 | 33 |
34 namespace printing { | 34 namespace printing { |
35 | 35 |
36 class PrintJobWorker::NotificationTask : public Task { | 36 void NotificationCallback(PrintJobWorkerOwner* print_job, |
37 public: | 37 JobEventDetails::Type detail_type, |
38 NotificationTask() : print_job_(NULL), details_(NULL) {} | 38 PrintedDocument* document, |
39 ~NotificationTask() {} | 39 PrintedPage* page) { |
40 | 40 JobEventDetails* details = new JobEventDetails(detail_type, document, page); |
41 // Initializes the object. This object can't be initialized in the constructor | 41 content::NotificationService::current()->Notify( |
42 // since it is not created directly. | 42 chrome::NOTIFICATION_PRINT_JOB_EVENT, |
43 void Init(PrintJobWorkerOwner* print_job, | 43 // We know that is is a PrintJob object in this circumstance. |
44 JobEventDetails::Type detail_type, | 44 content::Source<PrintJob>(static_cast<PrintJob*>(print_job)), |
45 PrintedDocument* document, | 45 content::Details<JobEventDetails>(details)); |
46 PrintedPage* page) { | 46 } |
47 DCHECK(!print_job_); | |
48 DCHECK(!details_); | |
49 print_job_ = print_job; | |
50 details_ = new JobEventDetails(detail_type, document, page); | |
51 } | |
52 | |
53 virtual void Run() { | |
54 // Send the notification in the right thread. | |
55 content::NotificationService::current()->Notify( | |
56 chrome::NOTIFICATION_PRINT_JOB_EVENT, | |
57 // We know that is is a PrintJob object in this circumstance. | |
58 content::Source<PrintJob>(static_cast<PrintJob*>(print_job_.get())), | |
59 content::Details<JobEventDetails>(details_)); | |
60 } | |
61 | |
62 // The job which originates this notification. | |
63 scoped_refptr<PrintJobWorkerOwner> print_job_; | |
64 scoped_refptr<JobEventDetails> details_; | |
65 }; | |
66 | 47 |
67 PrintJobWorker::PrintJobWorker(PrintJobWorkerOwner* owner) | 48 PrintJobWorker::PrintJobWorker(PrintJobWorkerOwner* owner) |
68 : Thread("Printing_Worker"), | 49 : Thread("Printing_Worker"), |
69 owner_(owner), | 50 owner_(owner), |
70 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 51 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
71 // The object is created in the IO thread. | 52 // The object is created in the IO thread. |
72 DCHECK_EQ(owner_->message_loop(), MessageLoop::current()); | 53 DCHECK_EQ(owner_->message_loop(), MessageLoop::current()); |
73 | 54 |
74 printing_context_.reset(PrintingContext::Create( | 55 printing_context_.reset(PrintingContext::Create( |
75 g_browser_process->GetApplicationLocale())); | 56 g_browser_process->GetApplicationLocale())); |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 scoped_refptr<PrintedPage> page; | 249 scoped_refptr<PrintedPage> page; |
269 if (!document_->GetPage(page_number_.ToInt(), &page)) { | 250 if (!document_->GetPage(page_number_.ToInt(), &page)) { |
270 // We need to wait for the page to be available. | 251 // We need to wait for the page to be available. |
271 MessageLoop::current()->PostDelayedTask( | 252 MessageLoop::current()->PostDelayedTask( |
272 FROM_HERE, | 253 FROM_HERE, |
273 base::Bind(&PrintJobWorker::OnNewPage, weak_factory_.GetWeakPtr()), | 254 base::Bind(&PrintJobWorker::OnNewPage, weak_factory_.GetWeakPtr()), |
274 500); | 255 500); |
275 break; | 256 break; |
276 } | 257 } |
277 // The page is there, print it. | 258 // The page is there, print it. |
278 SpoolPage(*page); | 259 SpoolPage(page); |
279 ++page_number_; | 260 ++page_number_; |
280 if (page_number_ == PageNumber::npos()) { | 261 if (page_number_ == PageNumber::npos()) { |
281 OnDocumentDone(); | 262 OnDocumentDone(); |
282 // Don't touch this anymore since the instance could be destroyed. | 263 // Don't touch this anymore since the instance could be destroyed. |
283 break; | 264 break; |
284 } | 265 } |
285 } | 266 } |
286 } | 267 } |
287 | 268 |
288 void PrintJobWorker::Cancel() { | 269 void PrintJobWorker::Cancel() { |
289 // This is the only function that can be called from any thread. | 270 // This is the only function that can be called from any thread. |
290 printing_context_->Cancel(); | 271 printing_context_->Cancel(); |
291 // Cannot touch any member variable since we don't know in which thread | 272 // Cannot touch any member variable since we don't know in which thread |
292 // context we run. | 273 // context we run. |
293 } | 274 } |
294 | 275 |
295 void PrintJobWorker::OnDocumentDone() { | 276 void PrintJobWorker::OnDocumentDone() { |
296 DCHECK_EQ(message_loop(), MessageLoop::current()); | 277 DCHECK_EQ(message_loop(), MessageLoop::current()); |
297 DCHECK_EQ(page_number_, PageNumber::npos()); | 278 DCHECK_EQ(page_number_, PageNumber::npos()); |
298 DCHECK(document_.get()); | 279 DCHECK(document_.get()); |
299 | 280 |
300 if (printing_context_->DocumentDone() != PrintingContext::OK) { | 281 if (printing_context_->DocumentDone() != PrintingContext::OK) { |
301 OnFailure(); | 282 OnFailure(); |
302 return; | 283 return; |
303 } | 284 } |
304 | 285 |
305 // Tell everyone! | 286 owner_->message_loop()->PostTask( |
306 NotificationTask* task = new NotificationTask(); | 287 FROM_HERE, base::Bind(NotificationCallback, make_scoped_refptr(owner_), |
307 task->Init(owner_, | 288 JobEventDetails::DOC_DONE, document_, |
308 JobEventDetails::DOC_DONE, | 289 scoped_refptr<PrintedPage>())); |
309 document_.get(), | |
310 NULL); | |
311 owner_->message_loop()->PostTask(FROM_HERE, task); | |
312 | 290 |
313 // Makes sure the variables are reinitialized. | 291 // Makes sure the variables are reinitialized. |
314 document_ = NULL; | 292 document_ = NULL; |
315 } | 293 } |
316 | 294 |
317 void PrintJobWorker::SpoolPage(PrintedPage& page) { | 295 void PrintJobWorker::SpoolPage(PrintedPage* page) { |
318 DCHECK_EQ(message_loop(), MessageLoop::current()); | 296 DCHECK_EQ(message_loop(), MessageLoop::current()); |
319 DCHECK_NE(page_number_, PageNumber::npos()); | 297 DCHECK_NE(page_number_, PageNumber::npos()); |
320 | 298 |
321 // Signal everyone that the page is about to be printed. | 299 // Signal everyone that the page is about to be printed. |
322 NotificationTask* task = new NotificationTask(); | 300 owner_->message_loop()->PostTask( |
323 task->Init(owner_, | 301 FROM_HERE, base::Bind(NotificationCallback, make_scoped_refptr(owner_), |
324 JobEventDetails::NEW_PAGE, | 302 JobEventDetails::NEW_PAGE, document_, |
325 document_.get(), | 303 make_scoped_refptr(page))); |
326 &page); | |
327 owner_->message_loop()->PostTask(FROM_HERE, task); | |
328 | 304 |
329 // Preprocess. | 305 // Preprocess. |
330 if (printing_context_->NewPage() != PrintingContext::OK) { | 306 if (printing_context_->NewPage() != PrintingContext::OK) { |
331 OnFailure(); | 307 OnFailure(); |
332 return; | 308 return; |
333 } | 309 } |
334 | 310 |
335 // Actual printing. | 311 // Actual printing. |
336 #if defined(OS_WIN) || defined(OS_MACOSX) | 312 #if defined(OS_WIN) || defined(OS_MACOSX) |
337 document_->RenderPrintedPage(page, printing_context_->context()); | 313 document_->RenderPrintedPage(*page, printing_context_->context()); |
338 #elif defined(OS_POSIX) | 314 #elif defined(OS_POSIX) |
339 document_->RenderPrintedPage(page, printing_context_.get()); | 315 document_->RenderPrintedPage(*page, printing_context_.get()); |
340 #endif | 316 #endif |
341 | 317 |
342 // Postprocess. | 318 // Postprocess. |
343 if (printing_context_->PageDone() != PrintingContext::OK) { | 319 if (printing_context_->PageDone() != PrintingContext::OK) { |
344 OnFailure(); | 320 OnFailure(); |
345 return; | 321 return; |
346 } | 322 } |
347 | 323 |
348 // Signal everyone that the page is printed. | 324 // Signal everyone that the page is printed. |
349 task = new NotificationTask(); | 325 owner_->message_loop()->PostTask( |
350 task->Init(owner_, | 326 FROM_HERE, |
351 JobEventDetails::PAGE_DONE, | 327 base::Bind(NotificationCallback, make_scoped_refptr(owner_), |
352 document_.get(), | 328 JobEventDetails::PAGE_DONE, document_, |
353 &page); | 329 make_scoped_refptr(page))); |
354 owner_->message_loop()->PostTask(FROM_HERE, task); | |
355 } | 330 } |
356 | 331 |
357 void PrintJobWorker::OnFailure() { | 332 void PrintJobWorker::OnFailure() { |
358 DCHECK_EQ(message_loop(), MessageLoop::current()); | 333 DCHECK_EQ(message_loop(), MessageLoop::current()); |
359 | 334 |
360 // We may loose our last reference by broadcasting the FAILED event. | 335 // We may loose our last reference by broadcasting the FAILED event. |
361 scoped_refptr<PrintJobWorkerOwner> handle(owner_); | 336 scoped_refptr<PrintJobWorkerOwner> handle(owner_); |
362 | 337 |
363 NotificationTask* task = new NotificationTask(); | 338 owner_->message_loop()->PostTask( |
364 task->Init(owner_, | 339 FROM_HERE, base::Bind(NotificationCallback, make_scoped_refptr(owner_), |
365 JobEventDetails::FAILED, | 340 JobEventDetails::FAILED, document_, |
366 document_.get(), | 341 scoped_refptr<PrintedPage>())); |
367 NULL); | |
368 owner_->message_loop()->PostTask(FROM_HERE, task); | |
369 Cancel(); | 342 Cancel(); |
370 | 343 |
371 // Makes sure the variables are reinitialized. | 344 // Makes sure the variables are reinitialized. |
372 document_ = NULL; | 345 document_ = NULL; |
373 page_number_ = PageNumber::npos(); | 346 page_number_ = PageNumber::npos(); |
374 } | 347 } |
375 | 348 |
376 } // namespace printing | 349 } // namespace printing |
OLD | NEW |