Commit 3b660d59 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Modernize code print_dialog_gtk.cc.

Use base::NoDestructor, range-based for loops, and initialize members in
headers. Also mark some constants as constexpr.

Change-Id: I7cfef2e80f8fc285af560f48cfbcaf264b03513e
Reviewed-on: https://chromium-review.googlesource.com/1189542Reviewed-by: default avatarThomas Anderson <thomasanderson@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#586370}
parent d6363e47
...@@ -15,9 +15,9 @@ ...@@ -15,9 +15,9 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/lazy_instance.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/no_destructor.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/task/post_task.h" #include "base/task/post_task.h"
#include "base/values.h" #include "base/values.h"
...@@ -43,19 +43,19 @@ const char kDuplexTumble[] = "DuplexTumble"; ...@@ -43,19 +43,19 @@ const char kDuplexTumble[] = "DuplexTumble";
const char kDuplexNoTumble[] = "DuplexNoTumble"; const char kDuplexNoTumble[] = "DuplexNoTumble";
#endif #endif
int kPaperSizeTresholdMicrons = 100; constexpr int kPaperSizeTresholdMicrons = 100;
int kMicronsInMm = 1000; constexpr int kMicronsInMm = 1000;
// Checks whether gtk_paper_size can be used to represent user selected media. // Checks whether |gtk_paper_size| can be used to represent user selected media.
// In fuzzy match mode checks that paper sizes are "close enough" (less than // In fuzzy match mode checks that paper sizes are "close enough" (less than
// 1mm difference). In the exact mode, looks for the paper with the same PPD // 1mm difference). In the exact mode, looks for the paper with the same PPD
// name and "close enough" size. // name and "close enough" size.
bool PaperSizeMatch(GtkPaperSize* gtk_paper_size, bool PaperSizeMatch(GtkPaperSize* gtk_paper_size,
const PrintSettings::RequestedMedia& media, const PrintSettings::RequestedMedia& media,
bool fuzzy_match) { bool fuzzy_match) {
if (!gtk_paper_size) { if (!gtk_paper_size)
return false; return false;
}
gfx::Size paper_size_microns( gfx::Size paper_size_microns(
static_cast<int>(gtk_paper_size_get_width(gtk_paper_size, GTK_UNIT_MM) * static_cast<int>(gtk_paper_size_get_width(gtk_paper_size, GTK_UNIT_MM) *
kMicronsInMm + kMicronsInMm +
...@@ -66,12 +66,12 @@ bool PaperSizeMatch(GtkPaperSize* gtk_paper_size, ...@@ -66,12 +66,12 @@ bool PaperSizeMatch(GtkPaperSize* gtk_paper_size,
int diff = std::max( int diff = std::max(
std::abs(paper_size_microns.width() - media.size_microns.width()), std::abs(paper_size_microns.width() - media.size_microns.width()),
std::abs(paper_size_microns.height() - media.size_microns.height())); std::abs(paper_size_microns.height() - media.size_microns.height()));
if (fuzzy_match) { bool close_enough = diff <= kPaperSizeTresholdMicrons;
return diff <= kPaperSizeTresholdMicrons; if (fuzzy_match)
} return close_enough;
return !media.vendor_id.empty() &&
media.vendor_id == gtk_paper_size_get_ppd_name(gtk_paper_size) && return close_enough && !media.vendor_id.empty() &&
diff <= kPaperSizeTresholdMicrons; media.vendor_id == gtk_paper_size_get_ppd_name(gtk_paper_size);
} }
// Looks up a paper size matching (in terms of PaperSizeMatch) the user selected // Looks up a paper size matching (in terms of PaperSizeMatch) the user selected
...@@ -82,12 +82,11 @@ GtkPaperSize* FindPaperSizeMatch(GList* gtk_paper_sizes, ...@@ -82,12 +82,11 @@ GtkPaperSize* FindPaperSizeMatch(GList* gtk_paper_sizes,
GtkPaperSize* first_fuzzy_match = nullptr; GtkPaperSize* first_fuzzy_match = nullptr;
for (GList* p = gtk_paper_sizes; p && p->data; p = g_list_next(p)) { for (GList* p = gtk_paper_sizes; p && p->data; p = g_list_next(p)) {
GtkPaperSize* gtk_paper_size = static_cast<GtkPaperSize*>(p->data); GtkPaperSize* gtk_paper_size = static_cast<GtkPaperSize*>(p->data);
if (PaperSizeMatch(gtk_paper_size, media, false)) { if (PaperSizeMatch(gtk_paper_size, media, false))
return gtk_paper_size; return gtk_paper_size;
}
if (!first_fuzzy_match && PaperSizeMatch(gtk_paper_size, media, true)) { if (!first_fuzzy_match && PaperSizeMatch(gtk_paper_size, media, true))
first_fuzzy_match = gtk_paper_size; first_fuzzy_match = gtk_paper_size;
}
} }
return first_fuzzy_match; return first_fuzzy_match;
} }
...@@ -96,7 +95,7 @@ class StickyPrintSettingGtk { ...@@ -96,7 +95,7 @@ class StickyPrintSettingGtk {
public: public:
StickyPrintSettingGtk() : last_used_settings_(gtk_print_settings_new()) {} StickyPrintSettingGtk() : last_used_settings_(gtk_print_settings_new()) {}
~StickyPrintSettingGtk() { ~StickyPrintSettingGtk() {
NOTREACHED(); // Intended to be used with a Leaky LazyInstance. NOTREACHED(); // Intended to be used with base::NoDestructor.
} }
GtkPrintSettings* settings() { return last_used_settings_; } GtkPrintSettings* settings() { return last_used_settings_; }
...@@ -113,21 +112,19 @@ class StickyPrintSettingGtk { ...@@ -113,21 +112,19 @@ class StickyPrintSettingGtk {
DISALLOW_COPY_AND_ASSIGN(StickyPrintSettingGtk); DISALLOW_COPY_AND_ASSIGN(StickyPrintSettingGtk);
}; };
base::LazyInstance<StickyPrintSettingGtk>::Leaky g_last_used_settings = StickyPrintSettingGtk& GetLastUsedSettings() {
LAZY_INSTANCE_INITIALIZER; static base::NoDestructor<StickyPrintSettingGtk> settings;
return *settings;
}
// Helper class to track GTK printers. // Helper class to track GTK printers.
class GtkPrinterList { class GtkPrinterList {
public: public:
GtkPrinterList() : default_printer_(nullptr) { GtkPrinterList() { gtk_enumerate_printers(SetPrinter, this, nullptr, TRUE); }
gtk_enumerate_printers(SetPrinter, this, nullptr, TRUE);
}
~GtkPrinterList() { ~GtkPrinterList() {
for (std::vector<GtkPrinter*>::iterator it = printers_.begin(); for (GtkPrinter* printer : printers_)
it < printers_.end(); ++it) { g_object_unref(printer);
g_object_unref(*it);
}
} }
// Can return nullptr if there's no default printer. E.g. Printer on a laptop // Can return nullptr if there's no default printer. E.g. Printer on a laptop
...@@ -141,11 +138,9 @@ class GtkPrinterList { ...@@ -141,11 +138,9 @@ class GtkPrinterList {
if (name.empty()) if (name.empty())
return nullptr; return nullptr;
for (std::vector<GtkPrinter*>::iterator it = printers_.begin(); for (GtkPrinter* printer : printers_) {
it < printers_.end(); ++it) { if (gtk_printer_get_name(printer) == name)
if (gtk_printer_get_name(*it) == name) { return printer;
return *it;
}
} }
return nullptr; return nullptr;
...@@ -165,7 +160,7 @@ class GtkPrinterList { ...@@ -165,7 +160,7 @@ class GtkPrinterList {
} }
std::vector<GtkPrinter*> printers_; std::vector<GtkPrinter*> printers_;
GtkPrinter* default_printer_; GtkPrinter* default_printer_ = nullptr;
}; };
} // namespace } // namespace
...@@ -178,12 +173,7 @@ printing::PrintDialogGtkInterface* PrintDialogGtk::CreatePrintDialog( ...@@ -178,12 +173,7 @@ printing::PrintDialogGtkInterface* PrintDialogGtk::CreatePrintDialog(
} }
PrintDialogGtk::PrintDialogGtk(PrintingContextLinux* context) PrintDialogGtk::PrintDialogGtk(PrintingContextLinux* context)
: context_(context), : context_(context) {}
dialog_(nullptr),
gtk_settings_(nullptr),
page_setup_(nullptr),
printer_(nullptr),
parent_(nullptr) {}
PrintDialogGtk::~PrintDialogGtk() { PrintDialogGtk::~PrintDialogGtk() {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
...@@ -213,8 +203,7 @@ void PrintDialogGtk::UseDefaultSettings() { ...@@ -213,8 +203,7 @@ void PrintDialogGtk::UseDefaultSettings() {
DCHECK(!printer_); DCHECK(!printer_);
// |gtk_settings_| is a new copy. // |gtk_settings_| is a new copy.
gtk_settings_ = gtk_settings_ = gtk_print_settings_copy(GetLastUsedSettings().settings());
gtk_print_settings_copy(g_last_used_settings.Get().settings());
page_setup_ = gtk_page_setup_new(); page_setup_ = gtk_page_setup_new();
PrintSettings settings; PrintSettings settings;
...@@ -222,10 +211,8 @@ void PrintDialogGtk::UseDefaultSettings() { ...@@ -222,10 +211,8 @@ void PrintDialogGtk::UseDefaultSettings() {
} }
void PrintDialogGtk::UpdateSettings(printing::PrintSettings* settings) { void PrintDialogGtk::UpdateSettings(printing::PrintSettings* settings) {
if (!gtk_settings_) { if (!gtk_settings_)
gtk_settings_ = gtk_settings_ = gtk_print_settings_copy(GetLastUsedSettings().settings());
gtk_print_settings_copy(g_last_used_settings.Get().settings());
}
auto printer_list = std::make_unique<GtkPrinterList>(); auto printer_list = std::make_unique<GtkPrinterList>();
printer_ = printer_list->GetPrinterWithName( printer_ = printer_list->GetPrinterWithName(
...@@ -319,7 +306,7 @@ void PrintDialogGtk::ShowDialog( ...@@ -319,7 +306,7 @@ void PrintDialogGtk::ShowDialog(
bool has_selection, bool has_selection,
PrintingContextLinux::PrintSettingsCallback callback) { PrintingContextLinux::PrintSettingsCallback callback) {
callback_ = std::move(callback); callback_ = std::move(callback);
DCHECK(!callback_.is_null()); DCHECK(callback_);
dialog_ = gtk_print_unix_dialog_new(nullptr, nullptr); dialog_ = gtk_print_unix_dialog_new(nullptr, nullptr);
libgtkui::SetGtkTransientForAura(dialog_, parent_view); libgtkui::SetGtkTransientForAura(dialog_, parent_view);
...@@ -504,7 +491,7 @@ void PrintDialogGtk::SendDocumentToPrinter( ...@@ -504,7 +491,7 @@ void PrintDialogGtk::SendDocumentToPrinter(
} }
// Save the settings for next time. // Save the settings for next time.
g_last_used_settings.Get().SetLastUsedSettings(gtk_settings_); GetLastUsedSettings().SetLastUsedSettings(gtk_settings_);
GtkPrintJob* print_job = GtkPrintJob* print_job =
gtk_print_job_new(base::UTF16ToUTF8(document_name).c_str(), printer_, gtk_print_job_new(base::UTF16ToUTF8(document_name).c_str(), printer_,
...@@ -539,6 +526,6 @@ void PrintDialogGtk::OnWindowDestroying(aura::Window* window) { ...@@ -539,6 +526,6 @@ void PrintDialogGtk::OnWindowDestroying(aura::Window* window) {
DCHECK_EQ(parent_, window); DCHECK_EQ(parent_, window);
parent_ = nullptr; parent_ = nullptr;
window->RemoveObserver(this); window->RemoveObserver(this);
if (!callback_.is_null()) if (callback_)
std::move(callback_).Run(PrintingContextLinux::CANCEL); std::move(callback_).Run(PrintingContextLinux::CANCEL);
} }
...@@ -75,16 +75,16 @@ class PrintDialogGtk : public printing::PrintDialogGtkInterface, ...@@ -75,16 +75,16 @@ class PrintDialogGtk : public printing::PrintDialogGtkInterface,
// Printing dialog callback. // Printing dialog callback.
PrintingContextLinux::PrintSettingsCallback callback_; PrintingContextLinux::PrintSettingsCallback callback_;
PrintingContextLinux* context_; PrintingContextLinux* const context_;
// Print dialog settings. PrintDialogGtk owns |dialog_| and holds references // Print dialog settings. PrintDialogGtk owns |dialog_| and holds references
// to the other objects. // to the other objects.
GtkWidget* dialog_; GtkWidget* dialog_ = nullptr;
GtkPrintSettings* gtk_settings_; GtkPrintSettings* gtk_settings_ = nullptr;
GtkPageSetup* page_setup_; GtkPageSetup* page_setup_ = nullptr;
GtkPrinter* printer_; GtkPrinter* printer_ = nullptr;
aura::Window* parent_; aura::Window* parent_ = nullptr;
base::FilePath path_to_pdf_; base::FilePath path_to_pdf_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment