Skip to content

Commit

Permalink
(1) Hook up the print button to send the pages to the default printer…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
kmadhusu@chromium.org committed Mar 5, 2011
1 parent f41067c commit 7868eca
Show file tree
Hide file tree
Showing 22 changed files with 353 additions and 49 deletions.
36 changes: 36 additions & 0 deletions chrome/browser/printing/print_job_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion chrome/browser/printing/print_job_worker.h
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -14,6 +14,8 @@
#include "printing/printing_context.h"
#include "ui/gfx/native_widget_types.h"

class DictionaryValue;

namespace printing {

class PrintedDocument;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
47 changes: 33 additions & 14 deletions chrome/browser/printing/printer_query.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// 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.

#include "chrome/browser/printing/printer_query.h"

#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 {
Expand Down Expand Up @@ -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()) {
Expand All @@ -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() {
Expand Down
11 changes: 10 additions & 1 deletion chrome/browser/printing/printer_query.h
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -11,6 +11,7 @@
#include "ui/gfx/native_widget_types.h"

class CancelableTask;
class DictionaryValue;
class MessageLoop;

namespace base {
Expand Down Expand Up @@ -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();

Expand All @@ -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_;
Expand Down
42 changes: 42 additions & 0 deletions chrome/browser/printing/printing_message_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(&params, 0, sizeof(params));
} else {
RenderParamsFromPrintSettings(printer_query->settings(), &params);
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();
}
}
}

8 changes: 8 additions & 0 deletions chrome/browser/printing/printing_message_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "base/shared_memory.h"
#endif

class DictionaryValue;
struct ViewHostMsg_ScriptedPrint_Params;

namespace printing {
Expand Down Expand Up @@ -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_;
Expand Down
15 changes: 6 additions & 9 deletions chrome/browser/resources/print_preview.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ <h1>Destination</h1>
<section>
<h3>Pages</h3>
<div>
<input id="pages" type="textbox"></input>
<input id="pages" type="text"></input>
<label>
<input id="all-pages" type="checkbox">
<span i18n-content="optionAllPages"></span>
Expand All @@ -42,7 +42,7 @@ <h3>Pages</h3>
<section>
<h3>Copies</h3>
<div>
<input id="copies" type="textbox"></input>
<input id="copies" type="text" value="1"></input>
<div>
<label>
<input id="collate" type="checkbox">
Expand All @@ -56,8 +56,8 @@ <h3>Copies</h3>
<h3>Layout</h3>
<div>
<select id="layout">
<option i18n-content="optionPortrait"></option>
<option i18n-content="optionLandscape"></option>
<option value="0" i18n-content="optionPortrait"></option>
<option value="1" i18n-content="optionLandscape"></option>
</select>
</div>
</section>
Expand All @@ -66,8 +66,8 @@ <h3>Layout</h3>
<h3>Color</h3>
<div>
<select id="color">
<option i18n-content="optionColor"></option>
<option i18n-content="optionBw"></option>
<option value="0" i18n-content="optionBw"></option>
<option value="1" i18n-content="optionColor"></option>
</select>
</div>
</section>
Expand All @@ -79,8 +79,5 @@ <h3>Color</h3>
<p id="no-plugin" class="hidden" i18n-content="noPlugin"></p>
</div>

<script>
console.log('in bottom script');
</script>
</body>
</html>
Loading

0 comments on commit 7868eca

Please sign in to comment.