| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/app_modal_dialogs/javascript_dialog_manager.h" | 5 #include "components/app_modal_dialogs/javascript_dialog_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/i18n/rtl.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/app_modal_dialogs/app_modal_dialog.h" |
| 11 #include "components/app_modal_dialogs/app_modal_dialog_queue.h" |
| 7 #include "components/app_modal_dialogs/javascript_dialog_extensions_client.h" | 12 #include "components/app_modal_dialogs/javascript_dialog_extensions_client.h" |
| 8 #include "components/app_modal_dialogs/javascript_dialog_manager_impl.h" | |
| 9 #include "components/app_modal_dialogs/javascript_native_dialog_factory.h" | 13 #include "components/app_modal_dialogs/javascript_native_dialog_factory.h" |
| 10 | 14 #include "components/app_modal_dialogs/native_app_modal_dialog.h" |
| 11 content::JavaScriptDialogManager* GetJavaScriptDialogManagerInstance() { | 15 #include "content/public/common/javascript_message_type.h" |
| 12 return JavaScriptDialogManagerImpl::GetInstance(); | 16 #include "grit/components_strings.h" |
| 13 } | 17 #include "net/base/net_util.h" |
| 14 | 18 #include "ui/base/l10n/l10n_util.h" |
| 15 void SetJavaScriptNativeDialogFactory( | 19 |
| 16 scoped_ptr<JavaScriptNativeDialogFactory> new_factory) { | 20 namespace { |
| 17 JavaScriptDialogManagerImpl::GetInstance()->SetNativeDialogFactory( | 21 |
| 18 new_factory.Pass()); | 22 class DefaultExtensionsClient : public JavaScriptDialogExtensionsClient { |
| 19 } | 23 public: |
| 20 | 24 DefaultExtensionsClient() {} |
| 21 void SetJavaScriptDialogExtensionsClient( | 25 ~DefaultExtensionsClient() override {} |
| 22 scoped_ptr<JavaScriptDialogExtensionsClient> new_client) { | 26 |
| 23 JavaScriptDialogManagerImpl::GetInstance()->SetExtensionsClient( | 27 private: |
| 24 new_client.Pass()); | 28 // JavaScriptDialogExtensionsClient: |
| 25 } | 29 void OnDialogOpened(content::WebContents* web_contents) override {} |
| 30 void OnDialogClosed(content::WebContents* web_contents) override {} |
| 31 bool GetExtensionName(content::WebContents* web_contents, |
| 32 const GURL& origin_url, |
| 33 std::string* name_out) override { |
| 34 return false; |
| 35 } |
| 36 |
| 37 DISALLOW_COPY_AND_ASSIGN(DefaultExtensionsClient); |
| 38 }; |
| 39 |
| 40 } // namespace |
| 41 |
| 42 //////////////////////////////////////////////////////////////////////////////// |
| 43 // JavaScriptDialogManager, public: |
| 44 |
| 45 // static |
| 46 JavaScriptDialogManager* JavaScriptDialogManager::GetInstance() { |
| 47 return Singleton<JavaScriptDialogManager>::get(); |
| 48 } |
| 49 |
| 50 void JavaScriptDialogManager::SetNativeDialogFactory( |
| 51 scoped_ptr<JavaScriptNativeDialogFactory> factory) { |
| 52 native_dialog_factory_ = factory.Pass(); |
| 53 } |
| 54 |
| 55 void JavaScriptDialogManager::SetExtensionsClient( |
| 56 scoped_ptr<JavaScriptDialogExtensionsClient> extensions_client) { |
| 57 extensions_client_ = extensions_client.Pass(); |
| 58 } |
| 59 |
| 60 //////////////////////////////////////////////////////////////////////////////// |
| 61 // JavaScriptDialogManager, private: |
| 62 |
| 63 JavaScriptDialogManager::JavaScriptDialogManager() |
| 64 : extensions_client_(new DefaultExtensionsClient) { |
| 65 } |
| 66 |
| 67 JavaScriptDialogManager::~JavaScriptDialogManager() { |
| 68 } |
| 69 |
| 70 void JavaScriptDialogManager::RunJavaScriptDialog( |
| 71 content::WebContents* web_contents, |
| 72 const GURL& origin_url, |
| 73 const std::string& accept_lang, |
| 74 content::JavaScriptMessageType message_type, |
| 75 const base::string16& message_text, |
| 76 const base::string16& default_prompt_text, |
| 77 const DialogClosedCallback& callback, |
| 78 bool* did_suppress_message) { |
| 79 *did_suppress_message = false; |
| 80 |
| 81 ChromeJavaScriptDialogExtraData* extra_data = |
| 82 &javascript_dialog_extra_data_[web_contents]; |
| 83 |
| 84 if (extra_data->suppress_javascript_messages_) { |
| 85 *did_suppress_message = true; |
| 86 return; |
| 87 } |
| 88 |
| 89 base::TimeDelta time_since_last_message = base::TimeTicks::Now() - |
| 90 extra_data->last_javascript_message_dismissal_; |
| 91 bool display_suppress_checkbox = false; |
| 92 // If a WebContents is impolite and displays a second JavaScript |
| 93 // alert within kJavaScriptMessageExpectedDelay of a previous |
| 94 // JavaScript alert being dismissed, show a checkbox offering to |
| 95 // suppress future alerts from this WebContents. |
| 96 const int kJavaScriptMessageExpectedDelay = 1000; |
| 97 |
| 98 if (time_since_last_message < |
| 99 base::TimeDelta::FromMilliseconds(kJavaScriptMessageExpectedDelay)) { |
| 100 display_suppress_checkbox = true; |
| 101 } else { |
| 102 display_suppress_checkbox = false; |
| 103 } |
| 104 |
| 105 bool is_alert = message_type == content::JAVASCRIPT_MESSAGE_TYPE_ALERT; |
| 106 base::string16 dialog_title = |
| 107 GetTitle(web_contents, origin_url, accept_lang, is_alert); |
| 108 |
| 109 extensions_client_->OnDialogOpened(web_contents); |
| 110 |
| 111 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( |
| 112 web_contents, |
| 113 &javascript_dialog_extra_data_, |
| 114 dialog_title, |
| 115 message_type, |
| 116 message_text, |
| 117 default_prompt_text, |
| 118 display_suppress_checkbox, |
| 119 false, // is_before_unload_dialog |
| 120 false, // is_reload |
| 121 base::Bind(&JavaScriptDialogManager::OnDialogClosed, |
| 122 base::Unretained(this), web_contents, callback))); |
| 123 } |
| 124 |
| 125 void JavaScriptDialogManager::RunBeforeUnloadDialog( |
| 126 content::WebContents* web_contents, |
| 127 const base::string16& message_text, |
| 128 bool is_reload, |
| 129 const DialogClosedCallback& callback) { |
| 130 const base::string16 title = l10n_util::GetStringUTF16(is_reload ? |
| 131 IDS_BEFORERELOAD_MESSAGEBOX_TITLE : IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE); |
| 132 const base::string16 footer = l10n_util::GetStringUTF16(is_reload ? |
| 133 IDS_BEFORERELOAD_MESSAGEBOX_FOOTER : IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER); |
| 134 |
| 135 base::string16 full_message = |
| 136 message_text + base::ASCIIToUTF16("\n\n") + footer; |
| 137 |
| 138 extensions_client_->OnDialogOpened(web_contents); |
| 139 |
| 140 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( |
| 141 web_contents, |
| 142 &javascript_dialog_extra_data_, |
| 143 title, |
| 144 content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM, |
| 145 full_message, |
| 146 base::string16(), // default_prompt_text |
| 147 false, // display_suppress_checkbox |
| 148 true, // is_before_unload_dialog |
| 149 is_reload, |
| 150 base::Bind(&JavaScriptDialogManager::OnDialogClosed, |
| 151 base::Unretained(this), web_contents, callback))); |
| 152 } |
| 153 |
| 154 bool JavaScriptDialogManager::HandleJavaScriptDialog( |
| 155 content::WebContents* web_contents, |
| 156 bool accept, |
| 157 const base::string16* prompt_override) { |
| 158 AppModalDialogQueue* dialog_queue = AppModalDialogQueue::GetInstance(); |
| 159 if (!dialog_queue->HasActiveDialog() || |
| 160 !dialog_queue->active_dialog()->IsJavaScriptModalDialog() || |
| 161 dialog_queue->active_dialog()->web_contents() != web_contents) { |
| 162 return false; |
| 163 } |
| 164 JavaScriptAppModalDialog* dialog = static_cast<JavaScriptAppModalDialog*>( |
| 165 dialog_queue->active_dialog()); |
| 166 if (accept) { |
| 167 if (prompt_override) |
| 168 dialog->SetOverridePromptText(*prompt_override); |
| 169 dialog->native_dialog()->AcceptAppModalDialog(); |
| 170 } else { |
| 171 dialog->native_dialog()->CancelAppModalDialog(); |
| 172 } |
| 173 return true; |
| 174 } |
| 175 |
| 176 void JavaScriptDialogManager::WebContentsDestroyed( |
| 177 content::WebContents* web_contents) { |
| 178 CancelActiveAndPendingDialogs(web_contents); |
| 179 javascript_dialog_extra_data_.erase(web_contents); |
| 180 } |
| 181 |
| 182 base::string16 JavaScriptDialogManager::GetTitle( |
| 183 content::WebContents* web_contents, |
| 184 const GURL& origin_url, |
| 185 const std::string& accept_lang, |
| 186 bool is_alert) { |
| 187 // If the URL hasn't any host, return the default string. |
| 188 if (!origin_url.has_host()) { |
| 189 return l10n_util::GetStringUTF16( |
| 190 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE |
| 191 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE); |
| 192 } |
| 193 |
| 194 // For extensions, show the extension name, but only if the origin of |
| 195 // the alert matches the top-level WebContents. |
| 196 std::string name; |
| 197 if (extensions_client_->GetExtensionName(web_contents, origin_url, &name)) |
| 198 return base::UTF8ToUTF16(name); |
| 199 |
| 200 // Otherwise, return the formatted URL. |
| 201 // In this case, force URL to have LTR directionality. |
| 202 base::string16 url_string = net::FormatUrl(origin_url, accept_lang); |
| 203 return l10n_util::GetStringFUTF16( |
| 204 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE |
| 205 : IDS_JAVASCRIPT_MESSAGEBOX_TITLE, |
| 206 base::i18n::GetDisplayStringInLTRDirectionality(url_string)); |
| 207 } |
| 208 |
| 209 void JavaScriptDialogManager::CancelActiveAndPendingDialogs( |
| 210 content::WebContents* web_contents) { |
| 211 AppModalDialogQueue* queue = AppModalDialogQueue::GetInstance(); |
| 212 AppModalDialog* active_dialog = queue->active_dialog(); |
| 213 if (active_dialog && active_dialog->web_contents() == web_contents) |
| 214 active_dialog->Invalidate(); |
| 215 for (AppModalDialogQueue::iterator i = queue->begin(); |
| 216 i != queue->end(); ++i) { |
| 217 if ((*i)->web_contents() == web_contents) |
| 218 (*i)->Invalidate(); |
| 219 } |
| 220 } |
| 221 |
| 222 void JavaScriptDialogManager::OnDialogClosed( |
| 223 content::WebContents* web_contents, |
| 224 DialogClosedCallback callback, |
| 225 bool success, |
| 226 const base::string16& user_input) { |
| 227 // If an extension opened this dialog then the extension may shut down its |
| 228 // lazy background page after the dialog closes. (Dialogs are closed before |
| 229 // their WebContents is destroyed so |web_contents| is still valid here.) |
| 230 extensions_client_->OnDialogClosed(web_contents); |
| 231 |
| 232 callback.Run(success, user_input); |
| 233 } |
| OLD | NEW |