Skip to content

Commit

Permalink
Printing: Implement PrintingContext::UseDefaultSettings for Linux/Cairo.
Browse files Browse the repository at this point in the history
BUG=none
TEST=none

Review URL: http://codereview.chromium.org/3389005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@60087 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
jhawkins@chromium.org committed Sep 21, 2010
1 parent b119a1e commit b719142
Show file tree
Hide file tree
Showing 17 changed files with 142 additions and 88 deletions.
18 changes: 13 additions & 5 deletions chrome/browser/printing/print_job_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ void PrintJobWorker::GetSettings(bool ask_user_for_settings,
parent_view, document_page_count,
has_selection));
#else
PrintingContext::Result result = printing_context_.AskUserForSettings(
parent_view, document_page_count, has_selection);
GetSettingsDone(result);
printing_context_.AskUserForSettings(
parent_view,
document_page_count,
has_selection,
NewCallback(this, &PrintJobWorker::GetSettingsDone));
#endif
} else {
PrintingContext::Result result = printing_context_.UseDefaultSettings();
Expand Down Expand Up @@ -116,8 +118,14 @@ void PrintJobWorker::GetSettingsWithUI(gfx::NativeView parent_view,
bool has_selection) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));

PrintingContext::Result result = printing_context_.AskUserForSettings(
parent_view, document_page_count, has_selection);
printing_context_.AskUserForSettings(
parent_view,
document_page_count,
has_selection,
NewCallback(this, &PrintJobWorker::GetSettingsWithUIDone));
}

