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