diff options
author | kmadhusu@chromium.org <kmadhusu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-05 00:12:53 +0000 |
---|---|---|
committer | kmadhusu@chromium.org <kmadhusu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-05 00:12:53 +0000 |
commit | 7868ecabafc39bcd917276899742be136cca2ae3 (patch) | |
tree | b008edd73081d0df868eae216e18cc6dc56d382d /chrome/browser/printing | |
parent | f41067c4ff04b9552c48e89169b28cf078c1a8ba (diff) | |
download | chromium_src-7868ecabafc39bcd917276899742be136cca2ae3.zip chromium_src-7868ecabafc39bcd917276899742be136cca2ae3.tar.gz chromium_src-7868ecabafc39bcd917276899742be136cca2ae3.tar.bz2 |
(1) Hook up the print button to send the pages to the default printer for printing without displaying a native dialog.
(2) Made code changes to accept a print page range from the user and to print only those specified pages.
BUG=none
TEST=Enable print preview on mac, provide a valid page range and make sure print button in print preview works.
Review URL: http://codereview.chromium.org/6533006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@77003 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/printing')
-rw-r--r-- | chrome/browser/printing/print_job_worker.cc | 36 | ||||
-rw-r--r-- | chrome/browser/printing/print_job_worker.h | 11 | ||||
-rw-r--r-- | chrome/browser/printing/printer_query.cc | 47 | ||||
-rw-r--r-- | chrome/browser/printing/printer_query.h | 11 | ||||
-rw-r--r-- | chrome/browser/printing/printing_message_filter.cc | 42 | ||||
-rw-r--r-- | chrome/browser/printing/printing_message_filter.h | 8 |
6 files changed, 139 insertions, 16 deletions
diff --git a/chrome/browser/printing/print_job_worker.cc b/chrome/browser/printing/print_job_worker.cc index db0c341..3c7f862 100644 --- a/chrome/browser/printing/print_job_worker.cc +++ b/chrome/browser/printing/print_job_worker.cc @@ -5,6 +5,7 @@ #include "chrome/browser/printing/print_job_worker.h" #include "base/message_loop.h" +#include "base/values.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/printing/print_job.h" #include "chrome/common/notification_service.h" @@ -98,6 +99,41 @@ void PrintJobWorker::GetSettings(bool ask_user_for_settings, } } +void PrintJobWorker::SetSettings(const DictionaryValue* const new_settings) { + DCHECK_EQ(message_loop(), MessageLoop::current()); + + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, + NewRunnableMethod(this, &PrintJobWorker::UpdatePrintSettings, + new_settings)); +} + +void PrintJobWorker::UpdatePrintSettings( + const DictionaryValue* const new_settings) { + // Create new PageRanges based on |new_settings|. + PageRanges new_ranges; + ListValue* page_range_array; + if (new_settings->GetList("pageRange", &page_range_array)) { + for (size_t index = 0; index < page_range_array->GetSize(); ++index) { + DictionaryValue* dict; + if (page_range_array->GetDictionary(index, &dict)) { + PageRange range; + if (dict->GetInteger("from", &range.from) && + dict->GetInteger("to", &range.to)) { + // Page numbers are 0-based. + range.from--; + range.to--; + new_ranges.push_back(range); + } + } + } + } + // We don't update any other print job settings now, so delete |new_settings|. + delete new_settings; + PrintingContext::Result result = + printing_context_->UpdatePrintSettings(new_ranges); + GetSettingsDone(result); +} + void PrintJobWorker::GetSettingsDone(PrintingContext::Result result) { // Most PrintingContext functions may start a message loop and process // message recursively, so disable recursive task processing. diff --git a/chrome/browser/printing/print_job_worker.h b/chrome/browser/printing/print_job_worker.h index 6b0183b..8b0ab68 100644 --- a/chrome/browser/printing/print_job_worker.h +++ b/chrome/browser/printing/print_job_worker.h @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -14,6 +14,8 @@ #include "printing/printing_context.h" #include "ui/gfx/native_widget_types.h" +class DictionaryValue; + namespace printing { class PrintedDocument; @@ -42,6 +44,10 @@ class PrintJobWorker : public base::Thread { bool has_selection, bool use_overlays); + // Set the new print settings. This function takes ownership of |new_settings| + // and frees it. + void SetSettings(const DictionaryValue* const new_settings); + // Starts the printing loop. Every pages are printed as soon as the data is // available. Makes sure the new_document is the right one. void StartPrinting(PrintedDocument* new_document); @@ -92,6 +98,9 @@ class PrintJobWorker : public base::Thread { // back into the IO thread for GetSettingsDone(). void GetSettingsWithUIDone(PrintingContext::Result result); + // Called on the UI thread to update the print settings. + void UpdatePrintSettings(const DictionaryValue* const new_settings); + // Reports settings back to owner_. void GetSettingsDone(PrintingContext::Result result); diff --git a/chrome/browser/printing/printer_query.cc b/chrome/browser/printing/printer_query.cc index f39960f..e20ebc5 100644 --- a/chrome/browser/printing/printer_query.cc +++ b/chrome/browser/printing/printer_query.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -6,6 +6,7 @@ #include "base/message_loop.h" #include "base/threading/thread_restrictions.h" +#include "base/values.h" #include "chrome/browser/printing/print_job_worker.h" namespace printing { @@ -78,10 +79,38 @@ void PrinterQuery::GetSettings(GetSettingsAskParam ask_user_for_settings, CancelableTask* callback) { DCHECK_EQ(io_message_loop_, MessageLoop::current()); DCHECK(!is_print_dialog_box_shown_); + if (!StartWorker(callback)) + return; + + // Real work is done in PrintJobWorker::Init(). + is_print_dialog_box_shown_ = ask_user_for_settings == ASK_USER; + worker_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( + worker_.get(), + &PrintJobWorker::GetSettings, + is_print_dialog_box_shown_, + parent_view, + expected_page_count, + has_selection, + use_overlays)); +} + +void PrinterQuery::SetSettings(const DictionaryValue& new_settings, + CancelableTask* callback) { + if (!StartWorker(callback)) + return; + + worker_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( + worker_.get(), + &PrintJobWorker::SetSettings, + new_settings.DeepCopy())); +} + +bool PrinterQuery::StartWorker(CancelableTask* callback) { DCHECK(!callback_.get()); DCHECK(worker_.get()); if (!worker_.get()) - return; + return false; + // Lazy create the worker thread. There is one worker thread per print job. if (!worker_->message_loop()) { if (!worker_->Start()) { @@ -90,21 +119,11 @@ void PrinterQuery::GetSettings(GetSettingsAskParam ask_user_for_settings, delete callback; } NOTREACHED(); - return; + return false; } } - callback_.reset(callback); - // Real work is done in PrintJobWorker::Init(). - is_print_dialog_box_shown_ = ask_user_for_settings == ASK_USER; - worker_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( - worker_.get(), - &PrintJobWorker::GetSettings, - is_print_dialog_box_shown_, - parent_view, - expected_page_count, - has_selection, - use_overlays)); + return true; } void PrinterQuery::StopWorker() { diff --git a/chrome/browser/printing/printer_query.h b/chrome/browser/printing/printer_query.h index edc0c63..2a4c4680 100644 --- a/chrome/browser/printing/printer_query.h +++ b/chrome/browser/printing/printer_query.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -11,6 +11,7 @@ #include "ui/gfx/native_widget_types.h" class CancelableTask; +class DictionaryValue; class MessageLoop; namespace base { @@ -51,6 +52,10 @@ class PrinterQuery : public PrintJobWorkerOwner { bool use_overlays, CancelableTask* callback); + // Updates the current settings with |new_settings| dictionary values. + void SetSettings(const DictionaryValue& new_settings, + CancelableTask* callback); + // Stops the worker thread since the client is done with this object. void StopWorker(); @@ -65,6 +70,10 @@ class PrinterQuery : public PrintJobWorkerOwner { private: virtual ~PrinterQuery(); + // Lazy create the worker thread. There is one worker thread per print job. + // Returns true, if worker thread exists or has been created. + bool StartWorker(CancelableTask* callback); + // Main message loop reference. Used to send notifications in the right // thread. MessageLoop* const io_message_loop_; diff --git a/chrome/browser/printing/printing_message_filter.cc b/chrome/browser/printing/printing_message_filter.cc index e1132b4..1f5c4db 100644 --- a/chrome/browser/printing/printing_message_filter.cc +++ b/chrome/browser/printing/printing_message_filter.cc @@ -99,6 +99,8 @@ bool PrintingMessageFilter::OnMessageReceived(const IPC::Message& message, IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_GetDefaultPrintSettings, OnGetDefaultPrintSettings) IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_ScriptedPrint, OnScriptedPrint) + IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_UpdatePrintSettings, + OnUpdatePrintSettings) IPC_MESSAGE_UNHANDLED(handled = false) IPC_END_MESSAGE_MAP() return handled; @@ -263,3 +265,43 @@ void PrintingMessageFilter::OnScriptedPrintReply( printer_query->StopWorker(); } } + +void PrintingMessageFilter::OnUpdatePrintSettings( + int document_cookie, const DictionaryValue& job_settings, + IPC::Message* reply_msg) { + scoped_refptr<printing::PrinterQuery> printer_query; + print_job_manager_->PopPrinterQuery(document_cookie, &printer_query); + if (printer_query.get()) { + CancelableTask* task = NewRunnableMethod( + this, + &PrintingMessageFilter::OnUpdatePrintSettingsReply, + printer_query, + reply_msg); + printer_query->SetSettings(job_settings, task); + } +} + +void PrintingMessageFilter::OnUpdatePrintSettingsReply( + scoped_refptr<printing::PrinterQuery> printer_query, + IPC::Message* reply_msg) { + ViewMsg_Print_Params params; + if (!printer_query.get() || + printer_query->last_status() != printing::PrintingContext::OK) { + memset(¶ms, 0, sizeof(params)); + } else { + RenderParamsFromPrintSettings(printer_query->settings(), ¶ms); + params.document_cookie = printer_query->cookie(); + } + ViewHostMsg_UpdatePrintSettings::WriteReplyParams(reply_msg, params); + Send(reply_msg); + // If printing was enabled. + if (printer_query.get()) { + // If user hasn't cancelled. + if (printer_query->cookie() && printer_query->settings().dpi()) { + print_job_manager_->QueuePrinterQuery(printer_query.get()); + } else { + printer_query->StopWorker(); + } + } +} + diff --git a/chrome/browser/printing/printing_message_filter.h b/chrome/browser/printing/printing_message_filter.h index 686a2fb..e2c9c3c 100644 --- a/chrome/browser/printing/printing_message_filter.h +++ b/chrome/browser/printing/printing_message_filter.h @@ -12,6 +12,7 @@ #include "base/shared_memory.h" #endif +class DictionaryValue; struct ViewHostMsg_ScriptedPrint_Params; namespace printing { @@ -66,6 +67,13 @@ class PrintingMessageFilter : public BrowserMessageFilter { int routing_id, IPC::Message* reply_msg); + void OnUpdatePrintSettings(int document_cookie, + const DictionaryValue& job_settings, + IPC::Message* reply_msg); + void OnUpdatePrintSettingsReply( + scoped_refptr<printing::PrinterQuery> printer_query, + IPC::Message* reply_msg); + printing::PrintJobManager* print_job_manager_; bool cloud_print_enabled_; |