Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1262)

Side by Side Diff: chrome/browser/ui/views/simple_message_box_views.cc

Issue 280483003: wm: Change the DispatcherClient interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix-tests Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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::DispatcherClient* client =
120 aura::client::GetDispatcherClient(anchor->GetRootWindow());
121 client->RunWithDispatcher(NULL, &quit_runloop_);
122 return result;
123 }
124
113 int SimpleMessageBoxViews::GetDialogButtons() const { 125 int SimpleMessageBoxViews::GetDialogButtons() const {
114 if (type_ == MESSAGE_BOX_TYPE_QUESTION || 126 if (type_ == MESSAGE_BOX_TYPE_QUESTION ||
115 type_ == MESSAGE_BOX_TYPE_OK_CANCEL) { 127 type_ == MESSAGE_BOX_TYPE_OK_CANCEL) {
116 return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL; 128 return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL;
117 } 129 }
118 130
119 return ui::DIALOG_BUTTON_OK; 131 return ui::DIALOG_BUTTON_OK;
120 } 132 }
121 133
122 base::string16 SimpleMessageBoxViews::GetDialogButtonLabel( 134 base::string16 SimpleMessageBoxViews::GetDialogButtonLabel(
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 } 171 }
160 172
161 const views::Widget* SimpleMessageBoxViews::GetWidget() const { 173 const views::Widget* SimpleMessageBoxViews::GetWidget() const {
162 return message_box_view_->GetWidget(); 174 return message_box_view_->GetWidget();
163 } 175 }
164 176
165 //////////////////////////////////////////////////////////////////////////////// 177 ////////////////////////////////////////////////////////////////////////////////
166 // SimpleMessageBoxViews, private: 178 // SimpleMessageBoxViews, private:
167 179
168 void SimpleMessageBoxViews::Done() { 180 void SimpleMessageBoxViews::Done() {
169 aura::Window* window = GetWidget()->GetNativeView(); 181 CHECK(!quit_runloop_.is_null());
170 aura::client::DispatcherClient* client = 182 quit_runloop_.Run();
171 aura::client::GetDispatcherClient(window->GetRootWindow());
172 client->QuitNestedMessageLoop();
173 } 183 }
174 184
175 #if defined(OS_WIN) 185 #if defined(OS_WIN)
176 UINT GetMessageBoxFlagsFromType(MessageBoxType type) { 186 UINT GetMessageBoxFlagsFromType(MessageBoxType type) {
177 UINT flags = MB_SETFOREGROUND; 187 UINT flags = MB_SETFOREGROUND;
178 switch (type) { 188 switch (type) {
179 case MESSAGE_BOX_TYPE_INFORMATION: 189 case MESSAGE_BOX_TYPE_INFORMATION:
180 return flags | MB_OK | MB_ICONINFORMATION; 190 return flags | MB_OK | MB_ICONINFORMATION;
181 case MESSAGE_BOX_TYPE_WARNING: 191 case MESSAGE_BOX_TYPE_WARNING:
182 return flags | MB_OK | MB_ICONWARNING; 192 return flags | MB_OK | MB_ICONWARNING;
(...skipping 27 matching lines...) Expand all
210 } 220 }
211 #else 221 #else
212 if (!base::MessageLoopForUI::IsCurrent() || 222 if (!base::MessageLoopForUI::IsCurrent() ||
213 !ResourceBundle::HasSharedInstance()) { 223 !ResourceBundle::HasSharedInstance()) {
214 LOG(ERROR) << "Unable to show a dialog outside the UI thread message loop: " 224 LOG(ERROR) << "Unable to show a dialog outside the UI thread message loop: "
215 << title << " - " << message; 225 << title << " - " << message;
216 return MESSAGE_BOX_RESULT_NO; 226 return MESSAGE_BOX_RESULT_NO;
217 } 227 }
218 #endif 228 #endif
219 229
220 MessageBoxResult result = MESSAGE_BOX_RESULT_NO;
221 SimpleMessageBoxViews* dialog = new SimpleMessageBoxViews( 230 SimpleMessageBoxViews* dialog = new SimpleMessageBoxViews(
222 title, 231 title,
223 message, 232 message,
224 type, 233 type,
225 yes_text, 234 yes_text,
226 no_text, 235 no_text,
227 parent == NULL, // is_system_modal 236 parent == NULL // is_system_modal
228 &result); 237 );
229 CreateBrowserModalDialogViews(dialog, parent)->Show(); 238 CreateBrowserModalDialogViews(dialog, parent)->Show();
230 239
231 // Use the widget's window itself so that the message loop exists when the 240 // NOTE: |dialog| may have been deleted by the time |RunDialogAndGetResult()|
232 // dialog is closed by some other means than |Cancel| or |Accept|. 241 // returns.
233 aura::Window* anchor = dialog->GetWidget()->GetNativeWindow(); 242 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 } 243 }
241 244
242 } // namespace 245 } // namespace
243 246
244 MessageBoxResult ShowMessageBox(gfx::NativeWindow parent, 247 MessageBoxResult ShowMessageBox(gfx::NativeWindow parent,
245 const base::string16& title, 248 const base::string16& title,
246 const base::string16& message, 249 const base::string16& message,
247 MessageBoxType type) { 250 MessageBoxType type) {
248 return ShowMessageBoxImpl( 251 return ShowMessageBoxImpl(
249 parent, title, message, type, base::string16(), base::string16()); 252 parent, title, message, type, base::string16(), base::string16());
250 } 253 }
251 254
252 MessageBoxResult ShowMessageBoxWithButtonText(gfx::NativeWindow parent, 255 MessageBoxResult ShowMessageBoxWithButtonText(gfx::NativeWindow parent,
253 const base::string16& title, 256 const base::string16& title,
254 const base::string16& message, 257 const base::string16& message,
255 const base::string16& yes_text, 258 const base::string16& yes_text,
256 const base::string16& no_text) { 259 const base::string16& no_text) {
257 return ShowMessageBoxImpl( 260 return ShowMessageBoxImpl(
258 parent, title, message, MESSAGE_BOX_TYPE_QUESTION, yes_text, no_text); 261 parent, title, message, MESSAGE_BOX_TYPE_QUESTION, yes_text, no_text);
259 } 262 }
260 263
261 } // namespace chrome 264 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698