Skip to content

Commit

Permalink
Make PrintingContext::PrintSettingsCallback non-repeatable.
Browse files Browse the repository at this point in the history
Bug: 625126
Change-Id: If67d226a432060596cf424e148f16b7a5035aeb5
Reviewed-on: https://chromium-review.googlesource.com/914821
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Vladislav Kuzkokov <vkuzkokov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#536734}
  • Loading branch information
Vladislav Kuzkokov authored and Commit Bot committed Feb 14, 2018
1 parent 9afd0ba commit 48ceab2
Show file tree
Hide file tree
Showing 20 changed files with 63 additions and 68 deletions.
11 changes: 6 additions & 5 deletions chrome/browser/printing/print_job_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ void NotificationCallback(PrintJobWorkerOwner* print_job,
void PostOnOwnerThread(scoped_refptr<PrintJobWorkerOwner> owner,
PrintingContext::PrintSettingsCallback callback,
PrintingContext::Result result) {
owner->PostTask(FROM_HERE, base::BindOnce(&HoldRefCallback, owner,
base::BindOnce(callback, result)));
owner->PostTask(FROM_HERE,
base::BindOnce(&HoldRefCallback, owner,
base::BindOnce(std::move(callback), result)));
}

#if defined(OS_WIN)
Expand Down Expand Up @@ -263,9 +264,9 @@ void PrintJobWorker::GetSettingsWithUI(
// weak_factory_ creates pointers valid only on owner_ thread.
printing_context_->AskUserForSettings(
document_page_count, has_selection, is_scripted,
base::Bind(&PostOnOwnerThread, base::WrapRefCounted(owner_),
base::Bind(&PrintJobWorker::GetSettingsDone,
weak_factory_.GetWeakPtr())));
base::BindOnce(&PostOnOwnerThread, base::WrapRefCounted(owner_),
base::BindOnce(&PrintJobWorker::GetSettingsDone,
weak_factory_.GetWeakPtr())));
}

void PrintJobWorker::UseDefaultSettings() {
Expand Down
7 changes: 4 additions & 3 deletions chrome/browser/printing/printer_query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ void PrinterQuery::GetSettings(GetSettingsAskParam ask_user_for_settings,
ask_user_for_settings == GetSettingsAskParam::ASK_USER;
worker_->PostTask(
FROM_HERE,
base::Bind(&PrintJobWorker::GetSettings, base::Unretained(worker_.get()),
is_print_dialog_box_shown_, expected_page_count, has_selection,
margin_type, is_scripted, is_modifiable));
base::BindOnce(&PrintJobWorker::GetSettings,
base::Unretained(worker_.get()),
is_print_dialog_box_shown_, expected_page_count,
has_selection, margin_type, is_scripted, is_modifiable));
}

