1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/printing/print_job_manager.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/printing/print_job.h"
9 #include "chrome/browser/printing/printer_query.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/notification_service.h"
12 #include "printing/printed_document.h"
13 #include "printing/printed_page.h"
17 PrintQueriesQueue::PrintQueriesQueue() {
20 PrintQueriesQueue::~PrintQueriesQueue() {
21 base::AutoLock
lock(lock_
);
22 queued_queries_
.clear();
25 void PrintQueriesQueue::SetDestination(PrintDestinationInterface
* destination
) {
26 base::AutoLock
lock(lock_
);
27 destination_
= destination
;
30 void PrintQueriesQueue::QueuePrinterQuery(PrinterQuery
* job
) {
31 base::AutoLock
lock(lock_
);
33 queued_queries_
.push_back(make_scoped_refptr(job
));
34 DCHECK(job
->is_valid());
37 scoped_refptr
<PrinterQuery
> PrintQueriesQueue::PopPrinterQuery(
38 int document_cookie
) {
39 base::AutoLock
lock(lock_
);
40 for (PrinterQueries::iterator itr
= queued_queries_
.begin();
41 itr
!= queued_queries_
.end(); ++itr
) {
42 if ((*itr
)->cookie() == document_cookie
&& !(*itr
)->is_callback_pending()) {
43 scoped_refptr
<printing::PrinterQuery
> current_query(*itr
);
44 queued_queries_
.erase(itr
);
45 DCHECK(current_query
->is_valid());
52 scoped_refptr
<PrinterQuery
> PrintQueriesQueue::CreatePrinterQuery(
53 int render_process_id
,
55 scoped_refptr
<PrinterQuery
> job
=
56 new printing::PrinterQuery(render_process_id
, render_view_id
);
57 base::AutoLock
lock(lock_
);
58 job
->SetWorkerDestination(destination_
.get());
62 void PrintQueriesQueue::Shutdown() {
63 PrinterQueries queries_to_stop
;
65 base::AutoLock
lock(lock_
);
66 queued_queries_
.swap(queries_to_stop
);
69 // Stop all pending queries, requests to generate print preview do not have
70 // corresponding PrintJob, so any pending preview requests are not covered
71 // by PrintJobManager::StopJobs and should be stopped explicitly.
72 for (PrinterQueries::iterator itr
= queries_to_stop
.begin();
73 itr
!= queries_to_stop
.end(); ++itr
) {
74 (*itr
)->PostTask(FROM_HERE
, base::Bind(&PrinterQuery::StopWorker
, *itr
));
78 PrintJobManager::PrintJobManager() : is_shutdown_(false) {
79 registrar_
.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT
,
80 content::NotificationService::AllSources());
83 PrintJobManager::~PrintJobManager() {
86 scoped_refptr
<PrintQueriesQueue
> PrintJobManager::queue() {
87 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
89 queue_
= new PrintQueriesQueue();
93 void PrintJobManager::Shutdown() {
94 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
95 DCHECK(!is_shutdown_
);
97 registrar_
.RemoveAll();
104 void PrintJobManager::StopJobs(bool wait_for_finish
) {
105 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
106 // Copy the array since it can be modified in transit.
108 to_stop
.swap(current_jobs_
);
110 for (PrintJobs::const_iterator job
= to_stop
.begin(); job
!= to_stop
.end();
112 // Wait for two minutes for the print job to be spooled.
114 (*job
)->FlushJob(base::TimeDelta::FromMinutes(2));
119 void PrintJobManager::Observe(int type
,
120 const content::NotificationSource
& source
,
121 const content::NotificationDetails
& details
) {
122 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
124 case chrome::NOTIFICATION_PRINT_JOB_EVENT
: {
125 OnPrintJobEvent(content::Source
<PrintJob
>(source
).ptr(),
126 *content::Details
<JobEventDetails
>(details
).ptr());
136 void PrintJobManager::OnPrintJobEvent(
138 const JobEventDetails
& event_details
) {
139 switch (event_details
.type()) {
140 case JobEventDetails::NEW_DOC
: {
141 DCHECK(current_jobs_
.end() == current_jobs_
.find(print_job
));
142 // Causes a AddRef().
143 current_jobs_
.insert(print_job
);
146 case JobEventDetails::JOB_DONE
: {
147 DCHECK(current_jobs_
.end() != current_jobs_
.find(print_job
));
148 current_jobs_
.erase(print_job
);
151 case JobEventDetails::FAILED
: {
152 current_jobs_
.erase(print_job
);
155 case JobEventDetails::USER_INIT_DONE
:
156 case JobEventDetails::USER_INIT_CANCELED
:
157 case JobEventDetails::DEFAULT_INIT_DONE
:
158 case JobEventDetails::NEW_PAGE
:
159 case JobEventDetails::PAGE_DONE
:
160 case JobEventDetails::DOC_DONE
:
161 case JobEventDetails::ALL_PAGES_REQUESTED
: {
172 } // namespace printing