Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / chrome / browser / printing / print_job_manager.cc
blobb600d1a12007db5013bc8df94b7468ccc83c6f82
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"
15 namespace printing {
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_);
32 DCHECK(job);
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());
46 return current_query;
49 return NULL;
52 scoped_refptr<PrinterQuery> PrintQueriesQueue::CreatePrinterQuery(
53 int render_process_id,
54 int render_view_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());
59 return job;
62 void PrintQueriesQueue::Shutdown() {
63 PrinterQueries queries_to_stop;
65 base::AutoLock lock(lock_);
66 queued_queries_.swap(queries_to_stop);
67 destination_ = NULL;
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));
88 if (!queue_.get())
89 queue_ = new PrintQueriesQueue();
90 return queue_;
93 void PrintJobManager::Shutdown() {
94 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
95 DCHECK(!is_shutdown_);
96 is_shutdown_ = true;
97 registrar_.RemoveAll();
98 StopJobs(true);
99 if (queue_.get())
100 queue_->Shutdown();
101 queue_ = NULL;
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.
107 PrintJobs to_stop;
108 to_stop.swap(current_jobs_);
110 for (PrintJobs::const_iterator job = to_stop.begin(); job != to_stop.end();
111 ++job) {
112 // Wait for two minutes for the print job to be spooled.
113 if (wait_for_finish)
114 (*job)->FlushJob(base::TimeDelta::FromMinutes(2));
115 (*job)->Stop();
119 void PrintJobManager::Observe(int type,
120 const content::NotificationSource& source,
121 const content::NotificationDetails& details) {
122 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
123 switch (type) {
124 case chrome::NOTIFICATION_PRINT_JOB_EVENT: {
125 OnPrintJobEvent(content::Source<PrintJob>(source).ptr(),
126 *content::Details<JobEventDetails>(details).ptr());
127 break;
129 default: {
130 NOTREACHED();
131 break;
136 void PrintJobManager::OnPrintJobEvent(
137 PrintJob* print_job,
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);
144 break;
146 case JobEventDetails::JOB_DONE: {
147 DCHECK(current_jobs_.end() != current_jobs_.find(print_job));
148 current_jobs_.erase(print_job);
149 break;
151 case JobEventDetails::FAILED: {
152 current_jobs_.erase(print_job);
153 break;
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: {
162 // Don't care.
163 break;
165 default: {
166 NOTREACHED();
167 break;
172 } // namespace printing