diff options
-rw-r--r-- | chrome/browser/printing/printer_query.cc | 56 | ||||
-rw-r--r-- | chrome/browser/printing/printer_query.h | 13 | ||||
-rw-r--r-- | chrome/browser/printing/printing_message_filter.cc | 48 |
3 files changed, 43 insertions, 74 deletions
diff --git a/chrome/browser/printing/printer_query.cc b/chrome/browser/printing/printer_query.cc index b70228a..0536b24 100644 --- a/chrome/browser/printing/printer_query.cc +++ b/chrome/browser/printing/printer_query.cc @@ -27,11 +27,6 @@ PrinterQuery::~PrinterQuery() { DCHECK(!is_print_dialog_box_shown_); // If this fires, it is that this pending printer context has leaked. DCHECK(!worker_.get()); - if (callback_.get()) { - // Be sure to cancel it. - callback_->Cancel(); - } - // It may get deleted in a different thread that the one that created it. } void PrinterQuery::GetSettingsDone(const PrintSettings& new_settings, @@ -45,18 +40,18 @@ void PrinterQuery::GetSettingsDone(const PrintSettings& new_settings, // Failure. cookie_ = 0; } - if (callback_.get()) { + + if (!callback_.is_null()) { // This may cause reentrancy like to call StopWorker(). - callback_->Run(); - callback_.reset(NULL); + callback_.Run(); + callback_.Reset(); } } PrintJobWorker* PrinterQuery::DetachWorker(PrintJobWorkerOwner* new_owner) { - DCHECK(!callback_.get()); + DCHECK(callback_.is_null()); DCHECK(worker_.get()); - if (!worker_.get()) - return NULL; + worker_->SetNewOwner(new_owner); return worker_.release(); } @@ -78,11 +73,11 @@ void PrinterQuery::GetSettings(GetSettingsAskParam ask_user_for_settings, int expected_page_count, bool has_selection, MarginType margin_type, - CancelableTask* callback) { + const base::Closure& callback) { DCHECK_EQ(io_message_loop_, MessageLoop::current()); DCHECK(!is_print_dialog_box_shown_); - if (!StartWorker(callback)) - return; + + StartWorker(callback); // Real work is done in PrintJobWorker::Init(). is_print_dialog_box_shown_ = ask_user_for_settings == ASK_USER; @@ -95,9 +90,8 @@ void PrinterQuery::GetSettings(GetSettingsAskParam ask_user_for_settings, } void PrinterQuery::SetSettings(const DictionaryValue& new_settings, - CancelableTask* callback) { - if (!StartWorker(callback)) - return; + const base::Closure& callback) { + StartWorker(callback); worker_->message_loop()->PostTask( FROM_HERE, @@ -106,25 +100,15 @@ void PrinterQuery::SetSettings(const DictionaryValue& new_settings, new_settings.DeepCopy())); } -bool PrinterQuery::StartWorker(CancelableTask* callback) { - DCHECK(!callback_.get()); +void PrinterQuery::StartWorker(const base::Closure& callback) { + DCHECK(callback_.is_null()); DCHECK(worker_.get()); - if (!worker_.get()) - return false; - - // Lazy create the worker thread. There is one worker thread per print job. - if (!worker_->message_loop()) { - if (!worker_->Start()) { - if (callback) { - callback->Cancel(); - delete callback; - } - NOTREACHED(); - return false; - } - } - callback_.reset(callback); - return true; + + // Lazily create the worker thread. There is one worker thread per print job. + if (!worker_->message_loop()) + worker_->Start(); + + callback_ = callback; } void PrinterQuery::StopWorker() { @@ -139,7 +123,7 @@ void PrinterQuery::StopWorker() { } bool PrinterQuery::is_callback_pending() const { - return callback_.get() != NULL; + return !callback_.is_null(); } bool PrinterQuery::is_valid() const { diff --git a/chrome/browser/printing/printer_query.h b/chrome/browser/printing/printer_query.h index 3add3e4..dcb2f6a 100644 --- a/chrome/browser/printing/printer_query.h +++ b/chrome/browser/printing/printer_query.h @@ -6,13 +6,13 @@ #define CHROME_BROWSER_PRINTING_PRINTER_QUERY_H_ #pragma once +#include "base/callback.h" #include "base/compiler_specific.h" #include "base/memory/scoped_ptr.h" #include "chrome/browser/printing/print_job_worker_owner.h" #include "printing/print_job_constants.h" #include "ui/gfx/native_widget_types.h" -class CancelableTask; class MessageLoop; namespace base { @@ -51,11 +51,11 @@ class PrinterQuery : public PrintJobWorkerOwner { int expected_page_count, bool has_selection, MarginType margin_type, - CancelableTask* callback); + const base::Closure& callback); // Updates the current settings with |new_settings| dictionary values. void SetSettings(const base::DictionaryValue& new_settings, - CancelableTask* callback); + const base::Closure& callback); // Stops the worker thread since the client is done with this object. void StopWorker(); @@ -72,8 +72,7 @@ class PrinterQuery : public PrintJobWorkerOwner { virtual ~PrinterQuery(); // Lazy create the worker thread. There is one worker thread per print job. - // Returns true, if worker thread exists or has been created. - bool StartWorker(CancelableTask* callback); + void StartWorker(const base::Closure& callback); // Main message loop reference. Used to send notifications in the right // thread. @@ -96,8 +95,8 @@ class PrinterQuery : public PrintJobWorkerOwner { // Results from the last GetSettingsDone() callback. PrintingContext::Result last_status_; - // Task waiting to be executed. - scoped_ptr<CancelableTask> callback_; + // Callback waiting to be run. + base::Closure callback_; DISALLOW_COPY_AND_ASSIGN(PrinterQuery); }; diff --git a/chrome/browser/printing/printing_message_filter.cc b/chrome/browser/printing/printing_message_filter.cc index a497bba..fc5723e 100644 --- a/chrome/browser/printing/printing_message_filter.cc +++ b/chrome/browser/printing/printing_message_filter.cc @@ -6,6 +6,7 @@ #include <string> +#include "base/bind.h" #include "base/process_util.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/printing/printer_query.h" @@ -181,23 +182,16 @@ void PrintingMessageFilter::OnGetDefaultPrintSettings(IPC::Message* reply_msg) { } print_job_manager_->PopPrinterQuery(0, &printer_query); - if (!printer_query.get()) { + if (!printer_query.get()) printer_query = new printing::PrinterQuery; - } - CancelableTask* task = NewRunnableMethod( - this, - &PrintingMessageFilter::OnGetDefaultPrintSettingsReply, - printer_query, - reply_msg); // Loads default settings. This is asynchronous, only the IPC message sender // will hang until the settings are retrieved. - printer_query->GetSettings(printing::PrinterQuery::DEFAULTS, - NULL, - 0, - false, - printing::DEFAULT_MARGINS, - task); + printer_query->GetSettings( + printing::PrinterQuery::DEFAULTS, NULL, 0, false, + printing::DEFAULT_MARGINS, + base::Bind(&PrintingMessageFilter::OnGetDefaultPrintSettingsReply, this, + printer_query, reply_msg)); } void PrintingMessageFilter::OnGetDefaultPrintSettingsReply( @@ -236,18 +230,11 @@ void PrintingMessageFilter::OnScriptedPrint( printer_query = new printing::PrinterQuery; } - CancelableTask* task = NewRunnableMethod( - this, - &PrintingMessageFilter::OnScriptedPrintReply, - printer_query, - reply_msg); - - printer_query->GetSettings(printing::PrinterQuery::ASK_USER, - host_view, - params.expected_pages_count, - params.has_selection, - params.margin_type, - task); + printer_query->GetSettings( + printing::PrinterQuery::ASK_USER, host_view, params.expected_pages_count, + params.has_selection, params.margin_type, + base::Bind(&PrintingMessageFilter::OnScriptedPrintReply, this, + printer_query, reply_msg)); } void PrintingMessageFilter::OnScriptedPrintReply( @@ -285,12 +272,11 @@ void PrintingMessageFilter::OnUpdatePrintSettings( print_job_manager_->PopPrinterQuery(document_cookie, &printer_query); if (!printer_query.get()) printer_query = new printing::PrinterQuery; - CancelableTask* task = NewRunnableMethod( - this, - &PrintingMessageFilter::OnUpdatePrintSettingsReply, - printer_query, - reply_msg); - printer_query->SetSettings(job_settings, task); + + printer_query->SetSettings( + job_settings, + base::Bind(&PrintingMessageFilter::OnUpdatePrintSettingsReply, this, + printer_query, reply_msg)); } void PrintingMessageFilter::OnUpdatePrintSettingsReply( |