void PrinterQuery::SetSettings(
Expand Down
16 changes: 6 additions & 10 deletions chrome/browser/ui/libgtkui/print_dialog_gtk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ bool PrintDialogGtk2::UpdateSettings(printing::PrintSettings* settings) {
void PrintDialogGtk2::ShowDialog(
gfx::NativeView parent_view,
bool has_selection,
const PrintingContextLinux::PrintSettingsCallback& callback) {
callback_ = callback;
PrintingContextLinux::PrintSettingsCallback callback) {
callback_ = std::move(callback);
DCHECK(!callback_.is_null());

dialog_ = gtk_print_unix_dialog_new(nullptr, nullptr);
Expand Down Expand Up @@ -477,14 +477,12 @@ void PrintDialogGtk2::OnResponse(GtkWidget* dialog, int response_id) {
settings.set_selection_only(print_selection_only);
InitPrintSettingsGtk(gtk_settings_, page_setup_, &settings);
context_->InitWithSettings(settings);
callback_.Run(PrintingContextLinux::OK);
callback_.Reset();
std::move(callback_).Run(PrintingContextLinux::OK);
return;
}
case GTK_RESPONSE_DELETE_EVENT: // Fall through.
case GTK_RESPONSE_CANCEL: {
callback_.Run(PrintingContextLinux::CANCEL);
callback_.Reset();
std::move(callback_).Run(PrintingContextLinux::CANCEL);
return;
}
case GTK_RESPONSE_APPLY:
Expand Down Expand Up @@ -551,8 +549,6 @@ void PrintDialogGtk2::OnWindowDestroying(aura::Window* window) {

libgtkui::ClearAuraTransientParent(dialog_);
window->RemoveObserver(this);
if (!callback_.is_null()) {
callback_.Run(PrintingContextLinux::CANCEL);
callback_.Reset();
}
if (!callback_.is_null())
std::move(callback_).Run(PrintingContextLinux::CANCEL);
}
2 changes: 1 addition & 1 deletion chrome/browser/ui/libgtkui/print_dialog_gtk.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class PrintDialogGtk2 : public printing::PrintDialogGtkInterface,
void ShowDialog(
gfx::NativeView parent_view,
bool has_selection,
const PrintingContextLinux::PrintSettingsCallback& callback) override;
PrintingContextLinux::PrintSettingsCallback callback) override;
void PrintDocument(const printing::MetafilePlayer& metafile,
const base::string16& document_name) override;
void AddRefToDialog() override;
Expand Down
2 changes: 1 addition & 1 deletion printing/print_dialog_gtk_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class PrintDialogGtkInterface {
virtual void ShowDialog(
gfx::NativeView parent_view,
bool has_selection,
const PrintingContextLinux::PrintSettingsCallback& callback) = 0;
PrintingContextLinux::PrintSettingsCallback callback) = 0;

// Prints the document named |document_name| contained in |metafile|.
// Called from the print worker thread. Once called, the
Expand Down
4 changes: 2 additions & 2 deletions printing/printing_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class PRINTING_EXPORT PrintingContext {

// Callback of AskUserForSettings, used to notify the PrintJobWorker when
// print settings are available.
typedef base::Callback<void(Result)> PrintSettingsCallback;
using PrintSettingsCallback = base::OnceCallback<void(Result)>;

// Asks the user what printer and format should be used to print. Updates the
// context with the select device settings. The result of the call is returned
Expand All @@ -63,7 +63,7 @@ class PRINTING_EXPORT PrintingContext {
virtual void AskUserForSettings(int max_pages,
bool has_selection,
bool is_scripted,
const PrintSettingsCallback& callback) = 0;
PrintSettingsCallback callback) = 0;

// Selects the user's default printer and format. Updates the context with the
// default device settings.
Expand Down
12 changes: 7 additions & 5 deletions printing/printing_context_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ void PrintingContextAndroid::AskUserForSettings(
int max_pages,
bool has_selection,
bool is_scripted,
const PrintSettingsCallback& callback) {
PrintSettingsCallback callback) {
// This method is always run in the UI thread.
callback_ = callback;
callback_ = std::move(callback);

JNIEnv* env = base::android::AttachCurrentThread();
if (j_printing_context_.is_null()) {
Expand All @@ -107,9 +107,10 @@ void PrintingContextAndroid::AskUserForSettingsReply(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
jboolean success) {
DCHECK(callback_);
if (!success) {
// TODO(cimamoglu): Differentiate between FAILED And CANCEL.
callback_.Run(FAILED);
std::move(callback_).Run(FAILED);
return;
}

Expand All @@ -134,14 +135,15 @@ void PrintingContextAndroid::AskUserForSettingsReply(
height = Round(ConvertUnitDouble(height, kInchToMil, 1.0) * dpi);
SetSizes(&settings_, dpi, width, height);

callback_.Run(OK);
std::move(callback_).Run(OK);
}

void PrintingContextAndroid::ShowSystemDialogDone(
JNIEnv* env,
const JavaParamRef<jobject>& obj) {
DCHECK(callback_);
// Settings are not updated, callback is called only to unblock javascript.
callback_.Run(CANCEL);
std::move(callback_).Run(CANCEL);
}

PrintingContext::Result PrintingContextAndroid::UseDefaultSettings() {
Expand Down
2 changes: 1 addition & 1 deletion printing/printing_context_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class PRINTING_EXPORT PrintingContextAndroid : public PrintingContext {
void AskUserForSettings(int max_pages,
bool has_selection,
bool is_scripted,
const PrintSettingsCallback& callback) override;
PrintSettingsCallback callback) override;
Result UseDefaultSettings() override;
gfx::Size GetPdfPaperSizeDeviceUnits() override;
Result UpdatePrinterSettings(bool external_preview,
Expand Down
2 changes: 1 addition & 1 deletion printing/printing_context_chromeos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ void PrintingContextChromeos::AskUserForSettings(
int max_pages,
bool has_selection,
bool is_scripted,
const PrintSettingsCallback& callback) {
PrintSettingsCallback callback) {
// We don't want to bring up a dialog here. Ever. This should not be called.
NOTREACHED();
}
Expand Down
2 changes: 1 addition & 1 deletion printing/printing_context_chromeos.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PRINTING_EXPORT PrintingContextChromeos : public PrintingContext {
void AskUserForSettings(int max_pages,
bool has_selection,
bool is_scripted,
const PrintSettingsCallback& callback) override;
PrintSettingsCallback callback) override;
Result UseDefaultSettings() override;
gfx::Size GetPdfPaperSizeDeviceUnits() override;
Result UpdatePrinterSettings(bool external_preview,
Expand Down
15 changes: 7 additions & 8 deletions printing/printing_context_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,20 @@ void PrintingContextLinux::PrintDocument(const MetafilePlayer& metafile) {
print_dialog_->PrintDocument(metafile, document_name_);
}

void PrintingContextLinux::AskUserForSettings(
int max_pages,
bool has_selection,
bool is_scripted,
const PrintSettingsCallback& callback) {
void PrintingContextLinux::AskUserForSettings(int max_pages,
bool has_selection,
bool is_scripted,
PrintSettingsCallback callback) {
if (!print_dialog_) {
// Can only get here if the renderer is sending bad messages.
// http://crbug.com/341777
NOTREACHED();
callback.Run(FAILED);
std::move(callback).Run(FAILED);
return;
}

print_dialog_->ShowDialog(
delegate_->GetParentView(), has_selection, callback);
print_dialog_->ShowDialog(delegate_->GetParentView(), has_selection,
std::move(callback));
}

PrintingContext::Result PrintingContextLinux::UseDefaultSettings() {
Expand Down
2 changes: 1 addition & 1 deletion printing/printing_context_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class PRINTING_EXPORT PrintingContextLinux : public PrintingContext {
void AskUserForSettings(int max_pages,
bool has_selection,
bool is_scripted,
const PrintSettingsCallback& callback) override;
PrintSettingsCallback callback) override;
gfx::Size GetPdfPaperSizeDeviceUnits() override;
Result UseDefaultSettings() override;
Result UpdatePrinterSettings(bool external_preview,
Expand Down
2 changes: 1 addition & 1 deletion printing/printing_context_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PRINTING_EXPORT PrintingContextMac : public PrintingContext {
void AskUserForSettings(int max_pages,
bool has_selection,
bool is_scripted,
const PrintSettingsCallback& callback) override;
PrintSettingsCallback callback) override;
Result UseDefaultSettings() override;
gfx::Size GetPdfPaperSizeDeviceUnits() override;
Result UpdatePrinterSettings(bool external_preview,
Expand Down
13 changes: 6 additions & 7 deletions printing/printing_context_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,10 @@ PMPaper MatchPaper(CFArrayRef paper_list,
ReleaseContext();
}

void PrintingContextMac::AskUserForSettings(
int max_pages,
bool has_selection,
bool is_scripted,
const PrintSettingsCallback& callback) {
void PrintingContextMac::AskUserForSettings(int max_pages,
bool has_selection,
bool is_scripted,
PrintSettingsCallback callback) {
// Exceptions can also happen when the NSPrintPanel is being
// deallocated, so it must be autoreleased within this scope.
base::mac::ScopedNSAutoreleasePool pool;
Expand Down Expand Up @@ -127,9 +126,9 @@ PMPaper MatchPaper(CFArrayRef paper_list,
print_info_.reset([[panel printInfo] retain]);
settings_.set_ranges(GetPageRangesFromPrintInfo());
InitPrintSettingsFromPrintInfo();
callback.Run(OK);
std::move(callback).Run(OK);
} else {
callback.Run(CANCEL);
std::move(callback).Run(CANCEL);
}
}

Expand Down
4 changes: 2 additions & 2 deletions printing/printing_context_no_system_dialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ void PrintingContextNoSystemDialog::AskUserForSettings(
int max_pages,
bool has_selection,
bool is_scripted,
const PrintSettingsCallback& callback) {
PrintSettingsCallback callback) {
// We don't want to bring up a dialog here. Ever. Just signal the callback.
callback.Run(OK);
std::move(callback).Run(OK);
}

PrintingContext::Result PrintingContextNoSystemDialog::UseDefaultSettings() {
Expand Down
2 changes: 1 addition & 1 deletion printing/printing_context_no_system_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PRINTING_EXPORT PrintingContextNoSystemDialog : public PrintingContext {
void AskUserForSettings(int max_pages,
bool has_selection,
bool is_scripted,
const PrintSettingsCallback& callback) override;
PrintSettingsCallback callback) override;
Result UseDefaultSettings() override;
gfx::Size GetPdfPaperSizeDeviceUnits() override;
Result UpdatePrinterSettings(bool external_preview,
Expand Down
6 changes: 3 additions & 3 deletions printing/printing_context_system_dialog_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void PrintingContextSystemDialogWin::AskUserForSettings(
int max_pages,
bool has_selection,
bool is_scripted,
const PrintSettingsCallback& callback) {
PrintSettingsCallback callback) {
DCHECK(!in_print_job_);

HWND window = GetRootWindow(delegate_->GetParentView());
Expand Down Expand Up @@ -66,12 +66,12 @@ void PrintingContextSystemDialogWin::AskUserForSettings(

if (ShowPrintDialog(&dialog_options) != S_OK) {
ResetSettings();
callback.Run(FAILED);
std::move(callback).Run(FAILED);
return;
}

// TODO(maruel): Support PD_PRINTTOFILE.
callback.Run(ParseDialogResultEx(dialog_options));
std::move(callback).Run(ParseDialogResultEx(dialog_options));
}

HRESULT PrintingContextSystemDialogWin::ShowPrintDialog(PRINTDLGEX* options) {
Expand Down
9 changes: 4 additions & 5 deletions printing/printing_context_system_dialog_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ class PRINTING_EXPORT PrintingContextSystemDialogWin
~PrintingContextSystemDialogWin() override;

// PrintingContext implementation.
void AskUserForSettings(
int max_pages,
bool has_selection,
bool is_scripted,
const PrintSettingsCallback& callback) override;
void AskUserForSettings(int max_pages,
bool has_selection,
bool is_scripted,
PrintSettingsCallback callback) override;

private:
friend class MockPrintingContextWin;
Expand Down
9 changes: 4 additions & 5 deletions printing/printing_context_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ PrintingContextWin::~PrintingContextWin() {
ReleaseContext();
}

void PrintingContextWin::AskUserForSettings(
int max_pages,
bool has_selection,
bool is_scripted,
const PrintSettingsCallback& callback) {
void PrintingContextWin::AskUserForSettings(int max_pages,
bool has_selection,
bool is_scripted,
PrintSettingsCallback callback) {
NOTIMPLEMENTED();
}

Expand Down
9 changes: 4 additions & 5 deletions printing/printing_context_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ class PRINTING_EXPORT PrintingContextWin : public PrintingContext {
Result InitWithSettingsForTest(const PrintSettings& settings);

// PrintingContext implementation.
void AskUserForSettings(
int max_pages,
bool has_selection,
bool is_scripted,
const PrintSettingsCallback& callback) override;
void AskUserForSettings(int max_pages,
bool has_selection,
bool is_scripted,
PrintSettingsCallback callback) override;
Result UseDefaultSettings() override;
gfx::Size GetPdfPaperSizeDeviceUnits() override;
Result UpdatePrinterSettings(bool external_preview,
Expand Down

0 comments on commit 48ceab2

Please sign in to comment.