namespace {
-#if defined(OS_CHROMEOS)
-typedef std::map<int, base::FilePath> SequenceToPathMap;
-
-struct PrintingSequencePathMap {
- SequenceToPathMap map;
- int sequence;
-};
-
-// No locking, only access on the FILE thread.
-static base::LazyInstance<PrintingSequencePathMap>
- g_printing_file_descriptor_map = LAZY_INSTANCE_INITIALIZER;
-#endif
-
void RenderParamsFromPrintSettings(const printing::PrintSettings& settings,
PrintMsg_Print_Params* params) {
params->page_size = settings.page_setup_device_units().physical_size();
PrintingMessageFilter::~PrintingMessageFilter() {
}
-void PrintingMessageFilter::OverrideThreadForMessage(
- const IPC::Message& message, BrowserThread::ID* thread) {
-#if defined(OS_CHROMEOS)
- if (message.type() == PrintHostMsg_AllocateTempFileForPrinting::ID ||
- message.type() == PrintHostMsg_TempFileForPrintingWritten::ID) {
- *thread = BrowserThread::FILE;
- }
-#elif defined(OS_ANDROID)
- if (message.type() == PrintHostMsg_AllocateTempFileForPrinting::ID ||
- message.type() == PrintHostMsg_TempFileForPrintingWritten::ID) {
- *thread = BrowserThread::UI;
- }
-#endif
-}
-
bool PrintingMessageFilter::OnMessageReceived(const IPC::Message& message,
bool* message_was_ok) {
bool handled = true;
#if defined(OS_WIN)
IPC_MESSAGE_HANDLER(PrintHostMsg_DuplicateSection, OnDuplicateSection)
#endif
- IPC_MESSAGE_HANDLER(PrintHostMsg_IsPrintingEnabled, OnIsPrintingEnabled)
IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings,
OnGetDefaultPrintSettings)
IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_ScriptedPrint, OnScriptedPrint)
- IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_UpdatePrintSettings,
- OnUpdatePrintSettings)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
callback.Run();
}
-void PrintingMessageFilter::OnIsPrintingEnabled(bool* is_enabled) {
- DCHECK_CURRENTLY_ON(BrowserThread::IO);
- *is_enabled = true;
-}
-
void PrintingMessageFilter::OnGetDefaultPrintSettings(IPC::Message* reply_msg) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
scoped_refptr<printing::PrinterQuery> printer_query;
scoped_refptr<printing::PrinterQuery> printer_query,
IPC::Message* reply_msg) {
PrintMsg_PrintPages_Params params;
-#if defined(OS_ANDROID)
- // We need to save the routing ID here because Send method below deletes the
- // |reply_msg| before we can get the routing ID for the Android code.
- int routing_id = reply_msg->routing_id();
-#endif
if (printer_query->last_status() != printing::PrintingContext::OK ||
!printer_query->settings().dpi()) {
params.Reset();
PrintHostMsg_ScriptedPrint::WriteReplyParams(reply_msg, params);
Send(reply_msg);
if (params.params.dpi && params.params.document_cookie) {
-#if defined(OS_ANDROID)
- int file_descriptor;
- const base::string16& device_name = printer_query->settings().device_name();
- if (base::StringToInt(device_name, &file_descriptor)) {
- BrowserThread::PostTask(
- BrowserThread::UI, FROM_HERE,
- base::Bind(&PrintingMessageFilter::UpdateFileDescriptor, this,
- routing_id, file_descriptor));
- }
-#endif
queue_->QueuePrinterQuery(printer_query.get());
} else {
printer_query->StopWorker();
}
}
-
-void PrintingMessageFilter::OnUpdatePrintSettings(
- int document_cookie, const base::DictionaryValue& job_settings,
- IPC::Message* reply_msg) {
- scoped_refptr<printing::PrinterQuery> printer_query;
- if (false) {
- // Reply with NULL query.
- OnUpdatePrintSettingsReply(printer_query, reply_msg);
- return;
- }
- printer_query = queue_->PopPrinterQuery(document_cookie);
- if (!printer_query)
- printer_query = queue_->CreatePrinterQuery();
- printer_query->SetSettings(
- job_settings,
- base::Bind(&PrintingMessageFilter::OnUpdatePrintSettingsReply, this,
- printer_query, reply_msg));
-}
-
-void PrintingMessageFilter::OnUpdatePrintSettingsReply(
- scoped_refptr<printing::PrinterQuery> printer_query,
- IPC::Message* reply_msg) {
- PrintMsg_PrintPages_Params params;
- if (!printer_query.get() ||
- printer_query->last_status() != printing::PrintingContext::OK) {
- params.Reset();
- } else {
- RenderParamsFromPrintSettings(printer_query->settings(), ¶ms.params);
- params.params.document_cookie = printer_query->cookie();
- params.pages =
- printing::PageRange::GetPages(printer_query->settings().ranges());
- }
- PrintHostMsg_UpdatePrintSettings::WriteReplyParams(reply_msg, params);
- Send(reply_msg);
- // If user hasn't cancelled.
- if (printer_query.get()) {
- if (printer_query->cookie() && printer_query->settings().dpi()) {
- queue_->QueuePrinterQuery(printer_query.get());
- } else {
- printer_query->StopWorker();
- }
- }
-}
explicit PrintingMessageFilter(int render_process_id);
// content::BrowserMessageFilter methods.
- virtual void OverrideThreadForMessage(
- const IPC::Message& message,
- content::BrowserThread::ID* thread) OVERRIDE;
virtual bool OnMessageReceived(const IPC::Message& message,
bool* message_was_ok) OVERRIDE;
const base::Closure& callback,
scoped_refptr<printing::PrinterQuery> printer_query);
- // Checks if printing is enabled.
- void OnIsPrintingEnabled(bool* is_enabled);
-
// Get the default print setting.
void OnGetDefaultPrintSettings(IPC::Message* reply_msg);
void OnGetDefaultPrintSettingsReply(
scoped_refptr<printing::PrinterQuery> printer_query,
IPC::Message* reply_msg);
- // Modify the current print settings based on |job_settings|. The task is
- // handled by the print worker thread and the UI thread. The reply occurs on
- // the IO thread.
- void OnUpdatePrintSettings(int document_cookie,
- const base::DictionaryValue& job_settings,
- IPC::Message* reply_msg);
- void OnUpdatePrintSettingsReply(
- scoped_refptr<printing::PrinterQuery> printer_query,
- IPC::Message* reply_msg);
-
const int render_process_id_;
scoped_refptr<printing::PrintQueriesQueue> queue_;
// Messages sent from the browser to the renderer.
-// Tells the render frame to initiate printing or print preview for a particular
-// node, depending on which mode the render frame is in.
-IPC_MESSAGE_ROUTED0(PrintMsg_PrintNodeUnderContextMenu)
-
// Tells the render view to switch the CSS to print media type, renders every
// requested pages and switch back the CSS to display media type.
IPC_MESSAGE_ROUTED0(PrintMsg_PrintPages)
base::SharedMemoryHandle /* browser handle */)
#endif
-// Check if printing is enabled.
-IPC_SYNC_MESSAGE_ROUTED0_1(PrintHostMsg_IsPrintingEnabled,
- bool /* is_enabled */)
-
// Tells the browser that the renderer is done calculating the number of
// rendered pages according to the specified settings.
IPC_MESSAGE_ROUTED2(PrintHostMsg_DidGetPrintedPagesCount,
IPC_SYNC_MESSAGE_ROUTED0_1(PrintHostMsg_GetDefaultPrintSettings,
PrintMsg_Print_Params /* default_settings */)
-// The renderer wants to update the current print settings with new
-// |job_settings|.
-IPC_SYNC_MESSAGE_ROUTED2_1(PrintHostMsg_UpdatePrintSettings,
- int /* document_cookie */,
- base::DictionaryValue /* job_settings */,
- PrintMsg_PrintPages_Params /* current_settings */)
-
// 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
PrintMsg_PrintPages_Params
/* settings chosen by the user*/)
-#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
-// Asks the browser to create a temporary file for the renderer to fill
-// in resulting NativeMetafile in printing.
-IPC_SYNC_MESSAGE_CONTROL1_2(PrintHostMsg_AllocateTempFileForPrinting,
- int /* render_view_id */,
- base::FileDescriptor /* temp file fd */,
- int /* fd in browser*/) // Used only by Chrome OS.
-IPC_MESSAGE_CONTROL2(PrintHostMsg_TempFileForPrintingWritten,
- int /* render_view_id */,
- int /* fd in browser */) // Used only by Chrome OS.
-#endif
-
-// Notify the browser of the default page layout according to the currently
-// selected printer and page size.
-// |printable_area_in_points| Specifies the printable area in points.
-// |has_custom_page_size_style| is true when the printing frame has a custom
-// page size css otherwise false.
-IPC_MESSAGE_ROUTED3(PrintHostMsg_DidGetDefaultPageLayout,
- printing::PageSizeMargins /* page layout in points */,
- gfx::Rect /* printable area in points */,
- bool /* has custom page size style */)
-
// This is sent when there are invalid printer settings.
IPC_MESSAGE_ROUTED0(PrintHostMsg_ShowInvalidPrinterSettingsError)
const double kMinDpi = 1.0;
-const char kPageLoadScriptFormat[] =
- "document.open(); document.write(%s); document.close();";
-
-const char kPageSetupScriptFormat[] = "setup(%s);";
-
-void ExecuteScript(blink::WebFrame* frame,
- const char* script_format,
- const base::Value& parameters) {
- std::string json;
- base::JSONWriter::Write(¶meters, &json);
- std::string script = base::StringPrintf(script_format, json.c_str());
- frame->executeScript(blink::WebString(base::UTF8ToUTF16(script)));
-}
-
int GetDPI(const PrintMsg_Print_Params* print_params) {
#if defined(OS_MACOSX)
// On the Mac, the printable area is in points, don't do any scaling based
return view_;
}
-// static - Not anonymous so that platform implementations can use it.
-void PrintWebViewHelper::PrintHeaderAndFooter(
- blink::WebCanvas* canvas,
- int page_number,
- int total_pages,
- float webkit_scale_factor,
- const PageSizeMargins& page_layout,
- const base::DictionaryValue& header_footer_info,
- const PrintMsg_Print_Params& params) {
- skia::VectorPlatformDeviceSkia* device =
- static_cast<skia::VectorPlatformDeviceSkia*>(canvas->getTopDevice());
- device->setDrawingArea(SkPDFDevice::kMargin_DrawingArea);
-
- SkAutoCanvasRestore auto_restore(canvas, true);
- canvas->scale(1 / webkit_scale_factor, 1 / webkit_scale_factor);
-
- blink::WebSize page_size(page_layout.margin_left + page_layout.margin_right +
- page_layout.content_width,
- page_layout.margin_top + page_layout.margin_bottom +
- page_layout.content_height);
-
- blink::WebView* web_view = blink::WebView::create(NULL);
- web_view->settings()->setJavaScriptEnabled(true);
-
- blink::WebLocalFrame* frame = blink::WebLocalFrame::create(NULL);
- web_view->setMainFrame(frame);
-
- base::StringValue html("Print Preview");
- // Load page with script to avoid async operations.
- ExecuteScript(frame, kPageLoadScriptFormat, html);
-
- scoped_ptr<base::DictionaryValue> options(header_footer_info.DeepCopy());
- options->SetDouble("width", page_size.width);
- options->SetDouble("height", page_size.height);
- options->SetDouble("topMargin", page_layout.margin_top);
- options->SetDouble("bottomMargin", page_layout.margin_bottom);
- options->SetString("pageNumber",
- base::StringPrintf("%d/%d", page_number, total_pages));
-
- ExecuteScript(frame, kPageSetupScriptFormat, *options);
-
- blink::WebPrintParams webkit_params(page_size);
- webkit_params.printerDPI = GetDPI(¶ms);
-
- frame->printBegin(webkit_params);
- frame->printPage(0, canvas);
- frame->printEnd();
-
- web_view->close();
- frame->close();
-
- device->setDrawingArea(SkPDFDevice::kContent_DrawingArea);
-}
-
// static - Not anonymous so that platform implementations can use it.
float PrintWebViewHelper::RenderPageContent(blink::WebFrame* frame,
int page_number,
PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view)
: content::RenderViewObserver(render_view),
content::RenderViewObserverTracker<PrintWebViewHelper>(render_view),
- reset_prep_frame_view_(false),
is_print_ready_metafile_sent_(false),
ignore_css_margins_(false),
notify_browser_of_print_failure_(true),
print_node_in_progress_(false),
- is_loading_(false),
weak_ptr_factory_(this) {
}
PrintWebViewHelper::~PrintWebViewHelper() {}
-void PrintWebViewHelper::DidStartLoading() {
- is_loading_ = true;
-}
-
-void PrintWebViewHelper::DidStopLoading() {
- is_loading_ = false;
-}
-
// Prints |frame| which called window.print().
void PrintWebViewHelper::PrintPage(blink::WebLocalFrame* frame,
bool user_initiated) {
DidFinishPrinting(success ? OK : FAIL_PRINT);
}
-bool PrintWebViewHelper::IsPrintingEnabled() {
- bool result = false;
- Send(new PrintHostMsg_IsPrintingEnabled(routing_id(), &result));
- return result;
-}
-
void PrintWebViewHelper::PrintNode(const blink::WebNode& node) {
if (node.isNull() || !node.document().frame()) {
// This can occur when the context menu refers to an invalid WebNode.
#include <vector>
-#include "base/gtest_prod_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/shared_memory.h"
#include "base/memory/weak_ptr.h"
explicit PrintWebViewHelper(content::RenderView* render_view);
virtual ~PrintWebViewHelper();
- bool IsPrintingEnabled();
-
void PrintNode(const blink::WebNode& node);
private:
- friend class PrintWebViewHelperTestBase;
- FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperTest,
- BlockScriptInitiatedPrinting);
- FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperTest,
- BlockScriptInitiatedPrintingFromPopup);
- FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperTest, OnPrintPages);
-
-#if defined(OS_WIN) || defined(OS_MACOSX)
- FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperTest, PrintLayoutTest);
- FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperTest, PrintWithIframe);
-#endif // defined(OS_WIN) || defined(OS_MACOSX)
-
enum PrintingResult {
OK,
FAIL_PRINT_INIT,
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
virtual void PrintPage(blink::WebLocalFrame* frame,
bool user_initiated) OVERRIDE;
- virtual void DidStartLoading() OVERRIDE;
- virtual void DidStopLoading() OVERRIDE;
// Message handlers ---------------------------------------------------------
void OnPrintPages();
- void OnPrintForSystemDialog();
void OnPrintingDone(bool success);
// Get |page_size| and |content_area| information from
// Returns true if the current destination printer is PRINT_TO_PDF.
bool IsPrintToPdfRequested(const base::DictionaryValue& settings);
- // Enable/Disable window.print calls. If |blocked| is true window.print
- // calls will silently fail. Call with |blocked| set to false to reenable.
- void SetScriptedPrintBlocked(bool blocked);
-
// Main printing code -------------------------------------------------------
void Print(blink::WebLocalFrame* frame, const blink::WebNode& node);
double* scale_factor,
PageSizeMargins* page_layout_in_points);
- // Given the |device| and |canvas| to draw on, prints the appropriate headers
- // and footers using strings from |header_footer_info| on to the canvas.
- static void PrintHeaderAndFooter(
- blink::WebCanvas* canvas,
- int page_number,
- int total_pages,
- float webkit_scale_factor,
- const PageSizeMargins& page_layout_in_points,
- const base::DictionaryValue& header_footer_info,
- const PrintMsg_Print_Params& params);
-
bool GetPrintFrame(blink::WebLocalFrame** frame);
// Script Initiated Printing ------------------------------------------------
// WebView used only to print the selection.
scoped_ptr<PrepareFrameAndViewForPrint> prep_frame_view_;
- bool reset_prep_frame_view_;
scoped_ptr<PrintMsg_PrintPages_Params> print_pages_params_;
- bool is_preview_enabled_;
- bool is_scripted_print_throttling_disabled_;
bool is_print_ready_metafile_sent_;
bool ignore_css_margins_;
// the failure came from the browser in the first place.
bool notify_browser_of_print_failure_;
- // Strings generated by the browser process to be printed as headers and
- // footers if requested by the user.
- scoped_ptr<base::DictionaryValue> header_footer_info_;
-
bool print_node_in_progress_;
- bool is_loading_;
- bool is_scripted_preview_delayed_;
+
base::WeakPtrFactory<PrintWebViewHelper> weak_ptr_factory_;
+
DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper);
};