| 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_; |
| 71 | 72 |
| 72 DISALLOW_COPY_AND_ASSIGN(SimpleMessageBoxViews); | 73 DISALLOW_COPY_AND_ASSIGN(SimpleMessageBoxViews); |
| 73 }; | 74 }; |
| 74 | 75 |
| 75 //////////////////////////////////////////////////////////////////////////////// | 76 //////////////////////////////////////////////////////////////////////////////// |
| 76 // SimpleMessageBoxViews, public: | 77 // SimpleMessageBoxViews, public: |
| 77 | 78 |
| 78 SimpleMessageBoxViews::SimpleMessageBoxViews(const base::string16& title, | 79 SimpleMessageBoxViews::SimpleMessageBoxViews( |
| 79 const base::string16& message, | 80 const base::string16& title, |
| 80 MessageBoxType type, | 81 const base::string16& message, |
| 81 const base::string16& yes_text, | 82 MessageBoxType type, |
| 82 const base::string16& no_text, | 83 const base::string16& yes_text, |
| 83 bool is_system_modal) | 84 const base::string16& no_text, |
| 85 const base::string16& checkbox_text, |
| 86 bool is_system_modal) |
| 84 : window_title_(title), | 87 : window_title_(title), |
| 85 type_(type), | 88 type_(type), |
| 86 yes_text_(yes_text), | 89 yes_text_(yes_text), |
| 87 no_text_(no_text), | 90 no_text_(no_text), |
| 88 result_(NULL), | 91 result_(NULL), |
| 89 is_system_modal_(is_system_modal), | |
| 90 message_box_view_(new views::MessageBoxView( | 92 message_box_view_(new views::MessageBoxView( |
| 91 views::MessageBoxView::InitParams(message))) { | 93 views::MessageBoxView::InitParams(message))), |
| 94 is_system_modal_(is_system_modal) { |
| 92 if (yes_text_.empty()) { | 95 if (yes_text_.empty()) { |
| 93 yes_text_ = | 96 yes_text_ = |
| 94 type_ == MESSAGE_BOX_TYPE_QUESTION | 97 type_ == MESSAGE_BOX_TYPE_QUESTION |
| 95 ? l10n_util::GetStringUTF16(IDS_CONFIRM_MESSAGEBOX_YES_BUTTON_LABEL) | 98 ? l10n_util::GetStringUTF16(IDS_CONFIRM_MESSAGEBOX_YES_BUTTON_LABEL) |
| 96 : l10n_util::GetStringUTF16(IDS_OK); | 99 : l10n_util::GetStringUTF16(IDS_OK); |
| 97 } | 100 } |
| 98 | 101 |
| 99 if (no_text_.empty() && type_ == MESSAGE_BOX_TYPE_QUESTION) | 102 if (no_text_.empty() && type_ == MESSAGE_BOX_TYPE_QUESTION) |
| 100 no_text_ = l10n_util::GetStringUTF16(IDS_CANCEL); | 103 no_text_ = l10n_util::GetStringUTF16(IDS_CANCEL); |
| 104 |
| 105 if (!checkbox_text.empty()) { |
| 106 message_box_view_->SetCheckBoxLabel(checkbox_text); |
| 107 message_box_view_->SetCheckBoxSelected(true); |
| 108 } |
| 101 } | 109 } |
| 102 | 110 |
| 103 SimpleMessageBoxViews::~SimpleMessageBoxViews() { | 111 SimpleMessageBoxViews::~SimpleMessageBoxViews() { |
| 104 } | 112 } |
| 105 | 113 |
| 106 MessageBoxResult SimpleMessageBoxViews::RunDialogAndGetResult() { | 114 MessageBoxResult SimpleMessageBoxViews::RunDialogAndGetResult() { |
| 107 MessageBoxResult result = MESSAGE_BOX_RESULT_NO; | 115 MessageBoxResult result = MESSAGE_BOX_RESULT_NO; |
| 108 result_ = &result; | 116 result_ = &result; |
| 109 // 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 |
| 110 // means than |Cancel| or |Accept|. crbug.com/404385 | 118 // means than |Cancel| or |Accept|. crbug.com/404385 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 130 return yes_text_; | 138 return yes_text_; |
| 131 } | 139 } |
| 132 | 140 |
| 133 bool SimpleMessageBoxViews::Cancel() { | 141 bool SimpleMessageBoxViews::Cancel() { |
| 134 *result_ = MESSAGE_BOX_RESULT_NO; | 142 *result_ = MESSAGE_BOX_RESULT_NO; |
| 135 Done(); | 143 Done(); |
| 136 return true; | 144 return true; |
| 137 } | 145 } |
| 138 | 146 |
| 139 bool SimpleMessageBoxViews::Accept() { | 147 bool SimpleMessageBoxViews::Accept() { |
| 140 *result_ = MESSAGE_BOX_RESULT_YES; | 148 if (!message_box_view_->HasCheckBox() || |
| 149 message_box_view_->IsCheckBoxSelected()) { |
| 150 *result_ = MESSAGE_BOX_RESULT_YES; |
| 151 } else { |
| 152 *result_ = MESSAGE_BOX_RESULT_NO; |
| 153 } |
| 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 = |
| 223 new SimpleMessageBoxViews(title, | 239 new SimpleMessageBoxViews(title, message, type, yes_text, no_text, |
| 224 message, | 240 checkbox_text, !parent /* is_system_modal */); |
| 225 type, | |
| 226 yes_text, | |
| 227 no_text, | |
| 228 parent == NULL // is_system_modal | |
| 229 ); | |
| 230 constrained_window::CreateBrowserModalDialogViews(dialog, parent)->Show(); | 241 constrained_window::CreateBrowserModalDialogViews(dialog, parent)->Show(); |
| 231 | 242 |
| 232 // NOTE: |dialog| may have been deleted by the time |RunDialogAndGetResult()| | 243 // NOTE: |dialog| may have been deleted by the time |RunDialogAndGetResult()| |
| 233 // returns. | 244 // returns. |
| 234 return dialog->RunDialogAndGetResult(); | 245 return dialog->RunDialogAndGetResult(); |
| 235 } | 246 } |
| 236 | 247 |
| 237 } // namespace | 248 } // namespace |
| 238 | 249 |
| 239 void ShowWarningMessageBox(gfx::NativeWindow parent, | 250 void ShowWarningMessageBox(gfx::NativeWindow parent, |
| 240 const base::string16& title, | 251 const base::string16& title, |
| 241 const base::string16& message) { | 252 const base::string16& message) { |
| 242 ShowMessageBoxImpl(parent, title, message, MESSAGE_BOX_TYPE_WARNING, | 253 ShowMessageBoxImpl(parent, title, message, MESSAGE_BOX_TYPE_WARNING, |
| 243 base::string16(), base::string16()); | 254 base::string16(), base::string16(), base::string16()); |
| 255 } |
| 256 |
| 257 bool ShowWarningMessageBoxWithCheckbox(gfx::NativeWindow parent, |
| 258 const base::string16& title, |
| 259 const base::string16& message, |
| 260 const base::string16& checkbox_text) { |
| 261 return ShowMessageBoxImpl(parent, title, message, MESSAGE_BOX_TYPE_WARNING, |
| 262 base::string16(), base::string16(), |
| 263 checkbox_text) == MESSAGE_BOX_RESULT_YES; |
| 244 } | 264 } |
| 245 | 265 |
| 246 MessageBoxResult ShowQuestionMessageBox(gfx::NativeWindow parent, | 266 MessageBoxResult ShowQuestionMessageBox(gfx::NativeWindow parent, |
| 247 const base::string16& title, | 267 const base::string16& title, |
| 248 const base::string16& message) { | 268 const base::string16& message) { |
| 249 return ShowMessageBoxImpl(parent, title, message, MESSAGE_BOX_TYPE_QUESTION, | 269 return ShowMessageBoxImpl(parent, title, message, MESSAGE_BOX_TYPE_QUESTION, |
| 250 base::string16(), base::string16()); | 270 base::string16(), base::string16(), |
| 271 base::string16()); |
| 251 } | 272 } |
| 252 | 273 |
| 253 MessageBoxResult ShowMessageBoxWithButtonText(gfx::NativeWindow parent, | 274 MessageBoxResult ShowMessageBoxWithButtonText(gfx::NativeWindow parent, |
| 254 const base::string16& title, | 275 const base::string16& title, |
| 255 const base::string16& message, | 276 const base::string16& message, |
| 256 const base::string16& yes_text, | 277 const base::string16& yes_text, |
| 257 const base::string16& no_text) { | 278 const base::string16& no_text) { |
| 258 return ShowMessageBoxImpl( | 279 return ShowMessageBoxImpl(parent, title, message, MESSAGE_BOX_TYPE_QUESTION, |
| 259 parent, title, message, MESSAGE_BOX_TYPE_QUESTION, yes_text, no_text); | 280 yes_text, no_text, base::string16()); |
| 260 } | 281 } |
| 261 | 282 |
| 262 } // namespace chrome | 283 } // namespace chrome |
| OLD | NEW |