diff options
author | sadrul@chromium.org <sadrul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-26 20:11:14 +0000 |
---|---|---|
committer | sadrul@chromium.org <sadrul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-26 20:11:14 +0000 |
commit | 198d2a8762562303eff56e395408c171f967224a (patch) | |
tree | 1562f54111448f250d3514a18b567696d51a6cc1 | |
parent | c2c05b561f5fd0d39744c87e41177ceeb22b29b7 (diff) | |
download | chromium_src-198d2a8762562303eff56e395408c171f967224a.zip chromium_src-198d2a8762562303eff56e395408c171f967224a.tar.gz chromium_src-198d2a8762562303eff56e395408c171f967224a.tar.bz2 |
views: Make SimpleMessageBoxViews ref-counted to make sure they get deleted at the right time.
Multiple SimpleMessageBoxViews can show up at the same time. Each of these
starts a nested message-loop. However, these SimpleMessageBoxViews can be
deleted in any order. This creates problems if a box in an inner-loop gets
destroyed before a box in an outer-loop. So to avoid this, ref-counting is
used so that the SimpleMessageBoxViews gets deleted at the right time.
BUG=134471
TEST=manually
Review URL: https://chromiumcodereview.appspot.com/10669032
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@144253 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/ui/views/simple_message_box_views.cc | 33 | ||||
-rw-r--r-- | ui/views/window/dialog_delegate.cc | 5 | ||||
-rw-r--r-- | ui/views/window/dialog_delegate.h | 4 |
3 files changed, 28 insertions, 14 deletions
diff --git a/chrome/browser/ui/views/simple_message_box_views.cc b/chrome/browser/ui/views/simple_message_box_views.cc index 7636341..805c6b5 100644 --- a/chrome/browser/ui/views/simple_message_box_views.cc +++ b/chrome/browser/ui/views/simple_message_box_views.cc @@ -6,6 +6,7 @@ #include "base/basictypes.h" #include "base/compiler_specific.h" +#include "base/memory/ref_counted.h" #include "base/message_loop.h" #include "chrome/browser/browser_process.h" #include "grit/generated_resources.h" @@ -25,8 +26,14 @@ namespace browser { namespace { +// Multiple SimpleMessageBoxViews can show up at the same time. Each of these +// start a nested message-loop. However, these SimpleMessageBoxViews can be +// deleted in any order. This creates problems if a box in an inner-loop gets +// destroyed before a box in an outer-loop. So to avoid this, ref-counting is +// used so that the SimpleMessageBoxViews gets deleted at the right time. class SimpleMessageBoxViews : public views::DialogDelegate, - public MessageLoop::Dispatcher { + public MessageLoop::Dispatcher, + public base::RefCounted<SimpleMessageBoxViews> { public: SimpleMessageBoxViews(const string16& title, const string16& message, @@ -34,8 +41,6 @@ class SimpleMessageBoxViews : public views::DialogDelegate, MessageBoxResult result() const { return result_; } - virtual ~SimpleMessageBoxViews(); - // Overridden from views::DialogDelegate: virtual int GetDialogButtons() const OVERRIDE; virtual string16 GetDialogButtonLabel(ui::DialogButton button) const OVERRIDE; @@ -54,6 +59,9 @@ class SimpleMessageBoxViews : public views::DialogDelegate, virtual bool Dispatch(const base::NativeEvent& event) OVERRIDE; private: + friend class base::RefCounted<SimpleMessageBoxViews>; + virtual ~SimpleMessageBoxViews(); + const string16 window_title_; const MessageBoxType type_; MessageBoxResult result_; @@ -78,12 +86,7 @@ SimpleMessageBoxViews::SimpleMessageBoxViews(const string16& title, message_box_view_(new views::MessageBoxView( views::MessageBoxView::InitParams(message))), should_show_dialog_(true) { -} - -//////////////////////////////////////////////////////////////////////////////// -// SimpleMessageBoxViews, private: - -SimpleMessageBoxViews::~SimpleMessageBoxViews() { + AddRef(); } int SimpleMessageBoxViews::GetDialogButtons() const { @@ -119,7 +122,7 @@ string16 SimpleMessageBoxViews::GetWindowTitle() const { } void SimpleMessageBoxViews::DeleteDelegate() { - delete this; + Release(); } ui::ModalType SimpleMessageBoxViews::GetModalType() const { @@ -148,14 +151,20 @@ bool SimpleMessageBoxViews::Dispatch(const base::NativeEvent& event) { return should_show_dialog_; } +//////////////////////////////////////////////////////////////////////////////// +// SimpleMessageBoxViews, private: + +SimpleMessageBoxViews::~SimpleMessageBoxViews() { +} + } // namespace MessageBoxResult ShowMessageBox(gfx::NativeWindow parent, const string16& title, const string16& message, MessageBoxType type) { - SimpleMessageBoxViews* dialog = - new SimpleMessageBoxViews(title, message, type); + scoped_refptr<SimpleMessageBoxViews> dialog( + new SimpleMessageBoxViews(title, message, type)); views::Widget::CreateWindowWithParent(dialog, parent)->Show(); diff --git a/ui/views/window/dialog_delegate.cc b/ui/views/window/dialog_delegate.cc index dfc6897..3922802 100644 --- a/ui/views/window/dialog_delegate.cc +++ b/ui/views/window/dialog_delegate.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -15,6 +15,9 @@ namespace views { DialogDelegate* DialogDelegate::AsDialogDelegate() { return this; } +DialogDelegate::~DialogDelegate() { +} + int DialogDelegate::GetDialogButtons() const { return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL; } diff --git a/ui/views/window/dialog_delegate.h b/ui/views/window/dialog_delegate.h index 28af328..34a8bb5 100644 --- a/ui/views/window/dialog_delegate.h +++ b/ui/views/window/dialog_delegate.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -31,6 +31,8 @@ class VIEWS_EXPORT DialogDelegate : public WidgetDelegate { public: virtual DialogDelegate* AsDialogDelegate() OVERRIDE; + virtual ~DialogDelegate(); + // Returns a mask specifying which of the available DialogButtons are visible // for the dialog. Note: If an OK button is provided, you should provide a // CANCEL button. A dialog box with just an OK button is frowned upon and |