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 @@
#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/no_destructor.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/post_task.h"
#include "base/values.h"
......@@ -43,19 +43,19 @@ const char kDuplexTumble[] = "DuplexTumble";
const char kDuplexNoTumble[] = "DuplexNoTumble";
#endif
int kPaperSizeTresholdMicrons = 100;
int kMicronsInMm = 1000;
constexpr int kPaperSizeTresholdMicrons = 100;
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
// 1mm difference). In the exact mode, looks for the paper with the same PPD
// name and "close enough" size.
bool PaperSizeMatch(GtkPaperSize* gtk_paper_size,
const PrintSettings::RequestedMedia& media,
bool fuzzy_match) {
if (!gtk_paper_size) {
if (!gtk_paper_size)
return false;
}
gfx::Size paper_size_microns(
static_cast<int>(gtk_paper_size_get_width(gtk_paper_size, GTK_UNIT_MM) *
kMicronsInMm +
......@@ -66,12 +66,12 @@ bool PaperSizeMatch(GtkPaperSize* gtk_paper_size,
int diff = std::max(
std::abs(paper_size_microns.width() - media.size_microns.width()),
std::abs(paper_size_microns.height() - media.size_microns.height()));
if (fuzzy_match) {
return diff <= kPaperSizeTresholdMicrons;
}
return !media.vendor_id.empty() &&
media.vendor_id == gtk_paper_size_get_ppd_name(gtk_paper_size) &&
diff <= kPaperSizeTresholdMicrons;
bool close_enough = diff <= kPaperSizeTresholdMicrons;
if (fuzzy_match)
return close_enough;
return close_enough && !media.vendor_id.empty() &&
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
......@@ -82,12 +82,11 @@ GtkPaperSize* FindPaperSizeMatch(GList* gtk_paper_sizes,
GtkPaperSize* first_fuzzy_match = nullptr;
for (GList* p = gtk_paper_sizes; p && p->data; p = g_list_next(p)) {
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;
}
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;
}
}
return first_fuzzy_match;
}
......@@ -96,7 +95,7 @@ class StickyPrintSettingGtk {
public:
StickyPrintSettingGtk() : last_used_settings_(gtk_print_settings_new()) {}
~StickyPrintSettingGtk() {
NOTREACHED(); // Intended to be used with a Leaky LazyInstance.
NOTREACHED(); // Intended to be used with base::NoDestructor.
}
GtkPrintSettings* settings() { return last_used_settings_; }
......@@ -113,21 +112,19 @@ class StickyPrintSettingGtk {
DISALLOW_COPY_AND_ASSIGN(StickyPrintSettingGtk);
};
base::LazyInstance<StickyPrintSettingGtk>::Leaky g_last_used_settings =
LAZY_INSTANCE_INITIALIZER;
StickyPrintSettingGtk& GetLastUsedSettings() {
static base::NoDestructor<StickyPrintSettingGtk> settings;
return *settings;
}
// Helper class to track GTK printers.
class GtkPrinterList {
public:
GtkPrinterList() : default_printer_(nullptr) {
gtk_enumerate_printers(SetPrinter, this, nullptr, TRUE);
}
GtkPrinterList() { gtk_enumerate_printers(SetPrinter, this, nullptr, TRUE); }
~GtkPrinterList() {
for (std::vector<GtkPrinter*>::iterator it = printers_.begin();
it < printers_.end(); ++it) {
g_object_unref(*it);
}
for (GtkPrinter* printer : printers_)
g_object_unref(printer);
}
// Can return nullptr if there's no default printer. E.g. Printer on a laptop
......@@ -141,11 +138,9 @@ class GtkPrinterList {
if (name.empty())
return nullptr;
for (std::vector<GtkPrinter*>::iterator it = printers_.begin();
it < printers_.end(); ++it) {
if (gtk_printer_get_name(*it) == name) {
return *it;
}
for (GtkPrinter* printer : printers_) {
if (gtk_printer_get_name(printer) == name)
return printer;
}
return nullptr;
......@@ -165,7 +160,7 @@ class GtkPrinterList {
}
std::vector<GtkPrinter*> printers_;
GtkPrinter* default_printer_;
GtkPrinter* default_printer_ = nullptr;
};
} // namespace
......@@ -178,12 +173,7 @@ printing::PrintDialogGtkInterface* PrintDialogGtk::CreatePrintDialog(
}
PrintDialogGtk::PrintDialogGtk(PrintingContextLinux* context)
: context_(context),
dialog_(nullptr),
gtk_settings_(nullptr),
page_setup_(nullptr),
printer_(nullptr),
parent_(nullptr) {}
: context_(context) {}
PrintDialogGtk::~PrintDialogGtk() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
......@@ -213,8 +203,7 @@ void PrintDialogGtk::UseDefaultSettings() {
DCHECK(!printer_);
// |gtk_settings_| is a new copy.
gtk_settings_ =
gtk_print_settings_copy(g_last_used_settings.Get().settings());
gtk_settings_ = gtk_print_settings_copy(GetLastUsedSettings().settings());
page_setup_ = gtk_page_setup_new();
PrintSettings settings;
......@@ -222,10 +211,8 @@ void PrintDialogGtk::UseDefaultSettings() {
}
void PrintDialogGtk::UpdateSettings(printing::PrintSettings* settings) {
if (!gtk_settings_) {
gtk_settings_ =
gtk_print_settings_copy(g_last_used_settings.Get().settings());
}
if (!gtk_settings_)
gtk_settings_ = gtk_print_settings_copy(GetLastUsedSettings().settings());
auto printer_list = std::make_unique<GtkPrinterList>();
printer_ = printer_list->GetPrinterWithName(
......@@ -319,7 +306,7 @@ void PrintDialogGtk::ShowDialog(
bool has_selection,
PrintingContextLinux::PrintSettingsCallback callback) {
callback_ = std::move(callback);
DCHECK(!callback_.is_null());
DCHECK(callback_);
dialog_ = gtk_print_unix_dialog_new(nullptr, nullptr);
libgtkui::SetGtkTransientForAura(dialog_, parent_view);
......@@ -504,7 +491,7 @@ void PrintDialogGtk::SendDocumentToPrinter(
}
// Save the settings for next time.
g_last_used_settings.Get().SetLastUsedSettings(gtk_settings_);
GetLastUsedSettings().SetLastUsedSettings(gtk_settings_);
GtkPrintJob* print_job =
gtk_print_job_new(base::UTF16ToUTF8(document_name).c_str(), printer_,
......@@ -539,6 +526,6 @@ void PrintDialogGtk::OnWindowDestroying(aura::Window* window) {
DCHECK_EQ(parent_, window);
parent_ = nullptr;
window->RemoveObserver(this);
if (!callback_.is_null())
if (callback_)
std::move(callback_).Run(PrintingContextLinux::CANCEL);
}
......@@ -75,16 +75,16 @@ class PrintDialogGtk : public printing::PrintDialogGtkInterface,
// Printing dialog callback.
PrintingContextLinux::PrintSettingsCallback callback_;
PrintingContextLinux* context_;
PrintingContextLinux* const context_;
// Print dialog settings. PrintDialogGtk owns |dialog_| and holds references
// to the other objects.
GtkWidget* dialog_;
GtkPrintSettings* gtk_settings_;
GtkPageSetup* page_setup_;
GtkPrinter* printer_;
GtkWidget* dialog_ = nullptr;
GtkPrintSettings* gtk_settings_ = nullptr;
GtkPageSetup* page_setup_ = nullptr;
GtkPrinter* printer_ = nullptr;
aura::Window* parent_;
aura::Window* parent_ = nullptr;
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