OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/app_modal_dialog.h" |
| 6 |
| 7 #include "chrome/browser/app_modal_dialog_queue.h" |
| 8 #include "chrome/browser/tab_contents/web_contents.h" |
| 9 #include "chrome/common/notification_service.h" |
| 10 #include "chrome/common/notification_type.h" |
| 11 #include "chrome/common/ipc_message.h" |
| 12 |
| 13 AppModalDialog::AppModalDialog(WebContents* web_contents, |
| 14 const std::wstring& title, |
| 15 int dialog_flags, |
| 16 const std::wstring& message_text, |
| 17 const std::wstring& default_prompt_text, |
| 18 bool display_suppress_checkbox, |
| 19 bool is_before_unload_dialog, |
| 20 IPC::Message* reply_msg) |
| 21 : web_contents_(web_contents), |
| 22 title_(title), |
| 23 dialog_flags_(dialog_flags), |
| 24 message_text_(message_text), |
| 25 default_prompt_text_(default_prompt_text), |
| 26 display_suppress_checkbox_(display_suppress_checkbox), |
| 27 is_before_unload_dialog_(is_before_unload_dialog), |
| 28 reply_msg_(reply_msg) { |
| 29 InitNotifications(); |
| 30 } |
| 31 |
| 32 void AppModalDialog::Observe(NotificationType type, |
| 33 const NotificationSource& source, |
| 34 const NotificationDetails& details) { |
| 35 if (!web_contents_) |
| 36 return; |
| 37 |
| 38 if (type == NotificationType::NAV_ENTRY_COMMITTED && |
| 39 Source<NavigationController>(source).ptr() == web_contents_->controller()) |
| 40 web_contents_ = NULL; |
| 41 |
| 42 if (type == NotificationType::TAB_CONTENTS_DESTROYED && |
| 43 Source<TabContents>(source).ptr() == |
| 44 static_cast<TabContents*>(web_contents_)) |
| 45 web_contents_ = NULL; |
| 46 |
| 47 if (!web_contents_) |
| 48 CloseModalDialog(); |
| 49 } |
| 50 |
| 51 void AppModalDialog::InitNotifications() { |
| 52 // Make sure we get navigation notifications so we know when our parent |
| 53 // contents will disappear or navigate to a different page. |
| 54 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, |
| 55 NotificationService::AllSources()); |
| 56 registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED, |
| 57 NotificationService::AllSources()); |
| 58 } |
| 59 |
| 60 void AppModalDialog::ShowModalDialog() { |
| 61 // If the WebContents that created this dialog navigated away before this |
| 62 // dialog became visible, simply show the next dialog if any. |
| 63 if (!web_contents_) { |
| 64 AppModalDialogQueue::ShowNextDialog(); |
| 65 delete this; |
| 66 return; |
| 67 } |
| 68 |
| 69 web_contents_->Activate(); |
| 70 CreateAndShowDialog(); |
| 71 } |
| 72 |
| 73 void AppModalDialog::OnCancel() { |
| 74 // We need to do this before WM_DESTROY (WindowClosing()) as any parent frame |
| 75 // will receive it's activation messages before this dialog receives |
| 76 // WM_DESTROY. The parent frame would then try to activate any modal dialogs |
| 77 // that were still open in the ModalDialogQueue, which would send activation |
| 78 // back to this one. The framework should be improved to handle this, so this |
| 79 // is a temporary workaround. |
| 80 AppModalDialogQueue::ShowNextDialog(); |
| 81 |
| 82 if (web_contents_) { |
| 83 web_contents_->OnJavaScriptMessageBoxClosed(reply_msg_, false, |
| 84 std::wstring()); |
| 85 } |
| 86 } |
| 87 |
| 88 void AppModalDialog::OnAccept(const std::wstring& prompt_text, |
| 89 bool suppress_js_messages) { |
| 90 AppModalDialogQueue::ShowNextDialog(); |
| 91 |
| 92 if (web_contents_) { |
| 93 web_contents_->OnJavaScriptMessageBoxClosed(reply_msg_, true, |
| 94 prompt_text); |
| 95 |
| 96 if (suppress_js_messages) |
| 97 web_contents()->set_suppress_javascript_messages(true); |
| 98 } |
| 99 } |
OLD | NEW |