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), | |
67 extra_data_(extra_data), | 60 extra_data_(extra_data), |
68 extension_host_(delegate->AsExtensionHost()), | |
69 dialog_flags_(dialog_flags), | 61 dialog_flags_(dialog_flags), |
70 display_suppress_checkbox_(display_suppress_checkbox), | 62 display_suppress_checkbox_(display_suppress_checkbox), |
71 is_before_unload_dialog_(is_before_unload_dialog), | 63 is_before_unload_dialog_(is_before_unload_dialog), |
72 reply_msg_(reply_msg), | 64 reply_msg_(reply_msg), |
73 use_override_prompt_text_(false) { | 65 use_override_prompt_text_(false) { |
74 EnforceMaxTextSize(message_text, &message_text_); | 66 EnforceMaxTextSize(message_text, &message_text_); |
75 EnforceMaxPromptSize(default_prompt_text, &default_prompt_text_); | 67 EnforceMaxPromptSize(default_prompt_text, &default_prompt_text_); |
76 | |
77 DCHECK((tab_contents_ != NULL) != (extension_host_ != NULL)); | |
78 InitNotifications(); | |
79 } | 68 } |
80 | 69 |
81 JavaScriptAppModalDialog::~JavaScriptAppModalDialog() { | 70 JavaScriptAppModalDialog::~JavaScriptAppModalDialog() { |
82 } | 71 } |
83 | 72 |
84 NativeAppModalDialog* JavaScriptAppModalDialog::CreateNativeDialog() { | 73 NativeAppModalDialog* JavaScriptAppModalDialog::CreateNativeDialog() { |
85 gfx::NativeWindow parent_window = delegate_->GetDialogRootWindow(); | 74 gfx::NativeWindow parent_window = delegate_->GetDialogRootWindow(); |
86 return NativeAppModalDialog::CreateNativeJavaScriptPrompt(this, | 75 return NativeAppModalDialog::CreateNativeJavaScriptPrompt(this, |
87 parent_window); | 76 parent_window); |
88 } | 77 } |
89 | 78 |
90 bool JavaScriptAppModalDialog::IsJavaScriptModalDialog() { | 79 bool JavaScriptAppModalDialog::IsJavaScriptModalDialog() { |
91 return true; | 80 return true; |
92 } | 81 } |
93 | 82 |
94 void JavaScriptAppModalDialog::Observe(NotificationType type, | 83 void JavaScriptAppModalDialog::Invalidate() { |
95 const NotificationSource& source, | 84 if (!valid_) |
96 const NotificationDetails& details) { | |
97 if (skip_this_dialog_) | |
98 return; | 85 return; |
99 | 86 |
100 if (NotificationType::EXTENSION_HOST_DESTROYED == type && | 87 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; | 88 delegate_ = NULL; |
110 if (native_dialog_) | 89 if (native_dialog_) |
111 CloseModalDialog(); | 90 CloseModalDialog(); |
112 } | 91 } |
113 | 92 |
114 void JavaScriptAppModalDialog::InitNotifications() { | 93 content::JavaScriptDialogDelegate* JavaScriptAppModalDialog::delegate() const { |
115 // Make sure we get relevant navigation notifications so we know when our | 94 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 } | 95 } |
131 | 96 |
132 void JavaScriptAppModalDialog::OnCancel(bool suppress_js_messages) { | 97 void JavaScriptAppModalDialog::OnCancel(bool suppress_js_messages) { |
133 // If we are shutting down and this is an onbeforeunload dialog, cancel the | 98 // If we are shutting down and this is an onbeforeunload dialog, cancel the |
134 // shutdown. | 99 // shutdown. |
135 if (is_before_unload_dialog_) | 100 if (is_before_unload_dialog_) |
136 browser_shutdown::SetTryingToQuit(false); | 101 browser_shutdown::SetTryingToQuit(false); |
137 | 102 |
138 // We need to do this before WM_DESTROY (WindowClosing()) as any parent frame | 103 // We need to do this before WM_DESTROY (WindowClosing()) as any parent frame |
139 // will receive its activation messages before this dialog receives | 104 // will receive its activation messages before this dialog receives |
(...skipping 23 matching lines...) Expand all Loading... |
163 | 128 |
164 void JavaScriptAppModalDialog::SetOverridePromptText( | 129 void JavaScriptAppModalDialog::SetOverridePromptText( |
165 const string16& override_prompt_text) { | 130 const string16& override_prompt_text) { |
166 override_prompt_text_ = override_prompt_text; | 131 override_prompt_text_ = override_prompt_text; |
167 use_override_prompt_text_ = true; | 132 use_override_prompt_text_ = true; |
168 } | 133 } |
169 | 134 |
170 void JavaScriptAppModalDialog::NotifyDelegate(bool success, | 135 void JavaScriptAppModalDialog::NotifyDelegate(bool success, |
171 const string16& user_input, | 136 const string16& user_input, |
172 bool suppress_js_messages) { | 137 bool suppress_js_messages) { |
173 if (skip_this_dialog_) | 138 if (!valid_) |
174 return; | 139 return; |
175 | 140 |
176 delegate_->OnDialogClosed(reply_msg_, success, user_input); | 141 delegate()->OnDialogClosed(reply_msg_, success, user_input); |
177 | 142 |
178 extra_data_->last_javascript_message_dismissal_ = base::TimeTicks::Now(); | 143 extra_data_->last_javascript_message_dismissal_ = base::TimeTicks::Now(); |
179 extra_data_->suppress_javascript_messages_ = suppress_js_messages; | 144 extra_data_->suppress_javascript_messages_ = suppress_js_messages; |
180 | 145 |
181 // On Views, we can end up coming through this code path twice :(. | 146 // On Views, we can end up coming through this code path twice :(. |
182 // See crbug.com/63732. | 147 // See crbug.com/63732. |
183 skip_this_dialog_ = true; | 148 valid_ = false; |
184 } | 149 } |
OLD | NEW |