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/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/message_loop/message_pump_dispatcher.h" | 11 #include "base/message_loop/message_pump_dispatcher.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/ui/views/constrained_window_views.h" | 14 #include "chrome/browser/ui/views/constrained_window_views.h" |
15 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
| 16 #include "ui/aura/client/dispatcher_client.h" |
| 17 #include "ui/aura/env.h" |
| 18 #include "ui/aura/root_window.h" |
16 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
17 #include "ui/gfx/native_widget_types.h" | 20 #include "ui/gfx/native_widget_types.h" |
18 #include "ui/views/controls/message_box_view.h" | 21 #include "ui/views/controls/message_box_view.h" |
19 #include "ui/views/widget/widget.h" | 22 #include "ui/views/widget/widget.h" |
20 #include "ui/views/window/dialog_delegate.h" | 23 #include "ui/views/window/dialog_delegate.h" |
21 | 24 |
22 #if defined(USE_AURA) | |
23 #include "ui/aura/client/dispatcher_client.h" | |
24 #include "ui/aura/env.h" | |
25 #include "ui/aura/root_window.h" | |
26 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
27 #include "chrome/browser/ui/views/simple_message_box_win.h" | 26 #include "chrome/browser/ui/views/simple_message_box_win.h" |
28 #endif | 27 #endif |
29 #endif | |
30 | 28 |
31 namespace chrome { | 29 namespace chrome { |
32 | 30 |
33 namespace { | 31 namespace { |
34 | 32 |
35 // Multiple SimpleMessageBoxViews can show up at the same time. Each of these | 33 // Multiple SimpleMessageBoxViews can show up at the same time. Each of these |
36 // start a nested message-loop. However, these SimpleMessageBoxViews can be | 34 // start a nested message-loop. However, these SimpleMessageBoxViews can be |
37 // deleted in any order. This creates problems if a box in an inner-loop gets | 35 // deleted in any order. This creates problems if a box in an inner-loop gets |
38 // destroyed before a box in an outer-loop. So to avoid this, ref-counting is | 36 // destroyed before a box in an outer-loop. So to avoid this, ref-counting is |
39 // used so that the SimpleMessageBoxViews gets deleted at the right time. | 37 // used so that the SimpleMessageBoxViews gets deleted at the right time. |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 | 166 |
169 views::Widget* SimpleMessageBoxViews::GetWidget() { | 167 views::Widget* SimpleMessageBoxViews::GetWidget() { |
170 return message_box_view_->GetWidget(); | 168 return message_box_view_->GetWidget(); |
171 } | 169 } |
172 | 170 |
173 const views::Widget* SimpleMessageBoxViews::GetWidget() const { | 171 const views::Widget* SimpleMessageBoxViews::GetWidget() const { |
174 return message_box_view_->GetWidget(); | 172 return message_box_view_->GetWidget(); |
175 } | 173 } |
176 | 174 |
177 bool SimpleMessageBoxViews::Dispatch(const base::NativeEvent& event) { | 175 bool SimpleMessageBoxViews::Dispatch(const base::NativeEvent& event) { |
178 #if defined(OS_WIN) | |
179 TranslateMessage(&event); | |
180 DispatchMessage(&event); | |
181 #elif defined(USE_AURA) | |
182 aura::Env::GetInstance()->GetDispatcher()->Dispatch(event); | 176 aura::Env::GetInstance()->GetDispatcher()->Dispatch(event); |
183 #endif | |
184 return should_show_dialog_; | 177 return should_show_dialog_; |
185 } | 178 } |
186 | 179 |
187 //////////////////////////////////////////////////////////////////////////////// | 180 //////////////////////////////////////////////////////////////////////////////// |
188 // SimpleMessageBoxViews, private: | 181 // SimpleMessageBoxViews, private: |
189 | 182 |
190 SimpleMessageBoxViews::~SimpleMessageBoxViews() { | 183 SimpleMessageBoxViews::~SimpleMessageBoxViews() { |
191 } | 184 } |
192 | 185 |
193 MessageBoxResult ShowMessageBoxImpl(gfx::NativeWindow parent, | 186 MessageBoxResult ShowMessageBoxImpl(gfx::NativeWindow parent, |
194 const base::string16& title, | 187 const base::string16& title, |
195 const base::string16& message, | 188 const base::string16& message, |
196 MessageBoxType type, | 189 MessageBoxType type, |
197 const base::string16& yes_text, | 190 const base::string16& yes_text, |
198 const base::string16& no_text) { | 191 const base::string16& no_text) { |
199 | 192 |
200 #if defined(OS_WIN) | 193 #if defined(OS_WIN) |
201 // If we're very early, we can't show a GPU-based dialog, so fallback to | 194 // If we're very early, we can't show a GPU-based dialog, so fallback to |
202 // plain Windows MessageBox. | 195 // plain Windows MessageBox. |
203 if (!ui::ContextFactory::GetInstance()) | 196 if (!ui::ContextFactory::GetInstance()) |
204 return NativeShowMessageBox(NULL, title, message, type); | 197 return NativeShowMessageBox(NULL, title, message, type); |
205 #endif | 198 #endif |
206 | 199 |
207 scoped_refptr<SimpleMessageBoxViews> dialog( | 200 scoped_refptr<SimpleMessageBoxViews> dialog( |
208 new SimpleMessageBoxViews(title, message, type, yes_text, no_text)); | 201 new SimpleMessageBoxViews(title, message, type, yes_text, no_text)); |
209 CreateBrowserModalDialogViews(dialog.get(), parent)->Show(); | 202 CreateBrowserModalDialogViews(dialog.get(), parent)->Show(); |
210 | 203 |
211 #if defined(USE_AURA) | |
212 aura::Window* anchor = parent; | 204 aura::Window* anchor = parent; |
213 aura::client::DispatcherClient* client = anchor ? | 205 aura::client::DispatcherClient* client = anchor ? |
214 aura::client::GetDispatcherClient(anchor->GetRootWindow()) : NULL; | 206 aura::client::GetDispatcherClient(anchor->GetRootWindow()) : NULL; |
215 if (!client) { | 207 if (!client) { |
216 // Use the widget's window itself so that the message loop | 208 // Use the widget's window itself so that the message loop |
217 // exists when the dialog is closed by some other means than | 209 // exists when the dialog is closed by some other means than |
218 // |Cancel| or |Accept|. | 210 // |Cancel| or |Accept|. |
219 anchor = dialog->GetWidget()->GetNativeWindow(); | 211 anchor = dialog->GetWidget()->GetNativeWindow(); |
220 client = aura::client::GetDispatcherClient(anchor->GetRootWindow()); | 212 client = aura::client::GetDispatcherClient(anchor->GetRootWindow()); |
221 } | 213 } |
222 client->RunWithDispatcher(dialog.get(), anchor, true); | 214 client->RunWithDispatcher(dialog.get(), anchor, true); |
223 #else | |
224 { | |
225 base::MessageLoop::ScopedNestableTaskAllower allow( | |
226 base::MessageLoopForUI::current()); | |
227 base::RunLoop run_loop(dialog); | |
228 run_loop.Run(); | |
229 } | |
230 #endif | |
231 return dialog->result(); | 215 return dialog->result(); |
232 } | 216 } |
233 | 217 |
234 } // namespace | 218 } // namespace |
235 | 219 |
236 MessageBoxResult ShowMessageBox(gfx::NativeWindow parent, | 220 MessageBoxResult ShowMessageBox(gfx::NativeWindow parent, |
237 const base::string16& title, | 221 const base::string16& title, |
238 const base::string16& message, | 222 const base::string16& message, |
239 MessageBoxType type) { | 223 MessageBoxType type) { |
240 return ShowMessageBoxImpl( | 224 return ShowMessageBoxImpl( |
241 parent, title, message, type, base::string16(), base::string16()); | 225 parent, title, message, type, base::string16(), base::string16()); |
242 } | 226 } |
243 | 227 |
244 #if defined(USE_AURA) | |
245 MessageBoxResult ShowMessageBoxWithButtonText(gfx::NativeWindow parent, | 228 MessageBoxResult ShowMessageBoxWithButtonText(gfx::NativeWindow parent, |
246 const base::string16& title, | 229 const base::string16& title, |
247 const base::string16& message, | 230 const base::string16& message, |
248 const base::string16& yes_text, | 231 const base::string16& yes_text, |
249 const base::string16& no_text) { | 232 const base::string16& no_text) { |
250 return ShowMessageBoxImpl( | 233 return ShowMessageBoxImpl( |
251 parent, title, message, MESSAGE_BOX_TYPE_QUESTION, yes_text, no_text); | 234 parent, title, message, MESSAGE_BOX_TYPE_QUESTION, yes_text, no_text); |
252 } | 235 } |
253 #endif | |
254 | 236 |
255 } // namespace chrome | 237 } // namespace chrome |
OLD | NEW |