Linux: Pass CefBrowser to CefPrintHandler callbacks (issue #2196)

This commit is contained in:
Marshall Greenblatt
2017-06-15 08:27:56 -07:00
parent 967f7e9c76
commit 1ebbf3c4da
10 changed files with 503 additions and 285 deletions

View File

@@ -7,10 +7,15 @@
#include <vector>
#include <gtk/gtk.h>
#include <gtk/gtkunixprint.h>
#include "include/base/cef_logging.h"
#include "include/base/cef_macros.h"
#include "include/wrapper/cef_helpers.h"
#include "tests/cefclient/browser/root_window.h"
namespace client {
namespace {
@@ -267,161 +272,328 @@ void InitPrintSettings(GtkPrintSettings* settings,
} // namespace
ClientPrintHandlerGtk::ClientPrintHandlerGtk()
: dialog_(NULL), gtk_settings_(NULL), page_setup_(NULL), printer_(NULL) {}
struct ClientPrintHandlerGtk::PrintHandler {
PrintHandler(CefRefPtr<CefBrowser> browser)
: browser_(browser),
dialog_(NULL),
gtk_settings_(NULL),
page_setup_(NULL),
printer_(NULL) {}
void ClientPrintHandlerGtk::OnPrintStart(CefRefPtr<CefBrowser> browser) {}
void ClientPrintHandlerGtk::OnPrintSettings(
CefRefPtr<CefPrintSettings> settings,
bool get_defaults) {
if (get_defaults) {
DCHECK(!page_setup_);
DCHECK(!printer_);
// |gtk_settings_| is a new copy.
gtk_settings_ = gtk_print_settings_copy(GetLastUsedSettings()->settings());
page_setup_ = gtk_page_setup_new();
} else {
if (!gtk_settings_) {
gtk_settings_ =
gtk_print_settings_copy(GetLastUsedSettings()->settings());
~PrintHandler() {
if (dialog_) {
gtk_widget_destroy(dialog_);
dialog_ = NULL;
}
if (gtk_settings_) {
g_object_unref(gtk_settings_);
gtk_settings_ = NULL;
}
if (page_setup_) {
g_object_unref(page_setup_);
page_setup_ = NULL;
}
GtkPrinterList* printer_list = new GtkPrinterList;
printer_ = printer_list->GetPrinterWithName(settings->GetDeviceName());
if (printer_) {
g_object_ref(printer_);
gtk_print_settings_set_printer(gtk_settings_,
gtk_printer_get_name(printer_));
if (!page_setup_) {
page_setup_ = gtk_printer_get_default_page_size(printer_);
}
g_object_unref(printer_);
printer_ = NULL;
}
gtk_print_settings_set_n_copies(gtk_settings_, settings->GetCopies());
gtk_print_settings_set_collate(gtk_settings_, settings->WillCollate());
std::string color_value;
std::string color_setting_name;
GetColorModelForMode(settings->GetColorModel(), &color_setting_name,
&color_value);
gtk_print_settings_set(gtk_settings_, color_setting_name.c_str(),
color_value.c_str());
if (settings->GetDuplexMode() != DUPLEX_MODE_UNKNOWN) {
const char* cups_duplex_mode = NULL;
switch (settings->GetDuplexMode()) {
case DUPLEX_MODE_LONG_EDGE:
cups_duplex_mode = kDuplexNoTumble;
break;
case DUPLEX_MODE_SHORT_EDGE:
cups_duplex_mode = kDuplexTumble;
break;
case DUPLEX_MODE_SIMPLEX:
cups_duplex_mode = kDuplexNone;
break;
default: // UNKNOWN_DUPLEX_MODE
NOTREACHED();
break;
}
gtk_print_settings_set(gtk_settings_, kCUPSDuplex, cups_duplex_mode);
}
if (!page_setup_)
page_setup_ = gtk_page_setup_new();
gtk_print_settings_set_orientation(
gtk_settings_, settings->IsLandscape() ? GTK_PAGE_ORIENTATION_LANDSCAPE
: GTK_PAGE_ORIENTATION_PORTRAIT);
delete printer_list;
}
InitPrintSettings(gtk_settings_, page_setup_, settings);
void OnPrintSettings(CefRefPtr<CefPrintSettings> settings,
bool get_defaults) {
if (get_defaults) {
DCHECK(!page_setup_);
DCHECK(!printer_);
// |gtk_settings_| is a new copy.
gtk_settings_ =
gtk_print_settings_copy(GetLastUsedSettings()->settings());
page_setup_ = gtk_page_setup_new();
} else {
if (!gtk_settings_) {
gtk_settings_ =
gtk_print_settings_copy(GetLastUsedSettings()->settings());
}
GtkPrinterList* printer_list = new GtkPrinterList;
printer_ = printer_list->GetPrinterWithName(settings->GetDeviceName());
if (printer_) {
g_object_ref(printer_);
gtk_print_settings_set_printer(gtk_settings_,
gtk_printer_get_name(printer_));
if (!page_setup_) {
page_setup_ = gtk_printer_get_default_page_size(printer_);
}
}
gtk_print_settings_set_n_copies(gtk_settings_, settings->GetCopies());
gtk_print_settings_set_collate(gtk_settings_, settings->WillCollate());
std::string color_value;
std::string color_setting_name;
GetColorModelForMode(settings->GetColorModel(), &color_setting_name,
&color_value);
gtk_print_settings_set(gtk_settings_, color_setting_name.c_str(),
color_value.c_str());
if (settings->GetDuplexMode() != DUPLEX_MODE_UNKNOWN) {
const char* cups_duplex_mode = NULL;
switch (settings->GetDuplexMode()) {
case DUPLEX_MODE_LONG_EDGE:
cups_duplex_mode = kDuplexNoTumble;
break;
case DUPLEX_MODE_SHORT_EDGE:
cups_duplex_mode = kDuplexTumble;
break;
case DUPLEX_MODE_SIMPLEX:
cups_duplex_mode = kDuplexNone;
break;
default: // UNKNOWN_DUPLEX_MODE
NOTREACHED();
break;
}
gtk_print_settings_set(gtk_settings_, kCUPSDuplex, cups_duplex_mode);
}
if (!page_setup_)
page_setup_ = gtk_page_setup_new();
gtk_print_settings_set_orientation(gtk_settings_,
settings->IsLandscape()
? GTK_PAGE_ORIENTATION_LANDSCAPE
: GTK_PAGE_ORIENTATION_PORTRAIT);
delete printer_list;
}
InitPrintSettings(gtk_settings_, page_setup_, settings);
}
bool OnPrintDialog(bool has_selection,
CefRefPtr<CefPrintDialogCallback> callback) {
dialog_callback_ = callback;
GtkWindow* parent = GetWindow();
// TODO(estade): We need a window title here.
dialog_ = gtk_print_unix_dialog_new(NULL, parent);
g_signal_connect(dialog_, "delete-event",
G_CALLBACK(gtk_widget_hide_on_delete), NULL);
// Set modal so user cannot focus the same tab and press print again.
gtk_window_set_modal(GTK_WINDOW(dialog_), TRUE);
// Since we only generate PDF, only show printers that support PDF.
// TODO(thestig) Add more capabilities to support?
GtkPrintCapabilities cap = static_cast<GtkPrintCapabilities>(
GTK_PRINT_CAPABILITY_GENERATE_PDF | GTK_PRINT_CAPABILITY_PAGE_SET |
GTK_PRINT_CAPABILITY_COPIES | GTK_PRINT_CAPABILITY_COLLATE |
GTK_PRINT_CAPABILITY_REVERSE);
gtk_print_unix_dialog_set_manual_capabilities(
GTK_PRINT_UNIX_DIALOG(dialog_), cap);
gtk_print_unix_dialog_set_embed_page_setup(GTK_PRINT_UNIX_DIALOG(dialog_),
TRUE);
gtk_print_unix_dialog_set_support_selection(GTK_PRINT_UNIX_DIALOG(dialog_),
TRUE);
gtk_print_unix_dialog_set_has_selection(GTK_PRINT_UNIX_DIALOG(dialog_),
has_selection);
gtk_print_unix_dialog_set_settings(GTK_PRINT_UNIX_DIALOG(dialog_),
gtk_settings_);
g_signal_connect(dialog_, "response", G_CALLBACK(OnDialogResponseThunk),
this);
gtk_widget_show(dialog_);
return true;
}
bool OnPrintJob(const CefString& document_name,
const CefString& pdf_file_path,
CefRefPtr<CefPrintJobCallback> callback) {
// If |printer_| is NULL then somehow the GTK printer list changed out under
// us. In which case, just bail out.
if (!printer_)
return false;
job_callback_ = callback;
// Save the settings for next time.
GetLastUsedSettings()->SetLastUsedSettings(gtk_settings_);
GtkPrintJob* print_job = gtk_print_job_new(
document_name.ToString().c_str(), printer_, gtk_settings_, page_setup_);
gtk_print_job_set_source_file(print_job, pdf_file_path.ToString().c_str(),
NULL);
gtk_print_job_send(print_job, OnJobCompletedThunk, this, NULL);
return true;
}
private:
// Returns the GtkWindow* for the browser. Will return NULL when using the
// Views framework.
GtkWindow* GetWindow() {
scoped_refptr<RootWindow> root_window =
RootWindow::GetForBrowser(browser_->GetIdentifier());
if (root_window)
return GTK_WINDOW(root_window->GetWindowHandle());
return NULL;
}
void OnDialogResponse(GtkDialog* dialog, gint response_id) {
int num_matched_handlers = g_signal_handlers_disconnect_by_func(
dialog_, reinterpret_cast<gpointer>(&OnDialogResponseThunk), this);
DCHECK_EQ(1, num_matched_handlers);
gtk_widget_hide(dialog_);
switch (response_id) {
case GTK_RESPONSE_OK: {
if (gtk_settings_)
g_object_unref(gtk_settings_);
gtk_settings_ =
gtk_print_unix_dialog_get_settings(GTK_PRINT_UNIX_DIALOG(dialog_));
if (printer_)
g_object_unref(printer_);
printer_ = gtk_print_unix_dialog_get_selected_printer(
GTK_PRINT_UNIX_DIALOG(dialog_));
g_object_ref(printer_);
if (page_setup_)
g_object_unref(page_setup_);
page_setup_ = gtk_print_unix_dialog_get_page_setup(
GTK_PRINT_UNIX_DIALOG(dialog_));
g_object_ref(page_setup_);
// Handle page ranges.
CefPrintSettings::PageRangeList ranges_vector;
gint num_ranges;
bool print_selection_only = false;
switch (gtk_print_settings_get_print_pages(gtk_settings_)) {
case GTK_PRINT_PAGES_RANGES: {
GtkPageRange* gtk_range =
gtk_print_settings_get_page_ranges(gtk_settings_, &num_ranges);
if (gtk_range) {
for (int i = 0; i < num_ranges; ++i) {
ranges_vector.push_back(
CefRange(gtk_range[i].start, gtk_range[i].end));
}
g_free(gtk_range);
}
break;
}
case GTK_PRINT_PAGES_SELECTION:
print_selection_only = true;
break;
case GTK_PRINT_PAGES_ALL:
// Leave |ranges_vector| empty to indicate print all pages.
break;
case GTK_PRINT_PAGES_CURRENT:
default:
NOTREACHED();
break;
}
CefRefPtr<CefPrintSettings> settings = CefPrintSettings::Create();
settings->SetPageRanges(ranges_vector);
settings->SetSelectionOnly(print_selection_only);
InitPrintSettings(gtk_settings_, page_setup_, settings);
dialog_callback_->Continue(settings);
dialog_callback_ = NULL;
return;
}
case GTK_RESPONSE_DELETE_EVENT: // Fall through.
case GTK_RESPONSE_CANCEL: {
dialog_callback_->Cancel();
dialog_callback_ = NULL;
return;
}
case GTK_RESPONSE_APPLY:
default: { NOTREACHED(); }
}
}
void OnJobCompleted(GtkPrintJob* print_job, GError* error) {
job_callback_->Continue();
job_callback_ = NULL;
}
static void OnDialogResponseThunk(GtkDialog* dialog,
gint response_id,
PrintHandler* handler) {
handler->OnDialogResponse(dialog, response_id);
}
static void OnJobCompletedThunk(GtkPrintJob* print_job,
void* handler,
GError* error) {
static_cast<PrintHandler*>(handler)->OnJobCompleted(print_job, error);
}
CefRefPtr<CefBrowser> browser_;
GtkWidget* dialog_; // Owned.
GtkPrintSettings* gtk_settings_; // Referenced.
GtkPageSetup* page_setup_; // Referenced.
GtkPrinter* printer_; // Referenced.
CefRefPtr<CefPrintDialogCallback> dialog_callback_;
CefRefPtr<CefPrintJobCallback> job_callback_;
};
ClientPrintHandlerGtk::ClientPrintHandlerGtk() {}
ClientPrintHandlerGtk::~ClientPrintHandlerGtk() {
DCHECK(print_handler_map_.empty());
}
void ClientPrintHandlerGtk::OnPrintStart(CefRefPtr<CefBrowser> browser) {
const int browser_id = browser->GetIdentifier();
#ifndef _NDEBUG
// Print handler should not already exist for the browser.
PrintHandlerMap::const_iterator it = print_handler_map_.find(browser_id);
DCHECK(it == print_handler_map_.end());
#endif
// Create a new print handler.
PrintHandler* ph = new PrintHandler(browser);
print_handler_map_.insert(std::make_pair(browser_id, ph));
}
void ClientPrintHandlerGtk::OnPrintSettings(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefPrintSettings> settings,
bool get_defaults) {
GetPrintHandler(browser)->OnPrintSettings(settings, get_defaults);
}
bool ClientPrintHandlerGtk::OnPrintDialog(
CefRefPtr<CefBrowser> browser,
bool has_selection,
CefRefPtr<CefPrintDialogCallback> callback) {
dialog_callback_ = callback;
// TODO(cef): Identify the correct parent window.
GtkWindow* parent = NULL;
// TODO(estade): We need a window title here.
dialog_ = gtk_print_unix_dialog_new(NULL, parent);
g_signal_connect(dialog_, "delete-event",
G_CALLBACK(gtk_widget_hide_on_delete), NULL);
// Set modal so user cannot focus the same tab and press print again.
gtk_window_set_modal(GTK_WINDOW(dialog_), TRUE);
// Since we only generate PDF, only show printers that support PDF.
// TODO(thestig) Add more capabilities to support?
GtkPrintCapabilities cap = static_cast<GtkPrintCapabilities>(
GTK_PRINT_CAPABILITY_GENERATE_PDF | GTK_PRINT_CAPABILITY_PAGE_SET |
GTK_PRINT_CAPABILITY_COPIES | GTK_PRINT_CAPABILITY_COLLATE |
GTK_PRINT_CAPABILITY_REVERSE);
gtk_print_unix_dialog_set_manual_capabilities(GTK_PRINT_UNIX_DIALOG(dialog_),
cap);
gtk_print_unix_dialog_set_embed_page_setup(GTK_PRINT_UNIX_DIALOG(dialog_),
TRUE);
gtk_print_unix_dialog_set_support_selection(GTK_PRINT_UNIX_DIALOG(dialog_),
TRUE);
gtk_print_unix_dialog_set_has_selection(GTK_PRINT_UNIX_DIALOG(dialog_),
has_selection);
gtk_print_unix_dialog_set_settings(GTK_PRINT_UNIX_DIALOG(dialog_),
gtk_settings_);
g_signal_connect(dialog_, "response", G_CALLBACK(OnDialogResponseThunk),
this);
gtk_widget_show(dialog_);
return true;
return GetPrintHandler(browser)->OnPrintDialog(has_selection, callback);
}
bool ClientPrintHandlerGtk::OnPrintJob(
CefRefPtr<CefBrowser> browser,
const CefString& document_name,
const CefString& pdf_file_path,
CefRefPtr<CefPrintJobCallback> callback) {
// If |printer_| is NULL then somehow the GTK printer list changed out under
// us. In which case, just bail out.
if (!printer_)
return false;
job_callback_ = callback;
// Save the settings for next time.
GetLastUsedSettings()->SetLastUsedSettings(gtk_settings_);
GtkPrintJob* print_job = gtk_print_job_new(
document_name.ToString().c_str(), printer_, gtk_settings_, page_setup_);
gtk_print_job_set_source_file(print_job, pdf_file_path.ToString().c_str(),
NULL);
gtk_print_job_send(print_job, OnJobCompletedThunk, this, NULL);
return true;
return GetPrintHandler(browser)->OnPrintJob(document_name, pdf_file_path,
callback);
}
void ClientPrintHandlerGtk::OnPrintReset() {
if (dialog_) {
gtk_widget_destroy(dialog_);
dialog_ = NULL;
}
if (gtk_settings_) {
g_object_unref(gtk_settings_);
gtk_settings_ = NULL;
}
if (page_setup_) {
g_object_unref(page_setup_);
page_setup_ = NULL;
}
if (printer_) {
g_object_unref(printer_);
printer_ = NULL;
}
void ClientPrintHandlerGtk::OnPrintReset(CefRefPtr<CefBrowser> browser) {
// Delete the print handler.
PrintHandlerMap::iterator it =
print_handler_map_.find(browser->GetIdentifier());
DCHECK(it != print_handler_map_.end());
delete it->second;
print_handler_map_.erase(it);
}
CefSize ClientPrintHandlerGtk::GetPdfPaperSize(int device_units_per_inch) {
CefSize ClientPrintHandlerGtk::GetPdfPaperSize(CefRefPtr<CefBrowser> browser,
int device_units_per_inch) {
GtkPageSetup* page_setup = gtk_page_setup_new();
float width = gtk_page_setup_get_paper_width(page_setup, GTK_UNIT_INCH);
@@ -432,85 +604,12 @@ CefSize ClientPrintHandlerGtk::GetPdfPaperSize(int device_units_per_inch) {
return CefSize(width * device_units_per_inch, height * device_units_per_inch);
}
void ClientPrintHandlerGtk::OnDialogResponse(GtkDialog* dialog,
gint response_id) {
int num_matched_handlers = g_signal_handlers_disconnect_by_func(
dialog_, reinterpret_cast<gpointer>(&OnDialogResponseThunk), this);
DCHECK_EQ(1, num_matched_handlers);
gtk_widget_hide(dialog_);
switch (response_id) {
case GTK_RESPONSE_OK: {
if (gtk_settings_)
g_object_unref(gtk_settings_);
gtk_settings_ =
gtk_print_unix_dialog_get_settings(GTK_PRINT_UNIX_DIALOG(dialog_));
if (printer_)
g_object_unref(printer_);
printer_ = gtk_print_unix_dialog_get_selected_printer(
GTK_PRINT_UNIX_DIALOG(dialog_));
g_object_ref(printer_);
if (page_setup_)
g_object_unref(page_setup_);
page_setup_ =
gtk_print_unix_dialog_get_page_setup(GTK_PRINT_UNIX_DIALOG(dialog_));
g_object_ref(page_setup_);
// Handle page ranges.
CefPrintSettings::PageRangeList ranges_vector;
gint num_ranges;
bool print_selection_only = false;
switch (gtk_print_settings_get_print_pages(gtk_settings_)) {
case GTK_PRINT_PAGES_RANGES: {
GtkPageRange* gtk_range =
gtk_print_settings_get_page_ranges(gtk_settings_, &num_ranges);
if (gtk_range) {
for (int i = 0; i < num_ranges; ++i) {
ranges_vector.push_back(
CefRange(gtk_range[i].start, gtk_range[i].end));
}
g_free(gtk_range);
}
break;
}
case GTK_PRINT_PAGES_SELECTION:
print_selection_only = true;
break;
case GTK_PRINT_PAGES_ALL:
// Leave |ranges_vector| empty to indicate print all pages.
break;
case GTK_PRINT_PAGES_CURRENT:
default:
NOTREACHED();
break;
}
CefRefPtr<CefPrintSettings> settings = CefPrintSettings::Create();
settings->SetPageRanges(ranges_vector);
settings->SetSelectionOnly(print_selection_only);
InitPrintSettings(gtk_settings_, page_setup_, settings);
dialog_callback_->Continue(settings);
dialog_callback_ = NULL;
return;
}
case GTK_RESPONSE_DELETE_EVENT: // Fall through.
case GTK_RESPONSE_CANCEL: {
dialog_callback_->Cancel();
dialog_callback_ = NULL;
return;
}
case GTK_RESPONSE_APPLY:
default: { NOTREACHED(); }
}
}
void ClientPrintHandlerGtk::OnJobCompleted(GtkPrintJob* print_job,
GError* error) {
job_callback_->Continue();
job_callback_ = NULL;
ClientPrintHandlerGtk::PrintHandler* ClientPrintHandlerGtk::GetPrintHandler(
CefRefPtr<CefBrowser> browser) {
PrintHandlerMap::const_iterator it =
print_handler_map_.find(browser->GetIdentifier());
DCHECK(it != print_handler_map_.end());
return it->second;
}
} // namespace client