void PrintJobWorker::GetSettingsWithUIDone(PrintingContext::Result result) {
message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
this, &PrintJobWorker::GetSettingsDone, result));
}
Expand Down
5 changes: 5 additions & 0 deletions chrome/browser/printing/print_job_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ class PrintJobWorker : public base::Thread {
void GetSettingsWithUI(gfx::NativeView parent_view,
int document_page_count,
bool has_selection);

// The callback used by PrintingContext::GetSettingsWithUI() to notify this
// object that the print settings are set. This is needed in order to bounce
// back into the IO thread for GetSettingsDone().
void GetSettingsWithUIDone(PrintingContext::Result result);
#endif

// Reports settings back to owner_.
Expand Down
10 changes: 0 additions & 10 deletions chrome/browser/renderer_host/resource_message_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ class WriteClipboardTask : public Task {
void RenderParamsFromPrintSettings(const printing::PrintSettings& settings,
ViewMsg_Print_Params* params) {
DCHECK(params);
#if defined(OS_WIN) || defined(OS_MACOSX)
params->page_size = settings.page_setup_device_units().physical_size();
params->printable_size.SetSize(
settings.page_setup_device_units().content_area().width(),
Expand All @@ -176,9 +175,6 @@ void RenderParamsFromPrintSettings(const printing::PrintSettings& settings,
// Always use an invalid cookie.
params->document_cookie = 0;
params->selection_only = settings.selection_only;
#else
NOTIMPLEMENTED();
#endif
}

class ClearCacheCompletion : public net::CompletionCallback {
Expand Down Expand Up @@ -497,10 +493,8 @@ bool ResourceMessageFilter::OnMessageReceived(const IPC::Message& msg) {
IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_ResolveProxy, OnResolveProxy)
IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_GetDefaultPrintSettings,
OnGetDefaultPrintSettings)
#if defined(OS_WIN) || defined(OS_MACOSX)
IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_ScriptedPrint,
OnScriptedPrint)
#endif
#if defined(OS_MACOSX)
IPC_MESSAGE_HANDLER(ViewHostMsg_AllocTransportDIB,
OnAllocTransportDIB)
Expand Down Expand Up @@ -1183,8 +1177,6 @@ void ResourceMessageFilter::OnGetDefaultPrintSettingsReply(
}
}

#if defined(OS_WIN) || defined(OS_MACOSX)

void ResourceMessageFilter::OnScriptedPrint(
const ViewHostMsg_ScriptedPrint_Params& params,
IPC::Message* reply_msg) {
Expand Down Expand Up @@ -1235,8 +1227,6 @@ void ResourceMessageFilter::OnScriptedPrintReply(
}
}

#endif // OS_WIN || OS_MACOSX

// static
Clipboard* ResourceMessageFilter::GetClipboard() {
// We have a static instance of the clipboard service for use by all message
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/renderer_host/resource_message_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ class ResourceMessageFilter : public IPC::ChannelProxy::MessageFilter,
void OnGetDefaultPrintSettingsReply(
scoped_refptr<printing::PrinterQuery> printer_query,
IPC::Message* reply_msg);
#if defined(OS_WIN) || defined(OS_MACOSX)

// A javascript code requested to print the current page. The renderer host
// have to show to the user the print dialog and returns the selected print
// settings.
Expand All @@ -313,7 +313,7 @@ class ResourceMessageFilter : public IPC::ChannelProxy::MessageFilter,
scoped_refptr<printing::PrinterQuery> printer_query,
int routing_id,
IPC::Message* reply_msg);
#endif

// Browser side transport DIB allocation
void OnAllocTransportDIB(size_t size,
bool cache_in_browser,
Expand Down
2 changes: 0 additions & 2 deletions chrome/common/render_messages_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1682,7 +1682,6 @@ IPC_BEGIN_MESSAGES(ViewHost)
IPC_SYNC_MESSAGE_ROUTED0_1(ViewHostMsg_GetDefaultPrintSettings,
ViewMsg_Print_Params /* default_settings */)

#if defined(OS_WIN) || defined(OS_MACOSX)
// It's the renderer that controls the printing process when it is generated
// by javascript. This step is about showing UI to the user to select the
// final print settings. The output parameter is the same as
Expand All @@ -1691,7 +1690,6 @@ IPC_BEGIN_MESSAGES(ViewHost)
ViewHostMsg_ScriptedPrint_Params,
ViewMsg_PrintPages_Params
/* settings chosen by the user*/)
#endif // defined(OS_WIN) || defined(OS_MACOSX)

// WebKit and JavaScript error messages to log to the console
// or debugger UI.
Expand Down
2 changes: 1 addition & 1 deletion chrome/renderer/mock_render_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ void MockRenderThread::OnMessageReceived(const IPC::Message& msg) {
IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWidget, OnMsgCreateWidget)
IPC_MESSAGE_HANDLER(ViewHostMsg_OpenChannelToExtension,
OnMsgOpenChannelToExtension)
#if defined(OS_WIN) || defined(OS_MACOSX)
IPC_MESSAGE_HANDLER(ViewHostMsg_GetDefaultPrintSettings,
OnGetDefaultPrintSettings)
IPC_MESSAGE_HANDLER(ViewHostMsg_ScriptedPrint,
OnScriptedPrint)
#if defined(OS_WIN) || defined(OS_MACOSX)
IPC_MESSAGE_HANDLER(ViewHostMsg_DidGetPrintedPagesCount,
OnDidGetPrintedPagesCount)
IPC_MESSAGE_HANDLER(ViewHostMsg_DidPrintPage, OnDidPrintPage)
Expand Down
2 changes: 0 additions & 2 deletions chrome/renderer/print_web_view_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ PrintWebViewHelper::PrintWebViewHelper(RenderView* render_view)

PrintWebViewHelper::~PrintWebViewHelper() {}

#if defined(OS_MACOSX) || defined(OS_WIN)
void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) {
const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2;
const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 2 * 60; // 2 Minutes.
Expand Down Expand Up @@ -224,7 +223,6 @@ void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) {
// printing resources, since we don't need them anymore.
DidFinishPrinting(print_cancelled);
}
#endif // OS_MACOSX || OS_WIN

void PrintWebViewHelper::DidFinishPrinting(bool success) {
if (!success) {
Expand Down
36 changes: 0 additions & 36 deletions chrome/renderer/print_web_view_helper_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,42 +46,6 @@ static void FillDefaultPrintParams(ViewMsg_Print_Params* params) {
params->desired_dpi = params->dpi;
}

void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) {
// If still not finished with earlier print request simply ignore.
if (IsPrinting())
return;

ViewMsg_Print_Params default_settings;
FillDefaultPrintParams(&default_settings);
double content_width, content_height;

{
// PrepareFrameAndViewForPrint instance must be destructed before calling
// PrintPages where another instance is created.
PrepareFrameAndViewForPrint prepare(default_settings,
frame,
frame->view());
GetPageSizeAndMarginsInPoints(frame, 0, default_settings,
&content_width, &content_height,
NULL, NULL, NULL, NULL);
}

default_settings.dpi = printing::kPointsPerInch;
default_settings.min_shrink = 1.25;
default_settings.max_shrink = 2.0;
default_settings.desired_dpi = printing::kPointsPerInch;
default_settings.document_cookie = 0;
default_settings.selection_only = false;

default_settings.printable_size = gfx::Size(
static_cast<int>(content_width), static_cast<int>(content_height));

ViewMsg_PrintPages_Params print_settings;
print_settings.params = default_settings;

PrintPages(print_settings, frame);
}

void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params,
WebFrame* frame) {
PrepareFrameAndViewForPrint prep_frame_view(params.params,
Expand Down
12 changes: 6 additions & 6 deletions gfx/native_widget_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,20 @@ static inline NativeView NativeViewFromId(NativeViewId id) {
return reinterpret_cast<NativeView>(id);
}
#define NativeViewFromIdInBrowser(x) NativeViewFromId(x)
#elif defined(OS_MACOSX)
// On the Mac, a NativeView is a pointer to an object, and is useless outside
// the process in which it was created. NativeViewFromId should only be used
// inside the appropriate platform ifdef outside of the browser.
#elif defined(OS_MACOSX) || defined(USE_X11)
// On Mac and Linux, a NativeView is a pointer to an object, and is useless
// outside the process in which it was created. NativeViewFromId should only be
// used inside the appropriate platform ifdef outside of the browser.
// (NativeViewFromIdInBrowser can be used everywhere in the browser.) If your
// cross-platform design involves a call to NativeViewFromId from outside the
// browser it will never work on the Mac and is fundamentally broken.
// browser it will never work on Mac or Linux and is fundamentally broken.

// Please do not call this from outside the browser. It won't work; the name
// should give you a subtle hint.
static inline NativeView NativeViewFromIdInBrowser(NativeViewId id) {
return reinterpret_cast<NativeView>(id);
}
#endif
#endif // defined(OS_MACOSX) || defined(USE_X11)

// Convert a NativeView to a NativeViewId. See the comments at the top of
// this file.
Expand Down
41 changes: 40 additions & 1 deletion printing/print_settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@

#include "printing/print_settings.h"

// TODO(jhawkins): Move platform-specific implementations to their own files.
#if defined(USE_X11)
#include <gtk/gtk.h>
#endif // defined(USE_X11)

#include "base/atomic_sequence_num.h"
#include "base/logging.h"
#include "base/string_piece.h"
#include "base/sys_string_conversions.h"
#include "base/utf_string_conversions.h"
#include "printing/units.h"

namespace printing {
Expand Down Expand Up @@ -37,7 +44,7 @@ void PrintSettings::Clear() {
landscape_ = false;
}

#ifdef WIN32
#if defined(OS_WIN)
void PrintSettings::Init(HDC hdc,
const DEVMODE& dev_mode,
const PageRanges& new_ranges,
Expand Down Expand Up @@ -119,6 +126,38 @@ void PrintSettings::Init(PMPrinter printer, PMPageFormat page_format,
printable_area_device_units,
72);
}
#elif defined(OS_LINUX)
void PrintSettings::Init(GtkPrintSettings* settings,
GtkPageSetup* page_setup,
const PageRanges& new_ranges,
bool print_selection_only) {
// TODO(jhawkins): |printer_name_| and |device_name_| should be string16.
base::StringPiece name(
reinterpret_cast<const char*>(gtk_print_settings_get_printer(settings)));
printer_name_ = UTF8ToWide(name);
device_name_ = printer_name_;
ranges = new_ranges;

GtkPageOrientation orientation = gtk_print_settings_get_orientation(settings);
landscape_ = orientation == GTK_PAGE_ORIENTATION_LANDSCAPE;
selection_only = print_selection_only;

dpi_ = gtk_print_settings_get_resolution(settings);

// Initialize page_setup_device_units_.
gfx::Size physical_size_device_units(
gtk_page_setup_get_paper_width(page_setup, GTK_UNIT_INCH) * dpi_,
gtk_page_setup_get_paper_height(page_setup, GTK_UNIT_INCH) * dpi_);
gfx::Rect printable_area_device_units(
gtk_page_setup_get_left_margin(page_setup, GTK_UNIT_INCH) * dpi_,
gtk_page_setup_get_top_margin(page_setup, GTK_UNIT_INCH) * dpi_,
gtk_page_setup_get_page_width(page_setup, GTK_UNIT_INCH) * dpi_,
gtk_page_setup_get_page_height(page_setup, GTK_UNIT_INCH) * dpi_);

SetPrinterPrintableArea(physical_size_device_units,
printable_area_device_units,
dpi_);
}
#endif

void PrintSettings::SetPrinterPrintableArea(
Expand Down
9 changes: 9 additions & 0 deletions printing/print_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

typedef struct HDC__* HDC;
typedef struct _devicemodeW DEVMODE;
typedef struct _GtkPrintSettings GtkPrintSettings;
typedef struct _GtkPageSetup GtkPageSetup;

namespace printing {

Expand All @@ -39,6 +41,13 @@ class PrintSettings {
// Reads the settings from the given PMPrinter and PMPageFormat.
void Init(PMPrinter printer, PMPageFormat page_format,
const PageRanges& new_ranges, bool print_selection_only);
#elif defined(OS_LINUX)
// Initializes the settings from the given GtkPrintSettings and GtkPageSetup.
// TODO(jhawkins): This method is a mess across the platforms. Refactor.
void Init(GtkPrintSettings* settings,
GtkPageSetup* page_setup,
const PageRanges& new_ranges,
bool print_selection_onl);
#endif

// Set printer printable area in in device units.
Expand Down
2 changes: 2 additions & 0 deletions printing/printing.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
'dependencies': [
# For FT_Init_FreeType and friends.
'../build/linux/system.gyp:freetype2',
'../build/linux/system.gyp:gtk',
'../build/linux/system.gyp:gtkprint',
],
}],
],
Expand Down
15 changes: 12 additions & 3 deletions printing/printing_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <string>

#include "base/basictypes.h"
#include "base/callback.h"
#if !(defined(OS_WIN) || defined(OS_MACOSX))
// TODO(port) Remove after implementing PrintingContext::context()
#include "base/logging.h"
Expand Down Expand Up @@ -50,10 +51,18 @@ class PrintingContext {
PrintingContext();
~PrintingContext();

// Callback of AskUserForSettings, used to notify the PrintJobWorker when
// print settings are available.
typedef Callback1<Result>::Type PrintSettingsCallback;

// Asks the user what printer and format should be used to print. Updates the
// context with the select device settings.
Result AskUserForSettings(gfx::NativeView parent_view, int max_pages,
bool has_selection);
// context with the select device settings. The result of the call is returned
// in the callback. This is necessary for Linux, which only has an
// asynchronous printing API.
void AskUserForSettings(gfx::NativeView parent_view,
int max_pages,
bool has_selection,
PrintSettingsCallback* callback);

#if defined(OS_WIN) && defined(UNIT_TEST)
// Sets a fake PrintDlgEx function pointer in tests.
Expand Down
Loading

0 comments on commit b719142

Please sign in to comment.