Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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/app_modal_dialog.h" | 5 #include "chrome/browser/app_modal_dialog.h" |
| 6 | 6 |
| 7 #include "chrome/browser/app_modal_dialog_queue.h" | 7 #include "chrome/browser/app_modal_dialog_queue.h" |
| 8 #include "chrome/browser/tab_contents/tab_contents.h" | 8 #include "chrome/browser/tab_contents/tab_contents.h" |
| 9 #include "chrome/common/notification_service.h" | 9 #include "chrome/common/notification_service.h" |
| 10 #include "chrome/common/notification_type.h" | 10 #include "chrome/common/notification_type.h" |
| 11 #include "ipc/ipc_message.h" | 11 #include "ipc/ipc_message.h" |
| 12 | 12 |
| 13 AppModalDialog::AppModalDialog(TabContents* tab_contents, | 13 AppModalDialog::AppModalDialog(JavaScriptMessageBoxClient* client, |
| 14 const std::wstring& title, | 14 const std::wstring& title, |
| 15 int dialog_flags, | 15 int dialog_flags, |
| 16 const std::wstring& message_text, | 16 const std::wstring& message_text, |
| 17 const std::wstring& default_prompt_text, | 17 const std::wstring& default_prompt_text, |
| 18 bool display_suppress_checkbox, | 18 bool display_suppress_checkbox, |
| 19 bool is_before_unload_dialog, | 19 bool is_before_unload_dialog, |
| 20 IPC::Message* reply_msg) | 20 IPC::Message* reply_msg) |
| 21 : dialog_(NULL), | 21 : dialog_(NULL), |
| 22 tab_contents_(tab_contents), | 22 client_(client), |
| 23 skip_this_dialog_(false), | |
| 23 title_(title), | 24 title_(title), |
| 24 dialog_flags_(dialog_flags), | 25 dialog_flags_(dialog_flags), |
| 25 message_text_(message_text), | 26 message_text_(message_text), |
| 26 default_prompt_text_(default_prompt_text), | 27 default_prompt_text_(default_prompt_text), |
| 27 display_suppress_checkbox_(display_suppress_checkbox), | 28 display_suppress_checkbox_(display_suppress_checkbox), |
| 28 is_before_unload_dialog_(is_before_unload_dialog), | 29 is_before_unload_dialog_(is_before_unload_dialog), |
| 29 reply_msg_(reply_msg) { | 30 reply_msg_(reply_msg) { |
| 30 InitNotifications(); | 31 InitNotifications(); |
| 31 } | 32 } |
| 32 | 33 |
| 33 void AppModalDialog::Observe(NotificationType type, | 34 void AppModalDialog::Observe(NotificationType type, |
| 34 const NotificationSource& source, | 35 const NotificationSource& source, |
| 35 const NotificationDetails& details) { | 36 const NotificationDetails& details) { |
| 36 if (!tab_contents_) | 37 const TabContents* tab_contents = client_->AsTabContents(); |
| 38 if (!tab_contents) | |
| 37 return; | 39 return; |
| 38 | 40 |
| 39 if (type == NotificationType::NAV_ENTRY_COMMITTED && | 41 if (type == NotificationType::NAV_ENTRY_COMMITTED && |
| 40 Source<NavigationController>(source).ptr() == | 42 Source<NavigationController>(source).ptr() == |
| 41 &tab_contents_->controller()) | 43 &tab_contents->controller()) |
| 42 tab_contents_ = NULL; | 44 skip_this_dialog_ = true; |
| 43 | 45 |
| 44 if (type == NotificationType::TAB_CONTENTS_DESTROYED && | 46 if (type == NotificationType::TAB_CONTENTS_DESTROYED && |
| 45 Source<TabContents>(source).ptr() == | 47 Source<TabContents>(source).ptr() == tab_contents) |
| 46 static_cast<TabContents*>(tab_contents_)) | 48 skip_this_dialog_ = true; |
| 47 tab_contents_ = NULL; | |
| 48 | 49 |
| 49 if (!tab_contents_) | 50 if (skip_this_dialog_) |
| 50 CloseModalDialog(); | 51 CloseModalDialog(); |
| 51 } | 52 } |
| 52 | 53 |
| 53 void AppModalDialog::SendCloseNotification() { | 54 void AppModalDialog::SendCloseNotification() { |
| 54 NotificationService::current()->Notify( | 55 NotificationService::current()->Notify( |
| 55 NotificationType::APP_MODAL_DIALOG_CLOSED, | 56 NotificationType::APP_MODAL_DIALOG_CLOSED, |
| 56 Source<AppModalDialog>(this), | 57 Source<AppModalDialog>(this), |
| 57 NotificationService::NoDetails()); | 58 NotificationService::NoDetails()); |
| 58 } | 59 } |
| 59 | 60 |
| 60 void AppModalDialog::InitNotifications() { | 61 void AppModalDialog::InitNotifications() { |
| 61 // Make sure we get navigation notifications so we know when our parent | 62 // Make sure we get navigation notifications so we know when our parent |
| 62 // contents will disappear or navigate to a different page. | 63 // contents will disappear or navigate to a different page. |
| 63 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | 64 if (client_->AsTabContents()) { |
| 64 NotificationService::AllSources()); | 65 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, |
| 65 registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED, | 66 NotificationService::AllSources()); |
| 66 NotificationService::AllSources()); | 67 registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED, |
| 68 NotificationService::AllSources()); | |
|
Matt Perry
2009/11/05 23:42:21
If you instead use Source<Tabcontents>(client_->As
| |
| 69 } | |
| 67 } | 70 } |
| 68 | 71 |
| 69 void AppModalDialog::ShowModalDialog() { | 72 void AppModalDialog::ShowModalDialog() { |
| 70 // If the TabContents that created this dialog navigated away before this | 73 // If the TabContents that created this dialog navigated away before this |
| 71 // dialog became visible, simply show the next dialog if any. | 74 // dialog became visible, simply show the next dialog if any. |
| 72 if (!tab_contents_) { | 75 if (skip_this_dialog_) { |
| 73 Singleton<AppModalDialogQueue>()->ShowNextDialog(); | 76 Singleton<AppModalDialogQueue>()->ShowNextDialog(); |
| 74 delete this; | 77 delete this; |
| 75 return; | 78 return; |
| 76 } | 79 } |
| 80 TabContents* tab_contents = client_->AsTabContents(); | |
| 81 if (tab_contents) | |
| 82 tab_contents->Activate(); | |
| 77 | 83 |
| 78 tab_contents_->Activate(); | |
| 79 CreateAndShowDialog(); | 84 CreateAndShowDialog(); |
| 80 | 85 |
| 81 NotificationService::current()->Notify( | 86 NotificationService::current()->Notify( |
| 82 NotificationType::APP_MODAL_DIALOG_SHOWN, | 87 NotificationType::APP_MODAL_DIALOG_SHOWN, |
| 83 Source<AppModalDialog>(this), | 88 Source<AppModalDialog>(this), |
| 84 NotificationService::NoDetails()); | 89 NotificationService::NoDetails()); |
| 85 } | 90 } |
| 86 | 91 |
| 87 void AppModalDialog::OnCancel() { | 92 void AppModalDialog::OnCancel() { |
| 88 // We need to do this before WM_DESTROY (WindowClosing()) as any parent frame | 93 // We need to do this before WM_DESTROY (WindowClosing()) as any parent frame |
| 89 // will receive it's activation messages before this dialog receives | 94 // will receive its activation messages before this dialog receives |
| 90 // WM_DESTROY. The parent frame would then try to activate any modal dialogs | 95 // WM_DESTROY. The parent frame would then try to activate any modal dialogs |
| 91 // that were still open in the ModalDialogQueue, which would send activation | 96 // that were still open in the ModalDialogQueue, which would send activation |
| 92 // back to this one. The framework should be improved to handle this, so this | 97 // back to this one. The framework should be improved to handle this, so this |
| 93 // is a temporary workaround. | 98 // is a temporary workaround. |
| 94 Singleton<AppModalDialogQueue>()->ShowNextDialog(); | 99 Singleton<AppModalDialogQueue>()->ShowNextDialog(); |
| 95 | 100 |
| 96 if (tab_contents_) { | 101 if (!skip_this_dialog_) { |
| 97 tab_contents_->OnJavaScriptMessageBoxClosed(reply_msg_, false, | 102 client_->OnMessageBoxClosed(reply_msg_, false, std::wstring()); |
| 98 std::wstring()); | |
| 99 } | 103 } |
| 100 | 104 |
| 101 SendCloseNotification(); | 105 SendCloseNotification(); |
| 102 } | 106 } |
| 103 | 107 |
| 104 void AppModalDialog::OnAccept(const std::wstring& prompt_text, | 108 void AppModalDialog::OnAccept(const std::wstring& prompt_text, |
| 105 bool suppress_js_messages) { | 109 bool suppress_js_messages) { |
| 106 Singleton<AppModalDialogQueue>()->ShowNextDialog(); | 110 Singleton<AppModalDialogQueue>()->ShowNextDialog(); |
| 107 | 111 |
| 108 if (tab_contents_) { | 112 if (!skip_this_dialog_) { |
| 109 tab_contents_->OnJavaScriptMessageBoxClosed(reply_msg_, true, | 113 client_->OnMessageBoxClosed(reply_msg_, true, prompt_text); |
| 110 prompt_text); | |
| 111 | |
| 112 if (suppress_js_messages) | 114 if (suppress_js_messages) |
| 113 tab_contents()->set_suppress_javascript_messages(true); | 115 client_->SetSuppressMessageBoxes(true); |
| 114 } | 116 } |
| 115 | 117 |
| 116 SendCloseNotification(); | 118 SendCloseNotification(); |
| 117 } | 119 } |
| 118 | 120 |
| 119 void AppModalDialog::OnClose() { | 121 void AppModalDialog::OnClose() { |
| 120 SendCloseNotification(); | 122 SendCloseNotification(); |
| 121 } | 123 } |
| OLD | NEW |