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/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 base::string16 yes_text_; | 66 base::string16 yes_text_; |
67 base::string16 no_text_; | 67 base::string16 no_text_; |
68 MessageBoxResult* result_; | 68 MessageBoxResult* result_; |
69 views::MessageBoxView* message_box_view_; | 69 views::MessageBoxView* message_box_view_; |
70 base::Closure quit_runloop_; | 70 base::Closure quit_runloop_; |
71 bool is_system_modal_; | 71 bool is_system_modal_; |
72 | 72 |
73 DISALLOW_COPY_AND_ASSIGN(SimpleMessageBoxViews); | 73 DISALLOW_COPY_AND_ASSIGN(SimpleMessageBoxViews); |
74 }; | 74 }; |
75 | 75 |
76 // The currently showing message box, if there is one. Used for tests. | |
77 SimpleMessageBoxViews* g_current_message_box = nullptr; | |
78 | |
79 //////////////////////////////////////////////////////////////////////////////// | 76 //////////////////////////////////////////////////////////////////////////////// |
80 // SimpleMessageBoxViews, public: | 77 // SimpleMessageBoxViews, public: |
81 | 78 |
82 SimpleMessageBoxViews::SimpleMessageBoxViews( | 79 SimpleMessageBoxViews::SimpleMessageBoxViews( |
83 const base::string16& title, | 80 const base::string16& title, |
84 const base::string16& message, | 81 const base::string16& message, |
85 MessageBoxType type, | 82 MessageBoxType type, |
86 const base::string16& yes_text, | 83 const base::string16& yes_text, |
87 const base::string16& no_text, | 84 const base::string16& no_text, |
88 const base::string16& checkbox_text, | 85 const base::string16& checkbox_text, |
(...skipping 19 matching lines...) Expand all Loading... |
108 if (!checkbox_text.empty()) { | 105 if (!checkbox_text.empty()) { |
109 message_box_view_->SetCheckBoxLabel(checkbox_text); | 106 message_box_view_->SetCheckBoxLabel(checkbox_text); |
110 message_box_view_->SetCheckBoxSelected(true); | 107 message_box_view_->SetCheckBoxSelected(true); |
111 } | 108 } |
112 } | 109 } |
113 | 110 |
114 SimpleMessageBoxViews::~SimpleMessageBoxViews() { | 111 SimpleMessageBoxViews::~SimpleMessageBoxViews() { |
115 } | 112 } |
116 | 113 |
117 MessageBoxResult SimpleMessageBoxViews::RunDialogAndGetResult() { | 114 MessageBoxResult SimpleMessageBoxViews::RunDialogAndGetResult() { |
118 g_current_message_box = this; | |
119 MessageBoxResult result = MESSAGE_BOX_RESULT_NO; | 115 MessageBoxResult result = MESSAGE_BOX_RESULT_NO; |
120 result_ = &result; | 116 result_ = &result; |
121 // TODO(pkotwicz): Exit message loop when the dialog is closed by some other | 117 // TODO(pkotwicz): Exit message loop when the dialog is closed by some other |
122 // means than |Cancel| or |Accept|. crbug.com/404385 | 118 // means than |Cancel| or |Accept|. crbug.com/404385 |
123 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | 119 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
124 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); | 120 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); |
125 base::RunLoop run_loop; | 121 base::RunLoop run_loop; |
126 quit_runloop_ = run_loop.QuitClosure(); | 122 quit_runloop_ = run_loop.QuitClosure(); |
127 run_loop.Run(); | 123 run_loop.Run(); |
128 g_current_message_box = nullptr; | |
129 return result; | 124 return result; |
130 } | 125 } |
131 | 126 |
132 int SimpleMessageBoxViews::GetDialogButtons() const { | 127 int SimpleMessageBoxViews::GetDialogButtons() const { |
133 if (type_ == MESSAGE_BOX_TYPE_QUESTION) | 128 if (type_ == MESSAGE_BOX_TYPE_QUESTION) |
134 return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL; | 129 return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL; |
135 | 130 |
136 return ui::DIALOG_BUTTON_OK; | 131 return ui::DIALOG_BUTTON_OK; |
137 } | 132 } |
138 | 133 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 checkbox_text, !parent /* is_system_modal */); | 240 checkbox_text, !parent /* is_system_modal */); |
246 constrained_window::CreateBrowserModalDialogViews(dialog, parent)->Show(); | 241 constrained_window::CreateBrowserModalDialogViews(dialog, parent)->Show(); |
247 | 242 |
248 // NOTE: |dialog| may have been deleted by the time |RunDialogAndGetResult()| | 243 // NOTE: |dialog| may have been deleted by the time |RunDialogAndGetResult()| |
249 // returns. | 244 // returns. |
250 return dialog->RunDialogAndGetResult(); | 245 return dialog->RunDialogAndGetResult(); |
251 } | 246 } |
252 | 247 |
253 } // namespace | 248 } // namespace |
254 | 249 |
255 bool CloseMessageBoxForTest(bool accept) { | |
256 if (!g_current_message_box) | |
257 return false; | |
258 | |
259 if (accept) | |
260 g_current_message_box->Accept(); | |
261 else | |
262 g_current_message_box->Cancel(); | |
263 return true; | |
264 } | |
265 | |
266 void ShowWarningMessageBox(gfx::NativeWindow parent, | 250 void ShowWarningMessageBox(gfx::NativeWindow parent, |
267 const base::string16& title, | 251 const base::string16& title, |
268 const base::string16& message) { | 252 const base::string16& message) { |
269 ShowMessageBoxImpl(parent, title, message, MESSAGE_BOX_TYPE_WARNING, | 253 ShowMessageBoxImpl(parent, title, message, MESSAGE_BOX_TYPE_WARNING, |
270 base::string16(), base::string16(), base::string16()); | 254 base::string16(), base::string16(), base::string16()); |
271 } | 255 } |
272 | 256 |
273 bool ShowWarningMessageBoxWithCheckbox(gfx::NativeWindow parent, | 257 bool ShowWarningMessageBoxWithCheckbox(gfx::NativeWindow parent, |
274 const base::string16& title, | 258 const base::string16& title, |
275 const base::string16& message, | 259 const base::string16& message, |
(...skipping 14 matching lines...) Expand all Loading... |
290 MessageBoxResult ShowMessageBoxWithButtonText(gfx::NativeWindow parent, | 274 MessageBoxResult ShowMessageBoxWithButtonText(gfx::NativeWindow parent, |
291 const base::string16& title, | 275 const base::string16& title, |
292 const base::string16& message, | 276 const base::string16& message, |
293 const base::string16& yes_text, | 277 const base::string16& yes_text, |
294 const base::string16& no_text) { | 278 const base::string16& no_text) { |
295 return ShowMessageBoxImpl(parent, title, message, MESSAGE_BOX_TYPE_QUESTION, | 279 return ShowMessageBoxImpl(parent, title, message, MESSAGE_BOX_TYPE_QUESTION, |
296 yes_text, no_text, base::string16()); | 280 yes_text, no_text, base::string16()); |
297 } | 281 } |
298 | 282 |
299 } // namespace chrome | 283 } // namespace chrome |
OLD | NEW |