• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2014 The Chromium Embedded Framework Authors.
2 // Portions Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 
6 #include "tests/cefclient/browser/print_handler_gtk.h"
7 
8 #include <vector>
9 
10 #include <gtk/gtk.h>
11 #include <gtk/gtkunixprint.h>
12 
13 #include "include/base/cef_bind.h"
14 #include "include/base/cef_logging.h"
15 #include "include/base/cef_macros.h"
16 #include "include/wrapper/cef_helpers.h"
17 
18 #include "tests/cefclient/browser/root_window.h"
19 #include "tests/cefclient/browser/util_gtk.h"
20 
21 namespace client {
22 
23 namespace {
24 
25 // CUPS Duplex attribute and values.
26 const char kCUPSDuplex[] = "cups-Duplex";
27 const char kDuplexNone[] = "None";
28 const char kDuplexTumble[] = "DuplexTumble";
29 const char kDuplexNoTumble[] = "DuplexNoTumble";
30 
31 // CUPS color mode attribute and values.
32 const char kCUPSColorMode[] = "cups-ColorMode";
33 const char kCUPSColorModel[] = "cups-ColorModel";
34 const char kCUPSPrintoutMode[] = "cups-PrintoutMode";
35 const char kCUPSProcessColorModel[] = "cups-ProcessColorModel";
36 const char kBlack[] = "Black";
37 const char kCMYK[] = "CMYK";
38 const char kCMY_K[] = "CMY+K";
39 const char kCMY[] = "CMY";
40 const char kColor[] = "Color";
41 const char kGray[] = "Gray";
42 const char kGrayscale[] = "Grayscale";
43 const char kGreyscale[] = "Greyscale";
44 const char kMonochrome[] = "Monochrome";
45 const char kNormal[] = "Normal";
46 const char kNormalGray[] = "Normal.Gray";
47 const char kRGB[] = "RGB";
48 const char kRGBA[] = "RGBA";
49 const char kRGB16[] = "RGB16";
50 
51 // Default margin settings.
52 const double kTopMarginInInch = 0.25;
53 const double kBottomMarginInInch = 0.56;
54 const double kLeftMarginInInch = 0.25;
55 const double kRightMarginInInch = 0.25;
56 
57 // Length of an inch in CSS's 1px unit.
58 // http://dev.w3.org/csswg/css3-values/#the-px-unit
59 const int kPixelsPerInch = 96;
60 
61 // LETTER: 8.5 x 11 inches
62 const float kLetterWidthInch = 8.5f;
63 const float kLetterHeightInch = 11.0f;
64 
65 class StickyPrintSettingGtk {
66  public:
StickyPrintSettingGtk()67   StickyPrintSettingGtk() : last_used_settings_(gtk_print_settings_new()) {}
~StickyPrintSettingGtk()68   ~StickyPrintSettingGtk() {
69     NOTREACHED();  // The instance is intentionally leaked.
70   }
71 
settings()72   GtkPrintSettings* settings() { return last_used_settings_; }
73 
SetLastUsedSettings(GtkPrintSettings * settings)74   void SetLastUsedSettings(GtkPrintSettings* settings) {
75     DCHECK(last_used_settings_);
76     g_object_unref(last_used_settings_);
77     last_used_settings_ = gtk_print_settings_copy(settings);
78   }
79 
80  private:
81   GtkPrintSettings* last_used_settings_;
82 
83   DISALLOW_COPY_AND_ASSIGN(StickyPrintSettingGtk);
84 };
85 
86 // Lazily initialize the singleton instance.
GetLastUsedSettings()87 StickyPrintSettingGtk* GetLastUsedSettings() {
88   static StickyPrintSettingGtk* settings = nullptr;
89   if (!settings)
90     settings = new StickyPrintSettingGtk();
91   return settings;
92 }
93 
94 // Helper class to track GTK printers.
95 class GtkPrinterList {
96  public:
GtkPrinterList()97   GtkPrinterList() : default_printer_(nullptr) {
98     gtk_enumerate_printers(SetPrinter, this, NULL, TRUE);
99   }
100 
~GtkPrinterList()101   ~GtkPrinterList() {
102     for (std::vector<GtkPrinter*>::iterator it = printers_.begin();
103          it < printers_.end(); ++it) {
104       g_object_unref(*it);
105     }
106   }
107 
108   // Can return NULL if there's no default printer. E.g. Printer on a laptop
109   // is "home_printer", but the laptop is at work.
default_printer()110   GtkPrinter* default_printer() { return default_printer_; }
111 
112   // Can return NULL if the printer cannot be found due to:
113   // - Printer list out of sync with printer dialog UI.
114   // - Querying for non-existant printers like 'Print to PDF'.
GetPrinterWithName(const std::string & name)115   GtkPrinter* GetPrinterWithName(const std::string& name) {
116     if (name.empty())
117       return nullptr;
118 
119     for (std::vector<GtkPrinter*>::iterator it = printers_.begin();
120          it < printers_.end(); ++it) {
121       if (gtk_printer_get_name(*it) == name) {
122         return *it;
123       }
124     }
125 
126     return nullptr;
127   }
128 
129  private:
130   // Callback function used by gtk_enumerate_printers() to get all printer.
SetPrinter(GtkPrinter * printer,gpointer data)131   static gboolean SetPrinter(GtkPrinter* printer, gpointer data) {
132     GtkPrinterList* printer_list = reinterpret_cast<GtkPrinterList*>(data);
133     if (gtk_printer_is_default(printer))
134       printer_list->default_printer_ = printer;
135 
136     g_object_ref(printer);
137     printer_list->printers_.push_back(printer);
138 
139     return FALSE;
140   }
141 
142   std::vector<GtkPrinter*> printers_;
143   GtkPrinter* default_printer_;
144 };
145 
GetColorModelForMode(CefPrintSettings::ColorModel color_mode,std::string * color_setting_name,std::string * color_value)146 void GetColorModelForMode(CefPrintSettings::ColorModel color_mode,
147                           std::string* color_setting_name,
148                           std::string* color_value) {
149   color_setting_name->assign(kCUPSColorModel);
150   switch (color_mode) {
151     case COLOR_MODEL_COLOR:
152       color_value->assign(kColor);
153       break;
154     case COLOR_MODEL_CMYK:
155       color_value->assign(kCMYK);
156       break;
157     case COLOR_MODEL_PRINTOUTMODE_NORMAL:
158       color_value->assign(kNormal);
159       color_setting_name->assign(kCUPSPrintoutMode);
160       break;
161     case COLOR_MODEL_PRINTOUTMODE_NORMAL_GRAY:
162       color_value->assign(kNormalGray);
163       color_setting_name->assign(kCUPSPrintoutMode);
164       break;
165     case COLOR_MODEL_RGB16:
166       color_value->assign(kRGB16);
167       break;
168     case COLOR_MODEL_RGBA:
169       color_value->assign(kRGBA);
170       break;
171     case COLOR_MODEL_RGB:
172       color_value->assign(kRGB);
173       break;
174     case COLOR_MODEL_CMY:
175       color_value->assign(kCMY);
176       break;
177     case COLOR_MODEL_CMY_K:
178       color_value->assign(kCMY_K);
179       break;
180     case COLOR_MODEL_BLACK:
181       color_value->assign(kBlack);
182       break;
183     case COLOR_MODEL_GRAY:
184       color_value->assign(kGray);
185       break;
186     case COLOR_MODEL_COLORMODE_COLOR:
187       color_setting_name->assign(kCUPSColorMode);
188       color_value->assign(kColor);
189       break;
190     case COLOR_MODEL_COLORMODE_MONOCHROME:
191       color_setting_name->assign(kCUPSColorMode);
192       color_value->assign(kMonochrome);
193       break;
194     case COLOR_MODEL_HP_COLOR_COLOR:
195       color_setting_name->assign(kColor);
196       color_value->assign(kColor);
197       break;
198     case COLOR_MODEL_HP_COLOR_BLACK:
199       color_setting_name->assign(kColor);
200       color_value->assign(kBlack);
201       break;
202     case COLOR_MODEL_PROCESSCOLORMODEL_CMYK:
203       color_setting_name->assign(kCUPSProcessColorModel);
204       color_value->assign(kCMYK);
205       break;
206     case COLOR_MODEL_PROCESSCOLORMODEL_GREYSCALE:
207       color_setting_name->assign(kCUPSProcessColorModel);
208       color_value->assign(kGreyscale);
209       break;
210     case COLOR_MODEL_PROCESSCOLORMODEL_RGB:
211       color_setting_name->assign(kCUPSProcessColorModel);
212       color_value->assign(kRGB);
213       break;
214     default:
215       color_value->assign(kGrayscale);
216       break;
217   }
218 }
219 
InitPrintSettings(GtkPrintSettings * settings,GtkPageSetup * page_setup,CefRefPtr<CefPrintSettings> print_settings)220 void InitPrintSettings(GtkPrintSettings* settings,
221                        GtkPageSetup* page_setup,
222                        CefRefPtr<CefPrintSettings> print_settings) {
223   DCHECK(settings);
224   DCHECK(page_setup);
225 
226   std::string device_name;
227   const gchar* name = gtk_print_settings_get_printer(settings);
228   if (name)
229     device_name = name;
230   print_settings->SetDeviceName(device_name);
231 
232   CefSize physical_size_device_units;
233   CefRect printable_area_device_units;
234   int dpi = gtk_print_settings_get_resolution(settings);
235   if (dpi) {
236     // Initialize page_setup_device_units_.
237     physical_size_device_units.Set(
238         gtk_page_setup_get_paper_width(page_setup, GTK_UNIT_INCH) * dpi,
239         gtk_page_setup_get_paper_height(page_setup, GTK_UNIT_INCH) * dpi);
240     printable_area_device_units.Set(
241         gtk_page_setup_get_left_margin(page_setup, GTK_UNIT_INCH) * dpi,
242         gtk_page_setup_get_top_margin(page_setup, GTK_UNIT_INCH) * dpi,
243         gtk_page_setup_get_page_width(page_setup, GTK_UNIT_INCH) * dpi,
244         gtk_page_setup_get_page_height(page_setup, GTK_UNIT_INCH) * dpi);
245   } else {
246     // Use default values if we cannot get valid values from the print dialog.
247     dpi = kPixelsPerInch;
248     double page_width_in_pixel = kLetterWidthInch * dpi;
249     double page_height_in_pixel = kLetterHeightInch * dpi;
250     physical_size_device_units.Set(static_cast<int>(page_width_in_pixel),
251                                    static_cast<int>(page_height_in_pixel));
252     printable_area_device_units.Set(
253         static_cast<int>(kLeftMarginInInch * dpi),
254         static_cast<int>(kTopMarginInInch * dpi),
255         page_width_in_pixel - (kLeftMarginInInch + kRightMarginInInch) * dpi,
256         page_height_in_pixel - (kTopMarginInInch + kBottomMarginInInch) * dpi);
257   }
258 
259   print_settings->SetDPI(dpi);
260 
261   // Note: With the normal GTK print dialog, when the user selects the landscape
262   // orientation, all that does is change the paper size. Which seems to be
263   // enough to render the right output and send it to the printer.
264   // The orientation value stays as portrait and does not actually affect
265   // printing.
266   // Thus this is only useful in print preview mode, where we manually set the
267   // orientation and change the paper size ourselves.
268   GtkPageOrientation orientation = gtk_print_settings_get_orientation(settings);
269   // Set before SetPrinterPrintableArea to make it flip area if necessary.
270   print_settings->SetOrientation(orientation == GTK_PAGE_ORIENTATION_LANDSCAPE);
271   print_settings->SetPrinterPrintableArea(physical_size_device_units,
272                                           printable_area_device_units, true);
273 }
274 
275 // Returns the GtkWindow* for the browser. Will return NULL when using the Views
276 // framework.
GetWindow(CefRefPtr<CefBrowser> browser)277 GtkWindow* GetWindow(CefRefPtr<CefBrowser> browser) {
278   scoped_refptr<RootWindow> root_window =
279       RootWindow::GetForBrowser(browser->GetIdentifier());
280   if (root_window)
281     return GTK_WINDOW(root_window->GetWindowHandle());
282   return nullptr;
283 }
284 
RunCallback(base::Callback<void (GtkWindow *)> callback,GtkWindow * window)285 void RunCallback(base::Callback<void(GtkWindow*)> callback, GtkWindow* window) {
286   callback.Run(window);
287 }
288 
GetWindowAndContinue(CefRefPtr<CefBrowser> browser,base::Callback<void (GtkWindow *)> callback)289 void GetWindowAndContinue(CefRefPtr<CefBrowser> browser,
290                           base::Callback<void(GtkWindow*)> callback) {
291   if (!CURRENTLY_ON_MAIN_THREAD()) {
292     MAIN_POST_CLOSURE(base::Bind(GetWindowAndContinue, browser, callback));
293     return;
294   }
295 
296   GtkWindow* window = GetWindow(browser);
297   if (window) {
298     CefPostTask(TID_UI, base::Bind(RunCallback, callback, window));
299   }
300 }
301 
302 }  // namespace
303 
304 struct ClientPrintHandlerGtk::PrintHandler {
PrintHandlerclient::ClientPrintHandlerGtk::PrintHandler305   PrintHandler(CefRefPtr<CefBrowser> browser)
306       : browser_(browser),
307         dialog_(nullptr),
308         gtk_settings_(nullptr),
309         page_setup_(nullptr),
310         printer_(nullptr) {}
311 
~PrintHandlerclient::ClientPrintHandlerGtk::PrintHandler312   ~PrintHandler() {
313     ScopedGdkThreadsEnter scoped_gdk_threads;
314 
315     if (dialog_) {
316       gtk_widget_destroy(dialog_);
317       dialog_ = nullptr;
318     }
319     if (gtk_settings_) {
320       g_object_unref(gtk_settings_);
321       gtk_settings_ = nullptr;
322     }
323     if (page_setup_) {
324       g_object_unref(page_setup_);
325       page_setup_ = nullptr;
326     }
327     if (printer_) {
328       g_object_unref(printer_);
329       printer_ = nullptr;
330     }
331   }
332 
OnPrintSettingsclient::ClientPrintHandlerGtk::PrintHandler333   void OnPrintSettings(CefRefPtr<CefPrintSettings> settings,
334                        bool get_defaults) {
335     ScopedGdkThreadsEnter scoped_gdk_threads;
336 
337     if (get_defaults) {
338       DCHECK(!page_setup_);
339       DCHECK(!printer_);
340 
341       // |gtk_settings_| is a new copy.
342       gtk_settings_ =
343           gtk_print_settings_copy(GetLastUsedSettings()->settings());
344       page_setup_ = gtk_page_setup_new();
345     } else {
346       if (!gtk_settings_) {
347         gtk_settings_ =
348             gtk_print_settings_copy(GetLastUsedSettings()->settings());
349       }
350 
351       GtkPrinterList* printer_list = new GtkPrinterList;
352       printer_ = printer_list->GetPrinterWithName(settings->GetDeviceName());
353       if (printer_) {
354         g_object_ref(printer_);
355         gtk_print_settings_set_printer(gtk_settings_,
356                                        gtk_printer_get_name(printer_));
357         if (!page_setup_) {
358           page_setup_ = gtk_printer_get_default_page_size(printer_);
359         }
360       }
361 
362       gtk_print_settings_set_n_copies(gtk_settings_, settings->GetCopies());
363       gtk_print_settings_set_collate(gtk_settings_, settings->WillCollate());
364 
365       std::string color_value;
366       std::string color_setting_name;
367       GetColorModelForMode(settings->GetColorModel(), &color_setting_name,
368                            &color_value);
369       gtk_print_settings_set(gtk_settings_, color_setting_name.c_str(),
370                              color_value.c_str());
371 
372       if (settings->GetDuplexMode() != DUPLEX_MODE_UNKNOWN) {
373         const char* cups_duplex_mode = nullptr;
374         switch (settings->GetDuplexMode()) {
375           case DUPLEX_MODE_LONG_EDGE:
376             cups_duplex_mode = kDuplexNoTumble;
377             break;
378           case DUPLEX_MODE_SHORT_EDGE:
379             cups_duplex_mode = kDuplexTumble;
380             break;
381           case DUPLEX_MODE_SIMPLEX:
382             cups_duplex_mode = kDuplexNone;
383             break;
384           default:  // UNKNOWN_DUPLEX_MODE
385             NOTREACHED();
386             break;
387         }
388         gtk_print_settings_set(gtk_settings_, kCUPSDuplex, cups_duplex_mode);
389       }
390 
391       if (!page_setup_)
392         page_setup_ = gtk_page_setup_new();
393 
394       gtk_print_settings_set_orientation(gtk_settings_,
395                                          settings->IsLandscape()
396                                              ? GTK_PAGE_ORIENTATION_LANDSCAPE
397                                              : GTK_PAGE_ORIENTATION_PORTRAIT);
398 
399       delete printer_list;
400     }
401 
402     InitPrintSettings(gtk_settings_, page_setup_, settings);
403   }
404 
OnPrintDialogclient::ClientPrintHandlerGtk::PrintHandler405   void OnPrintDialog(bool has_selection,
406                      CefRefPtr<CefPrintDialogCallback> callback,
407                      GtkWindow* parent) {
408     dialog_callback_ = callback;
409 
410     ScopedGdkThreadsEnter scoped_gdk_threads;
411 
412     // TODO(estade): We need a window title here.
413     dialog_ = gtk_print_unix_dialog_new(NULL, parent);
414     g_signal_connect(dialog_, "delete-event",
415                      G_CALLBACK(gtk_widget_hide_on_delete), NULL);
416 
417     // Set modal so user cannot focus the same tab and press print again.
418     gtk_window_set_modal(GTK_WINDOW(dialog_), TRUE);
419 
420     // Since we only generate PDF, only show printers that support PDF.
421     // TODO(thestig) Add more capabilities to support?
422     GtkPrintCapabilities cap = static_cast<GtkPrintCapabilities>(
423         GTK_PRINT_CAPABILITY_GENERATE_PDF | GTK_PRINT_CAPABILITY_PAGE_SET |
424         GTK_PRINT_CAPABILITY_COPIES | GTK_PRINT_CAPABILITY_COLLATE |
425         GTK_PRINT_CAPABILITY_REVERSE);
426     gtk_print_unix_dialog_set_manual_capabilities(
427         GTK_PRINT_UNIX_DIALOG(dialog_), cap);
428     gtk_print_unix_dialog_set_embed_page_setup(GTK_PRINT_UNIX_DIALOG(dialog_),
429                                                TRUE);
430     gtk_print_unix_dialog_set_support_selection(GTK_PRINT_UNIX_DIALOG(dialog_),
431                                                 TRUE);
432     gtk_print_unix_dialog_set_has_selection(GTK_PRINT_UNIX_DIALOG(dialog_),
433                                             has_selection);
434     gtk_print_unix_dialog_set_settings(GTK_PRINT_UNIX_DIALOG(dialog_),
435                                        gtk_settings_);
436     g_signal_connect(dialog_, "response", G_CALLBACK(OnDialogResponseThunk),
437                      this);
438     gtk_widget_show(dialog_);
439   }
440 
OnPrintJobclient::ClientPrintHandlerGtk::PrintHandler441   bool OnPrintJob(const CefString& document_name,
442                   const CefString& pdf_file_path,
443                   CefRefPtr<CefPrintJobCallback> callback) {
444     // If |printer_| is NULL then somehow the GTK printer list changed out under
445     // us. In which case, just bail out.
446     if (!printer_)
447       return false;
448 
449     ScopedGdkThreadsEnter scoped_gdk_threads;
450 
451     job_callback_ = callback;
452 
453     // Save the settings for next time.
454     GetLastUsedSettings()->SetLastUsedSettings(gtk_settings_);
455 
456     GtkPrintJob* print_job = gtk_print_job_new(
457         document_name.ToString().c_str(), printer_, gtk_settings_, page_setup_);
458     gtk_print_job_set_source_file(print_job, pdf_file_path.ToString().c_str(),
459                                   NULL);
460     gtk_print_job_send(print_job, OnJobCompletedThunk, this, NULL);
461 
462     return true;
463   }
464 
465  private:
OnDialogResponseclient::ClientPrintHandlerGtk::PrintHandler466   void OnDialogResponse(GtkDialog* dialog, gint response_id) {
467     int num_matched_handlers = g_signal_handlers_disconnect_by_func(
468         dialog_, reinterpret_cast<gpointer>(&OnDialogResponseThunk), this);
469     DCHECK_EQ(1, num_matched_handlers);
470 
471     gtk_widget_hide(dialog_);
472 
473     switch (response_id) {
474       case GTK_RESPONSE_OK: {
475         if (gtk_settings_)
476           g_object_unref(gtk_settings_);
477         gtk_settings_ =
478             gtk_print_unix_dialog_get_settings(GTK_PRINT_UNIX_DIALOG(dialog_));
479 
480         if (printer_)
481           g_object_unref(printer_);
482         printer_ = gtk_print_unix_dialog_get_selected_printer(
483             GTK_PRINT_UNIX_DIALOG(dialog_));
484         g_object_ref(printer_);
485 
486         if (page_setup_)
487           g_object_unref(page_setup_);
488         page_setup_ = gtk_print_unix_dialog_get_page_setup(
489             GTK_PRINT_UNIX_DIALOG(dialog_));
490         g_object_ref(page_setup_);
491 
492         // Handle page ranges.
493         CefPrintSettings::PageRangeList ranges_vector;
494         gint num_ranges;
495         bool print_selection_only = false;
496         switch (gtk_print_settings_get_print_pages(gtk_settings_)) {
497           case GTK_PRINT_PAGES_RANGES: {
498             GtkPageRange* gtk_range =
499                 gtk_print_settings_get_page_ranges(gtk_settings_, &num_ranges);
500             if (gtk_range) {
501               for (int i = 0; i < num_ranges; ++i) {
502                 ranges_vector.push_back(
503                     CefRange(gtk_range[i].start, gtk_range[i].end));
504               }
505               g_free(gtk_range);
506             }
507             break;
508           }
509           case GTK_PRINT_PAGES_SELECTION:
510             print_selection_only = true;
511             break;
512           case GTK_PRINT_PAGES_ALL:
513             // Leave |ranges_vector| empty to indicate print all pages.
514             break;
515           case GTK_PRINT_PAGES_CURRENT:
516           default:
517             NOTREACHED();
518             break;
519         }
520 
521         CefRefPtr<CefPrintSettings> settings = CefPrintSettings::Create();
522         settings->SetPageRanges(ranges_vector);
523         settings->SetSelectionOnly(print_selection_only);
524         InitPrintSettings(gtk_settings_, page_setup_, settings);
525         dialog_callback_->Continue(settings);
526         dialog_callback_ = nullptr;
527         return;
528       }
529       case GTK_RESPONSE_DELETE_EVENT:  // Fall through.
530       case GTK_RESPONSE_CANCEL: {
531         dialog_callback_->Cancel();
532         dialog_callback_ = nullptr;
533         return;
534       }
535       case GTK_RESPONSE_APPLY:
536       default: {
537         NOTREACHED();
538       }
539     }
540   }
541 
OnJobCompletedclient::ClientPrintHandlerGtk::PrintHandler542   void OnJobCompleted(GtkPrintJob* print_job, const GError* error) {
543     // Continue() will result in a call to ClientPrintHandlerGtk::OnPrintReset
544     // which deletes |this|. Execute it asnychronously so the call stack has a
545     // chance to unwind.
546     CefPostTask(TID_UI, base::Bind(&CefPrintJobCallback::Continue,
547                                    job_callback_.get()));
548     job_callback_ = nullptr;
549   }
550 
OnDialogResponseThunkclient::ClientPrintHandlerGtk::PrintHandler551   static void OnDialogResponseThunk(GtkDialog* dialog,
552                                     gint response_id,
553                                     PrintHandler* handler) {
554     handler->OnDialogResponse(dialog, response_id);
555   }
556 
OnJobCompletedThunkclient::ClientPrintHandlerGtk::PrintHandler557   static void OnJobCompletedThunk(GtkPrintJob* print_job,
558                                   void* handler,
559                                   const GError* error) {
560     static_cast<PrintHandler*>(handler)->OnJobCompleted(print_job, error);
561   }
562 
563   CefRefPtr<CefBrowser> browser_;
564 
565   GtkWidget* dialog_;               // Owned.
566   GtkPrintSettings* gtk_settings_;  // Referenced.
567   GtkPageSetup* page_setup_;        // Referenced.
568   GtkPrinter* printer_;             // Referenced.
569 
570   CefRefPtr<CefPrintDialogCallback> dialog_callback_;
571   CefRefPtr<CefPrintJobCallback> job_callback_;
572 };
573 
ClientPrintHandlerGtk()574 ClientPrintHandlerGtk::ClientPrintHandlerGtk() {}
575 
~ClientPrintHandlerGtk()576 ClientPrintHandlerGtk::~ClientPrintHandlerGtk() {
577   DCHECK(!print_handler_);
578 }
579 
OnPrintStart(CefRefPtr<CefBrowser> browser)580 void ClientPrintHandlerGtk::OnPrintStart(CefRefPtr<CefBrowser> browser) {
581   CEF_REQUIRE_UI_THREAD();
582   DCHECK(!print_handler_);
583   print_handler_.reset(new PrintHandler(browser));
584 }
585 
OnPrintSettings(CefRefPtr<CefBrowser> browser,CefRefPtr<CefPrintSettings> settings,bool get_defaults)586 void ClientPrintHandlerGtk::OnPrintSettings(
587     CefRefPtr<CefBrowser> browser,
588     CefRefPtr<CefPrintSettings> settings,
589     bool get_defaults) {
590   CEF_REQUIRE_UI_THREAD();
591 
592   print_handler_->OnPrintSettings(settings, get_defaults);
593 }
594 
OnPrintDialog(CefRefPtr<CefBrowser> browser,bool has_selection,CefRefPtr<CefPrintDialogCallback> callback)595 bool ClientPrintHandlerGtk::OnPrintDialog(
596     CefRefPtr<CefBrowser> browser,
597     bool has_selection,
598     CefRefPtr<CefPrintDialogCallback> callback) {
599   CEF_REQUIRE_UI_THREAD();
600 
601   GetWindowAndContinue(
602       browser, base::Bind(&PrintHandler::OnPrintDialog,
603                           base::Unretained(print_handler_.get()), has_selection,
604                           callback));
605   return true;
606 }
607 
OnPrintJob(CefRefPtr<CefBrowser> browser,const CefString & document_name,const CefString & pdf_file_path,CefRefPtr<CefPrintJobCallback> callback)608 bool ClientPrintHandlerGtk::OnPrintJob(
609     CefRefPtr<CefBrowser> browser,
610     const CefString& document_name,
611     const CefString& pdf_file_path,
612     CefRefPtr<CefPrintJobCallback> callback) {
613   CEF_REQUIRE_UI_THREAD();
614 
615   return print_handler_->OnPrintJob(document_name, pdf_file_path, callback);
616 }
617 
OnPrintReset(CefRefPtr<CefBrowser> browser)618 void ClientPrintHandlerGtk::OnPrintReset(CefRefPtr<CefBrowser> browser) {
619   CEF_REQUIRE_UI_THREAD();
620 
621   // Delete the print handler.
622   print_handler_.reset();
623 }
624 
GetPdfPaperSize(CefRefPtr<CefBrowser> browser,int device_units_per_inch)625 CefSize ClientPrintHandlerGtk::GetPdfPaperSize(CefRefPtr<CefBrowser> browser,
626                                                int device_units_per_inch) {
627   CEF_REQUIRE_UI_THREAD();
628 
629   ScopedGdkThreadsEnter scoped_gdk_threads;
630 
631   GtkPageSetup* page_setup = gtk_page_setup_new();
632 
633   float width = gtk_page_setup_get_paper_width(page_setup, GTK_UNIT_INCH);
634   float height = gtk_page_setup_get_paper_height(page_setup, GTK_UNIT_INCH);
635 
636   g_object_unref(page_setup);
637 
638   return CefSize(width * device_units_per_inch, height * device_units_per_inch);
639 }
640 
641 }  // namespace client
642