summaryrefslogtreecommitdiffstats
path: root/chrome/browser/printing/print_job_manager.cc
diff options
context:
space:
mode:
authorinitial.commit <initial.commit@0039d316-1c4b-4281-b951-d872f2087c98>2008-07-26 23:55:29 +0000
committerinitial.commit <initial.commit@0039d316-1c4b-4281-b951-d872f2087c98>2008-07-26 23:55:29 +0000
commit09911bf300f1a419907a9412154760efd0b7abc3 (patch)
treef131325fb4e2ad12c6d3504ab75b16dd92facfed /chrome/browser/printing/print_job_manager.cc
parent586acc5fe142f498261f52c66862fa417c3d52d2 (diff)
downloadchromium_src-09911bf300f1a419907a9412154760efd0b7abc3.zip
chromium_src-09911bf300f1a419907a9412154760efd0b7abc3.tar.gz
chromium_src-09911bf300f1a419907a9412154760efd0b7abc3.tar.bz2
Add chrome to the repository.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@15 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/printing/print_job_manager.cc')
-rw-r--r--chrome/browser/printing/print_job_manager.cc220
1 files changed, 220 insertions, 0 deletions
diff --git a/chrome/browser/printing/print_job_manager.cc b/chrome/browser/printing/print_job_manager.cc
new file mode 100644
index 0000000..794af34
--- /dev/null
+++ b/chrome/browser/printing/print_job_manager.cc
@@ -0,0 +1,220 @@
+// Copyright 2008, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "chrome/browser/printing/print_job_manager.h"
+
+#include "base/file_util.h"
+#include "base/string_util.h"
+#include "chrome/browser/printing/print_job.h"
+#include "chrome/browser/printing/printer_query.h"
+#include "chrome/browser/printing/printed_document.h"
+#include "chrome/browser/printing/printed_page.h"
+#include "chrome/common/gfx/emf.h"
+
+namespace printing {
+
+PrintJobManager::PrintJobManager()
+ : debug_dump_path_() {
+ NotificationService::current()->AddObserver(
+ this,
+ NOTIFY_PRINT_JOB_EVENT,
+ NotificationService::AllSources());
+ NotificationService::current()->AddObserver(
+ this,
+ NOTIFY_PRINTED_DOCUMENT_UPDATED,
+ NotificationService::AllSources());
+}
+
+PrintJobManager::~PrintJobManager() {
+ // When this object is destroyed, the shared NotificationService instance is
+ // already destroyed.
+ AutoLock lock(lock_);
+ queued_queries_.clear();
+ NotificationService::current()->RemoveObserver(
+ this,
+ NOTIFY_PRINT_JOB_EVENT,
+ NotificationService::AllSources());
+ NotificationService::current()->RemoveObserver(
+ this,
+ NOTIFY_PRINTED_DOCUMENT_UPDATED,
+ NotificationService::AllSources());
+}
+
+void PrintJobManager::OnQuit() {
+ // Common case, no print job pending.
+ if (current_jobs_.size() == 0)
+ return;
+ {
+ // Don't take a chance and copy the array since it can be modified in transit.
+ PrintJobs current_jobs(current_jobs_);
+ // Wait for every jobs to finish.
+ for (size_t i = 0; i < current_jobs.size(); ++i) {
+ PrintJob* job = current_jobs[i];
+ if (!job)
+ continue;
+ // Wait for 120 seconds for the print job to be spooled.
+ job->FlushJob(120000);
+ job->Stop();
+ }
+ }
+ current_jobs_.clear();
+ NotificationService::current()->RemoveObserver(
+ this,
+ NOTIFY_PRINT_JOB_EVENT,
+ NotificationService::AllSources());
+ NotificationService::current()->RemoveObserver(
+ this,
+ NOTIFY_PRINTED_DOCUMENT_UPDATED,
+ NotificationService::AllSources());
+ DCHECK_EQ(current_jobs_.size(), 0);
+}
+
+void PrintJobManager::QueuePrinterQuery(PrinterQuery* job) {
+ AutoLock lock(lock_);
+ DCHECK(job);
+ queued_queries_.push_back(job);
+ DCHECK(job->is_valid());
+}
+
+void PrintJobManager::PopPrinterQuery(int document_cookie,
+ scoped_refptr<PrinterQuery>* job) {
+ AutoLock lock(lock_);
+ for (PrinterQueries::iterator itr = queued_queries_.begin();
+ itr != queued_queries_.end();
+ ++itr) {
+ PrinterQuery* current_query = *itr;
+ if (current_query->cookie() == document_cookie &&
+ !current_query->is_callback_pending()) {
+ *job = current_query;
+ queued_queries_.erase(itr);
+ DCHECK(current_query->is_valid());
+ return;
+ }
+ }
+}
+
+
+void PrintJobManager::Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ switch (type) {
+ case NOTIFY_PRINT_JOB_EVENT: {
+ OnPrintJobEvent(Source<PrintJob>(source).ptr(),
+ *Details<JobEventDetails>(details).ptr());
+ break;
+ }
+ case NOTIFY_PRINTED_DOCUMENT_UPDATED: {
+ PrintedPage* printed_page = Details<PrintedPage>(details).ptr();
+ if (printed_page)
+ OnPrintedDocumentUpdated(*Source<PrintedDocument>(source).ptr(),
+ *printed_page);
+ break;
+ }
+ default: {
+ NOTREACHED();
+ break;
+ }
+ }
+}
+
+void PrintJobManager::OnPrintJobEvent(
+ PrintJob* print_job,
+ const JobEventDetails& event_details) {
+ switch (event_details.type()) {
+ case JobEventDetails::NEW_DOC: {
+ DCHECK(current_jobs_.end() == std::find(current_jobs_.begin(),
+ current_jobs_.end(),
+ print_job));
+ // Causes a AddRef().
+ current_jobs_.push_back(print_job);
+ break;
+ }
+ case JobEventDetails::JOB_DONE: {
+ PrintJobs::iterator itr = std::find(current_jobs_.begin(),
+ current_jobs_.end(),
+ print_job);
+ DCHECK(current_jobs_.end() != itr);
+ current_jobs_.erase(itr);
+ DCHECK(current_jobs_.end() == std::find(current_jobs_.begin(),
+ current_jobs_.end(),
+ print_job));
+ break;
+ }
+ case JobEventDetails::FAILED: {
+ PrintJobs::iterator itr = std::find(current_jobs_.begin(),
+ current_jobs_.end(),
+ print_job);
+ // A failed job may have never started.
+ if (current_jobs_.end() != itr) {
+ current_jobs_.erase(itr);
+ DCHECK(current_jobs_.end() ==
+ std::find(current_jobs_.begin(),
+ current_jobs_.end(),
+ print_job));
+ }
+ break;
+ }
+ case JobEventDetails::USER_INIT_DONE:
+ case JobEventDetails::USER_INIT_CANCELED:
+ case JobEventDetails::DEFAULT_INIT_DONE:
+ case JobEventDetails::NEW_PAGE:
+ case JobEventDetails::PAGE_DONE:
+ case JobEventDetails::DOC_DONE:
+ case JobEventDetails::ALL_PAGES_REQUESTED: {
+ // Don't care.
+ break;
+ }
+ default: {
+ NOTREACHED();
+ break;
+ }
+ }
+}
+
+void PrintJobManager::OnPrintedDocumentUpdated(const PrintedDocument& document,
+ const PrintedPage& page) {
+ if (debug_dump_path_.empty())
+ return;
+
+ std::wstring filename;
+ filename += document.date();
+ filename += L"_";
+ filename += document.time();
+ filename += L"_";
+ filename += document.name();
+ filename += L"_";
+ filename += StringPrintf(L"%02d", page.page_number());
+ filename += L"_.emf";
+ file_util::ReplaceIllegalCharacters(&filename, '_');
+ std::wstring path(debug_dump_path_);
+ file_util::AppendToPath(&path, filename);
+ page.emf()->SaveTo(path);
+}
+
+} // namespace printing