OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/app_modal_dialogs/js_modal_dialog.h" | 5 #include "chrome/browser/ui/app_modal_dialogs/js_modal_dialog.h" |
6 | 6 |
7 #include "base/string_util.h" | |
8 #include "base/utf_string_conversions.h" | |
9 #include "chrome/browser/browser_shutdown.h" | 7 #include "chrome/browser/browser_shutdown.h" |
10 #include "chrome/browser/extensions/extension_host.h" | |
11 #include "chrome/browser/ui/app_modal_dialogs/native_app_modal_dialog.h" | 8 #include "chrome/browser/ui/app_modal_dialogs/native_app_modal_dialog.h" |
12 #include "content/browser/tab_contents/tab_contents.h" | |
13 #include "content/common/notification_service.h" | |
14 #include "content/common/notification_type.h" | |
15 #include "ui/base/text/text_elider.h" | 9 #include "ui/base/text/text_elider.h" |
16 | 10 |
17 namespace { | 11 namespace { |
18 | 12 |
19 // Control maximum sizes of various texts passed to us from javascript. | 13 // Control maximum sizes of various texts passed to us from javascript. |
20 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 14 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
21 // Two-dimensional eliding. Reformat the text of the message dialog | 15 // Two-dimensional eliding. Reformat the text of the message dialog |
22 // inserting line breaks because otherwise a single long line can overflow | 16 // inserting line breaks because otherwise a single long line can overflow |
23 // the message dialog (and crash/hang the GTK, depending on the version). | 17 // the message dialog (and crash/hang the GTK, depending on the version). |
24 const int kMessageTextMaxRows = 32; | 18 const int kMessageTextMaxRows = 32; |
(...skipping 30 matching lines...) Expand all Loading... |
55 JavaScriptAppModalDialog::JavaScriptAppModalDialog( | 49 JavaScriptAppModalDialog::JavaScriptAppModalDialog( |
56 content::JavaScriptDialogDelegate* delegate, | 50 content::JavaScriptDialogDelegate* delegate, |
57 ChromeJavaScriptDialogExtraData* extra_data, | 51 ChromeJavaScriptDialogExtraData* extra_data, |
58 const string16& title, | 52 const string16& title, |
59 int dialog_flags, | 53 int dialog_flags, |
60 const string16& message_text, | 54 const string16& message_text, |
61 const string16& default_prompt_text, | 55 const string16& default_prompt_text, |
62 bool display_suppress_checkbox, | 56 bool display_suppress_checkbox, |
63 bool is_before_unload_dialog, | 57 bool is_before_unload_dialog, |
64 IPC::Message* reply_msg) | 58 IPC::Message* reply_msg) |
65 : AppModalDialog(delegate->AsTabContents(), title), | 59 : AppModalDialog(delegate, title), |
66 delegate_(delegate), | 60 delegate_(delegate), |
67 extra_data_(extra_data), | 61 extra_data_(extra_data), |
68 extension_host_(delegate->AsExtensionHost()), | |
69 dialog_flags_(dialog_flags), | 62 dialog_flags_(dialog_flags), |
70 display_suppress_checkbox_(display_suppress_checkbox), | 63 display_suppress_checkbox_(display_suppress_checkbox), |
71 is_before_unload_dialog_(is_before_unload_dialog), | 64 is_before_unload_dialog_(is_before_unload_dialog), |
72 reply_msg_(reply_msg), | 65 reply_msg_(reply_msg), |
73 use_override_prompt_text_(false) { | 66 use_override_prompt_text_(false) { |
74 EnforceMaxTextSize(message_text, &message_text_); | 67 EnforceMaxTextSize(message_text, &message_text_); |
75 EnforceMaxPromptSize(default_prompt_text, &default_prompt_text_); | 68 EnforceMaxPromptSize(default_prompt_text, &default_prompt_text_); |
76 | |
77 DCHECK((tab_contents_ != NULL) != (extension_host_ != NULL)); | |
78 InitNotifications(); | |
79 } | 69 } |
80 | 70 |
81 JavaScriptAppModalDialog::~JavaScriptAppModalDialog() { | 71 JavaScriptAppModalDialog::~JavaScriptAppModalDialog() { |
82 } | 72 } |
83 | 73 |
84 NativeAppModalDialog* JavaScriptAppModalDialog::CreateNativeDialog() { | 74 NativeAppModalDialog* JavaScriptAppModalDialog::CreateNativeDialog() { |
85 gfx::NativeWindow parent_window = delegate_->GetDialogRootWindow(); | 75 gfx::NativeWindow parent_window = delegate_->GetDialogRootWindow(); |
86 return NativeAppModalDialog::CreateNativeJavaScriptPrompt(this, | 76 return NativeAppModalDialog::CreateNativeJavaScriptPrompt(this, |
87 parent_window); | 77 parent_window); |
88 } | 78 } |
89 | 79 |
90 bool JavaScriptAppModalDialog::IsJavaScriptModalDialog() { | 80 bool JavaScriptAppModalDialog::IsJavaScriptModalDialog() { |
91 return true; | 81 return true; |
92 } | 82 } |
93 | 83 |
94 void JavaScriptAppModalDialog::Observe(NotificationType type, | 84 void JavaScriptAppModalDialog::Invalidate() { |
95 const NotificationSource& source, | 85 if (!valid_) |
96 const NotificationDetails& details) { | |
97 if (skip_this_dialog_) | |
98 return; | 86 return; |
99 | 87 |
100 if (NotificationType::EXTENSION_HOST_DESTROYED == type && | 88 valid_ = false; |
101 Details<ExtensionHost>(extension_host_) != details) | |
102 return; | |
103 | |
104 // If we reach here, we know the notification is relevant to us, either | |
105 // because we're only observing applicable sources or because we passed the | |
106 // check above. Both of those indicate that we should ignore this dialog. | |
107 // Also clear the delegate, since it's now invalid. | |
108 skip_this_dialog_ = true; | |
109 delegate_ = NULL; | 89 delegate_ = NULL; |
110 if (native_dialog_) | 90 if (native_dialog_) |
111 CloseModalDialog(); | 91 CloseModalDialog(); |
112 } | 92 } |
113 | 93 |
114 void JavaScriptAppModalDialog::InitNotifications() { | 94 content::JavaScriptDialogDelegate* JavaScriptAppModalDialog::delegate() const { |
115 // Make sure we get relevant navigation notifications so we know when our | 95 return static_cast<content::JavaScriptDialogDelegate*>(delegate_); |
116 // parent contents will disappear or navigate to a different page. | |
117 if (tab_contents_) { | |
118 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | |
119 Source<NavigationController>(&tab_contents_->controller())); | |
120 registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED, | |
121 Source<TabContents>(tab_contents_)); | |
122 } else if (extension_host_) { | |
123 // EXTENSION_HOST_DESTROYED uses the Profile as its source, but we care | |
124 // about the ExtensionHost (which is passed in the details). | |
125 registrar_.Add(this, NotificationType::EXTENSION_HOST_DESTROYED, | |
126 NotificationService::AllSources()); | |
127 } else { | |
128 NOTREACHED(); | |
129 } | |
130 } | 96 } |
131 | 97 |
132 void JavaScriptAppModalDialog::OnCancel(bool suppress_js_messages) { | 98 void JavaScriptAppModalDialog::OnCancel(bool suppress_js_messages) { |
133 // If we are shutting down and this is an onbeforeunload dialog, cancel the | 99 // If we are shutting down and this is an onbeforeunload dialog, cancel the |
134 // shutdown. | 100 // shutdown. |
135 if (is_before_unload_dialog_) | 101 if (is_before_unload_dialog_) |
136 browser_shutdown::SetTryingToQuit(false); | 102 browser_shutdown::SetTryingToQuit(false); |
137 | 103 |
138 // We need to do this before WM_DESTROY (WindowClosing()) as any parent frame | 104 // We need to do this before WM_DESTROY (WindowClosing()) as any parent frame |
139 // will receive its activation messages before this dialog receives | 105 // will receive its activation messages before this dialog receives |
(...skipping 23 matching lines...) Expand all Loading... |
163 | 129 |
164 void JavaScriptAppModalDialog::SetOverridePromptText( | 130 void JavaScriptAppModalDialog::SetOverridePromptText( |
165 const string16& override_prompt_text) { | 131 const string16& override_prompt_text) { |
166 override_prompt_text_ = override_prompt_text; | 132 override_prompt_text_ = override_prompt_text; |
167 use_override_prompt_text_ = true; | 133 use_override_prompt_text_ = true; |
168 } | 134 } |
169 | 135 |
170 void JavaScriptAppModalDialog::NotifyDelegate(bool success, | 136 void JavaScriptAppModalDialog::NotifyDelegate(bool success, |
171 const string16& user_input, | 137 const string16& user_input, |
172 bool suppress_js_messages) { | 138 bool suppress_js_messages) { |
173 if (skip_this_dialog_) | 139 if (!valid_) |
174 return; | 140 return; |
175 | 141 |
176 delegate_->OnDialogClosed(reply_msg_, success, user_input); | 142 delegate()->OnDialogClosed(reply_msg_, success, user_input); |
177 | 143 |
178 extra_data_->last_javascript_message_dismissal_ = base::TimeTicks::Now(); | 144 extra_data_->last_javascript_message_dismissal_ = base::TimeTicks::Now(); |
179 extra_data_->suppress_javascript_messages_ = suppress_js_messages; | 145 extra_data_->suppress_javascript_messages_ = suppress_js_messages; |
180 | 146 |
181 // On Views, we can end up coming through this code path twice :(. | 147 // On Views, we can end up coming through this code path twice :(. |
182 // See crbug.com/63732. | 148 // See crbug.com/63732. |
183 skip_this_dialog_ = true; | 149 valid_ = false; |
184 } | 150 } |
OLD | NEW |