Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Unified Diff: chrome/browser/printing/printing_message_filter.cc

Issue 2540253002: Fix heap use-after-free in PrintingMessageFilter (Closed)
Patch Set: Fix compile error Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/printing/printing_message_filter.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/printing/printing_message_filter.cc
diff --git a/chrome/browser/printing/printing_message_filter.cc b/chrome/browser/printing/printing_message_filter.cc
index a4f2597779073a05f469dee5c38d2eb5dc0199ec..0bb82c0f3c2053a9d8f396d3eecb2d9f41a25a03 100644
--- a/chrome/browser/printing/printing_message_filter.cc
+++ b/chrome/browser/printing/printing_message_filter.cc
@@ -9,12 +9,14 @@
#include <utility>
#include "base/bind.h"
+#include "base/memory/singleton.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/printing/print_job_manager.h"
#include "chrome/browser/printing/printer_query.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
+#include "components/keyed_service/content/browser_context_keyed_service_shutdown_notifier_factory.h"
#include "components/printing/browser/print_manager_utils.h"
#include "components/printing/common/print_messages.h"
#include "content/public/browser/browser_thread.h"
@@ -39,6 +41,25 @@ namespace printing {
namespace {
+class ShutdownNotifierFactory
+ : public BrowserContextKeyedServiceShutdownNotifierFactory {
+ public:
+ static ShutdownNotifierFactory* GetInstance() {
+ return base::Singleton<ShutdownNotifierFactory>::get();
+ }
+
+ private:
+ friend struct base::DefaultSingletonTraits<ShutdownNotifierFactory>;
+
+ ShutdownNotifierFactory()
+ : BrowserContextKeyedServiceShutdownNotifierFactory(
+ "PrintingMessageFilter") {}
+
+ ~ShutdownNotifierFactory() override {}
+
+ DISALLOW_COPY_AND_ASSIGN(ShutdownNotifierFactory);
+};
+
#if defined(OS_ANDROID)
content::WebContents* GetWebContentsForRenderFrame(int render_process_id,
int render_frame_id) {
@@ -63,16 +84,26 @@ PrintViewManagerBasic* GetPrintManager(int render_process_id,
PrintingMessageFilter::PrintingMessageFilter(int render_process_id,
Profile* profile)
: BrowserMessageFilter(PrintMsgStart),
- is_printing_enabled_(new BooleanPrefMember),
render_process_id_(render_process_id),
queue_(g_browser_process->print_job_manager()->queue()) {
DCHECK(queue_.get());
- is_printing_enabled_->Init(prefs::kPrintingEnabled, profile->GetPrefs());
- is_printing_enabled_->MoveToThread(
+ printing_shutdown_notifier_ =
+ ShutdownNotifierFactory::GetInstance()->Get(profile)->Subscribe(
+ base::Bind(&PrintingMessageFilter::ShutdownOnUIThread,
+ base::Unretained(this)));
+ is_printing_enabled_.Init(prefs::kPrintingEnabled, profile->GetPrefs());
+ is_printing_enabled_.MoveToThread(
Vitaly Buka (NO REVIEWS) 2016/12/02 18:51:00 Thy it's moved to IO thread here, but destroyed on
rbpotter 2016/12/02 19:28:34 It's moved to the IO thread so that GetValue can o
BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
}
PrintingMessageFilter::~PrintingMessageFilter() {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+}
+
+void PrintingMessageFilter::ShutdownOnUIThread() {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ is_printing_enabled_.Destroy();
Vitaly Buka (NO REVIEWS) 2016/12/02 18:51:00 what is guarantee that OnMessageReceived is not go
rbpotter 2016/12/02 19:28:34 If it does get called I think it will be okay as t
+ printing_shutdown_notifier_.reset();
}
void PrintingMessageFilter::OverrideThreadForMessage(
@@ -85,6 +116,10 @@ void PrintingMessageFilter::OverrideThreadForMessage(
#endif
}
+void PrintingMessageFilter::OnDestruct() const {
+ BrowserThread::DeleteOnUIThread::Destruct(this);
+}
+
bool PrintingMessageFilter::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(PrintingMessageFilter, message)
@@ -137,7 +172,7 @@ void PrintingMessageFilter::OnTempFileForPrintingWritten(int render_frame_id,
void PrintingMessageFilter::OnGetDefaultPrintSettings(IPC::Message* reply_msg) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
scoped_refptr<PrinterQuery> printer_query;
- if (!is_printing_enabled_->GetValue()) {
+ if (!is_printing_enabled_.GetValue()) {
// Reply with NULL query.
OnGetDefaultPrintSettingsReply(printer_query, reply_msg);
return;
@@ -250,7 +285,7 @@ void PrintingMessageFilter::OnUpdatePrintSettings(
std::unique_ptr<base::DictionaryValue> new_settings(job_settings.DeepCopy());
scoped_refptr<PrinterQuery> printer_query;
- if (!is_printing_enabled_->GetValue()) {
+ if (!is_printing_enabled_.GetValue()) {
// Reply with NULL query.
OnUpdatePrintSettingsReply(printer_query, reply_msg);
return;
« no previous file with comments | « chrome/browser/printing/printing_message_filter.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698