| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/simple_message_box.h" | 5 #include "chrome/browser/ui/simple_message_box.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "chrome/browser/ui/views/constrained_window_views.h" | 10 #include "chrome/browser/ui/views/constrained_window_views.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 class SimpleMessageBoxViews : public views::DialogDelegate { | 30 class SimpleMessageBoxViews : public views::DialogDelegate { |
| 31 public: | 31 public: |
| 32 SimpleMessageBoxViews(const base::string16& title, | 32 SimpleMessageBoxViews(const base::string16& title, |
| 33 const base::string16& message, | 33 const base::string16& message, |
| 34 MessageBoxType type, | 34 MessageBoxType type, |
| 35 const base::string16& yes_text, | 35 const base::string16& yes_text, |
| 36 const base::string16& no_text, | 36 const base::string16& no_text, |
| 37 bool is_system_modal, | 37 bool is_system_modal); |
| 38 MessageBoxResult* result); | |
| 39 virtual ~SimpleMessageBoxViews(); | 38 virtual ~SimpleMessageBoxViews(); |
| 40 | 39 |
| 40 MessageBoxResult RunDialogAndGetResult(); |
| 41 |
| 41 // Overridden from views::DialogDelegate: | 42 // Overridden from views::DialogDelegate: |
| 42 virtual int GetDialogButtons() const OVERRIDE; | 43 virtual int GetDialogButtons() const OVERRIDE; |
| 43 virtual base::string16 GetDialogButtonLabel( | 44 virtual base::string16 GetDialogButtonLabel( |
| 44 ui::DialogButton button) const OVERRIDE; | 45 ui::DialogButton button) const OVERRIDE; |
| 45 virtual bool Cancel() OVERRIDE; | 46 virtual bool Cancel() OVERRIDE; |
| 46 virtual bool Accept() OVERRIDE; | 47 virtual bool Accept() OVERRIDE; |
| 47 | 48 |
| 48 // Overridden from views::WidgetDelegate: | 49 // Overridden from views::WidgetDelegate: |
| 49 virtual base::string16 GetWindowTitle() const OVERRIDE; | 50 virtual base::string16 GetWindowTitle() const OVERRIDE; |
| 50 virtual void DeleteDelegate() OVERRIDE; | 51 virtual void DeleteDelegate() OVERRIDE; |
| 51 virtual ui::ModalType GetModalType() const OVERRIDE; | 52 virtual ui::ModalType GetModalType() const OVERRIDE; |
| 52 virtual views::View* GetContentsView() OVERRIDE; | 53 virtual views::View* GetContentsView() OVERRIDE; |
| 53 virtual views::Widget* GetWidget() OVERRIDE; | 54 virtual views::Widget* GetWidget() OVERRIDE; |
| 54 virtual const views::Widget* GetWidget() const OVERRIDE; | 55 virtual const views::Widget* GetWidget() const OVERRIDE; |
| 55 | 56 |
| 56 private: | 57 private: |
| 57 | 58 |
| 58 // This terminates the nested message-loop. | 59 // This terminates the nested message-loop. |
| 59 void Done(); | 60 void Done(); |
| 60 | 61 |
| 61 const base::string16 window_title_; | 62 const base::string16 window_title_; |
| 62 const MessageBoxType type_; | 63 const MessageBoxType type_; |
| 63 base::string16 yes_text_; | 64 base::string16 yes_text_; |
| 64 base::string16 no_text_; | 65 base::string16 no_text_; |
| 65 MessageBoxResult* result_; | 66 MessageBoxResult* result_; |
| 66 bool is_system_modal_; | 67 bool is_system_modal_; |
| 67 views::MessageBoxView* message_box_view_; | 68 views::MessageBoxView* message_box_view_; |
| 69 base::Closure quit_runloop_; |
| 68 | 70 |
| 69 DISALLOW_COPY_AND_ASSIGN(SimpleMessageBoxViews); | 71 DISALLOW_COPY_AND_ASSIGN(SimpleMessageBoxViews); |
| 70 }; | 72 }; |
| 71 | 73 |
| 72 //////////////////////////////////////////////////////////////////////////////// | 74 //////////////////////////////////////////////////////////////////////////////// |
| 73 // SimpleMessageBoxViews, public: | 75 // SimpleMessageBoxViews, public: |
| 74 | 76 |
| 75 SimpleMessageBoxViews::SimpleMessageBoxViews(const base::string16& title, | 77 SimpleMessageBoxViews::SimpleMessageBoxViews(const base::string16& title, |
| 76 const base::string16& message, | 78 const base::string16& message, |
| 77 MessageBoxType type, | 79 MessageBoxType type, |
| 78 const base::string16& yes_text, | 80 const base::string16& yes_text, |
| 79 const base::string16& no_text, | 81 const base::string16& no_text, |
| 80 bool is_system_modal, | 82 bool is_system_modal) |
| 81 MessageBoxResult* result) | |
| 82 : window_title_(title), | 83 : window_title_(title), |
| 83 type_(type), | 84 type_(type), |
| 84 yes_text_(yes_text), | 85 yes_text_(yes_text), |
| 85 no_text_(no_text), | 86 no_text_(no_text), |
| 86 result_(result), | 87 result_(NULL), |
| 87 is_system_modal_(is_system_modal), | 88 is_system_modal_(is_system_modal), |
| 88 message_box_view_(new views::MessageBoxView( | 89 message_box_view_(new views::MessageBoxView( |
| 89 views::MessageBoxView::InitParams(message))) { | 90 views::MessageBoxView::InitParams(message))) { |
| 90 CHECK(result_); | |
| 91 if (yes_text_.empty()) { | 91 if (yes_text_.empty()) { |
| 92 if (type_ == MESSAGE_BOX_TYPE_QUESTION) | 92 if (type_ == MESSAGE_BOX_TYPE_QUESTION) |
| 93 yes_text_ = | 93 yes_text_ = |
| 94 l10n_util::GetStringUTF16(IDS_CONFIRM_MESSAGEBOX_YES_BUTTON_LABEL); | 94 l10n_util::GetStringUTF16(IDS_CONFIRM_MESSAGEBOX_YES_BUTTON_LABEL); |
| 95 else if (type_ == MESSAGE_BOX_TYPE_OK_CANCEL) | 95 else if (type_ == MESSAGE_BOX_TYPE_OK_CANCEL) |
| 96 yes_text_ = l10n_util::GetStringUTF16(IDS_OK); | 96 yes_text_ = l10n_util::GetStringUTF16(IDS_OK); |
| 97 else | 97 else |
| 98 yes_text_ = l10n_util::GetStringUTF16(IDS_OK); | 98 yes_text_ = l10n_util::GetStringUTF16(IDS_OK); |
| 99 } | 99 } |
| 100 | 100 |
| 101 if (no_text_.empty()) { | 101 if (no_text_.empty()) { |
| 102 if (type_ == MESSAGE_BOX_TYPE_QUESTION) | 102 if (type_ == MESSAGE_BOX_TYPE_QUESTION) |
| 103 no_text_ = | 103 no_text_ = |
| 104 l10n_util::GetStringUTF16(IDS_CONFIRM_MESSAGEBOX_NO_BUTTON_LABEL); | 104 l10n_util::GetStringUTF16(IDS_CONFIRM_MESSAGEBOX_NO_BUTTON_LABEL); |
| 105 else if (type_ == MESSAGE_BOX_TYPE_OK_CANCEL) | 105 else if (type_ == MESSAGE_BOX_TYPE_OK_CANCEL) |
| 106 no_text_ = l10n_util::GetStringUTF16(IDS_CANCEL); | 106 no_text_ = l10n_util::GetStringUTF16(IDS_CANCEL); |
| 107 } | 107 } |
| 108 } | 108 } |
| 109 | 109 |
| 110 SimpleMessageBoxViews::~SimpleMessageBoxViews() { | 110 SimpleMessageBoxViews::~SimpleMessageBoxViews() { |
| 111 } | 111 } |
| 112 | 112 |
| 113 MessageBoxResult SimpleMessageBoxViews::RunDialogAndGetResult() { |
| 114 MessageBoxResult result = MESSAGE_BOX_RESULT_NO; |
| 115 result_ = &result; |
| 116 // Use the widget's window itself so that the message loop exists when the |
| 117 // dialog is closed by some other means than |Cancel| or |Accept|. |
| 118 aura::Window* anchor = GetWidget()->GetNativeWindow(); |
| 119 aura::client::DispatcherRunLoop run_loop( |
| 120 aura::client::GetDispatcherClient(anchor->GetRootWindow()), NULL); |
| 121 quit_runloop_ = run_loop.QuitClosure(); |
| 122 run_loop.Run(); |
| 123 return result; |
| 124 } |
| 125 |
| 113 int SimpleMessageBoxViews::GetDialogButtons() const { | 126 int SimpleMessageBoxViews::GetDialogButtons() const { |
| 114 if (type_ == MESSAGE_BOX_TYPE_QUESTION || | 127 if (type_ == MESSAGE_BOX_TYPE_QUESTION || |
| 115 type_ == MESSAGE_BOX_TYPE_OK_CANCEL) { | 128 type_ == MESSAGE_BOX_TYPE_OK_CANCEL) { |
| 116 return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL; | 129 return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL; |
| 117 } | 130 } |
| 118 | 131 |
| 119 return ui::DIALOG_BUTTON_OK; | 132 return ui::DIALOG_BUTTON_OK; |
| 120 } | 133 } |
| 121 | 134 |
| 122 base::string16 SimpleMessageBoxViews::GetDialogButtonLabel( | 135 base::string16 SimpleMessageBoxViews::GetDialogButtonLabel( |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 } | 172 } |
| 160 | 173 |
| 161 const views::Widget* SimpleMessageBoxViews::GetWidget() const { | 174 const views::Widget* SimpleMessageBoxViews::GetWidget() const { |
| 162 return message_box_view_->GetWidget(); | 175 return message_box_view_->GetWidget(); |
| 163 } | 176 } |
| 164 | 177 |
| 165 //////////////////////////////////////////////////////////////////////////////// | 178 //////////////////////////////////////////////////////////////////////////////// |
| 166 // SimpleMessageBoxViews, private: | 179 // SimpleMessageBoxViews, private: |
| 167 | 180 |
| 168 void SimpleMessageBoxViews::Done() { | 181 void SimpleMessageBoxViews::Done() { |
| 169 aura::Window* window = GetWidget()->GetNativeView(); | 182 CHECK(!quit_runloop_.is_null()); |
| 170 aura::client::DispatcherClient* client = | 183 quit_runloop_.Run(); |
| 171 aura::client::GetDispatcherClient(window->GetRootWindow()); | |
| 172 client->QuitNestedMessageLoop(); | |
| 173 } | 184 } |
| 174 | 185 |
| 175 #if defined(OS_WIN) | 186 #if defined(OS_WIN) |
| 176 UINT GetMessageBoxFlagsFromType(MessageBoxType type) { | 187 UINT GetMessageBoxFlagsFromType(MessageBoxType type) { |
| 177 UINT flags = MB_SETFOREGROUND; | 188 UINT flags = MB_SETFOREGROUND; |
| 178 switch (type) { | 189 switch (type) { |
| 179 case MESSAGE_BOX_TYPE_INFORMATION: | 190 case MESSAGE_BOX_TYPE_INFORMATION: |
| 180 return flags | MB_OK | MB_ICONINFORMATION; | 191 return flags | MB_OK | MB_ICONINFORMATION; |
| 181 case MESSAGE_BOX_TYPE_WARNING: | 192 case MESSAGE_BOX_TYPE_WARNING: |
| 182 return flags | MB_OK | MB_ICONWARNING; | 193 return flags | MB_OK | MB_ICONWARNING; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 210 } | 221 } |
| 211 #else | 222 #else |
| 212 if (!base::MessageLoopForUI::IsCurrent() || | 223 if (!base::MessageLoopForUI::IsCurrent() || |
| 213 !ResourceBundle::HasSharedInstance()) { | 224 !ResourceBundle::HasSharedInstance()) { |
| 214 LOG(ERROR) << "Unable to show a dialog outside the UI thread message loop: " | 225 LOG(ERROR) << "Unable to show a dialog outside the UI thread message loop: " |
| 215 << title << " - " << message; | 226 << title << " - " << message; |
| 216 return MESSAGE_BOX_RESULT_NO; | 227 return MESSAGE_BOX_RESULT_NO; |
| 217 } | 228 } |
| 218 #endif | 229 #endif |
| 219 | 230 |
| 220 MessageBoxResult result = MESSAGE_BOX_RESULT_NO; | 231 SimpleMessageBoxViews* dialog = |
| 221 SimpleMessageBoxViews* dialog = new SimpleMessageBoxViews( | 232 new SimpleMessageBoxViews(title, |
| 222 title, | 233 message, |
| 223 message, | 234 type, |
| 224 type, | 235 yes_text, |
| 225 yes_text, | 236 no_text, |
| 226 no_text, | 237 parent == NULL // is_system_modal |
| 227 parent == NULL, // is_system_modal | 238 ); |
| 228 &result); | |
| 229 CreateBrowserModalDialogViews(dialog, parent)->Show(); | 239 CreateBrowserModalDialogViews(dialog, parent)->Show(); |
| 230 | 240 |
| 231 // Use the widget's window itself so that the message loop exists when the | 241 // NOTE: |dialog| may have been deleted by the time |RunDialogAndGetResult()| |
| 232 // dialog is closed by some other means than |Cancel| or |Accept|. | 242 // returns. |
| 233 aura::Window* anchor = dialog->GetWidget()->GetNativeWindow(); | 243 return dialog->RunDialogAndGetResult(); |
| 234 aura::client::DispatcherClient* client = | |
| 235 aura::client::GetDispatcherClient(anchor->GetRootWindow()); | |
| 236 client->RunWithDispatcher(NULL); | |
| 237 // NOTE: |dialog| will have been deleted by the time control returns here. | |
| 238 | |
| 239 return result; | |
| 240 } | 244 } |
| 241 | 245 |
| 242 } // namespace | 246 } // namespace |
| 243 | 247 |
| 244 MessageBoxResult ShowMessageBox(gfx::NativeWindow parent, | 248 MessageBoxResult ShowMessageBox(gfx::NativeWindow parent, |
| 245 const base::string16& title, | 249 const base::string16& title, |
| 246 const base::string16& message, | 250 const base::string16& message, |
| 247 MessageBoxType type) { | 251 MessageBoxType type) { |
| 248 return ShowMessageBoxImpl( | 252 return ShowMessageBoxImpl( |
| 249 parent, title, message, type, base::string16(), base::string16()); | 253 parent, title, message, type, base::string16(), base::string16()); |
| 250 } | 254 } |
| 251 | 255 |
| 252 MessageBoxResult ShowMessageBoxWithButtonText(gfx::NativeWindow parent, | 256 MessageBoxResult ShowMessageBoxWithButtonText(gfx::NativeWindow parent, |
| 253 const base::string16& title, | 257 const base::string16& title, |
| 254 const base::string16& message, | 258 const base::string16& message, |
| 255 const base::string16& yes_text, | 259 const base::string16& yes_text, |
| 256 const base::string16& no_text) { | 260 const base::string16& no_text) { |
| 257 return ShowMessageBoxImpl( | 261 return ShowMessageBoxImpl( |
| 258 parent, title, message, MESSAGE_BOX_TYPE_QUESTION, yes_text, no_text); | 262 parent, title, message, MESSAGE_BOX_TYPE_QUESTION, yes_text, no_text); |
| 259 } | 263 } |
| 260 | 264 |
| 261 } // namespace chrome | 265 } // namespace chrome |
| OLD | NEW |