Chromium Code Reviews| 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 19 matching lines...) Expand all Loading... | |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 class SimpleMessageBoxViews : public views::DialogDelegate { | 33 class SimpleMessageBoxViews : public views::DialogDelegate { |
| 34 public: | 34 public: |
| 35 SimpleMessageBoxViews(const base::string16& title, | 35 SimpleMessageBoxViews(const base::string16& title, |
| 36 const base::string16& message, | 36 const base::string16& message, |
| 37 MessageBoxType type, | 37 MessageBoxType type, |
| 38 const base::string16& yes_text, | 38 const base::string16& yes_text, |
| 39 const base::string16& no_text, | 39 const base::string16& no_text, |
| 40 const base::string16& checkbox_text, | |
| 40 bool is_system_modal); | 41 bool is_system_modal); |
| 41 ~SimpleMessageBoxViews() override; | 42 ~SimpleMessageBoxViews() override; |
| 42 | 43 |
| 43 MessageBoxResult RunDialogAndGetResult(); | 44 MessageBoxResult RunDialogAndGetResult(); |
| 44 | 45 |
| 45 // Overridden from views::DialogDelegate: | 46 // Overridden from views::DialogDelegate: |
| 46 int GetDialogButtons() const override; | 47 int GetDialogButtons() const override; |
| 47 base::string16 GetDialogButtonLabel(ui::DialogButton button) const override; | 48 base::string16 GetDialogButtonLabel(ui::DialogButton button) const override; |
| 48 bool Cancel() override; | 49 bool Cancel() override; |
| 49 bool Accept() override; | 50 bool Accept() override; |
| 50 | 51 |
| 51 // Overridden from views::WidgetDelegate: | 52 // Overridden from views::WidgetDelegate: |
| 52 base::string16 GetWindowTitle() const override; | 53 base::string16 GetWindowTitle() const override; |
| 53 void DeleteDelegate() override; | 54 void DeleteDelegate() override; |
| 54 ui::ModalType GetModalType() const override; | 55 ui::ModalType GetModalType() const override; |
| 55 views::View* GetContentsView() override; | 56 views::View* GetContentsView() override; |
| 56 views::Widget* GetWidget() override; | 57 views::Widget* GetWidget() override; |
| 57 const views::Widget* GetWidget() const override; | 58 const views::Widget* GetWidget() const override; |
| 58 | 59 |
| 59 private: | 60 private: |
| 60 // This terminates the nested message-loop. | 61 // This terminates the nested message-loop. |
| 61 void Done(); | 62 void Done(); |
| 62 | 63 |
| 63 const base::string16 window_title_; | 64 const base::string16 window_title_; |
| 64 const MessageBoxType type_; | 65 const MessageBoxType type_; |
| 65 base::string16 yes_text_; | 66 base::string16 yes_text_; |
| 66 base::string16 no_text_; | 67 base::string16 no_text_; |
| 67 MessageBoxResult* result_; | 68 MessageBoxResult* result_; |
| 68 bool is_system_modal_; | |
| 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_; | |
| 72 bool has_checkbox_; | |
| 71 | 73 |
| 72 DISALLOW_COPY_AND_ASSIGN(SimpleMessageBoxViews); | 74 DISALLOW_COPY_AND_ASSIGN(SimpleMessageBoxViews); |
| 73 }; | 75 }; |
| 74 | 76 |
| 75 //////////////////////////////////////////////////////////////////////////////// | 77 //////////////////////////////////////////////////////////////////////////////// |
| 76 // SimpleMessageBoxViews, public: | 78 // SimpleMessageBoxViews, public: |
| 77 | 79 |
| 78 SimpleMessageBoxViews::SimpleMessageBoxViews(const base::string16& title, | 80 SimpleMessageBoxViews::SimpleMessageBoxViews( |
| 79 const base::string16& message, | 81 const base::string16& title, |
| 80 MessageBoxType type, | 82 const base::string16& message, |
| 81 const base::string16& yes_text, | 83 MessageBoxType type, |
| 82 const base::string16& no_text, | 84 const base::string16& yes_text, |
| 83 bool is_system_modal) | 85 const base::string16& no_text, |
| 86 const base::string16& checkbox_text, | |
| 87 bool is_system_modal) | |
| 84 : window_title_(title), | 88 : window_title_(title), |
| 85 type_(type), | 89 type_(type), |
| 86 yes_text_(yes_text), | 90 yes_text_(yes_text), |
| 87 no_text_(no_text), | 91 no_text_(no_text), |
| 88 result_(NULL), | 92 result_(NULL), |
| 93 message_box_view_(new views::MessageBoxView( | |
| 94 views::MessageBoxView::InitParams(message))), | |
| 89 is_system_modal_(is_system_modal), | 95 is_system_modal_(is_system_modal), |
| 90 message_box_view_(new views::MessageBoxView( | 96 has_checkbox_(!checkbox_text.empty()) { |
| 91 views::MessageBoxView::InitParams(message))) { | |
| 92 if (yes_text_.empty()) { | 97 if (yes_text_.empty()) { |
| 93 yes_text_ = | 98 yes_text_ = |
| 94 type_ == MESSAGE_BOX_TYPE_QUESTION | 99 type_ == MESSAGE_BOX_TYPE_QUESTION |
| 95 ? l10n_util::GetStringUTF16(IDS_CONFIRM_MESSAGEBOX_YES_BUTTON_LABEL) | 100 ? l10n_util::GetStringUTF16(IDS_CONFIRM_MESSAGEBOX_YES_BUTTON_LABEL) |
| 96 : l10n_util::GetStringUTF16(IDS_OK); | 101 : l10n_util::GetStringUTF16(IDS_OK); |
| 97 } | 102 } |
| 98 | 103 |
| 99 if (no_text_.empty() && type_ == MESSAGE_BOX_TYPE_QUESTION) | 104 if (no_text_.empty() && type_ == MESSAGE_BOX_TYPE_QUESTION) |
| 100 no_text_ = l10n_util::GetStringUTF16(IDS_CANCEL); | 105 no_text_ = l10n_util::GetStringUTF16(IDS_CANCEL); |
| 106 | |
| 107 if (has_checkbox_) { | |
| 108 message_box_view_->SetCheckBoxLabel(checkbox_text); | |
| 109 message_box_view_->SetCheckBoxSelected(true); | |
| 110 } | |
| 101 } | 111 } |
| 102 | 112 |
| 103 SimpleMessageBoxViews::~SimpleMessageBoxViews() { | 113 SimpleMessageBoxViews::~SimpleMessageBoxViews() { |
| 104 } | 114 } |
| 105 | 115 |
| 106 MessageBoxResult SimpleMessageBoxViews::RunDialogAndGetResult() { | 116 MessageBoxResult SimpleMessageBoxViews::RunDialogAndGetResult() { |
| 107 MessageBoxResult result = MESSAGE_BOX_RESULT_NO; | 117 MessageBoxResult result = MESSAGE_BOX_RESULT_NO; |
| 108 result_ = &result; | 118 result_ = &result; |
| 109 // TODO(pkotwicz): Exit message loop when the dialog is closed by some other | 119 // TODO(pkotwicz): Exit message loop when the dialog is closed by some other |
| 110 // means than |Cancel| or |Accept|. crbug.com/404385 | 120 // means than |Cancel| or |Accept|. crbug.com/404385 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 130 return yes_text_; | 140 return yes_text_; |
| 131 } | 141 } |
| 132 | 142 |
| 133 bool SimpleMessageBoxViews::Cancel() { | 143 bool SimpleMessageBoxViews::Cancel() { |
| 134 *result_ = MESSAGE_BOX_RESULT_NO; | 144 *result_ = MESSAGE_BOX_RESULT_NO; |
| 135 Done(); | 145 Done(); |
| 136 return true; | 146 return true; |
| 137 } | 147 } |
| 138 | 148 |
| 139 bool SimpleMessageBoxViews::Accept() { | 149 bool SimpleMessageBoxViews::Accept() { |
| 140 *result_ = MESSAGE_BOX_RESULT_YES; | 150 if (!has_checkbox_ || message_box_view_->IsCheckBoxSelected()) |
| 151 *result_ = MESSAGE_BOX_RESULT_YES; | |
| 152 else | |
| 153 *result_ = MESSAGE_BOX_RESULT_NO; | |
| 154 | |
| 141 Done(); | 155 Done(); |
| 142 return true; | 156 return true; |
| 143 } | 157 } |
| 144 | 158 |
| 145 base::string16 SimpleMessageBoxViews::GetWindowTitle() const { | 159 base::string16 SimpleMessageBoxViews::GetWindowTitle() const { |
| 146 return window_title_; | 160 return window_title_; |
| 147 } | 161 } |
| 148 | 162 |
| 149 void SimpleMessageBoxViews::DeleteDelegate() { | 163 void SimpleMessageBoxViews::DeleteDelegate() { |
| 150 delete this; | 164 delete this; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 186 NOTREACHED(); | 200 NOTREACHED(); |
| 187 return flags | MB_OK | MB_ICONWARNING; | 201 return flags | MB_OK | MB_ICONWARNING; |
| 188 } | 202 } |
| 189 #endif | 203 #endif |
| 190 | 204 |
| 191 MessageBoxResult ShowMessageBoxImpl(gfx::NativeWindow parent, | 205 MessageBoxResult ShowMessageBoxImpl(gfx::NativeWindow parent, |
| 192 const base::string16& title, | 206 const base::string16& title, |
| 193 const base::string16& message, | 207 const base::string16& message, |
| 194 MessageBoxType type, | 208 MessageBoxType type, |
| 195 const base::string16& yes_text, | 209 const base::string16& yes_text, |
| 196 const base::string16& no_text) { | 210 const base::string16& no_text, |
| 211 const base::string16& checkbox_text) { | |
| 197 startup_metric_utils::SetNonBrowserUIDisplayed(); | 212 startup_metric_utils::SetNonBrowserUIDisplayed(); |
| 198 if (internal::g_should_skip_message_box_for_test) | 213 if (internal::g_should_skip_message_box_for_test) |
| 199 return MESSAGE_BOX_RESULT_YES; | 214 return MESSAGE_BOX_RESULT_YES; |
| 200 | 215 |
| 201 // Views dialogs cannot be shown outside the UI thread message loop or if the | 216 // Views dialogs cannot be shown outside the UI thread message loop or if the |
| 202 // ResourceBundle is not initialized yet. | 217 // ResourceBundle is not initialized yet. |
| 203 // Fallback to logging with a default response or a Windows MessageBox. | 218 // Fallback to logging with a default response or a Windows MessageBox. |
| 204 #if defined(OS_WIN) | 219 #if defined(OS_WIN) |
| 205 if (!base::MessageLoopForUI::IsCurrent() || | 220 if (!base::MessageLoopForUI::IsCurrent() || |
| 206 !base::MessageLoopForUI::current()->is_running() || | 221 !base::MessageLoopForUI::current()->is_running() || |
| 207 !ResourceBundle::HasSharedInstance()) { | 222 !ResourceBundle::HasSharedInstance()) { |
| 223 LOG_IF(ERROR, !checkbox_text.empty()) << "Dialog checkbox won't be shown"; | |
| 208 int result = ui::MessageBox(views::HWNDForNativeWindow(parent), message, | 224 int result = ui::MessageBox(views::HWNDForNativeWindow(parent), message, |
| 209 title, GetMessageBoxFlagsFromType(type)); | 225 title, GetMessageBoxFlagsFromType(type)); |
| 210 return (result == IDYES || result == IDOK) ? | 226 return (result == IDYES || result == IDOK) ? |
| 211 MESSAGE_BOX_RESULT_YES : MESSAGE_BOX_RESULT_NO; | 227 MESSAGE_BOX_RESULT_YES : MESSAGE_BOX_RESULT_NO; |
| 212 } | 228 } |
| 213 #else | 229 #else |
| 214 if (!base::MessageLoopForUI::IsCurrent() || | 230 if (!base::MessageLoopForUI::IsCurrent() || |
| 215 !ResourceBundle::HasSharedInstance()) { | 231 !ResourceBundle::HasSharedInstance()) { |
| 216 LOG(ERROR) << "Unable to show a dialog outside the UI thread message loop: " | 232 LOG(ERROR) << "Unable to show a dialog outside the UI thread message loop: " |
| 217 << title << " - " << message; | 233 << title << " - " << message; |
| 218 return MESSAGE_BOX_RESULT_NO; | 234 return MESSAGE_BOX_RESULT_NO; |
| 219 } | 235 } |
| 220 #endif | 236 #endif |
| 221 | 237 |
| 222 SimpleMessageBoxViews* dialog = | 238 SimpleMessageBoxViews* dialog = new SimpleMessageBoxViews( |
| 223 new SimpleMessageBoxViews(title, | 239 title, message, type, yes_text, no_text, checkbox_text, |
| 224 message, | 240 parent == NULL // is_system_modal |
|
Lei Zhang
2016/07/16 00:35:21
nit: foo == NULL -> !foo ?
afakhry
2016/07/18 17:43:51
Done.
| |
| 225 type, | 241 ); |
| 226 yes_text, | |
| 227 no_text, | |
| 228 parent == NULL // is_system_modal | |
| 229 ); | |
| 230 constrained_window::CreateBrowserModalDialogViews(dialog, parent)->Show(); | 242 constrained_window::CreateBrowserModalDialogViews(dialog, parent)->Show(); |
| 231 | 243 |
| 232 // NOTE: |dialog| may have been deleted by the time |RunDialogAndGetResult()| | 244 // NOTE: |dialog| may have been deleted by the time |RunDialogAndGetResult()| |
| 233 // returns. | 245 // returns. |
| 234 return dialog->RunDialogAndGetResult(); | 246 return dialog->RunDialogAndGetResult(); |
| 235 } | 247 } |
| 236 | 248 |
| 237 } // namespace | 249 } // namespace |
| 238 | 250 |
| 239 void ShowWarningMessageBox(gfx::NativeWindow parent, | 251 void ShowWarningMessageBox(gfx::NativeWindow parent, |
| 240 const base::string16& title, | 252 const base::string16& title, |
| 241 const base::string16& message) { | 253 const base::string16& message) { |
| 242 ShowMessageBoxImpl(parent, title, message, MESSAGE_BOX_TYPE_WARNING, | 254 ShowMessageBoxImpl(parent, title, message, MESSAGE_BOX_TYPE_WARNING, |
| 243 base::string16(), base::string16()); | 255 base::string16(), base::string16(), base::string16()); |
| 256 } | |
| 257 | |
| 258 bool ShowWarningMessageBoxWithCheckbox(gfx::NativeWindow parent, | |
| 259 const base::string16& title, | |
| 260 const base::string16& message, | |
| 261 const base::string16& checkbox_text) { | |
| 262 return ShowMessageBoxImpl(parent, title, message, MESSAGE_BOX_TYPE_WARNING, | |
| 263 base::string16(), base::string16(), | |
| 264 checkbox_text) == MESSAGE_BOX_RESULT_YES; | |
| 244 } | 265 } |
| 245 | 266 |
| 246 MessageBoxResult ShowQuestionMessageBox(gfx::NativeWindow parent, | 267 MessageBoxResult ShowQuestionMessageBox(gfx::NativeWindow parent, |
| 247 const base::string16& title, | 268 const base::string16& title, |
| 248 const base::string16& message) { | 269 const base::string16& message) { |
| 249 return ShowMessageBoxImpl(parent, title, message, MESSAGE_BOX_TYPE_QUESTION, | 270 return ShowMessageBoxImpl(parent, title, message, MESSAGE_BOX_TYPE_QUESTION, |
| 250 base::string16(), base::string16()); | 271 base::string16(), base::string16(), |
| 272 base::string16()); | |
| 251 } | 273 } |
| 252 | 274 |
| 253 MessageBoxResult ShowMessageBoxWithButtonText(gfx::NativeWindow parent, | 275 MessageBoxResult ShowMessageBoxWithButtonText(gfx::NativeWindow parent, |
| 254 const base::string16& title, | 276 const base::string16& title, |
| 255 const base::string16& message, | 277 const base::string16& message, |
| 256 const base::string16& yes_text, | 278 const base::string16& yes_text, |
| 257 const base::string16& no_text) { | 279 const base::string16& no_text) { |
| 258 return ShowMessageBoxImpl( | 280 return ShowMessageBoxImpl(parent, title, message, MESSAGE_BOX_TYPE_QUESTION, |
| 259 parent, title, message, MESSAGE_BOX_TYPE_QUESTION, yes_text, no_text); | 281 yes_text, no_text, base::string16()); |
| 260 } | 282 } |
| 261 | 283 |
| 262 } // namespace chrome | 284 } // namespace chrome |
| OLD | NEW |