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 scoped_refptr<PrinterQuery> job = new printing::PrinterQuery;
54 base::AutoLock lock(lock_);
55 job->SetWorkerDestination(destination_);
59 void PrintQueriesQueue::Shutdown() {
60 base::AutoLock lock(lock_);
61 queued_queries_.clear();
65 PrintJobManager::PrintJobManager() : is_shutdown_(false) {
66 registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
67 content::NotificationService::AllSources());
70 PrintJobManager::~PrintJobManager() {
73 scoped_refptr<PrintQueriesQueue> PrintJobManager::queue() {
74 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
76 queue_ = new PrintQueriesQueue();
80 void PrintJobManager::Shutdown() {
81 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
82 DCHECK(!is_shutdown_);
84 registrar_.RemoveAll();
91 void PrintJobManager::StopJobs(bool wait_for_finish) {
92 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
93 // Copy the array since it can be modified in transit.
95 to_stop.swap(current_jobs_);
97 for (PrintJobs::const_iterator job = to_stop.begin(); job != to_stop.end();
99 // Wait for two minutes for the print job to be spooled.
101 (*job)->FlushJob(base::TimeDelta::FromMinutes(2));
106 void PrintJobManager::Observe(int type,
107 const content::NotificationSource& source,
108 const content::NotificationDetails& details) {
109 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
111 case chrome::NOTIFICATION_PRINT_JOB_EVENT: {
112 OnPrintJobEvent(content::Source<PrintJob>(source).ptr(),
113 *content::Details<JobEventDetails>(details).ptr());
123 void PrintJobManager::OnPrintJobEvent(
125 const JobEventDetails& event_details) {
126 switch (event_details.type()) {
127 case JobEventDetails::NEW_DOC: {
128 DCHECK(current_jobs_.end() == current_jobs_.find(print_job));
129 // Causes a AddRef().
130 current_jobs_.insert(print_job);
133 case JobEventDetails::JOB_DONE: {
134 DCHECK(current_jobs_.end() != current_jobs_.find(print_job));
135 current_jobs_.erase(print_job);
138 case JobEventDetails::FAILED: {
139 current_jobs_.erase(print_job);
142 case JobEventDetails::USER_INIT_DONE:
143 case JobEventDetails::USER_INIT_CANCELED:
144 case JobEventDetails::DEFAULT_INIT_DONE:
145 case JobEventDetails::NEW_PAGE:
146 case JobEventDetails::PAGE_DONE:
147 case JobEventDetails::DOC_DONE:
148 case JobEventDetails::ALL_PAGES_REQUESTED: {
159 } // namespace printing